Reputation: 1
I have written a piece of code that declares three instance variables and two instance methods. However, when I run the program I receive a syntax error in the checkTemperature method. I have checked my syntax for any missing/over used braces, semi-colons etc. but the code looks fine. I am not sure why I am receiving this error can someone help with this problem? Here is my code below.
public class volcanoRobots {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* declared instance variables status, speed,temperature
*/
String status;
int speed;
float temperature;
/*
* create first instance method checkTemperature
*/
void checkTemperature(){
if (temperature > 660){
status = "returning home";
speed = 5;
}
}
/*
* create second instance method showAttributes
*/
void showAttributes(){
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("Temperature: " + temperature);
}
}
}
Upvotes: 0
Views: 80
Reputation: 95598
You can't have a method inside another method. Move that method into the main body of the class and your code should compile. Also, your instance variables aren't actually instance variables; they are variables that are local to the main
method. Move those into the main body of the class as well, with proper visibility-modifiers; I suggest making them private
.
A few other things:
private
. Without any modifiers, they are package-private, which is probably not what you want.VolcanoRobots
. Class names are in PascalCase, whereas variable and method names are in camelCase.So your class will eventually look like this:
public class VolcanoRobots {
/*
* declared instance variables status, speed,temperature
*/
private String status;
private int speed;
private float temperature;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/*
* create first instance method checkTemperature
*/
private void checkTemperature(){
if (temperature > 660){
status = "returning home";
speed = 5;
}
}
/*
* create second instance method showAttributes
*/
private void showAttributes(){
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("Temperature: " + temperature);
}
}
Upvotes: 2