1. I need to create a class named ValenciaCrs that includes data fields that hol
ID: 3565852 • Letter: 1
Question
1. I need to create a class named ValenciaCrs that includes data fields that hold
A. The department (for example, ENG)
B. The course number (for example, 101)
C. The credits (for example, 3)
D. 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.
2. I need to create a subclass named LabCrs that adds $50 to the course fee.
I need to override the parent class display() method to indicate that the course is a lab course and to display all the data.
3. I need to write an application named UseCrs that prompts the user for course information.
If the user enters a class in any of the following departments, create a LabCrs: COP, CGS, MAT, or CHM. If the user enters any other department, create a ValenciaCrs that does not include the lab fee. Then display the course data. Save the files as ValenciaCrs.java, LabCrs.java, and UseCrs.java.
Explanation / Answer
// ValenciaCrs.java
public class ValenciaCrs
{
protected String department;
protected String number;
protected int credits;
protected double fee;
public ValenciaCrs(String dept, String num, int cdt)
{
department = dept;
number = num;
credits = cdt;
fee = 120 * credits;
}
public void display()
{
System.out.println("Department: " + department);
System.out.println("Course Number: " + number);
System.out.println("Credits: " + credits);
System.out.println("Total Fee: $" + fee);
}
}
// LabCrs.java
public class LabCrs extends ValenciaCrs
{
public LabCrs(String dept, String num, int cdt)
{
super(dept, num, cdt);
fee += 50;
}
public void display()
{
System.out.println("Lab Course");
super.display();
}
}
// UseCrs.java
import java.util.Scanner;
public class UseCrs
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter department: ");
String dept = input.nextLine().trim();
System.out.print("Enter course number: ");
String num = input.nextLine().trim();
System.out.print("Enter credits: ");
int credit = input.nextInt();
ValenciaCrs cr = null;
if (dept.equals("COP") ||
dept.equals("CGS") ||
dept.equals("MAT") ||
dept.equals("CHM") )
{
cr = new LabCrs(dept, num, credit);
}
else
{
cr = new ValenciaCrs(dept, num, credit);
}
System.out.println();
cr.display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.