Java This workshop is an extension of Workshop 8 so use Workshop 8 as a starting
ID: 3814054 • Letter: J
Question
Java
This workshop is an extension of Workshop 8 so use Workshop 8 as a starting point. You can either use your Workshop 8 code or download the Workshop 8 solution.
Additions to the Student Class
Instance variable : classification - String representing the students classification (freshman, sophomore, junior, and senior)
Modify the constructors to include setting the new classification instance variable.
Add accessor and mutator method for the classification instance variable.
New Methods
String toString() method - returns a String that represents the instance variables of the Student class in a sentence. Use the accessor method to get the instance variables. An example of the String might be "John Smith is a Junior and has taken 10 quizzes with an average of 78.45"
boolean equals( Student otherStudent) - that compares two Student object based on classification and quiz average and returns true if the classification and average are the same and false if any are different.
Overloaded void setClassification(int classCode)
an overloaded setClassification mutator method that converts parameter classCode to a String representing a classification and assigns that String to the instance variable classification.
The conversion of classCode to color is: 1 - freshman 2 - sophomore 3 - Junior, 4 - Senior. If the classiCode is not 1 - 4, print an error message stating that the code is invalid.
Add to the StudentTest.java as follows:
Modify the parameterized constructor call to include classification.
Use the mutator method, to pass a String representing the classification, to set the classification for the Student object that was created using the default constructor.
Test the equals method. in testing this method, use an if statement where the condition is the call to the equals method.
Test the overloaded setClassification method by changing the classification of a Student object.
Call the toString method as needed to show your tests worked, for example after you call change the color using one of the mutator methods call the toString to show that the color has changed.
Document your code and follow submission instructions and submit to the Workshop 9 Dropbox Folder.
Explanation / Answer
package college;
public class Student {
String name;
String classification;
int quizzes;
float average;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
public void setClassification(int classCode) {
if(classCode == 1)
this.classification = "freshman";
else if(classCode == 2)
this.classification = "sophomore";
else if(classCode == 3)
this.classification = "junior";
else if(classCode == 4)
this.classification = "senior";
else
System.out.println("code is invalid");
}
public Student(String name, String classification, int quizzes, float average) {
super();
this.name = name;
this.classification = classification;
this.quizzes = quizzes;
this.average = average;
}
public Student(String name, int classCode, int quizzes, float average) {
super(); String clfication;
if(classCode == 1)
clfication = "freshman";
else if(classCode == 2)
clfication = "sophomore";
else if(classCode == 3)
clfication = "junior";
else //constructor can never return null,so 4 is fall back parameter
clfication = "senior";
this.name = name;
this.classification = clfication;
this.quizzes = quizzes;
this.average = average;
}
public int getQuizzes() {
return quizzes;
}
public void setQuizzes(int quizzes) {
this.quizzes = quizzes;
}
public float getAverage() {
return average;
}
public void setAverage(float average) {
this.average = average;
}
@Override
public String toString() {
return "Student's name is " + name + " he's a " + classification + ",has taken " + quizzes + " quizzes with an average of "
+ average ;
}
@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 (Float.floatToIntBits(average) != Float.floatToIntBits(other.average))
return false;
if (classification == null) {
if (other.classification != null)
return false;
} else if (!classification.equals(other.classification))
return false;
return true;
}
}
package college;
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student("john","junior",20,40);
System.out.println(s1.toString());
Student s2 = new Student("john",3,20,40);
System.out.println(s2.toString());
if(s1.equals(s2))
System.out.println("objects are equal");
s2.setClassification(2);
System.out.println(s2.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.