Question

In: Computer Science

Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person...

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.

    1. Add a method
      Public static Measurable max(Measurable[ ] objects)
      to the Data class that returns the object with the largest measure.
    2. Implement a class Quiz that implements the Measurable interface (in Slide 6 of Chapter 10). A quiz has a score and a letter grade (such as B+). Supply a test program QuizTester that uses the Data class in a) to process an array of quizzes and display the average score and the quiz with the highest score (both letter grade and score).

    1. (Chapter 10, 30 points) Supply a class Person that implements the Comparable interface. Compare persons by their name. Ask the user to input ten names and generate ten Person objects. Using the compareTo method, determine the first and last person among them (in lexicographic order) and print them.

    Solutions

    Expert Solution

    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!!


    Related Solutions

    JAVA Design a class named Person and its two derived classes named Student and Employee. Make...
    JAVA Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and datehired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a...
    In Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime,...
    In Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime, Day and Month. An appointment has a description (for example, "Root Canal"), and dates information (you can use Date object or int Year, Int Month, Int Day). Fill an array of Appointment objects with a mixture of appointments. Write a method OccursOn inside each of the sub classes that checks whether the appointment occurs on that date (OneTime), day (Day) or month (Month). Ask...
    Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child...
    Here I'm using "person" as an abstract superclass or parent class, and "Student" as a derived/child class. // File name: Person.h // Person is the base, or parent for chapter11 #pragma once #include <iostream> #include <string> using namespace std; class Person { private:    string fName;    string lName;    int areaCode;    int phone; public:    Person();    Person(string, string);    void setFirst(string);    void setLast(string);    void setPhoneNumber(int, int);    string getFirstlast();    string getLastFirst();    string getPhoneNumber();...
    Using Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime,...
    Using Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime, Day and Month. An appointment has a description (for example, "Root Canal"), and dates information (you can use Date object or int Year, Int Month, Int Day). Fill an array of Appointment objects with a mixture of appointments. Write a method OccursOn inside each of the sub classes that checks whether the appointment occurs on that date (OneTime), day (Day) or month (Month). Ask...
    Design a class named Person and its two subclasses named Student and Employee. Make Faculty and...
    Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to...
    Create two child classes, UnderGraduateStudent and GraduateStudent that will extend from the Student class. Override the...
    Create two child classes, UnderGraduateStudent and GraduateStudent that will extend from the Student class. Override the char getLetterGrade() method in each of the child classes. Use Student.java class defined below: (complete and compile) class Student {    private int id;    private int midtermExam;    private int finalExam;    public double calcAvg() {       double avg;       avg = (midtermExam + finalExam) / 2.0;       return avg;    }    public char getLetterGrade() {       char letterGrade = ‘ ‘;...
    To make class DressShirt inherit the functionality of both Clothing and Shirt (from previous question), the...
    To make class DressShirt inherit the functionality of both Clothing and Shirt (from previous question), the class header would be: Group of answer choices public class DressShirt inherits Shirt, Clothing public class DressShirt inherits Shirt public class DressShirt extends Shirt public class DressShirt extends Shirt, Clothing If Shirt inherits Clothing, then Shirt has direct access to the ______ members of Clothing Group of answer choices public and protected public private protected private and protected Shirt inherits Clothing. What is the...
    Java Question Design a class named Person and its two subclasses named Student and Employee. Make...
    Java Question Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the Date class to create an object for date hired. A faculty member has office hours and a rank. A...
    C++ Assignment 1: Make two classes practice For each of the two classes you will create...
    C++ Assignment 1: Make two classes practice For each of the two classes you will create for this assignment, create an *.h and *.cpp file that contains the class definition and the member functions. You will also have a main file 'main.cpp' that obviously contains the main() function, and will instantiate the objects and test them. Class #1 Ideas for class one are WebSite, Shoes, Beer, Book, Song, Movie, TVShow, Computer, Bike, VideoGame, Car, etc Take your chosen class from...
    The answer should be in JAVA. You will design and implement two classes to support a...
    The answer should be in JAVA. You will design and implement two classes to support a client program, RockPaperScissorsGame.java, to simulate Rock-Paper-Scissors game. Read and understand the client program to find out the requirements for the HandShape and Player classes. The rules of the Rock-Paper-Scissors game are:     Scissors✌️ beats Paper✋ that beats Rock✊ that beats Scissors✌️ Additionally, to simplify the game logic (and complexify a little bit the HandSahpe class) , two players cannot show the same hand shape....
    ADVERTISEMENT
    ADVERTISEMENT
    ADVERTISEMENT