简体   繁体   中英

Java Test Class Won't Compile

I have written four cooperating classes, one of which is called WorkStation :

Here is the code for the WorkStation class.

import java.util.ArrayList;

public class WorkStation {

    /**
     * the pc
     */
    private Hardware pc;

    /**
     * list of available software
     */
    private ArrayList<Software> applications;

    /**
     * whether a web camera is attached or not
     */
    private boolean hasWebCamera;

    /**
     * id number of the Workstation
     */
    private final int idNumber;

    /*
     * Constructor with three parameters
     */
    public WorkStation(Hardware pc, boolean hasWebCamera, int idNumber) {

        this.pc = pc;
        this.hasWebCamera = hasWebCamera;
        this.idNumber = idNumber;
        applications = new ArrayList<Software>();

    }

    //------------------------------- Getter Methods ------------------------------
    /*
     * Gets the pc
     * 
     * @return The pc
     */
    public Hardware getPc() {
        return pc;
    }

    /*
     * Gets whether there is a web camera
     * 
     * @return True or false
     */
    public boolean isHasWebCamera() {
        return hasWebCamera;
    }

    /*
     * Gets the id number
     * 
     * @return The id number
     */
    public double getIdNumber() {
        return idNumber;
    }

    //--------------------------- Setter Methods -------------------------
    /*
     * Sets the pc
     * 
     * @param The pc
     */
    public void setPc(Hardware pc) {
        this.pc = pc;
    }

    /*
     * Sets whether there is a web camera
     * 
     * @param True or false
     */
    public void setHasWebCamera(boolean hasWebCamera) {
        this.hasWebCamera = hasWebCamera;
    }

    // --------------------- ArrayList Methods --------------------
    /*
     * Method to add a piece of software to the list
     */
    public void addSoftware(Software software) {
        applications.add(software);
    }

    /*
     * Method to remove a piece of software from the list
     */
    public void removeSoftware(Software software) {
        applications.remove(software);
    }

    /*
     * Method to remove all software from the list
     */
    public void clearApplications(Software software) {
        applications.clear();
    }

    /*
     * toString Method
     * 
     * @param aPc
     * 
     * @param aHasWebCamera
     * 
     * @param aIdNumber
     */
    public String toString() {
        return "Pc is " + getPc() + ", web camera is " + isHasWebCamera() + 
                " and ID number is " + getIdNumber();
    }

}//end of class

I then have to test the cooperating classes in one test class.

Here is the code for the the Test class so far.

public class Test{

    public static void main(String[] args){

    //Software
    Software s1 = new Software();
    s1.setName("Database");
    s1.setManufacturer("Microsoft");
        System.out.println(s1.toString());

    Software s2 = new Software("Drawing Package", "Sony");
    System.out.println(s2.toString());

    //Hardware
    Hardware h1 = new Hardware();
    h1.setManufacturer("Dell");
    h1.setProcessorType("Dual Core");
    h1.setHardDiskCapacity(1);
        System.out.println(h1.toString());

    Hardware h2 = new Hardware("Oracle", "Intel Core i7", 3);
        System.out.println(h2.toString());

    //WorkStation
    WorkStation w1 = new WorkStation();

The problem I'm having is that when I try to compile what I have in the test class it comes up with one error message that says:

cannot find symbol
symbol  : constructor WorkStation() location: class WorkStation: 
WorkStation w1 = new WorkStation();

I really can't understand why this is. The WorkStation class compiled perfectly and the other classes in the Test class compiled so can anyone see why the WorkStation class won't compile in the Test class?

The reason is this Line:

WorkStation w1 = new WorkStation();

Your WorkStation class isn't containing any parameterless constructor. And Java compiler doesn't insert the default constructor in your class if you already have parametric constructor defined in that class. So you must define a parameterless constructor within your WorkStation class.

You defined a constructor:

public WorkStation(Hardware pc, boolean hasWebCamera, int idNumber){

        this.pc = pc;
        this.hasWebCamera = hasWebCamera;
        this.idNumber = idNumber;
        applications = new ArrayList<Software>();

}

but you make a call to:

WorkStation w1 = new WorkStation();

You need to define a constructor:

public WorkStation(){...}

The default constructor is the no-argument constructor automatically generated unless you define another constructor. It initialises any uninitialised fields to their default values. Since you defined a constructor, no default constructor was generated...

Your workstation class only has one constructor, and it takes 3 arguments. You are trying to use a paramaterless constructor, which doesn't exist.

If you don't define any constructor, a parameterless one is added for you, but as soon as you define at least one constructor, the default parameterless one won't be created and if you want one, you have to define it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM