Question

(To be written in Java code) Create a class named CollegeCourse that includes the following data...

(To be written in Java code)

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.

GIven code:

// CollegeCourse.java
import java.util.*;
public class CollegeCourse
{
// attributes

// constructor

// display()

}

//LabCourse.java
import java.util.*;
public class LabCourse
{
// attributes

// constructor

// override display()
  
}

//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;
  
// your code here

  
}
}

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

CollegeCourse.java :


// CollegeCourse.java
import java.util.*;//import package
//Java class

public class CollegeCourse {
// attributes
   protected String dept;
   protected int id;
   protected double credits;
   protected double price;

// constructor
   public CollegeCourse(String d, int i, double c) {
       this.dept = d;
       this.id = i;
       this.credits = c;
       this.price = this.credits * 120;
   }

// display()
   public void display() {
       System.out.println(this.dept + "" + this.id);
       System.out.println("Non-lab course");
       System.out.println(this.credits + " credits");
       System.out.println("Total fee is $" + this.price);
   }

}

*****************************

LabCourse.java :


//LabCourse.java
import java.util.*;

public class LabCourse extends CollegeCourse {
// attributes
   private int labfee;

// constructor
   public LabCourse(String d, int i, double c, int lf) {
       // calling base class constructor
       super(d, i, c);
       this.labfee = lf;// set lab fee
   }

// override display()
   @Override
   public void display() {
       System.out.println(this.dept + "" + this.id);
       System.out.println("Lab course");
       System.out.println(this.credits + " credits");
       System.out.println("Lab fee is $" + this.labfee);
       System.out.println("Total fee is $" + (this.price + this.labfee));
   }

}

***************************

UseCourse.java :


//UseCourse.java
import java.util.*;

//java class
public class UseCourse {
   // main method
   public static void main(String[] args) {
       // creating object of Scanner class
       Scanner input = new Scanner(System.in);
       String dept, inStr;
       String[] labCourses = { "BIO", "CHM", "CIS", "PHY" };
       int id, credits;
       int found = 0;
       int x;
       // asking user course
       System.out.print("Enter course : ");
       dept = input.nextLine();// reading input
       // asking user course id
       System.out.print("Enter course id : ");
       id = input.nextInt();// reading course id
       // asking user credits
       System.out.print("Enter course credits : ");
       credits = input.nextInt();// reading course credits
       // checking course in labCourses
       for (x = 0; x < labCourses.length; x++) {
           // checking course is whether lab course or not
           if (labCourses[x].equals(dept)) {
               // if course is lab course then
               found = 1;// set found
           }
       }
       // checking value of found
       if (found == 1) {
           // if value of found is 1 then
           // this means course is lab course
           // create object of LabCourse
           LabCourse lc = new LabCourse(dept, id, credits, 50);
           // call method to display object details
           lc.display();
       } else {
           // if not a lab course then create a object of CollegeCourse
           CollegeCourse cc = new CollegeCourse(dept, id, credits);
           // call method to display object details
           cc.display();
       }

   }
}
==============================

Screen 1 :For non lab course

Debug Shell Console <terminated> UseCourse [Java Application] C:\Program FilesJava\jdk1.8.0_181\bin\javaw.exe (19-Oct-2019, 9

Screen 2 :For lab course

Console Debug Shell <terminated> UseCourse [Java Application] C:\Program FilesJava\jdk1.8.0_181\bin\javaw.exe (19-Oct-2019, 9

Add a comment
Know the answer?
Add Answer to:
(To be written in Java code) Create a class named CollegeCourse that includes the following data...
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 a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program...

    Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...

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

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

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

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

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

  • In Java Create a class Worker. Your Worker class should include the following attributes as String...

    In Java Create a class Worker. Your Worker class should include the following attributes as String variables: Name, Worker ID, Worker Address, Worker City, Worker State Create the constructor that initializes the above worker attributes. Then make another class name HourlyWorker that inherits from the Worker class.   HourlyWOrker has to use the inherited parent class variables and add in hourlySalary and billableHours. Your HourlyWorker class should contain a constructor that calls the constructor from the Worker class to initialize the...

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

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