Question

a) Create an abstract class Student. The class contains fields for student Id number, name, and...

a) Create an abstract class Student. The class contains fields for student Id number, name, and tuition. Includes a constructor that requires parameters for the ID number and name. Include get and set method for each field; setTuition() method is abstract.

b) The Student class should throw an exception named InvalidNameException when it receives an invalid student name (student name is invalid if it contains digits).

c) Create three student subclasses named UndergradStudent, GradeStudent, and StudentAtLarge, each with a unique setTuition() method and a field CGPA. Create one or more constructors for each class. Tuition for an UnderGraduateStudent is $4000 per semester, tuition for an GraduateStudent is $6000 per semester, and tutition for a StudentAtLarge is $2000 per semester.

d) The UndergradStudent, GradeStudent, and StudentAtLarge classes should throw an exception names InvalidCGPAException when it receives an invalid cgpa, a valid cgpa is between 0 and 4.

e) Write an application named Driver that creates objects of each subclass and demonstrate how the methods work.



2

For example creating an object of UndergradStudent

UndergradStudent ugStud= new UndergradStudent(111, "Lambert1",3.4);
would raise an exception Student name is invalid
UndergradStudent ugStud= new UndergradStudent (111, "Lambert",4.4);
would raise an exception Student name cgpa is invalid
UndergradStudent ugStud= new UndergradStudent (111, "Lambert",3.4);
The output would be
Student Id 111 name Lambert CGPA 3.4 Tuition is 4000.0.

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

Student.java

import javax.naming.InvalidNameException;

public abstract class Student {

   int studentID;
   String name;
   double tutuion;
   public Student(int studentID, String name) {
       super();
       this.studentID = studentID;
       setName(name);
   }
   public int getStudentID() {
       return studentID;
   }
   public void setStudentID(int studentID) {
       this.studentID = studentID;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
      
       if(name.contains("[0-9]")) {
           try {
               throw new InvalidNameException("Name should not contain Digits");
           } catch (InvalidNameException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
          
           return;
       }
          
       this.name = name;
   }
   public double getTutuion() {
       return tutuion;
   }
   public abstract void setTutuion();
  
  
}

UndergraduateStudent.java

import javax.naming.InvalidNameException;

public class UndergraduateStudent extends Student{
  
   double cgpa;
  

   public UndergraduateStudent(int studentID, String name,double cgpa) {
       super(studentID, name);
       setCgpa(cgpa);
       setTutuion();
       // TODO Auto-generated constructor stub
   }

   @Override
   public void setTutuion() {
       // TODO Auto-generated method stub
       this.tutuion=4000;
   }

   public void setCgpa(double cgpa) {
      
       if(cgpa>0 && cgpa<=4)
       this.cgpa = cgpa;
       else
           try {
               throw new InvalidNameException("CGPA should be Between 0 and 4");
           } catch (InvalidNameException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
   }
  
public void print() {
      
       System.out.println("Student Id "+this.studentID+" name "+this.name+" CGPA "+this.cgpa+" Tution is "+this.tutuion);
   }
  
  

}

GradeStudent.java

import javax.naming.InvalidNameException;

public class GradeStudent extends Student{
  
   double cgpa;

   public GradeStudent(int studentID, String name,double cgpa) {
       super(studentID, name);
       setCgpa(cgpa);
       setTutuion();
       // TODO Auto-generated constructor stub
   }

   @Override
   public void setTutuion() {
       // TODO Auto-generated method stub
       this.tutuion=6000;
   }

   public void setCgpa(double cgpa) {
       if(cgpa>0 && cgpa<=4)
           this.cgpa = cgpa;
           else
               try {
                   throw new InvalidNameException("CGPA should be Between 0 and 4");
               } catch (InvalidNameException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
   }
  
public void print() {
      
       System.out.println("Student Id "+this.studentID+" name "+this.name+" CGPA "+this.cgpa+" Tution is "+this.tutuion);
   }
}

StudentAtLarge.java

import javax.naming.InvalidNameException;

public class StudentAtLarge extends Student{
  
   double cgpa;

   public StudentAtLarge(int studentID, String name,double cgpa) {
       super(studentID, name);
       setCgpa(cgpa);
       setTutuion();
       // TODO Auto-generated constructor stub
   }

   @Override
   public void setTutuion() {
       // TODO Auto-generated method stub
       this.tutuion=2000;
      
      
   }
  
   public void setCgpa(double cgpa) {
       if(cgpa>0 && cgpa<=4)
           this.cgpa = cgpa;
           else
               try {
                   throw new InvalidNameException("CGPA should be Between 0 and 4");
               } catch (InvalidNameException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
   }
  
   public void print() {
      
       System.out.println("Student Id "+this.studentID+" name "+this.name+" CGPA "+this.cgpa+" Tution is "+this.tutuion);
   }

}

Driver.java

public class Driver {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

      
       UndergraduateStudent ugStud= new UndergraduateStudent(111, "Lambert1",3.4);
       ugStud.print();
       UndergraduateStudent ugStud1= new UndergraduateStudent (111, "Lambert",4.4);
      
      
       GradeStudent grStud= new GradeStudent(111, "Lambert1",3.4);
       grStud.print();
       GradeStudent grStud1= new GradeStudent(111, "Lambert",4.4);
      
      
       StudentAtLarge st= new StudentAtLarge(111, "Lambert1",3.4);
       st.print();
       StudentAtLarge st1= new StudentAtLarge(111, "Lambert",4.4);
      
      
   }

}

Output

Student Id 111 name Lambert1 CGPA 3.4 Tution is 4000.0
javax.naming.InvalidNameException: CGPA should be Between 0 and 4
   at test.UndergraduateStudent.setCgpa(UndergraduateStudent.java:29)
   at test.UndergraduateStudent.<init>(UndergraduateStudent.java:12)
   at test.Driver.main(Driver.java:11)
Student Id 111 name Lambert1 CGPA 3.4 Tution is 6000.0
javax.naming.InvalidNameException: CGPA should be Between 0 and 4
   at test.GradeStudent.setCgpa(GradeStudent.java:27)
   at test.GradeStudent.<init>(GradeStudent.java:11)
   at test.Driver.main(Driver.java:16)
Student Id 111 name Lambert1 CGPA 3.4 Tution is 2000.0
javax.naming.InvalidNameException: CGPA should be Between 0 and 4
   at test.StudentAtLarge.setCgpa(StudentAtLarge.java:29)
   at test.StudentAtLarge.<init>(StudentAtLarge.java:11)
   at test.Driver.main(Driver.java:21)

Feel free to ask any doubts, if you face any difficulty in understanding.

Please upvote the answer if you find it helpful

Add a comment
Know the answer?
Add Answer to:
a) Create an abstract class Student. The class contains fields for student Id number, name, and...
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
  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • 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...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Graded Exercise Create a class named Student. A Student has fields for an ID number, number...

    Graded Exercise Create a class named Student. A Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

  • A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 21...

    A. Create a CollegeCourse class. The class contains fields for the course ID (for example, CIS 210), credit hours (for example, 3), and a letter grade (for example, A). Include get and set methods for each field. Create a Student class containing an ID number and an array of five CollegeCourse objects. Create a get and set method for the Student ID number. Also create a get method that returns one of the Student’s CollegeCourses; the method takes an integer...

  • Create a class named Poem that contains the following fields: title - the name of the...

    Create a class named Poem that contains the following fields: title - the name of the poem (of type String) lines - the number of lines in the poem (of type int) Include a constructor that requires values for both fields. Also include get methods to retrieve field values. Create three subclasses: Couplet, Limerick, and Haiku. The constructor for each subclass requires only a title; the lines field is set using a constant value. A couplet has two lines, a...

  • Create a class named Student, where each Student consists of a student id (a number between...

    Create a class named Student, where each Student consists of a student id (a number between 0 and 99999), grade level (1-12), and a full name. Include set and get methods for all fields. Save this as Student.java. Then, write an application that declares a Student object and prompts the user for the three required pieces of information, one at a time. For student id, keep prompting until the user enters a valid number (0-99999). Likewise, keep prompting for a...

  • PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a...

    PYTHON*************** Create a Person Class that: Accepts First Name, Last Name, Age, and Gender Has a method that adds all persons Validate all input, not left empty for First Name, Last Name, Gender and numeric for age and throw an exception if it does not validate within the class. Create a Student Class that: Inherits the Person Class Accepts GPA Validate GPA (>= 0 and <= 4.0) and throw exception if it does not validate within the class. Has methods...

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