In: Computer Science
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods.
Question 1: --------------------------------------------------- file-name: Person.java ----------------------------------------------------- public class Person { String personName; int year; Person(String name, int year) { this.personName = name; this.year = year; } // END of constructor public String getPersonName() { return personName; } public int getYear() { return year; } public String toString() { String str = "\nName: "+getPersonName() + "\nBirth year: "+getYear(); return str; } } // END of class
-----------------------------------------------------------------------------------------------------------------------------------
file-name: Student.java --------------------------- public class Student extends Person { String major; Student(String name, int year, String major) { super(name,year); this.major = major; } //END of constructor public String getMajor() { return major; } @Override public String toString() { System.out.println(super.toString()); String str = "Major: "+getMajor(); return str; } } // END of class
--------------------------------------------------------------------------------------------------------------------------------------------------
file-name: Instructor.java --------------------------------- public class Instructor extends Person { int salary; Instructor(String name, int year, int salary) { super(name,year); this.salary = salary; } // END of constructor public int getSalary() { return salary; } public String toString() { System.out.println(super.toString()); String str = "Salary: "+getSalary(); return str; } } // END of Instructor class
-----------------------------------------------------------------------------------------------------------------------------------------------------------
file-name: Main.java -------------------------------- public class Main { public static void main(String[] args) { Person p = new Person("John",1985); Student s = new Student("Balayya",1976,"CSE"); Instructor i = new Instructor("Chiranjeevi",1967,25000); System.out.println(p); System.out.println(s); System.out.println(i); } // END of main( ) } // END of Main class
------------------------------------------------
OUTPUT:
________________________________________________________________________________________________________________________________________
NOTE: Person class using Comparable interface. -------------------------------------------------- file-name: Person.java -------------------------- public class Person implements Comparable<Person> { String personName; int year; Person(String name, int year) { this.personName = name; this.year = year; } // END of constructor public String getPersonName() { return personName; } public int getYear() { return year; } public String toString() { String str = "Name: "+getPersonName() + "\nBirth year: "+getYear(); return str; } public int compareTo(Person p) { return this.getPersonName().compareTo(p.getPersonName()); } } // END of class
-------------------------------------------------------------------------------------------------------------------------
file-name: Main.java -------------------------- public class Main { public static void main(String[] args) { // creating an array of size 10, to store person objects. Person[] people = new Person[10]; people[0] = new Person("John",1985); people[1] = new Person("Kiran",1995); people[2] = new Person("Chiranjeevi",1969); people[3] = new Person("Nagarjuna",1965); people[4] = new Person("Pawan kalyan",2005); people[5] = new Person("Prabhas",2015); people[6] = new Person("Shourya",2000); people[7] = new Person("NTR",1999); people[8] = new Person("Rajnikanth",1985); people[9] = new Person("Agasthya",1979); System.out.println("\nArray before sorting: "); for(int i=0; i<people.length; i++) { System.out.print(people[i].getPersonName()+" "); } // sorting the array // using Bubble sort to sort the array for( int i=1; i < people.length; i++ ) { for(int j=0; j<people.length - i; j++) { if( (people[j].getPersonName().compareTo(people[j+1].getPersonName())) > 0 ) { // swapping Person temp = people[j]; people[j] = people[j+1]; people[j+1] = temp; } } } // System.out.println("\nAfter sorting: "); // for(int i=0; i<people.length; i++) { // System.out.print(people[i].getPersonName()+" "); // } System.out.println("\nFirst Name in the array: "); System.out.println(people[0]); System.out.println("Last Name in the array: "); System.out.println(people[people.length-1]); } // END of main( ) } // END of Main class
--------------------------------------------------------------------------------------------
OUTPUT:
___________________________________________________________________________________________________________________________________________________________________
Hey, I found some difficulty in understanding a,b problems. Please can you provide more details about data class , and what should be written in Measurable interface.
I will implement those as soon as you provide the required data.
Thank You!!