4 min readβ’december 29, 2022
Peter Cao
Milo Chang
Peter Cao
Milo Chang
/** Represents an assignment that a student will complete
*/
public class Assignment {
private boolean correctAnswer; // represents the answer to an assignment, either T/F
/** Makes a new assignment with one True/False question and sets the correct answer
*/
public Assignment(boolean answer) {
correctAnswer = answer;
}
/** Prints details about the assignment
*/
@Override
public String toString() {
return "This is an assignment with correct answer " + answer;
}
}
/** Represents a high school student
*/
public class Student {
private int gradeLevel; // a grade between 9-12
private String name; // the students name in the form "FirstName LastName"
private int age; // the student's age, must be positive
private Assignment assignment; // the current assignment the student is working on
/** Makes a new student with grade gradeLev, name fullName, and age ageNum
*/
public Student(int gradeLev, String fullName, int ageNum) {
gradeLevel = gradeLev;
name = fullName;
age = ageNum;
assignment = null; // There is no active assignment at the moment
}
/** Returns the student's grade level
*/
public int getGradeLevel() {
return gradeLevel;
}
/** Returns the student's name
*/
public String getName() {
return name;
}
/** Returns the current assignment the student is working on
*/
public Assignment returnCurrentAssignment() {
return assignment;
}
/** Prints details about the student
*/
@Override
public String toString() {
return name + ", a " + gradeLevel + "th grade high school student";
}
}
public String getName()
.public int getGradeLevel()
.return
keyword is triggered in a method, the program immediately returns to the point after whatever called the method.System.out.println("Starting the example.");
Student bob = new Student(10, "Bob Smith", 16);
System.out.println(bob);
System.out.println("End of example.");
gradeLevel
to 10, name
to "Bob Smith," age
to 16, and assignment
to null
. With the creation of this object, the computer will move on to the next line of the main method. bob
, the program in the main method will use the toString() method of the Student class, which will return a String ("Bob Smith, a 10th grade high school student") that will then be printed.Β© 2023 Fiveable Inc. All rights reserved.