在java中实现以Object为参数的方法

时间:2018-05-29 09:28:43

标签: java class object

我试图实现一个方法printDegreeClassification,它打印出给定学生平均分数的学位分类。该方法应该采用一个参数ob对象类型Student并且不返回任何值。班级学生有以下方法:public int getAverage();分类如下:前70+,上半场60-67,下二轮50-59,第三场40-49,传球30-39,失败0-29

我的代码是:

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);
    }
}

我是对的吗?这是我的第一个Java程序。

4 个答案:

答案 0 :(得分:1)

不是不正确,因为你没有实现get函数而且没有初始化avarage

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_BLUETOOTH)
    {
        if(resultCode == RESULT_OK)
        {
            // the user allow Bluetooth, RESULT_OK is a defined constant
            acceptConnect(BTAdapter);
            beginListenForData();
        }
        else if(resultCode == RESULT_CANCELED)
        {
            // the user deny Bluetooth, RESULT_CANCELED is a defined constant
        }
    }
}

答案 1 :(得分:1)

假设学生的某个地方有一个名为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
    };
}

这应该有用。

答案 2 :(得分:1)

您需要在学生班级中设置平均值,然后您才能获得并能够比较它:您可以尝试以下内容:

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");    
             }
        }
    }
}

答案 3 :(得分:1)

当你在学习时,要正确学习。以下是您的模块化程序。接近我们在上线项目中的权利:

StudentDto.java 封装学生对象属性的类。领域是私人的&有他们的getter / setter方法来访问/修改相应的字段。

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 根据平均分数评估学位是一种实用工具方法,并保存在包含所有实用工具方法的公共util类之下。请注意,这些方法应该是静态,因为它们不绑定到任何对象。

由于您只使用student对象的一个​​属性来评估度,因此无需传递对象本身,只需传递平均值即可。此方法应该将值返回给调用方法。

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 是main方法所在的类。此类是应用程序启动的位置。主要方法不应保存在学生,教师等任何域对象中,

public class Student {

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

希望这对你有所帮助!