0

I need some help with compiling a simple java program using javac.

So I have these 2 files that were given to me by my teacher:

Robot.java

class Robot {

  String name;
  int numLegs;
  float powerLevel;

  Robot(String name) {
    this.name = name;
    numLegs = 2;
    powerLevel = 2.0f;
  }

  Robot() {
    this(Standard Model);
  }

  void talk(String phrase) {
    if (powerLevel = 1.0f) {
      System.out.println(name +  says  + phrase);
      powerLevel -= 1.0f;
    } else {
      System.out.println(name +  is too weak to talk.);
    }
  }

  void charge(float amount) {
    System.out.println(name +  charges.);
    powerLevel = powerLevel + amount;
  }
}

RobotWorld.java

class RobotWorld {

  public static void main (String[] args) {
    Robot c3po = new Robot("C3PO");
    c3po.talk("'Hello, Java!'");   
  }

}

When I try to compile it by typing "javac RobotWorld.java" into my command prompt, it just returns a bunch of errors. How can I fix this? I have JDK 8 installed.

enter image description here

8
  • 1
    Looks to me your just missing a bunch of "" around strings Commented Jan 31, 2018 at 20:17
  • 1
    And = instead of ==, and an invalid constructor. Was your assignment to fix the errors? Since it clearly contains incorrect syntax. Commented Jan 31, 2018 at 20:19
  • 1
    powerLevel == 1.0f use 2 = signs Commented Jan 31, 2018 at 20:20
  • 1
    put double quotes around Standard Model so this("Standard Model"); Commented Jan 31, 2018 at 20:21
  • I think the purpose of the assignment was for you to figure out what is wrong with the program instead of asking for help here on SO Commented Jan 31, 2018 at 20:25

2 Answers 2

3

It looks like all of the double quote characters were removed. I also added another equal sign after "powerLevel =". Try this:

class Robot {

    String name;
    int numLegs;
    float powerLevel;

    Robot(String name) {
        this.name = name;
        numLegs = 2;
        powerLevel = 2.0f;
    }

    Robot() {
        this("Standard Model");
    }

    void talk(String phrase) {
        if (powerLevel == 1.0f) {
            System.out.println(name + " says " + phrase);
            powerLevel -= 1.0f;
        } else {
            System.out.println(name + " is too weak to talk.");
        }
    }

    void charge(float amount) {
        System.out.println(name + "charges.");
        powerLevel = powerLevel + amount;
    }
}

The program output is: C3PO is too weak to talk.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you're missing double quotes "" for strings. Also, what does Standard Model represent?

It looks like you're intending for your default constructor to call the constructor with parameters, with a robot name of "Standard Model"?

2 Comments

Looks like Standard Model is another String without quotes.
Putting the "" around the strings fixed it. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.