In: Computer Science
CompSci 251: Intermediate Computer ProgrammingLab 4 – 2019
Introduction
For this lab we will be looking at static, or class variables. Remember, instance variables are associated with a particular instance, where static variables belong to the class. If there are n instances of a class, there are n copies of an instance variable, but exactly one copy of a static variable. Underlined items are static members of the class.
The lab also uses enums to control what is an acceptable major. Enums are a special kind of class in Java that represent constant values. Enums can also have methods, which I have overridden the toString method. You do not need to change the enum CollegeMajor class. Look at the driver for how they are accessed. Also, they can be compared against other enums as if they were primitive types.
What you need to do
Create a Student class conforming to the diagram above.
Each student has a name, GPA, and major. Assume once a name is chosen it can never be
changed. This means no setter is required for name.
The class keeps track of how many students have been created and their combined GPA. These
values are used for getStudentAvg(), which computes the average for all students.
The class also manages the total number of CompSci students and their total GPA. These values
should only be affected by CompSci majors and are used for getCompSciAvg(). Take note,
CompSci majors are still considered part of all students.
The setGPA sets the GPA to be between 0.0 and 4.0. When changing a student’s GPA, it must
accurately update the totalGPA, and possibly totalCompSciGPA, static variable. If the value is not
in range, nothing happens.
The equals method returns true if all instance variables are equal.
The toString() should return a String in the format.
o Student Name: <name>, Major: <major>, GPA:
<gpa>
A simple driver is provided. When complete your code should look
like below.
Driver Output
All students:
Student name: Eva major: Biology GPA: 3.5
Student name: Taiyu major: Computer Science GPA: 4.0 Student name:
Shaina major: Civil Engineer GPA: 3.5 Student name: Megan major:
Accounting GPA: 3.0
Student name: Antonio major: Computer Science GPA: 3.0 Student
name: Jim major: Chemistry GPA: 1.5
Student name: Morgan major: Computer Science GPA: 3.5
Testing equals methods.
Does Eva equal Taiyu? They are not the same.
Does Antonio equal Antonio? They are the same.
Testing static variables.
All Students avg = 3.14 All CompSci Students avg = 3.50
Time to set all gpa values to 3.0.
If done correctly, both averages should be 3.0.
All Students avg = 3.00 All CompSci Students avg = 3.00
Time to set all gpa values of CompSci Students to 4.0. If done correctly, both averages should be different.
All Students avg = 3.43 All CompSci Students avg = 4.00
given code
public enum CollegeMajor {
COMPSCI,BIOLOGY,NURSING,ACCOUNTING,ARCHITECTURE,FINANCE,CHEMISTRY,CIVILENGINEERING;
@Override
public String toString() {
switch(this) {
case COMPSCI:
return "Computer
Science";
case BIOLOGY:
return
"Biology";
case NURSING:
return
"Nursing";
case ACCOUNTING:
return
"Accounting";
case ARCHITECTURE:
return
"Architecture and Interior Design";
case FINANCE:
return
"Finance";
case CHEMISTRY:
return
"Chemistry";
case CIVILENGINEERING:
return "Civil
Engineer";
default:
return "";
}
}
}
ublic class Driver {
public static void main(String[] args) {
Student[] students = new Student[7];
students[0] = new Student("Eva",
3.5, CollegeMajor.BIOLOGY);
students[1] = new Student("Taiyu",
4.0, CollegeMajor.COMPSCI);
students[2] = new Student("Shaina",
3.5, CollegeMajor.CIVILENGINEERING);
students[3] = new Student("Megan",
3.0, CollegeMajor.ACCOUNTING);
students[4] = new
Student("Antonio", 3.0, CollegeMajor.COMPSCI);
students[5] = new Student("Jim",
1.5, CollegeMajor.CHEMISTRY);
students[6] = new Student("Morgan",
3.5, CollegeMajor.COMPSCI);
System.out.println("All
students:");
for(Student s: students) {
System.out.println(s);
}
System.out.println("\n\nTesting
equals methods.\n");
System.out.println("Does " +
students[0].getName() + " equal " + students[1].getName() +
"?");
if(students[0].equals(students[1]))
{
System.out.println("They are the same.");
} else {
System.out.println("They are not the same.");
}
System.out.println("\nDoes " +
students[4].getName() + " equal " + students[4].getName() +
"?");
if(students[4].equals(students[4]))
{
System.out.println("They are the same.");
} else {
System.out.println("They are not the same.");
}
System.out.println("\n\nTesting
static variables.\n");
System.out.printf("All Students avg
= %.2f\n", Student.getStudentAvg());
System.out.printf("All CompSci
Students avg = %.2f\n", Student.getCompSciAvg());
System.out.println("\nTime to
set all gpa values to 3.0.");
System.out.println("If done
correctly, both averages should be 3.0.\n");
for(Student s: students) {
s.setGPA(3.0);
}
System.out.printf("All Students
avg = %.2f\n", Student.getStudentAvg());
System.out.printf("All CompSci
Students avg = %.2f\n", Student.getCompSciAvg());
System.out.println("\nTime to
set all gpa values of CompSci Students to 4.0.");
System.out.println("If done
correctly, both averages should be different.\n");
for(Student s: students) {
if(s.getMajor()==CollegeMajor.COMPSCI)
s.setGPA(4.0);
}
System.out.printf("All Students
avg = %.2f\n", Student.getStudentAvg());
System.out.printf("All CompSci
Students avg = %.2f\n", Student.getCompSciAvg());
}
}
// Student.java
class Student {
private String name;
private double gpa;
private CollegeMajor major;
private static double totalGpa = 0;
private static int studentsCount = 0;
private static double totalCompSciGpa = 0;
private static int studentsCountCompSci = 0;
public Student(String name, double gpa, CollegeMajor major) {
this.name = name;
this.gpa = gpa;
this.major = major;
totalGpa += gpa;
studentsCount++;
if(major.equals(CollegeMajor.COMPSCI)) {
totalCompSciGpa += gpa;
studentsCountCompSci++;
}
}
public String getName() {
return name;
}
public double getGpa() {
return gpa;
}
public CollegeMajor getMajor() {
return major;
}
public static double getStudentAvg() {
if(studentsCount == 0) {
return 0;
}
return totalGpa/studentsCount;
}
public static double getCompSciAvg() {
if(studentsCountCompSci == 0) {
return 0;
}
return totalCompSciGpa/studentsCountCompSci;
}
public void setGPA(double d) {
if(d >= 0 && d <= 4) {
if(major.equals(CollegeMajor.COMPSCI)) {
totalCompSciGpa -= gpa;
totalCompSciGpa += d;
}
totalGpa -= gpa;
totalGpa += d;
gpa = d;
}
}
@Override
public String toString() {
return "Student Name: " + name + ", Major: " + major + ", GPA: " + gpa;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (Double.doubleToLongBits(gpa) != Double.doubleToLongBits(other.gpa))
return false;
if (major != other.major)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.