Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#10. Are the two definitions of the constructors for the class Student (Segment C.2) an example of overloading or overriding? Why? #11. If you add
#10. Are the two definitions of the constructors for the class Student (Segment C.2) an example of overloading or overriding? Why?
#11. If you add the method setStudent( Name, String ) to the class CollegeStudent and let it give some default values to the fields year and degree, are you overloading or overriding setStudent() in the class Student? Why?
- Write a program to use the two constructors of Student. One Student is default and the second is you (Name and student id). Write comments (not JavaDoc) in your code to answer #10.
- Continue your program with a default CollegeStudent and then using the added method setStudent( Name, String ) in CollegeStudent and me, new Name( "Mr.", "Reed" ), "000-01". Write comments in your code to answer #11. Note you have to write code for the added method setStudent().
- Attach your program and CollegeStudent.java.
ATTACHED FILES
CollegeStudent.Java
/** A class that represents a college student. (Listing D-3 in Appendix D.) @author Frank M. Carrano @author Timothy M. Henry @version 4.0 */ public class CollegeStudent //extends Student { private int year; // Year of graduation private String degree; // Degree sought public CollegeStudent() { super(); // Must be first statement in constructor year = 0; degree = ""; // Or replace the previous three statements with // this(studentName, studentId, 0, ""); (see Segment D.10) } // end default constructor public CollegeStudent(Name studentName, String studentId, int graduationYear, String degreeSought) { super(studentName, studentId); // Must be first year = graduationYear; degree = degreeSought; } // end constructor public void setStudent(Name studentName, String studentId, int graduationYear, String degreeSought) { setName(studentName); // NOT fullName = studentName; setId(studentId); // NOT id = studentId; // Or setStudent(studentName, studentId); (see Segment D.16) year = graduationYear; degree = degreeSought; } // end setStudent // < The methods setYear, getYear, setDegree, and getDegree go here. > public String toString() { return super.toString() + ", " + degree + ", " + year; } // end toString } // end CollegeStudent
Student.java
/** A class that represents a student. @author Frank M. Carrano @author Timothy M. Henry @version 5.0 */ public class Student { private Name fullName; private String id; // Identification number public Student() { fullName = new Name(); id = ""; } // end default constructor public Student(Name studentName, String studentId) { fullName = studentName; id = studentId; } // end constructor public void setStudent(Name studentName, String studentId) { setName(studentName); // Or fullName = studentName; setId(studentId); // Or id = studentId; } // end setStudent public void setName(Name studentName) { fullName = studentName; } // end setName public Name getName() { return fullName; } // end getName public void setId(String studentId) { id = studentId; } // end setId public String getId() { return id; } // end getId public String toString() { return id + " " + fullName.toString(); } // end toString } // end Student
Name.java
/** A class that represents a person's name. Listing B-1 in Segment B.16 of Appendix B. @author Frank M. Carrano @author Timothy M. Henry @version 5.0 */ public class Name //implements Comparable{ private String first; // First name private String last; // Last name public Name() { } // end default constructor public Name(String firstName, String lastName) { first = firstName; last = lastName; } // end constructor public void setName(String firstName, String lastName) { setFirst(firstName); setLast(lastName); } // end setName public String getName() { return toString(); } // end getName public void setFirst(String firstName) { first = firstName; } // end setFirst public String getFirst() { return first; } // end getFirst public void setLast(String lastName) { last = lastName; } // end setLast public String getLast() { return last; } // end getLast public void giveLastNameTo(Name aName) { aName.setLast(last); } // end giveLastNameTo public String toString() { return first + " " + last; } // end toString public int compareTo( Name name2 ) { return last.compareTo( name2.getLast() ); } public static void main( String[] args ) { Name n0 = new Name( "Mr.", "Reed" ); Name n1 = new Name( "Mrs.", "Read" ); System.out.println( n0.compareTo( n1 ) ); } } // end Name
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started