Question

In: Computer Science

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 = ‘ ‘;

      return letterGrade;

   }

}

For the GraduateStudent class, the lowest passing grade in the getLetterGrade() is a ‘C’.

90-100 =A

80-89 = B

70-79 =C

<70 = F

For the UnderGraduateStudent, the lowest passing grade in the getLetterGrade() is a ‘D’.

90-100 =A

80-89 = B

70-79 =C

60-69 = D

<60 =F

class UnderGraduateStudent extends Student {

}

class GraduateStudent extends Student {

}

Create a main() method in a separate class to test child classes created.

Solutions

Expert Solution

The program is given below. The comments are provided for the better understanding of the logic.

class StudentTest{  
        public static void main(String[] args) {
                Student s;
                
                //Call the classes for testing.
                //Print the returned values.
                //The getLetterGrade method will call the corresponding overridden method, 
                //depending on whether the calling instance is UnderGraduateStudent or GraduateStudent
                System.out.println("UnderGraduateStudents");
                s = new UnderGraduateStudent(1001, 89, 98);
                System.out.println("Average: " + s.calcAvg());
                System.out.println("Grade: " + s.getLetterGrade());
                
                s = new UnderGraduateStudent(1001, 40, 34);
                System.out.println("Average: " + s.calcAvg());
                System.out.println("Grade: " + s.getLetterGrade());             
                
                System.out.println("\nGraduateStudents");
                s = new GraduateStudent(1002, 55, 66);
                System.out.println("Average: " + s.calcAvg());
                System.out.println("Grade: " + s.getLetterGrade());             
                
                s = new GraduateStudent(1002, 55, 98);
                System.out.println("Average: " + s.calcAvg());
                System.out.println("Grade: " + s.getLetterGrade());                             
        }
}  
class Student {
        private int id;
        private int midtermExam;
        private int finalExam;

        //This constructor is required to pass the id and marks and assign it to the private members..
        public Student(int id, int midtermExam, int finalExam) {
                this.id = id;
                this.midtermExam = midtermExam;
                this.finalExam = finalExam;
        }

        public double calcAvg() {
                double avg;
                avg = (midtermExam + finalExam) / 2.0;
                return avg;
        }

        public char getLetterGrade() {
                char letterGrade = ' ';
                return letterGrade;
        }
}
class UnderGraduateStudent extends Student {
        //This constructor is required to pass the id and marks and assign it to the private members.
        public UnderGraduateStudent(int id, int midtermExam, int finalExam) {
                //Call the base class constructor
                super(id, midtermExam, finalExam);
        }       
        
        @Override
        public char getLetterGrade() {
                //Calculate the grade based on the conditions given and return the value.
                char letterGrade = ' ';         
                double avg = calcAvg();
                if(avg >= 90)
                        letterGrade = 'A';
                else if(avg >= 80)
                        letterGrade = 'B';
                else if(avg >= 70)
                        letterGrade = 'C';
                else if(avg >= 60)
                        letterGrade = 'D';
                else
                        letterGrade = 'F';              

                return letterGrade;
        }       
}
class GraduateStudent extends Student {
        //This constructor is required to pass the id and marks and assign it to the private members.
        public GraduateStudent(int id, int midtermExam, int finalExam) {
                //Call the base class constructor
                super(id, midtermExam, finalExam);
        }       
        
        @Override
        public char getLetterGrade() {
                //Calculate the grade based on the conditions given and return the value.
                char letterGrade = ' ';
                double avg = calcAvg();
                if(avg >= 90)
                        letterGrade = 'A';
                else if(avg >= 80)
                        letterGrade = 'B';
                else if(avg >= 70)
                        letterGrade = 'C';
                else
                        letterGrade = 'F';                      
                return letterGrade;
        }       
}

The screenshots of the code and output are provided below.


Related Solutions

Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash()...
Create 2 derived classes from Clothing class: Pants class Write only necessary constructors Override the wash() method to indicate that pants are dry clean only. Include additional method hang() Add to your driver/tester file to make and print new pants objects and test it. Shirt class Include additional property of type string called sleeves. Write necessary constructors For sleeves only allow it to be set to {"short", "long", "none"} For size, only allow {"S","M","L"} Override the wash() method to indicate...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override...
Create a concrete LinkedList class that extends the provided ALinkedList class. You will need to override the extract()method in your class. You can use your main() method for testing your method (although you do not need to provide a main method). Recall that a list is an ordered collection of data X_0, X_1, X_2, ..., X_n-1 The extract(int start, int end) method removes all elements X_start, X_start_1, ..., X_end-1 from the list. It also returns all removed elements as a...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services...
Classes and Objects Write a program that will create two classes; Services and Supplies. Class Services should have two private attributes numberOfHours and ratePerHour of type float. Class Supplies should also have two private attributes numberOfItems and pricePerItem of type float. For each class, provide its getter and setter functions, and a constructor that will take the two of its private attributes. Create method calculateSales() for each class that will calculate the cost accrued. For example, the cost accrued for...
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
Design You will need to have at least four classes: a parent class, a child class,...
Design You will need to have at least four classes: a parent class, a child class, a component class, and an unrelated class. The component object can be included as a field in any of the other three classes. Think about what each of the classes will represent. What added or modified methods will the child class have? What added fields will the child class have? Where does the component belong? How will the unrelated class interact with the others?...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Each student need to get a partner from the class and create a team that needs...
Each student need to get a partner from the class and create a team that needs to use Excel from and read how to prepare a: Frequency Histogram, Dot diagram, Stem and Leaf diagram. Each member of the team needs to collect 20 samples values of any variable and prepare a report. * Frequency Histogram (5 bins, Intervals or class) Steam and Leaf Diagram Dot Diagram Identify the concentration and variation tendencies from the data charts.
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT