Question
Using Java, write a class HighSchoolStudent with the UML below. It extends the class Student. Answer questions 1 and 2, comment out the one that
Using Java, write a class HighSchoolStudent with the UML below. It extends the class Student. Answer questions 1 and 2, comment out the one that does not work and comment the error message it generated.
#1. If HighSchoolStudent is a subclass of Student, can you assign an object of HighSchoolStudent to a variable of type Student? Why or why not?
#2. Can you assign an object of Student to a variable of type HighSchoolStudent? Why or why not?
HighSchoolStudent |
-grade : int // 9,10,11,12 |
+HighSchoolStudent() +HighSchoolStudent( Name, String, int ) +toString() : String |
===============================================
/** 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( "John", "Doe" ); Name n1 = new Name( "Jane.", "Doe" ); System.out.println( n0.compareTo( n1 ) ); } } // end Name
==============================================
/** 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
===============================================
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