Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

After completing this assignment, you should know how to write a class with multiple constructors write the toString and equals methods throw an exception Read

After completing this assignment, you should know how to

  • write a class with multiple constructors
  • write the toString and equals methods
  • throw an exception

Read the instructions and coding standards before uploading your code.

Your code should not contain compiler errors and it must run with my test file to receive credit.

Copy your output into Canvas when you upload your assignment.

Very important

  • Changing an array value in the main method should not change an array value in a Student object
  • Changing an array value in a Student object should not change an array value the main method
  • Changing an array value in a Student object should not change an array value in another Student object

  1. Write a public class called Student with the following features

  • private instance variables
    • String firstName
    • String lastName
    • int[] grades
    • int age

  • constructor with the following header

public Student(String firstName, String lastName, int[] grades, int age)

use the input parameters to initialize the instance variables.

  • constructor with the following header

public Student(Student student)

use the input parameter to initialize the instance variables. This constructor creates a copy of the input parameter.

  • mutator and accessor methods for each instance variable

  • A method with the header

public String toString()

this method returns a String that contains the firstName, lastName, age, highest grade, and average grade. The average grade should contain exactly two decimal places of accuracy. You must use the getHighestGrade and getAverageGrade methods to find the highest grade and average grade. The output should be in a user-friendly format.

Hint: Use String.format; see Book.java an example.

  • A method with the header

public boolean equals(Object o)

this method returns true if the parameter o is a Student object with the same name and age as the current object. Otherwise, the method returns false. (The Book class contains an example of how to write an equals method.) Do not compare grades in the equals method.

  • A method with the header

public int getHighestGrade()

this method returns the greatest value stored in grades.

  • A method with the header

public double getAverageGrade()

this method returns the average grade. If there are more than four grades then drop the lowest score before computing the average.

  • A method with the header

public void incrementGrade(int i, int value)

throws an IllegalArgumentException if value < 0 or value> 100.

Otherwise, add value to grade[i]. If the revised score is greater than

100 set the grade to 100.

Very Important

To avoid side-effect your code should not contain statements similar to

  • this.grades = grades;

  • return grades;

In the constructors and mutator method

  • allocate memory for this.grades; the length should be identical to the length of the parameter grades.
  • copy the data in the parameter grades to this.grades.

In the accessor method

  • declare a local int array; the length should be identical to the length of this.grades.
  • copy the data in this.grades to the local variable
  • return the local variable

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

The nature and importance of the global marketplace.

Answered: 1 week ago