[英]How do I pass these variables to calculate a new GPA in my class?
sorry for this very similar question to my last post, but I'm having a slightly different problem to my last question. 非常抱歉,这个问题与我的上一个帖子非常相似,但是我遇到的问题与我的上一个问题稍有不同。 I have gotten everything to compile and work in my project, but I am not sure how to calculate a new gpa for student1 and student2 in my class by passing a total amount of points and a total amount of classes. 我已经获得了在项目中可以编译和工作的所有内容,但是我不确定如何通过传递总点数和总班级数来为班级中的Student1和Student2计算新的gpa。 My teacher wants us to calculate student1's gpa by passing 40 total points for 7 classes and to calculate student2's gpa by passing 36 total points for 8 classes. 我的老师希望我们通过7个班的总得分40计算学生1的gpa,并通过8个班的总得分36计算学生2的gpa。 Like I said, the rest of my code works, I'm just not sure how to do this yet as I'm still fairly new to coding. 就像我说的那样,我的其余代码都可以正常工作,但我仍然不确定该如何做,因为我对编码还很陌生。 The answer is probably pretty simple, but I can't figure it out for the life of me. 答案可能很简单,但我无法终生解决。 Here is my code: 这是我的代码:
public class student
{
private String name;
private int year;
private int age;
private double gpa;
/*
* Default Constructor
*/
public student()
{
name = "John";
year = 2016;
age = 16;
gpa = 4.0;
}
/*
* other constructor
*/
public student(String name, int year, int age, double gpa)
{
this.name = name;
this.year = year;
this.age = age;
this.gpa = gpa;
}
/*
* accessors
*/
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int getYear()
{
return year;
}
public double getGpa()
{
return gpa;
}
/*
* mutators
*/
public void setName(String name)
{
this.name=name;
}
public void setGpa(double gpa)
{
this.gpa=gpa;
}
public void setYear(int year)
{
this.year=year;
}
public void setAge(int age)
{
this.age=age;
}
/*
* calculate GPA
*/
public double calcGpa(double points, int classes)
{
gpa = points / classes;
return gpa;
}
public String toString()
{
return name + year + age + gpa;
}
}
public class studentReal
{
public static void main(String[] args)
{
student student1= new student("Jackie Java", 2018, 16, 4.7);
student student2= new student("John Java", 2019, 15, 3.8);
student student3= new student();
System.out.println(student1.getName() + ", class " + student1.getYear()+ ", " + student1.getAge() + ", " + student1.getGpa());
System.out.println(student2.getName() + ", class " + student2.getYear()+ ", " + student2.getAge() + ", " + student2.getGpa());
System.out.println(student3.getName() + ", class " + student3.getYear()+ ", " + student3.getAge() + ", " + student3.getGpa());
}
}
To do this with the functions you have currently I would go this route. 为此,请使用当前的功能。
double newGpa = student.gpaCalc(<any number>, <any number>);
student.setGpa(newGpa);
Your other options are in the calcGpa
function, call the setGpa
function rather than returning the value, or set it within the function like so: 您的其他选项在calcGpa
函数中,调用setGpa
函数而不是返回值,或者在函数内进行设置,如下所示:
public double calcGpa(double points, int classes)
{
gpa = points / classes;
this.gpa = gpa
}
In your studentReal class, inside the main method, use the calcGpa method you created within the Student class, for each student. 在您的studentReal类的main方法内,对每个学生使用您在Student类中创建的calcGpa方法。 The method, just like the variables, belongs to that student. 该方法就像变量一样,属于该学生。
For example: 例如:
student1.calcGpa(40, 7);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.