I am writing a program for my assignment, but for my defaultFan and toString methods I am getting an error stating "invalid method declaration; return type required. However I am unsure what how to resolve this. I tried putting void in front of the two methods and it worked but then I get errors stating I cannot assign variables to final variables slow, medium, and fast. I am not sure if this is correct. How would I fix this?

I also have a hard time using test programs. My professor wants us to use a test program that creates 2 fan objets; the first assign maximum speed, radius 10, color yellow and on status. and the second assign medium speed, radius 5 color blue and off status, and to display the fan objects by invoking their toString methods. Would it be possible for someone to explain how test programs work, and how I would go about creating one for this program. Here is my code:

public class fan {

  private final int slow = 1;
  private final int medium = 2;
  private final int fast = 3;
  private int speed;
  private boolean fanOn;
  private double radius;
  private String color;

  public void defaultFan( )
  {
  int speed = 1;
  boolean fanOn = false;
  double radius = 5;
  String color = "blue";
  }

  public fan(final int slow, final int medium, final int fast, int
speed, boolean fanOn, double radius, String color) {

  this.slow = slow;
  this.medium = medium;
  this.fast = fast;
  this.speed = speed;
  this.fanOn = fanOn;
  this.radius = radius;
  this.color = color;
  }

  public final int getSlow(){
    return slow;
  }

  public final int getMedium() {
    return medium;
  }

  public final int getFast() {
    return fast;
  }

  public int getSpeed() {
    return speed;
  }

  public boolean getfanOn() {
    return fanOn;
  }

  public double getradius() {
    return radius;
  }

  public String getcolor() {
    return color;
  }

  public void setSlow(final int slow) {
    this.slow = slow;
  }

  public void setMedium(final int medium) {
    this.medium = medium;
  }

  public void setFast(final int fast) {
    this.fast = fast;
  }

  public void setSpeed(int speed) {
    this.speed = speed;
  }

  public void setFanOn(boolean fanOn) {
    this.fanOn = fanOn;
  }

  public void setRadius(double radius) {
    this.radius = radius;
  }

  public void setColor(String color) {
    this.color = color;
  }

  public void toString() {
    if(fanOn = true ) {
  System.out.println("The speed of the fan is " + speed + ", the color
of the the fan is " + color + ", and the radius of the fan is " +
radius + ".");
}
  else {
    System.out.println("The fan is off but the color is " + color +"
and the radius is " + radius + ".");
  }

} }

有帮助吗?

解决方案 2

Write your toString method like this

public String toString() {
    String description = "";
    if (fanOn = true) {
        description += "The speed of the fan is " + speed
                + ", the color  of the the fan is " + color
                + ", and the radius of the fan is " + radius + ".";
    } else {
        description += "The fan is off but the color is " + color
                + " and the radius is " + radius + ".";
    }
    return description;
}

I am not sure what you want to do with slow/medium/fast(seems a redundancy with speed). But if you want to modify it, don't declare it as final.

private int slow = 1;
private int medium = 2;
private int fast = 3;

You need a constructor for your test program. (by the way, you should name your class Fan)

public fan(int speed, double radius, String color, boolean fanOn ) {
    this.speed = speed;
    this.radius = radius;
    this.color = color;
    this.fanOn = fanOn;     
}

Your test program should look like this.

public static void main(String args[]) {
    fan fan1 = new fan(100, 100, "red", true);
    fan fan2 = new fan(200, 200, "green", false);
}

其他提示

  1. The variables slow, medium, and fast are final; you set each of them in their declaration, and you need not and cannot reinitialize them. You need to remove them from your constructor:

    public fan(int speed, boolean fanOn, double radius, String color) {
        this.speed = speed;
        this.fanOn = fanOn;
        this.radius = radius;
        this.color = color;
    }
    
  2. Now, get rid of the setSlow and getSlow methods, etc. Keep the others.

  3. You would want to invoke the constructor with code like:

    fan myFan = new fan(/* medium */ 2, true, 10.0, "blue");
    // But see 4 and 5 below.
    
  4. The variables slow, medium, and fast are not tied to any specific instance of fan. So, you want to declare these like so:

    public static final int SLOW = 1;
    public static final int MEDIUM = 2;
    public static final int FAST = 3;
    // The constructor call becomes:
    fan myFan = new fan(fan.MEDIUM, true, 10.0, "blue");
    
  5. Typically, classes in Java have capitalized names. Call the class Fan. Replace all instances of fan with Fan.

  6. The toString method should not be so chatty. Typically, people write these methods to help them debug their code, not to provide friendly access to users. Just report the values of the instance variables, which do not include SLOW, MEDIUM, or FAST. Don't use conditional logic.

  7. Your toString method actually overrides the basic one in Object. Java will nag you until you add the @Override annotation. For fun, write your toString code, use it, and then comment the code out. See what happens to the output. You'll see why you need to override the method in Object.

    @Override
    public String toString() {
         return "Fan" + "[speed: " + speed +
                        ",on: " + fanOn +
                        ",radius: " + radius +
                        ",color: " + color + "]";
    }
    
  8. For future work, consider using Java's own Color class instead of a String. Also, consider writing a Java enum of your own named Speed instead of using those three constants.

  9. Ask yourself what someone using the code would want the code to do, both if everything goes right, and if things go wrong or the class is used incorrectly. For example, perhaps the Fan class should obey these rules:

    • If I construct a Fan, and I ask it for its speed, I get the speed I put in.
    • Ditto for whether it's on, its radius, and its color.
    • If I take a Fan, and call a set method on one of its instance variables, and I then query the variable with a get method, I obtain the value I put in.
    • If I construct a Fan with a negative radius or with null for its color, the constructor fails, throwing an IllegalArgumentException. Your class may not have covered that yet.
    • Similarly, if I call myFan.setRadius(-10.0), the set method throws the same exception and myFan is left untouched.
    • If I try to set the speed of a Fan to something other than SLOW, MEDIUM, or FAST, this should fail too. Remember the advice about enums? This is a good reason why.

There are many frameworks to help with software testing; sadly, people don't do it enough in practice. But look up JUnit; your IDE almost certainly has ways to help you create JUnit tests.

public void toString()

This is causing the error. Knowingly or Unknowingly, you're trying to override the Object.toString() method, and that's the reason its showing that error. You either need to change the return type of your toString() method to String or change the method name to something else, to avoid conflict with Object.toString().

Apart from the major issue mentioned above, you've a few other bugs as well in your code, which could be resolved with a good IDE.

And for your final question: there are any number of tutorials on testing in Java. Search for JUnit. Here's an example tutorial.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top