1

I have three classes in java that i created:

CollegeCourse

package CollegeCourse;
import javax.swing.JOptionPane;
public class CollegeCourse {

protected String Dept;
protected int CourseNum;
protected int Cred;
protected int fee;
protected int Charge = 120;
protected int labfee;

public void inputDepartment(){
    Dept = JOptionPane.showInputDialog(null, "Enter Department");
}

public void inputCourseNumber(){
    String CourseNumString = new String(" ");
    CourseNumString = JOptionPane.showInputDialog(null, "Enter Course Number");
    CourseNum = Integer.parseInt(CourseNumString);
}

public void inputCredits(){
    String CredString = new String(" ");
    CredString = JOptionPane.showInputDialog(null, "Enter Credits");
    Cred = Integer.parseInt(CredString);
}

 public void displayNonLabCourse(){
    fee = Cred * Charge;
    JOptionPane.showMessageDialog(null, Dept + CourseNum + 
            "\nNon-lab Course" + "\n" + Cred +" Credits" + "\nTotal fee is $" + fee);
 }

}

LabCourse

package CollegeCourse;
import javax.swing.JOptionPane;
public class LabCourse extends CollegeCourse {

 public void displayLabCourse(){
    labfee = fee + 50;
    JOptionPane.showMessageDialog(null, Dept + CourseNum + 
            "\nLab Course" + "\n" + Cred +" Credits" + "\nTotal fee is $" + fee);
 }
 }

UseCourse

package CollegeCourse;
public class UseCourse{
public static void main(String[] args) {
    CollegeCourse college = new CollegeCourse();
    college.inputDepartment();
    college.inputCourseNumber();
    college.inputCredits();
    college.displayNonLabCourse();
    }
}

How can i make the UseCourse class display the LabCourse display method when the user enters a certain department like "BIO"???

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 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 course data. 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 to display all the data. Write an application named UseCourse that prompts the user for 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 departments, create a CollegeCourse that does not include the lab fee. Then display the course data. Save the files as CollegeCourse.java, LabCourse.java, and UseCourse.java.

4

2 回答 2

2

This is quite different to your original implementation. But I believe it does what you want it to. There's probably a better way of implementing it, but I'm only implementing based on the information you've given me. Hope you understand it all...


UseCourse:

import javax.swing.JOptionPane;

public class UseCourse
{   
    public UseCourse()
    {
        CollegeCourse college;
        // Collect all the necessary data first to determine whether it is a lab course or not.
        String dept = JOptionPane.showInputDialog(null, "Enter Department");

        String courseNumString = JOptionPane.showInputDialog(null, "Enter Course Number");
        int courseNum = Integer.parseInt(courseNumString);

        String credString = JOptionPane.showInputDialog(null, "Enter Credits");
        int cred = Integer.parseInt(credString);

        // Now that we have the info we can ask the question.
        if (dept.equals("BIO") | dept.equals("CHM") | dept.equals("CIS") | dept.equals("PHY"))
        {college = new LabCourse(dept, courseNum, cred);}

        else {college = new LabCourse(dept, courseNum, cred);}
        college.display();
    }

    public static void main(String[] args) 
    {new UseCourse();}
}

CollegeCourse:

import javax.swing.JOptionPane;
public class CollegeCourse {

protected String dept;
protected int courseNum;
protected int cred;
protected int fee;
protected int Charge = 120;
protected int labfee;

public CollegeCourse(String dept, int courseNum, int cred)
{
    this.dept = dept;
    this.courseNum = courseNum;
    this.cred = cred;
    fee = cred * Charge;
}

public void display(){

    JOptionPane.showMessageDialog(null, dept + courseNum + 
            "\nNon-lab Course" + "\n" + cred +" Credits" + "\nTotal fee is $" + fee);
 }
}


LabCourse:

import javax.swing.JOptionPane;
public class LabCourse extends CollegeCourse 
{
    public LabCourse(String dept, int courseNum, int cred)
    {super(dept, courseNum, cred);}

    public void display()
    {
        fee += 50;
        JOptionPane.showMessageDialog(null, dept + courseNum + 
                "\nLab Course" + "\n" + cred +" Credits" + "\nTotal fee is $" + fee);
    }  
}
于 2012-12-10T11:23:20.073 回答
1

If you want to do this using inheritance then you should have same method name something like "displayCourse" in both CollegeCourse and LabCourse classes. Then based on the inputs by the user:

CollegeCourse c = null;
if (userSelectedCollegeCourse) {
c = new CollegeCourse(param1, ...);
}else{
c = new LabCourse(param1, ...);
}

c.displayCourse();
于 2012-12-10T11:22:54.120 回答