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....
Complete the following tasks: a. Create a class named Trip that includes four string variables: destination...
Complete the following tasks: a. Create a class named Trip that includes four string variables: destination (for example, “London”), means of transportation (for example, “air”), departure date (for example, “12/15/2015”), and trip's purpose (for example, “business”). Include two overloaded constructors. The default constructor sets each field to “XXX”. The nondefault constructor accepts four parameters—one for each field. Include two overloaded display() methods. The parameterless version displays all the Trip details. The second version accepts a string that represents a destination...
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...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and...
Java Q1: Create a class named Triangle, the class must contain: Private data fields base and height with setter and getter methods. A constructor that sets the values of base and height. A method named toString() that prints the values of base and height. A method named area() that calculates and prints the area of a triangle. Draw the UML diagram for the class. Implement the class. Q2: Write a Java program that creates a two-dimensional array of type integer...
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....
in c#: Create a class named Square that contains fields for area and the length of...
in c#: Create a class named Square that contains fields for area and the length of a side and whose constructor requires a parameter for the length of one side of a Square. The constructor assigns its parameter to the length of the Square’s side field and calls a private method that computes the area field. Also include read-only properties to get a Square’s side and area. Create a class named DemoSquares that instantiates an array of ten Square objects...
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....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT