CompSci 251: Intermediate Computer Programming Solution

$30.00

Category:

Description

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.

 

Student

– name: String

– gpa: double

– major: String

–  totalStudents: int

–  totalGPA: double

–  totalCompSciMajors: int

–  totalCompSciGPA: double

+ Student(name: String, gpa: double, major: String)

+ getName(): String

+ setName(name: String)

+ getGPA(): double

+ setGPA(gpa: double)

+ getMajor(): String

+  getStudentAvg(): double

+  getCompSciAvg(): double

+ toString(): String

+ equals(other: Object): boolean

 

  • Create a Student class conforming to the diagram above.
  • Each student has a name, gpa, and major. There are no restrictions on the name, and assume once a major is chosen it can never be changed. This means no setter is required for major.
  • 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. (Note: Be mindful of division by zero)
  • 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.  (Note: Be mindful of division by zero)
  • The setGPA sets the gpa to be between 0.0 and 4.0. When changing a students 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: CompSci, GPA: 3.5

Student Name: Ahmed, Major: CompSci, GPA: 4.0

Student Name: Shaina, Major: EE, GPA: 3.5

Student Name: Megan, Major: Accounting, GPA: 3.0

Student Name: Antonio, Major: CompSci, GPA: 3.0

Student Name: Jim, Major: Business, GPA: 1.5

Student Name: Morgan, Major: Architecture, GPA: 3.5

 

Testing equals methods. Does Eva equal Ahmed?

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


error: Content is protected !!