In the group class I need : - A method that calculates and displays the average
ID: 3752920 • Letter: I
Question
In the group class I need:
- A method that calculates and displays the average GPA of a group.
- semesterGPA is calculated using the GPA attribute in student, not the semesterGPA() method in student
Below is what I have so far!
public class group {
String groupName;
group(String groupName)
{
this.groupName = groupName;
}
student[] students = new student[4];
public void addStudents(student[] students)
{
this.students[0] = students[0];
this.students[1] = students[1];
this.students[2] = students[2];
this.students[3] = students[3];
}
public void printStudents()
{
System.out.println("Group Name: " + groupName);
for (student s : students) {
System.out.println(s.getInfo());
}
}
}
import java.util.Random;
class student
{
String firstName;
String lastName;
int age;
Random r = new Random();
double myGPA = 0 + (r.nextDouble() * 4);
student(String firstName, String lastName, int age, double GPA)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.myGPA = GPA;
}
student(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.myGPA = semesterGPA();
}
public String getName()
{
return firstName + " " + lastName;
}
public String getInfo()
{
return "Name: " + firstName + " " + lastName + " " + "Age: " + age + " " + "GPA: " + myGPA;
}
public double semesterGPA()
{
myGPA = Math.round((r.nextDouble() * 4) * 10.0) / 10.0;
return myGPA;
}
}
Explanation / Answer
Here is code:
group.java:
public class group {
String groupName;
group(String groupName)
{
this.groupName = groupName;
}
student[] students = new student[4];
public void addStudents(student[] students)
{
this.students[0] = students[0];
this.students[1] = students[1];
this.students[2] = students[2];
this.students[3] = students[3];
}
public void printStudents()
{
System.out.println("Group Name: " + groupName);
for (student s : students) {
System.out.println(s.getInfo());
}
}
public double semesterGPA() {
double sum = 0;
for (Student s : students) {
sum += s.myGPA;
}
return sum/students.length;
}
}
student.java:
import java.util.Random;
class student
{
String firstName;
String lastName;
int age;
Random r = new Random();
double myGPA = 0 + (r.nextDouble() * 4);
student(String firstName, String lastName, int age, double GPA)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.myGPA = GPA;
}
student(String firstName, String lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.myGPA = semesterGPA();
}
public String getName()
{
return firstName + " " + lastName;
}
public String getInfo()
{
return "Name: " + firstName + " " + lastName + " " + "Age: " + age + " " + "GPA: " + myGPA;
}
public double semesterGPA()
{
myGPA = Math.round((r.nextDouble() * 4) * 10.0) / 10.0;
return myGPA;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.