Question

A Java Program Purpose:         Work with Abstract classes. Purpose:         Abstract classes are import because they ensure...

A Java Program

Purpose:         Work with Abstract classes.

Purpose:         Abstract classes are import because they ensure than any children which inherit from it must implement certain methods. This is done to enforce consistency across many different classes.

Problem:        Implement a class called Student. This class needs to be abstract and should contain a String for the name, and a String for the major (and they should be private). The class must have a constructor to set the name and major. The class needs the following public methods: getName() and getMajor() that are used to return the associated data. It also needs two public abstract methods: setClassification(String) takes a string that will be used to set classification and should return nothing. getClassification() that will return a string containing the classification.

                       

Implement a class call UnderGraduate that inherits Student. This class must contain a private String for storing classification. It must have a constructor that takes 3 strings: name, major, and classification. This constructor must pass the name and major to the parent constructor and set the classification in its own member variable.

                       

Implement a class called Graduate that inherits from Student. This should look the same as the UnderGraduate class as far as the constructor and member variable.

For an UnderGraduate, the classification will be “Freshman”, “Sophomore”, “Junior”, and “Senior”. For a Graduate the classification will be “Masters” or “Doctoral”. Be careful we used consistent spelling (for now).

Input:             Write a test class, HW7, that creates at least 2 Undergrad objects and 2 Grad objects and populate them with information and different classifications. Have this class test out all of the functionality.

Output:           Output the required information as indicated in the problem in the "Input" section above.

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

Code:

abstract class Student  //abstract class Student
{
    private String name, major; //all data members are private
    Student(String name, String major)  //parametrized constructor
    {
        this.name = name;
        this.major = major;
    }
    public String getName() //getter method for name
    {
        return name;
    }
    public String getMajor()    //getter method for major
    {
        return major;
    }
    public abstract void setClassification(String classfication);   //abstract method which has to be overriden by child class
    public abstract String getClassification(); //abstract method which has to be overriden by child class
}
class UnderGraduate extends Student //UnderGraduate class extending from Student class
{
    private String classification;
    UnderGraduate(String name, String major, String classification) //Parametrized constructor
    {
        super(name, major); //base class constructor call
        this.classification = classification;   
    }
    public void setClassification(String classification)   //overriden setter method for classification
    {
        this.classification = classification;
    }
    public String getClassification()   //overriden getter method for classification
    {
        return classification;
    }
}
class Graduate extends Student  //Graduate class extending from Student class
{
    private String classification;
    Graduate(String name, String major, String classification)  //Parametrized constructor
    {
        super(name, major); //base class constructor call
        this.classification = classification;
    }
    public void setClassification(String classification)    //overriden setter method for classification
    {
        this.classification = classification;
    }
    public String getClassification()   //overriden getter method for classification
    {
        return classification;
    }
}
public class HW7{   //HW7 the tester class
     public static void main(String []args){    //main method
        UnderGraduate ug1 = new UnderGraduate("UG Name 1", "UG Major 1", "Freshman");   //defined 2 undergraduates
        UnderGraduate ug2 = new UnderGraduate("UG Name 2", "UG Major 2", "Senior");
        System.out.println("Name: "+ug1.getName()+" Major: "+ug1.getMajor()+" Classification: "+ug1.getClassification());
        ug2.setClassification("Junior");
        System.out.println("Name: "+ug2.getName()+" Major: "+ug2.getMajor()+" Classification: "+ug2.getClassification());
        Graduate g1 = new Graduate("G Name 1", "G Major 1", "Masters"); //definied 2 graduates
        Graduate g2 = new Graduate("G Name 1", "G Major 2", "Doctoral");
        System.out.println("Name: "+g1.getName()+" Major: "+g1.getMajor()+" Classification: "+g1.getClassification());
        g2.setClassification("Masters");
        System.out.println("Name: "+g2.getName()+" Major: "+g2.getMajor()+" Classification: "+g2.getClassification());
     }
}

Code Screenshots:

{ ہا { } 36 } 37 class Graduate extends Student //Graduate class extending from Student class 38 { 39 private String classifi

Output:

Name: UG Name 1 Major: UG Major 1 Classification: Freshman Name: UG Name 2 Major: UG Major 2 Classification: Junior Name: G N

Each and everything is explained in the comment section of the code.

Thank you! Hit like if you like my work.

Add a comment
Know the answer?
Add Answer to:
A Java Program Purpose:         Work with Abstract classes. Purpose:         Abstract classes are import because they ensure...
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
  • PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class...

    PART I: Create an abstract Java class named “Student” in a package named “STUDENTS”. This class has 4 attributes: (1) student ID: protected, an integer of 10 digits (2) student name: protected (3) student group code: protected, an integer (1 for undergraduates, 2 for graduate students) (4) student major: protected (e.g.: Business, Computer Sciences, Engineering) Class Student declaration provides a default constructor, get-methods and set-methods for the attributes, a public abstract method (displayStudentData()). PART II: Create a Java class named...

  • PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

    PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is the output of the program as it is written? (Program begins on p. 2) 2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)? /********************************************************************** *           Program:          PRG/421 Week 1 Analyze Assignment *           Purpose:         Analyze the coding for...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • Write java program The purpose of this assignment is to practice OOP with Array and Arraylist,...

    Write java program The purpose of this assignment is to practice OOP with Array and Arraylist, Class design, Interfaces and Polymorphism. Create a NetBeans project named HW3_Yourld. Develop classes for the required solutions. Important: Apply good programming practices Use meaningful variable and constant names. Provide comment describing your program purpose and major steps of your solution. Show your name, university id and section number as a comment at the start of each class. Submit to Moodle a compressed folder of...

  • Write code for 3 classes described below. - Write abstract class called AbstractPerson which had two...

    Write code for 3 classes described below. - Write abstract class called AbstractPerson which had two methods. One called getSchool which is implemented and returns String "Rutgers". Another abstract method called getType to be implemented by subclasses. - 5 points - Write Faculty class which inherits AbstractPerson and implements getType to return String "Faculty" . - 3 points - Write Student class which inherits AbstractPerson and implements getType to return String "Student" . - 3 points

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • Create an abstract class Employee. Your Employee class should include the following attributes: First name (string)...

    Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings.      Create another class HourlyEmployee that inherits from the abstract Employee class.   HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

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