Java’yı Şöyle Bir Hatırlamak

Baya olmuştu Java ile uğraşmayalı, şöyle bir hatırlamak gayet hoş oldu. Aşağıda iki class yazdım. Pek fazla yorum yok ancak anlaşılabileceğini umuyorum.

Student.java

public class Student {

    private String studentName;
    private int studentSection;
    private int[] grades;

    //Constructor
    public Student(String name, int numberOfGrades){
        studentName = name;
        grades = new int[numberOfGrades];
        studentSection = 1;
    }

    //accesors
    public String getName(){
        return studentName;
    }
    public void setSection(int section){
        studentSection = section;
    }
    public int getSection(){
        return studentSection;
    }
    public int getNumberOfGrades(){
        return grades.length;
    }

    //private methods
    private double getAverageGrade(){
        int totalPoints = 0;
        int numberOfGrades = grades.length;
        for(int i = 0; i<numberOfGrades; i++){
            totalPoints += grades[i];
        }
        double result = totalPoints/numberOfGrades;
        return result;
    }

    private int getMinumumGrade(){
        int temp = 101;
        for(int i=0; i<grades.length;i++){
            if(grades[i]<temp){
                temp = grades[i];
            }
        }
        return temp;
    }

    private int getMaximumGrade(){
        int temp = -1;
        for(int i=0; i<grades.length;i++){
            if(grades[i]>temp){
                temp = grades[i];
            }
        }
        return temp;
    }

    //public methods
    public void setGrade(int gradeIndex, int grade){
        grades[gradeIndex] = grade;
    }
    public int getGrade(int gradeIndex){
        return grades[gradeIndex];
    }
    public char getLetterGrade(){
        char result;
        if(getAverageGrade()<=100 && getAverageGrade()>=90){
            result = 'A';
        }else if(getAverageGrade()<90 && getAverageGrade()>=80){
            result = 'B';
        }else if(getAverageGrade()<80 && getAverageGrade()>=70){
            result = 'C';
        }else if(getAverageGrade()<70 && getAverageGrade()>=60){
            result = 'D';
        }else{
            result = 'F';
        }
        return result;
    }
    public void print(){
        System.out.println("Name\t: " + getName());
        System.out.println("Section\t: " + getSection());
        System.out.println("Number of grades\t: " + grades.length);
        System.out.print("Grades\t: ");
        for(int i = 0; i<grades.length; i++){
            if(getAverageGrade()<grades[i]){
                System.out.print(grades[i]+"* ");
            }else{
                System.out.print(grades[i]+" ");
            }

        }
        System.out.println();
        System.out.println("Minumum Grade\t: " +getMinumumGrade());
        System.out.println("Maximum Grade\t: " +getMaximumGrade());
        System.out.println("Average Grade\t: " +getAverageGrade());
        System.out.println("Letter Grade\t: " +getLetterGrade());

    }

}

StudentApp.java


import java.util.Scanner;

public class StudentApp {

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter Student Name:");
        String name = scan.nextLine();
        System.out.println("Please enter section of Student:");
        int section = scan.nextInt();
        System.out.println("Please also enter how many grades Student has:");
        int numberOfGrades = scan.nextInt();

        Student studentObject = new Student(name, numberOfGrades);
        studentObject.setSection(section);

        System.out.println("Please enter each grade of Student");

        for(int i = 0; i<studentObject.getNumberOfGrades(); i++){
            int temp = scan.nextInt();
            studentObject.setGrade(i, temp);
        }

        studentObject.print();

    }

}

Umarım birilerinin işine yarar! :)



Bu ve buna benzer yazılardan haberdar olmak için kaydol:

One thought on “Java’yı Şöyle Bir Hatırlamak

Leave a Reply to selami Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>