Question

In: Computer Science

This program will calculate the amount for a semester bill at The University of Nantucket. Part...

This program will calculate the amount for a semester bill at The University of Nantucket.

Part 1:

Create a class Course.java:

The class has non-static instance variables:

private String department
private int courseNumber
private int courseCredits
private double courseCost
The class must have a default constructor which will set values as follows:

department = 0
courseNumber = 0
courseCost = 0
The class must a non-default constructor which will set values as follows:

department = value passed to constructor
courseNumber = value passed to constructor
courseCredits = value passed to constructor
courseCost = no value will be passed to the constructor, courseCost will be calculated as courseCredits * $120
The class must have the appropriate getter and setter methods. The setter methods must trap the user to enter valid information as follows:

courseNumbers must be 100 - 499 (inclusive)
courseCredits must be 1, 3, or 4
department must be ENG, MTH, CSC, HST, HUM, SCI, LAN, PHY
The class must have a toString method and an equals method.


Part 2:

Create a subclass LabCourse.java which adds $50 to the courseCost.

The class must have the appropriate default and non-default constructors.

Getter and setter methods are not needed.

The class must have a toString method and an equals method. Both of those methods must override the inherited toString and equals methods.

A course is a lab course if the department is CSC, SCI or PHY.

Part 3:

Develop a program (DemoNantucket.java) that asks the user for the number of courses they will take, creates an array of Courses, gets the information for each course (department, courseNumber, courseCredits), creates instances of the appropriate class, populates the array with the instances, calculates the cost of each course and the total bill and then prints the information for each course and the total of the bill to the user.


Submit all 3 files.

Solutions

Expert Solution

Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.

Code:

DemoNantucket.java

import java.util.Scanner;

public class DemoNantucket {

   static void print(String msg){
       System.out.println(msg);
   }
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner input=new Scanner(System.in);
       int totalCourses;
       String department;
       int courseNumber;
       int courseCredits;
       double totalCost=0;
      
       print("Please give total number of course.");
       totalCourses=Integer.parseInt(input.nextLine());
       Course[] courseArray = new Course[totalCourses];
       for(int i=0;i<totalCourses;i++){
               print("Please give department :");
               department=input.nextLine();
               print("Please give course Number (100-499) :");
               courseNumber=Integer.parseInt(input.nextLine());
               print("Please give course credits (1,2 or 3) :");
               courseCredits=Integer.parseInt(input.nextLine());              
               if(department.equals("PHY") || department.equals("CSC")|| department.equals("SCI")){
                   LabCourse temp=new LabCourse(department,courseNumber,courseCredits);
                   courseArray[i]=temp;
               }else{
                   Course temp=new Course(department,courseNumber,courseCredits);
                   courseArray[i]=temp;
               }
               totalCost+=courseArray[i].getCourseCost();
       }
       for(int i=0;i<totalCourses;i++){
           print(courseArray[i].toString());
       }
       print("Total Bill is :"+totalCost);
       input.close();
   }

}

---------------------------------------

Course.java


public class Course {
       private String department;
       private int courseNumber;
       private int courseCredits;
       private double courseCost;
      
  
   public Course() {
       this.department="0";
       this.courseNumber=0;
       this.courseCost=0;
   }
  
   public Course(String department,int courseNumber,int courseCredits){
       setDepartment(department);
       setCourseNumber(courseNumber);
       setCourseCredits(courseCredits);
       setCourseCost(courseCredits*120);
   }
  
   public double getCourseCost() {
       return courseCost;
   }
   public void setCourseCost(double courseCost) {
       this.courseCost = courseCost;
   }
   public int getCourseCredits() {
       return courseCredits;
   }
   public void setCourseCredits(int courseCredits) {
       if(courseCredits == 1 || courseCredits == 2 ||courseCredits == 3){
       this.courseCredits = courseCredits;
       }else{
           throw new IllegalArgumentException("Course Credits can be 1, 2 or 3 only.");
       }
   }
   public int getCourseNumber() {
       return courseNumber;
   }
   public void setCourseNumber(int courseNumber) {
       if(courseNumber>=100 && courseNumber<=499){
       this.courseNumber = courseNumber;
       }else{
           throw new IllegalArgumentException("CourseNumbers must be 100 - 499 (inclusive)");
       }
   }
   public String getDepartment() {
       return department;
   }
   public void setDepartment(String department) {
       if(department.equals("ENG") || department.equals("MTH") || department.equals("CSC") || department.equals("HST") || department.equals("HUM") || department.equals("SCI") || department.equals("LAN") || department.equals("PHY"))
       this.department = department;
       else
           throw new IllegalArgumentException("department must be ENG, MTH, CSC, HST, HUM, SCI, LAN, PHY");
   }
  
   @Override
   public String toString(){
       String courseDetails;
       courseDetails = "Department :"+getDepartment()+"\n";
       courseDetails += "Course Number :"+getCourseNumber()+"\n";
       courseDetails += "Course Cost :"+getCourseCost()+"\n";
       courseDetails += "Course credits :"+getCourseCredits()+"\n";
       return courseDetails;
   }
  
   public boolean equals(Course obj)
   {
       if (this == obj)
       return true;
       if (obj == null)
       return false;
       if (getClass() != obj.getClass())
       return false;
   if(obj.getDepartment().equals(getDepartment()) && obj.getCourseCredits()== getCourseCredits() && obj.getCourseNumber() == getCourseNumber()){
       return true;
   }else{
       return false;
   }
   }
}
---------------------------------------------

LabCourse.java


public class LabCourse extends Course {
   public LabCourse(){
       super();
   }
  
   public LabCourse(String department,int courseNumber,int courseCredits){
       super(department,courseNumber,courseCredits);
       if(department.equals("PHY") || department.equals("CSC")|| department.equals("SCI")){
           setCourseCost(getCourseCost()+50);
       }else{
           throw new IllegalArgumentException("A course is a lab course if the department is CSC, SCI or PHY.");
       }
   }
  
   @Override
   public String toString(){
       String courseDetails;
       courseDetails ="Its a lab course :\n";
       courseDetails += "Department :"+getDepartment()+"\n";
       courseDetails += "Course Number :"+getCourseNumber()+"\n";
       courseDetails += "Course Cost :"+getCourseCost()+"\n";
       courseDetails += "Course credits :"+getCourseCredits()+"\n";
       return courseDetails;
   }
  
   public boolean equals(LabCourse obj)
   {
       if (this == obj)
       return true;
       if (obj == null)
       return false;
       if (getClass() != obj.getClass())
       return false;
   if(obj.getDepartment().equals(getDepartment()) && obj.getCourseCredits()== getCourseCredits() && obj.getCourseNumber() == getCourseNumber()){
       return true;
   }else{
       return false;
   }
   }
}
------------------

Output:


Related Solutions

Write a program in python to calculate the amount each person must pay toward the bill...
Write a program in python to calculate the amount each person must pay toward the bill and toward the tip, for a group of friends who are eating out together. Since you are all friends, it is okay to split the costs evenly. Your program should take as input: The restaurant bill (without tax or tip) as a floating point number The sales tax rate as a floating point number (for example: an 8% tax rate would be 0.08) The...
In this exercise, you will create a program that displays the amount of a cable bill....
In this exercise, you will create a program that displays the amount of a cable bill. The amount is based on the type of customer, as shown in Figure 10-30. For a residential cus- tomer, the user will need to enter the number of premium channels only. For a business customer, the user will need to enter the number of connections and the number of premium channels. Use a separate void function for each customer type. If necessary, create a...
In this exercise, you will create a program that displays the amount of a cable bill....
In this exercise, you will create a program that displays the amount of a cable bill. The amount is based on the type of customer shown in figure 10-30. For a residential customer, the user will need to enter the number of premium channels only. For a business customer, the user will need to enter the number of connections and the number of premium channels. Use a separate void function for each customer type. Enter your C++ instructions into the...
Javascript Calculator Algorithm to calculate a tip percentage given the bill amount and total bill including...
Javascript Calculator Algorithm to calculate a tip percentage given the bill amount and total bill including tip. Asker suer for bill without tip: Ask the user for total bill with tip: Ask the user how many people splitting bill: Submit button to calculate the tip percentage
You are the IT director at Big Ten University. As part of a training program, you...
You are the IT director at Big Ten University. As part of a training program, you decide to draw a DFD that includes some obvious mistakes to see whether your newly hired junior analysts can find them. You came up with the diagram 0 DFD shown in Figure 5-19. Based on the rules explained in this chapter, how many problems should the analysts find?
Overview For this assignment, write a program that will calculate the amount that a customer spends...
Overview For this assignment, write a program that will calculate the amount that a customer spends on tickets at a movie theater. This program will be continued in program 3 so it is important that this program is completed. Basic Program Logic The basic logic for this program is similar to program 1: the user is asked to enter values, a calculation is performed, and the result of the calculation is displayed. For this program, prompt the user for the...
You are currently part of a university work experience program. Your job placement is at the...
You are currently part of a university work experience program. Your job placement is at the municipal transit centre. Your supervisor is responsible for the recording and distribution of monthly transit passes to authorized vendors throughout the city. The vendors pay €50 per bus pass and sell them for €55. Your job is to prepare a monthly reconciliation of the transit passes including the quantity sold, the number actually distributed, the unsold passes, and the cash proceeds. You are unable...
Customer Amount of Tip Amount of Bill Number of Diners Customer Amount of Tip Amount of...
Customer Amount of Tip Amount of Bill Number of Diners Customer Amount of Tip Amount of Bill Number of Diners 1 $ 7.70 $ 46.02 1 16 $ 3.30 $ 23.59 2 2 4.50 28.23 4 17 3.50 22.30 2 3 1.00 10.65 1 18 3.25 32.00 2 4 2.40 19.82 3 19 5.40 50.02 4 5 5.00 28.62 3 20 2.25 17.60 3 6 4.25 24.83 2 21 3.90 58.18 1 7 0.50 6.25 1 22 3.00 20.27 2...
The distribution of the amount of money spent by students on textbooks in a semester is...
The distribution of the amount of money spent by students on textbooks in a semester is approximately normal in shape with a mean of 349 and a standard deviation of 24. According to the standard deviation rule, approximately 95% of the students spent between _____$ and _____$ on textbooks in a semester. Question 12 Type numbers in the boxes. 1 points The distribution of IQ (Intelligence Quotient) is approximately normal in shape with a mean of 100 and a standard...
The distribution of the amount of money spent by students on textbooks in a semester is...
The distribution of the amount of money spent by students on textbooks in a semester is approximately normal in shape with a mean of 349 and a standard deviation of 24. According to the standard deviation rule, approximately 95% of the students spent between ____$ and ____$ on textbooks in a semester. Question 12 Type numbers in the boxes. 1 points The distribution of IQ (Intelligence Quotient) is approximately normal in shape with a mean of 100 and a standard...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT