Can anyone tell me why my code will not print.

0 Nova Dadow · February 20, 2015
This is what comes up when I click run "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method setpluggedIn(boolean) is undefined for the type Stove

    at TestStove.main(TestStove.java:19)"

I don't know what I am doing wrong. I had to create two separate classes per professor request.

public class Stove {

    // Constants
    public static final int Low = 1;
    public static final int Medium = 2;
    public static final int High = 3;

    // Private data fields
    private int heat;
    private boolean pluggedIn;

    // Default Constructor
    public Stove() {
        heat = Low;
        pluggedIn = true;
    }// End Default Constructor

    // If/else statement to display heat
    public void setHeat(int h) {
        if (h < Low) {
            heat = Low;
        } else if (h > High) {
            heat = High;
        } else {
            heat = h;
        }// end if else statement
    }

    // Setter method boolean
    public void setPulggedIn(boolean pluggedIn) {
        this.pluggedIn = pluggedIn;
    }// End Setter

    // Getter method int
    public int getHeat() {
        return heat;
    }// End Getter

    // Getter method boolean
    public boolean getPluggedIn() {
        return pluggedIn;
    }// End Getter

    public void changeHeat(int heat) {
        setHeat(heat);
    }// end method

    // toString Method
    public String stovetoString() {
        return String.format("Heat: %d \nPluggedIn: %b", heat, pluggedIn);
    }// End String
}// End Class

public class TestStove {
    public static void main(String args[]) {

        Stove Stove1[] = new Stove[3];
        // begin for statement to print array
        for (int i = 0; i < 3; ++i) {
            Stove1 = new Stove();
        }// end for

        // setter to print
        Stove1[0].setHeat(Stove.Low);
        Stove1[0].setpluggedIn(true);

        for (int i = 0; i < 3; ++i) {
            System.out.printf("Heat: %d\nPlugged In: %b\n", Stove1.getHeat(),
                    Stove1.getPluggedIn());
        }//End for
    }// End Main
}// End Class



 

Post a Reply

Replies

Oldest  Newest  Rating
0 Name Surname · February 20, 2015
I think the problem is here:

for (int i = 0; i < 3; ++i) {
Stove1 = new Stove();
}// end for

The name of instance is almost the same as class name, which is bad habit of programming.
The thing you need to change is:
        Stove1 = new Stove(i); // it's an array so it should have own index, right ?

And I think there is a few mistake in the final loop. You call Stove1.getHeat() and so on. 
But should not it have index ?
Maybe you need to change like Stove.
  • 1

Java / Android Development

107,058 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator