Tools: VSCode | Source Code: GitHub
The basamak
program is designed to process a number, break it into its individual digits, display each digit alongside its position, and calculate the sum of all its digits. The program has a straightforward structure that focuses on both user input handling and digit-by-digit processing. It also includes a main method that demonstrates the functionality using a predefined number.
The program begins with a class definition, basamak
, which contains private fields for storing information about the number. These fields include sayi
, which holds the integer input, sayi2
, the string representation of the number, basamaksayi
, which stores the total count of digits, and basamaktoplami
, which stores the sum of the digits. The class includes a constructor that initializes the number when creating an instance of the class. This constructor ensures the object starts with a valid input number for processing.
To allow dynamic input, the sayiInput
method prompts the user to enter a number through the console. It uses a Scanner
object to capture the input and stores the result in the sayi
field. The scanner is then closed to avoid resource leaks, showcasing proper resource management practices. This method enables the program to work with user-provided numbers instead of relying solely on predefined values.
The primary functionality of the program lies in the basamakSayi
method, which calculates the number of digits and their sum. The method begins by converting the integer sayi
to a string sayi2
, making it easier to access individual digits. The length of this string is used to determine the total count of digits, which is stored in basamaksayi
. Using a for
loop, the method iterates through each character in the string, converts it back to an integer, and adds it to basamaktoplami
. During each iteration, the program prints the position of the digit (starting from 1) and its value. Once all digits have been processed, the method returns the sum of the digits as the final result.
The program’s functionality is demonstrated in the main
method. An instance of the basamak
class is created with the predefined number 1234, and the basamakSayi
method is called to perform the digit processing. The sum of the digits is then printed to the console. This example highlights how the program can be used in practical scenarios, either with predefined numbers or through user input.
Working on this project helped deepen my understanding of several Java concepts. I learned how to convert integers to strings and manipulate them character by character, which is useful for breaking down numbers into their components. I also practiced implementing loops for iterative processing and managing resources like Scanner
objects effectively. Additionally, the project reinforced the importance of encapsulation, as the class's private fields are only modified through its methods, ensuring a controlled and structured flow of data. Overall, this program demonstrates a clear and practical approach to digit processing in Java.
import java.util.Scanner;
public class basamak {
private int basamaksayi;
private int basamaktoplami;
private String sayi2;
private int sayi;
public basamak(int anasayi) {
sayi = anasayi;
}
public void sayiInput() {
Scanner sayigir = new Scanner(System.in);
System.out.println("Sayi gir: ");
sayi = sayigir.nextInt();
sayigir.close();
}
public int basamakSayi() {
sayi2 = String.valueOf(sayi);
basamaksayi = sayi2.length();
basamaktoplami = 0;
for (int i = 0; i < basamaksayi; i++) {
int counter = Integer.parseInt(sayi2.substring(i, i + 1));
basamaktoplami += counter;
System.out.println((i + 1) + ". basamak: " + counter);
}
return basamaktoplami;
}
public static void main(String[] args) {
basamak b = new basamak(1234);
int toplam = b.basamakSayi();
System.out.println("Basamak Toplami: " + toplam);
}
}