Question

In: Computer Science

Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...

Create a class named CollegeCourse that includes the following data fields:

  • dept (String) - holds the department (for example, ENG)
  • id (int) - the course number (for example, 101)
  • credits (double) - the credits (for example, 3)
  • price (double) - the fee for the course (for example, $360).

All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data. For example, if dept is 'SE', id is 225, and credits is 3, the output from display() should be:

SE225
Non-lab course
3.0 credits
Total fee is $360.0

Create a subclass named LabCourse that adds $50 to the course fee. Override the parent class display() method to indicate that the course is a lab course and to display all the data. For example, if dept is 'BIO', id is 201, and credits is 4, the output from display() should be:

BIO201
Lab course
4.0 credits
Lab fee is $50
Total fee is $530.0

Write an application named UseCourse that prompts the user for course information. If the user enters a class in any of the following departments, create a LabCourse: BIO, CHM, CIS, or PHY. If the user enters any other department, create a CollegeCourse that does not include the lab fee. Then display the course data.

Solutions

Expert Solution

If you have any doubts, please give me comment...

import java.util.*;

public class CollegeCourse {

    // attributes

    String dept;

    int id;

    double credits;

    double price;

    // constructor

    public CollegeCourse(String dept, int id, double credits){

        this.dept = dept;

        this.id = id;

        this.credits = credits;

    }

    // display()

    public void display(){

        System.out.println(dept+id+"\nNon-lab course\n"+credits+" credits");

        System.out.println("Total fee is $"+credits*120);

    }

}

LabCourse.java

import java.util.*;

public class LabCourse extends CollegeCourse{

    // attributes

    private double courseFee;

    // constructor

    public LabCourse(String dept, int id, double credits, double courseFee){

        super(dept, id, credits);

        this.courseFee = courseFee;

    }

    // override display()

    public void display(){

        System.out.println(dept+id+"\nLab course\n"+credits+" credits");

        System.out.println("Lab fee is $"+courseFee);

        System.out.println("Total fee is $"+((credits*120)+courseFee));

    }

}

UseCourse.java

import java.util.*;

public class UseCourse {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String dept, inStr;

        String[] labCourses = { "BIO", "CHM", "CIS", "PHY" };

        int id, credits;

        int found = 0;

        int x;

        System.out.print("Enter course: ");

        inStr = input.next();

        System.out.print("Enter id: ");

        id = input.nextInt();

        System.out.print("Enter credits: ");

        credits = input.nextInt();

        for(int i=0; i<labCourses.length; i++){

            if(inStr.equalsIgnoreCase(labCourses[i])){

                found = 1;

                break;

            }

        }

        if(found==1){

            LabCourse labCourse = new LabCourse(inStr, id, credits, 50);

            labCourse.display();

        }

        else{

            CollegeCourse collegeCourse = new CollegeCourse(inStr, id, credits);

            collegeCourse.display();

        }

    }

}


Related Solutions

Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG),...
Create a class named CollegeCourse that includes data fields that hold the department (for example, ENG), the course number (for example, 101), the credits (for example, 3), and the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data. Create a subclass named LabCourse that adds $50 to the course fee....
Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields...
This is 1 java question with its parts. Thanks! Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT