Question

Page 1/2 ECE25100 Object Oriented Programming Lab 8: Inheritance Description: The purpose of this lab is to practice inherita

Page 2/2 person <Cinterface>> Comparable anstance variables private String mysigtName private String myLastName private Strin

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution of Above java code is provided in below code snippets. Code explaination is given in code comments. I have tried to implement the code in very easy to understandable manner. If any more help is needed regarding the code write in comment section.

Person.java

 public class Person {      private String myFirstName;     private String myLastName;      private String myEmailAddress;  /**      * @param myForstName    * @param myLastName     * @param myEmailAddress         */     public Person(String myFirstName, String myLastName) {          super();                this.myFirstName = myFirstName;                 this.myLastName = myLastName;           if(myLastName.length() >= 5) {               myEmailAddress = myFirstName.charAt(0)+""+myLastName.substring(0,5)+"@yahoo.com";               }               if(myLastName.length() < 5) {                        myEmailAddress = myFirstName.charAt(0)+""+myLastName+"@yahoo.com";              }                       }               /* Getter and setterMethods of Person */        public String getMyFirstName() {                return myFirstName;     }       public void setMyFirstName(String myFirstName) {                this.myFirstName = myFirstName;         }       public String getMyLastName() {                 return myLastName;      }       public void setMyLastName(String myLastName) {          this.myLastName = myLastName;   }       public String getMyEmailAddress() {             return myEmailAddress;  }       public void setMyEmailAddress(String myEmailAddress) {          this.myEmailAddress = myEmailAddress;   }               /* Overriding toString() to provide custom implementation */    @Override       public String toString() {              return "First Name: "+getMyFirstName()+" Last Name: "+getMyLastName()+" has EmailAddress: "+getMyEmailAddress();        }                                                } 

Student.java

  public class Student extends Person implements Comparable<Student> {          String myEmailAddress;  private int myStudentID;        double myGPA;   static int lastIDAssigned = 1000;       /**      * @param myFirstName    * @param myLastName     * @param myGPA          * @param myStudentID    */     public Student(String myFirstName, String myLastName) {                 super(myFirstName, myLastName);                 myFirstName = super.getMyFirstName();           myLastName = super.getMyLastName();             myEmailAddress = super.getMyEmailAddress();             myGPA = 0;              lastIDAssigned++;               myStudentID = lastIDAssigned;   }               /* Getter and setterMethods of Student */       public String getMyFirstName() {                return super.getMyFirstName();  }       public void setMyFirstName(String myFirstName) {                super.setMyFirstName(myFirstName);      }       public String getMyLastName() {                 return super.getMyLastName();   }       public void setMyLastName(String myLastName) {          super.setMyLastName(myLastName);        }       public String getMyEmailAddress() {             return super.getMyEmailAddress();       }       public void setMyEmailAddress(String myEmailAddress) {          this.myEmailAddress = myEmailAddress;   }       public int getMyStudentID() {           return myStudentID;     }       public void setMyStudentID(int myStudentID) {           this.myStudentID = myStudentID;         }       public double getMyGPA() {              return myGPA;   }       public void setMyGPA(double myGPA) {            this.myGPA = myGPA;     }               public String superClassToString() {            return super.toString();        }               /* Overriding toString() to provide custom implementation */    @Override       public String toString() {              return super.toString()+" has StudentID: "+getMyStudentID()+" has GPA: "+getMyGPA();    }               /* Implementing ComapreTo method to Provide comparision betwee students on the basis of their GPA */    @Override       public int compareTo(Student student) {                 if(this.getMyGPA() > student.getMyGPA()) {                   System.out.println(this.getMyFirstName()+" "+this.getMyLastName()+" has greater GPA than "+student.getMyFirstName()+" "+student.getMyLastName());                       return 1;               }               if(this.getMyGPA() < student.getMyGPA()) {                   System.out.println(this.getMyFirstName()+" "+this.getMyLastName()+" has less GPA than "+student.getMyFirstName()+" "+student.getMyLastName());                  return -1;              }                                       System.out.println(this.getMyFirstName()+" "+this.getMyLastName()+" has equal GPA to "+student.getMyFirstName()+" "+student.getMyLastName());                   return 0;                                       }                                                        } 

Main.java <Contains driver code of program>

  public class Test {              public static void main(String[] args) {                                /* creating Person Objects */           Person p1 = new Person("Tom", "Pal");           Person p2 = new Person("Peter", "Parker");                              /* Creating Student object and setting their GPA value respectively */          Student s1 = new Student("Scarlett", "Johansson");              s1.setMyGPA(3.2);               Student s2 = new Student("Will", "Smith");              s2.setMyGPA(3.0);                               /* Printing All Person's Info */                System.out.println("List of All People");               System.out.println(p1.toString());              System.out.println(p2.toString());                              System.out.println(s1.superClassToString());            System.out.println(s2.superClassToString());                            /* Printing only Students Info*/                System.out.println("\n\nList of All Students");                 System.out.println(s1.toString());              System.out.println(s2.toString());                              System.out.println("\n\nComparison Of Students");               s1.compareTo(s2);                                       } } 

Output:

Project Run Window Help File Edit Source Refactor Navigate Search Helal FREL Person.java * Student.java Test.java Nationa 3 p

Add a comment
Know the answer?
Add Answer to:
Page 1/2 ECE25100 Object Oriented Programming Lab 8: Inheritance Description: The purpose of this lab is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description:...

    This is for Java Programming Please use comments Customer and Employee data (15 pts) Problem Description: Write a program that uses inheritance features of object-oriented programming, including method overriding and polymorphism. Console Welcome to the Person Tester application Create customer or employee? (c/e): c Enter first name: Frank Enter last name: Jones Enter email address: frank44@ hot mail. com Customer number: M10293 You entered: Name: Frank Jones Email: frank44@hot mail . com Customer number: M10293 Continue? (y/n): y Create customer...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • **Using NetBeans** Lesson Using inheritance to reuse classes. Deliverables person.java student.java studentAthlete.java app.java Using inheritance and...

    **Using NetBeans** Lesson Using inheritance to reuse classes. Deliverables person.java student.java studentAthlete.java app.java Using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student...

  • CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!)....

    CS 2050 – Programming Project # 1 Due Date: Session 3 (that is, in one week!). Can be resubmitted up to two times until Session 6 if your first version is submitted by session 3. In this project, you create two classes for tracking students. One class is the Student class that holds student data; the other is the GradeItem class that holds data about grades that students earned in various courses. Note: the next three projects build on this...

  • CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be...

    CompSci 251: Intermediate Computer Programming Spring 2017 -Java Lab 5 For this lab we will be looking at static, or class variables. Remember that where instances variables are associated with a particular instance, static variables are associated with the particular class. Specifically, if there are n instances of a class, there are n copies of an instance variable, but exactly one copy of a static variable (even when n = 0). Student - uniqueId : int - id: int -...

  • 2 Lab5 - Compatibility Mode - Saved to my Mac View Tell me jout References Mailings...

    2 Lab5 - Compatibility Mode - Saved to my Mac View Tell me jout References Mailings Review A Аа C 21 पा daBbCode AaBbCcDc AaBbCcDe AaBb CcD ABCDdF Emphasis Heading 1 Heading 2 Heading Heading ANA- Objectives: In this lab, the following topics will be covered 1. Inheritance in Java), 2. Overriding, and Hiding Inheritance (in Java): Inheritance is an important object-oriented concept that allows classes to be reused in order to define similar, but distinct, classes. In this lab...

  • Amend the Date.java file on p. 77 (also available on the lab drive) so that the...

    Amend the Date.java file on p. 77 (also available on the lab drive) so that the Date class: a. Has a month field that is a String instead of an int. b. Has a third constructor that will take in a String parameter (only.) This constructor will parse the String into the month, day, and year fields. The String is provided in the following format: “Month Day, Year”. If the tokenizing and/or parsing generates an exception, the date defaults to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT