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

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 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...
Write a program that can calculate the amount of federal tax aperson owes for the...
Write a program that can calculate the amount of federal tax a person owes for the upcoming year.After calculating the amount of tax owed, you should report to the user their filing status (single/joint), which tax rate they fell under, as well as the tax owed. Example: “As a single filer you fell under 12% tax bracket and you owe $3500.”Disclaimer: This example is simplified and is not intended to be an accurate representation of how to calculate your taxes.Your...
You are asked to write a program to help a small company calculate the amount of...
You are asked to write a program to help a small company calculate the amount of money to pay their employees. In this simplistic world, the company has exactly three employees. However, the number of hours per employee may vary. The company will apply the same tax rate to every employee. Problem Description Inputs (entered by user of the program) • Name of first employee • Hourly rate • Number of hours worked • Name of second employee • Hourly...
Create a program that calculate the amount of a mortgage payment. Refer to the appropriate video...
Create a program that calculate the amount of a mortgage payment. Refer to the appropriate video or any online source for how the mathematics of the calculation works. If given these three things: The amount of the loan in whole dollars The number of payments (e.g. 360 for 30-year) The interest rate per payment period in percent ( a positive floating-point number) The program should print out the correct value (in dollars and cents) of the per-period payment of principal...
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