Question
Inheritance: Build a new Student class(Student.java). This class will be a subclass of Person. So it will inherit all the properties of Person(FirstName, LastName, Address,
Inheritance:
Build a new Student class("Student.java"). This class will be a subclass of Person. So it will inherit all the properties of Person(FirstName, LastName, Address, and Email).Also, add 3 new properties: studentId, major, and GPA. A total of 7 properties. Create this class, make sure to add 2 constructors, all set and get methods, a display() method, and a toString method. Then use main to test out this class.
Main code:
Student s1;
s1 = new Student(900077661, "Bill", "Clinton", new Address ("123 Main", "Atlanta", "GA", 30111), "HYPERLINK "mailto:bc@msn.com" bc@msn.com", "CS", 3.5);
s1.display();//print all 7 properties.
Here is what I got so far:
import java.util.*; import java.lang.*; import java.io.*; class Person { //class properties private String firstName; private String lastName; private Address address; private String email;
//constructor with parameters Person(String firstName,String lastName,Address address,String email) { this.firstName = firstName; this.lastName = lastName; this.address = address; this.email = email; }
//constructor with no parameters Person() { this.firstName = ""; this.lastName = ""; this.address = new Address(); this.email = ""; }
//set methods //method that sets firstname public void setFirstName(String firstName) { this.firstName = firstName; }
//method that sets lastname public void setLastName(String LastName) { this.lastName = lastName; } //method that sets address public void setAddress(Address address) { this.address = address; }
//method that sets email public void setEmail(String email) { this.email = email; }
//get methods //method that returns firstname public String getFirstName() { return this.firstName; } //method that returns lastname public String getLastName() { return this.lastName; } //method that returns address public Address getAddress() { return this.address; }
//method that returns email public String getEmail() { return this.email; }
//method that displays person data void display() { //print the person details System.out.println("FIRST NAME : "+this.firstName); System.out.println("LAST NAME : "+this.lastName); this.address.display(); //in order to print the address use the display method of Address class System.out.println("EMAIL :"+this.email); }
//overridding toString method public String toString() { return "First name : "+getFirstName() + " Last name : "+getLastName() + " " +this.address.toString() +" Email :"+getEmail(); } //main method public static void main (String[] args) throws java.lang.Exception { Person p1; p1 = new Person("James", "Jones", new Address ("123 Main St.","Dallas","TX",56565), "jj@yahoo.com"); p1.display(); } }
//===================================================================
import java.util.*; import java.lang.*; import java.io.*; class Address { private String street; private String city; private String state; private int zipcode; Address(String street,String city,String state,int zipcode){ this.street=street; this.city=city; this.state=state; this.zipcode=zipcode; } Address(){ street=""; city=""; state=""; zipcode=0; } void display(){ System.out.println("ADDRESS: "); System.out.println("STREET : "+this.street); System.out.println("CITY : "+this.city); System.out.println("STATE : "+this.state); System.out.println("ZIPCODE :"+this.zipcode); } public String toString() { return "STREET : "+this.street + " CITY : "+this.city + " STATE : " +this.state +" ZIPCODE :"+this.zipcode; } public static void main (String[] args) throws java.lang.Exception { Address a1; a1=new Address("1313 MockingBirdLn","Atlanta","GA",30001); a1.display(); } }
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