Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Inheritance

phillyluvr
phillyluvr 


Objectives Understand how to use inheritance in Java Key Concepts Inheritance allows you to extend a class in Java. Lab Assignment This lab assignment will ask you to write a program using inheritance. Exercise 1 1. Create a New Project named InheritanceDemo . 2. Create a new class called Person and cut and paste the Person.java file from Blackboard into the class. Create a new class called Student and cut and paste the Student.java file from Blackboard into the class. Cut and paste the InheritanceDemo.java file from Blackboard into the InheritanceDemo class. 3. Create a new class called GraduateStudent that inherits from class Student. Add two instance variables, a String called major and a double called gpa. Add two constructors, one with no parameters and one with four. Make sure you call the super constructors for each of them. Write the accessor methods and the mutator methods for major and gpa. Write a reset() method that resets the name, studentNumber, major, and gpa. Finally, write a writeOutput() method that prints out all four values for the GraduateStudent class. 4. Uncomment the commented lines in InheritanceDemo.java. Compile the project and then execute it. What does it output? Attachments Tags Clarifications

Answers

Inheritance

mariobeka
mariobeka 



Below you'll find the solution for the inheritance problem as well as a screen shot of the programming running. Attachments
GraduateStudent.java (1 K) Download Preview
private double gpa; public GraduateStudent() { super(); this.major = "No major set yet"; this.gpa = 0; } public GraduateStudent(String initialName, int initialStudentNumber, String major, double gpa) { super(initialName, initialStudentNumber); this.major = major; this.gpa = gpa; }

InheritanceDemo.java (1 K) Download Preview
s.setName ("Warren Peace"); s.setStudentNumber (1234); s.writeOutput (); GraduateStudent g = new GraduateStudent("Ariel Bender", 5678, "Computer Science", 4.0); g.writeOutput

Person.java (1 K) Download Preview
} public String getName () { return name; } public void writeOutput () { System.out.println ("Name: " + name);

Student.java (1 K) Download Preview
Person { private int studentNumber; public Student () { super (); studentNumber = 0; //Indicating no number yet } public Student (String initialName, int initialStudentNumber) { super (initialName); studentNumber = initialStudentNumber; }

Screen Shot 2015-11-06 at 10.30.11.png (59 K) Download Preview

Screenshots
Screenshot
Purchase Answer

Inheritance

sudheer488
sudheer488 



Please find the code along with screenshots Attachments
inheritancedemo.rar (6 K) Download







Preview
public void reset(String s,int i,String s1,double j){ super.setName(s); super.setStudentNumber(i); setMajor(s1);setGpa(j); } public void writeOutput () { System.out.println ("Name: " +super.getName()); System.out.println ("Student Number: " +super.getStudentNumber() );

Preview
s = new Student (); s.setName ("Warren Peace"); s.setStudentNumber (1234); s.writeOutput (); GraduateStudent g = new GraduateStudent("Ariel Bender", 5678,

Preview
public void writeOutput () { System.out.println ("Name: " + name); } public boolean hasSameName (Person otherPerson) { return this.name.equalsIgnoreCase (otherPerson.name); }

Preview
} public void writeOutput () { System.out.println ("Name: " + getName ()); System.out.println ("Student Number: " + studentNumber); } public boolean equals (Student otherStudent) { return this.hasSameName (otherStudent) && (this.studentNumber == otherStudent.studentNumber);

output.PNG (65 K) Download Preview

Screenshots
Screenshot
Purchase Answer