0

I'm trying to implement a method printDegreeClassification that prints out the degree classification given the average mark of a student. The method should take one argument ob object type Student and return no value. Class student has the following method: public int getAverage(); And classification as following: First 70+, Upper Second 60-67, Lower Second 50-59, Third 40-49 ,Pass 30-39, Fail 0-29

My code is:

public void printDegreeClassification(Student a){
    int b  = a.getAverage();

         if (b>=70){
             System.out.println("First");    
         }else if(b>=60){ 
              System.out.println("Upper Second");    
         }else if(b>=50){ 
              System.out.println("Lower Second");             
         }else if(b>=40){ 
             System.out.println("Third");    
         }else if(b>=30){ 
             System.out.println("Pass");    
         }else{ 
             System.out.println("Fail");    
         }


public class Student {
        public int getAverage();
   }


    public static void main(String[] args) {
        Student result = new Student();
        result.printDegreeClassification(result);
    }
}

Am I right? This is my first ever Java program.

6
  • Have you tried running it yet? You are not reporting any errors, but just ask if it is syntactically correct? Commented May 29, 2018 at 9:31
  • 1
    This will not compile. Commented May 29, 2018 at 9:31
  • 1
    How about testing it with multiple values? Commented May 29, 2018 at 9:32
  • If you implement getAverage (not avarage) and fix the typos, this would be correct. Note that this is not a code review, there is Code Review for that. But for that this would need to run, what you don't even tried, why ? Commented May 29, 2018 at 9:36
  • 1
    Presumably both getAvarage and getAvartage should be getAverage. Commented May 29, 2018 at 9:37

4 Answers 4

1

When you are learning, learn it right. Below is your modularized program. Close to what we right in projects that go live:

StudentDto.java A class that encapsulates properties of student object. Fields are private & has their getter/setter methods to access/modify the respective field.

public class StudentDto {

    private int average;

    public StudentDto(int average) {
        this.average = average;
    }

    public int getAverage() {
        return average;
    }

    public void setAverage(int average) {
        this.average = average;
    }   
}

StudentUtil.java Evaluating the degree based on average marks is a utility method and be kept under a common util class containing all utility methods. Note that these methods should be static as they do not bound to any object.

Since you are using only one property of student object to evaluate degree, no need to pass the object itself, just pass the average value. And this method should return the value to the calling method.

public class StudentUtil {

    public static String getDegreeClassification(int averageMarks) {
        String degreeClassification = null;
        if (averageMarks >= 0) {
            if (averageMarks >= 70) {
                degreeClassification = "First";
            } else if (averageMarks >= 60) {
                degreeClassification = "Upper Second";
            } else if (averageMarks >= 50) {
                degreeClassification = "Lower Second";
            } else if (averageMarks >= 40) {
                degreeClassification = "Third";
            } else if (averageMarks >= 30) {
                degreeClassification = "Pass";
            } else {
                degreeClassification = "Fail";
            }
        } else {
            degreeClassification = "Average cannot be less than ZERO";
        }
        return degreeClassification;
    }
}

Student.java is the class where the main method resides. This class is from where the application launches. The main method is not to be kept in any domain objects like Student, Teacher, etc.,

public class Student {

    public static void main(String[] args) {
        StudentDto studentDto = new StudentDto(50);
        String degree = StudentUtil.getDegreeClassification(studentDto.getAverage());
        System.out.println(degree);
    }
}

Hope this is helpful to you!

Sign up to request clarification or add additional context in comments.

Comments

1

Supposing that somewhere in Student there is a field named grades:

private int [] grades = /*some declaration*/
public class Student {
    public int getAvarage(){
         int sum = 0;
         for (int d : grades) sum += d;
         double average = ((double)sum) / grades.length; // to let the result be double
         return (int) average; // back to integer
    };
}

This should work.

1 Comment

That's not the question, or this is not what I understood
1

You need to set average value in student class then you will be able to get and the able to compare it: You may try like following:

public class Student {
    private int average;

    public int getAverage() {
        return average;
    }

    public void setAverage(int average) {
        this.average = average;
    }
    public static void main(String[] args) {
         Student result = new Student();
         result.setAverage(45);
         printDegreeClassification(result);
    }

    public static void printDegreeClassification(Student s){
        if(s!=null && s.getAverage()>0){
             int b  = s.getAverage();
             if (b>=70){
                 System.out.println("First");    
             }else if(b>=60){ 
                  System.out.println("Upper Second");    
             }else if(b>=50){ 
                  System.out.println("Lower Second");             
             }else if(b>=40){ 
                 System.out.println("Third");    
             }else if(b>=30){ 
                 System.out.println("Pass");    
             }else{ 
                 System.out.println("Fail");    
             }
        }
    }
}

Comments

1

No not is correct because you don't implement the get function and don't initialize the avarage

public class Student {
    public int avarage;

    public Student(){
        this.avarage = 0;
    }

    public int getAvarage(){
      return this.avarage;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.