Boolean Method Syntax Problem

• 08-17-2013, 03:20 AM
SamJava_the_Hut
Boolean Method Syntax Problem
I'm trying to write a program that asks the user to enter a temperature, and then (based on the temperature entered) display the substances that would boil and/or freeze at that temperature. Here's the code I have so far:

Code:

```/*PURPOSE:  * Design a class that stores a temperature in a temperature field and has the  * appropriate accessor and mutator methods for the field. Of the 3 substances  * we are going to use, here is the freezing and boiling point information for  * those substances:  *  * Ethyl Alcohol: freezing point = -173, boiling point = 172    * Oxygen: freezing point = -362, boiling point = -306    * Water: freezing point = 32, boiling point = 212  */ package chapter4; import java.util.Scanner; /**  * Chapter 4 programming challenge 10, page 259  * @author speterson86  */ public class FreezingBoilingPoints {         int temperature;//variable declared not getting along with boolean methods below     //INPUT:        //Create a Scanner object to read input:       Scanner keyboard = new Scanner(System.in);           //Ask the user to enter a temperature:        System.out.print("Enter a temperature in Fahrenheit: ");     temperature = keyboard.nextInt();         //PROCESS:     if(temperature <= -173){         isEthylFreezing = true; }     if(temperature >= 172){         isEthylBoiling = true; }     if(temperature <= -362){         isOxygenFreezing = true; }     if(temperature >= -306){         isOxygenBoiling = true; }     if(temperature <= 32){         isWaterFreezing = true; }     if(temperature >= 212){         isWaterBoiling = true; }     //OUTPUT:            }//end of class```
For some reason, it doesn't seem to be liking my already-declared temperature variable. I'm getting red squiggly errors from "System.out.print("Enter a temperature in Fahrenheit: ");" all the way down to "isWaterBoiling = true;" Any idea of what's going on?

Oh, and by the way, is this an advanced java question, or is this still basic enough to qualify as a new-to-java question?
• 08-17-2013, 05:05 AM
Junky
Re: Boolean Method Syntax Problem
What is the EXACT error message you get. Copy and paste it here. You can either compile the code to get the error messages. If you are using an IDE you should be able to hover your mouse over the code or the maybe an icon on the margin which will display the message.

I just had another quick look at the code, have you imported the Scanner class?
• 08-17-2013, 05:47 AM
DarrylBurke
Re: Boolean Method Syntax Problem
Yeah the Scanner import is there, masked by the forum's formatting bug.

Executable statements have to be inside a method, constructor or initialization block. They can't be out in the body of the class declaration.

Code:

```// if(temperature <= -173){         // isEthylFreezing = true; //  } isEthylFreezing = temperature <= -173;```
And outdents should equal indents.

db
• 08-17-2013, 06:38 PM
SamJava_the_Hut
Re: Boolean Method Syntax Problem
The error message over each method is:

illegal start of type

cannot find symbol
symbol: class temperature
location: class FreezingBoilingPoints

<identifier> expected
• 08-17-2013, 08:25 PM
SamJava_the_Hut
Re: Boolean Method Syntax Problem
Quote:

Originally Posted by DarrylBurke
Yeah the Scanner import is there, masked by the forum's formatting bug.

Executable statements have to be inside a method, constructor or initialization block. They can't be out in the body of the class declaration.

Code:

```// if(temperature <= -173){         // isEthylFreezing = true; //  } isEthylFreezing = temperature <= -173;```
And outdents should equal indents.

db

I've updated my code:

Code:

```/*PURPOSE:  * Design a class that stores a temperature in a temperature field and has the  * appropriate accessor and mutator methods for the field. Of the 3 substances  * we are going to use, here is the freezing and boiling point information for  * those substances:  *  * Ethyl Alcohol: freezing point = -173, boiling point = 172    * Oxygen: freezing point = -362, boiling point = -306    * Water: freezing point = 32, boiling point = 212  */ package chapter4; import java.util.Scanner; /**  * Chapter 4 programming challenge 10, page 259  *  * @author speterson86  */ public class FreezingBoilingPoints {     int temperature;     //INPUT:        //Create a Scanner object to read input:     Scanner keyboard = new Scanner(System.in);     //Ask the user to enter a temperature:        System.out.print ("Enter a temperature in Fahrenheit: ");     temperature  = keyboard.nextInt();     //PROCESS:     public BooleanTemp() {         isEthylFreezing  = (temperature <= -173);         if (temperature <= -173) {             isEthylFreezing = true;             /*DarrylBurke, I get this same error using both of our verbose ways:             *             * cannot find symbol             symbol:  variable isEthylFreezing             location: class FreezingBoilingPoints             */         }         if (temperature >= 172) {             isEthylBoiling = true;         }         if (temperature <= -362) {             isOxygenFreezing = true;         }         if (temperature >= -306) {             isOxygenBoiling = true;         }         if (temperature <= 32) {             isWaterFreezing = true;         }         if (temperature >= 212) {             isWaterBoiling = true;         }     }//end of BooleanTemp() constructor         /* Lets take a closer look before we code the output:     *     * At Oxygen's freezing point all 3 substances are frozen;     * At Ethyl Alcohol's freezing point, Water is also frozen;     * At Water's boiling point, Ethyl Alcohol is also boiling;     * At Oxygen's boiling point, Water and Ethyl Alcohol are frozen;     */       //OUTPUT:     public FreezeBoil() {         if (isEthylFreezing) {             System.out.println("Ethyl Alcohol would be frozen at "                     + "this temperature.");         }         if (isEthylBoiling) {             System.out.println("Ethyl Alcohol would be boiling at "                     + "this temperature.");         }         if (isOxygenFreezing) {             System.out.println("Oxygen would be frozen at this temperature.");         }         if (isOxygenBoiling) {             System.out.println("Oxygen would be boiling at this temperature.");         }         if (isWaterFreezing) {             System.out.println("Water would be frozen at this temperature.");         }         if (isWaterBoiling) {             System.out.println("Water would be boiling at this temperature.");         }     }//end of FreezeBoil() constructor    }//end of class```
Please help me think here guys. Would it be redundant to use all of those Boolean execution statements?
• 08-17-2013, 10:18 PM
jim829
Re: Boolean Method Syntax Problem
Quote:

Originally Posted by SamJava_the_Hut
Please help me think here guys. Would it be redundant to use all of those Boolean execution statements?

There are probably many ways to do this. Nothing wrong with the way you chose to do it (except for possible syntax errors of course). Another way would be to simply add the string to a list each time a condition is met. Then at the end, just iterate thru the list, printing the strings.

Regards,
Jim
• 08-17-2013, 11:02 PM
jim829
Re: Boolean Method Syntax Problem
Where do you declare your boolean values?

Example:

Code:

`boolean isEthylBoiliing;`
You are treating then as instance fields so you need to declare them as such before you use them. And you didn't use Darryl's excellent suggestion.
e.g.
Code:

```The following two code examples are equivalent: if (temperature >= 172) {     isEthylBoiling = true; } // or isEthyBoiling = temperature >= 172;```
Regards,
Jim
• 08-17-2013, 11:09 PM
SamJava_the_Hut
Re: Boolean Method Syntax Problem
I just figured out that I was supposed to initialize boolean variables at the beginning of the class. But I have 6 different boolean variables. What am I supposed to put for the return type?:

Code:

```public class FreezingBoilingPoints {     int temperature;     boolean isEthylFreezing;     boolean isEthylBoiling;     boolean isOxygenFreezing;     boolean isOxygenBoiling;     boolean isWaterFreezing;     boolean isWaterBoiling;     //INPUT:        //Create a Scanner object to read input:     Scanner keyboard = new Scanner(System.in);     //Ask the user to enter a temperature:        System.out.print ("Enter a temperature in Fahrenheit: ");     /*error message from System.out.print line of code:     *     * <identifier> expected     * missing method body, or declare abstract     * illegal start of type     */     temperature  = keyboard.nextInt();//error message: <identifier> expected     //PROCESS:     public BooleanTemp() {         //isEthylFreezing  = (temperature <= -173);         if (temperature <= -173) {             isEthylFreezing = true;                    }         if (temperature >= 172) {             isEthylBoiling = true;         }         if (temperature <= -362) {             isOxygenFreezing = true;         }         if (temperature >= -306) {             isOxygenBoiling = true;         }         if (temperature <= 32) {             isWaterFreezing = true;         }         if (temperature >= 212) {             isWaterBoiling = true;         }                    return (what am I supposed to put here?);             }//end of BooleanTemp() constructor```
I realize that Darryl's code works too, but for now, it's easier for me to read it using the if conditionals.
• 08-17-2013, 11:22 PM
jim829
Re: Boolean Method Syntax Problem
Ok, upon further inspection you have lots of problems here.

First, your two "constructors", BooleanTemp and FreezeBoil are not constructors. They should probably be methods but they need return types. In both cases, void should suffice. You BooleanTemp method should probably take an argument of temperature.

You need a main method from which to run the program and invoke the methods.

In the main method you need to do the following:

Step1: prompt for temperature.
Step2: Invoke BooleanTemp with the temperature
Step3: Invoke FreezeBoil to display the results

You may want to check the Tutorials in my signature to see how to run a program and construct the main entry point. Check out the Hello World app.

Regards,
Jim
• 08-18-2013, 12:16 AM
SamJava_the_Hut
Re: Boolean Method Syntax Problem
Quote:

Originally Posted by jim829
Ok, upon further inspection you have lots of problems here.

First, your two "constructors", BooleanTemp and FreezeBoil are not constructors. They should probably be methods but they need return types. In both cases, void should suffice. You BooleanTemp method should probably take an argument of temperature.

You need a main method from which to run the program and invoke the methods.

In the main method you need to do the following:

Step1: prompt for temperature.
Step2: Invoke BooleanTemp with the temperature
Step3: Invoke FreezeBoil to display the results

You may want to check the Tutorials in my signature to see how to run a program and construct the main entry point. Check out the Hello World app.

Regards,
Jim

Well, I tried to do what you said. Here's what I came up with:

Code:

```/*PURPOSE:  * Design a class that stores a temperature in a temperature field and has the  * appropriate accessor and mutator methods for the field. Of the 3 substances  * we are going to use, here is the freezing and boiling point information for  * those substances:  *  * Ethyl Alcohol: freezing point = -173, boiling point = 172    * Oxygen: freezing point = -362, boiling point = -306    * Water: freezing point = 32, boiling point = 212  */ package chapter4; import java.util.Scanner; /**  * Chapter 4 programming challenge 10, page 259  *  * @author speterson86  */ public class FreezingBoilingPoints {     int temperature;     boolean isEthylFreezing;     boolean isEthylBoiling;     boolean isOxygenFreezing;     boolean isOxygenBoiling;     boolean isWaterFreezing;     boolean isWaterBoiling;     //INPUT:        //Create a Scanner object to read input:     Scanner keyboard = new Scanner(System.in);     //Ask the user to enter a temperature:        System.out.print ("Enter a temperature in Fahrenheit: ");     /*error message from System.out.print line of code:     *     * <identifier> expected     * missing method body, or declare abstract     * illegal start of type     */     temperature  = keyboard.nextInt();//error message: <identifier> expected     //PROCESS:     public BooleanTemp(int temperature) {//invalid method declaration; return type required         //isEthylFreezing  = (temperature <= -173);         if (temperature <= -173) {             isEthylFreezing = true;                    }         if (temperature >= 172) {             isEthylBoiling = true;         }         if (temperature <= -362) {             isOxygenFreezing = true;         }         if (temperature >= -306) {             isOxygenBoiling = true;         }         if (temperature <= 32) {             isWaterFreezing = true;         }         if (temperature >= 212) {             isWaterBoiling = true;         }                    return;//return void             }//end of BooleanTemp() method         /* Lets take a closer look before we code the output:     *     * At Oxygen's freezing point all 3 substances are frozen;     * At Ethyl Alcohol's freezing point, Water is also frozen;     * At Water's boiling point, Ethyl Alcohol is also boiling;     * At Oxygen's boiling point, Water and Ethyl Alcohol are frozen;     */       //OUTPUT:     public FreezeBoil() {//invalid method declaration; return type required         if (isEthylFreezing) {             System.out.println("Ethyl Alcohol would be frozen at "                     + "this temperature.");         }         if (isEthylBoiling) {             System.out.println("Ethyl Alcohol would be boiling at "                     + "this temperature.");         }         if (isOxygenFreezing) {             System.out.println("Oxygen would be frozen at this temperature.");         }         if (isOxygenBoiling) {             System.out.println("Oxygen would be boiling at this temperature.");         }         if (isWaterFreezing) {             System.out.println("Water would be frozen at this temperature.");         }         if (isWaterBoiling) {             System.out.println("Water would be boiling at this temperature.");         }         return;//return void             }//end of FreezeBoil() method  }//end of class```
Also, how do I find your tutorials? I checked the Tutorials section on the forums main index, but I couldn't find any that were started by you.
• 08-18-2013, 12:30 AM
jim829
Re: Boolean Method Syntax Problem
Change your method declarations to this:

Code:

```public void BooleanTemp(int temperature) public void FreezeBoil() //You should also add the following: public static void main(String[] args) { // The code to exercise your class //  goes here   }```
The tutorials are from Oracle (I provide a link in my signature). Here it is The Java™ Tutorial
• 08-18-2013, 12:49 AM
SamJava_the_Hut
Re: Boolean Method Syntax Problem
Adding the "public static void main(String[] args)", a lot of my code has gone red again. How do I know where to place the closing curly brace for public static void main(String[] args){ ?
• 08-18-2013, 12:54 AM
jim829
Re: Boolean Method Syntax Problem
That section of code with "main" goes somewhere within the class declaration. Think of it as just a public static method of special meaning.

Regards,
Jim
• 08-18-2013, 01:07 AM
SamJava_the_Hut
Re: Boolean Method Syntax Problem
But why do we need it there? Also, I carefully examined my curly braces, but the red is still there (from line 49 down):

Code:

```/*PURPOSE:  * Design a class that stores a temperature in a temperature field and has the  * appropriate accessor and mutator methods for the field. Of the 3 substances  * we are going to use, here is the freezing and boiling point information for  * those substances:  *  * Ethyl Alcohol: freezing point = -173, boiling point = 172    * Oxygen: freezing point = -362, boiling point = -306    * Water: freezing point = 32, boiling point = 212  */ package chapter4; import java.util.Scanner; /**  * Chapter 4 programming challenge 10, page 259  *  * @author speterson86  */ public class FreezingBoilingPoints {     public static void main(String[] args) {         int temperature;         boolean isEthylFreezing;         boolean isEthylBoiling;         boolean isOxygenFreezing;         boolean isOxygenBoiling;         boolean isWaterFreezing;         boolean isWaterBoiling;         //INPUT:            //Create a Scanner object to read input:         Scanner keyboard = new Scanner(System.in);         //Ask the user to enter a temperature:            System.out.print("Enter a temperature in Fahrenheit: ");         /*error message from System.out.print line of code:         *         * <identifier> expected         * missing method body, or declare abstract         * illegal start of type         */         temperature = keyboard.nextInt();//error message: <identifier> expected         //PROCESS:         public void BooleanTemp(int temperature) {//invalid method declaration; return type required         //isEthylFreezing  = (temperature <= -173);         if (temperature <= -173) {             isEthylFreezing = true;         }         if (temperature >= 172) {             isEthylBoiling = true;         }         if (temperature <= -362) {             isOxygenFreezing = true;         }         if (temperature >= -306) {             isOxygenBoiling = true;         }         if (temperature <= 32) {             isWaterFreezing = true;         }         if (temperature >= 212) {             isWaterBoiling = true;         }         return;//return void     }//end of BooleanTemp() method     /* Lets take a closer look before we code the output:     *     * At Oxygen's freezing point all 3 substances are frozen;     * At Ethyl Alcohol's freezing point, Water is also frozen;     * At Water's boiling point, Ethyl Alcohol is also boiling;     * At Oxygen's boiling point, Water and Ethyl Alcohol are frozen;     */     //OUTPUT:     public void FreezeBoil() {//invalid method declaration; return type required         if (isEthylFreezing) {             System.out.println("Ethyl Alcohol would be frozen at "                     + "this temperature.");         }         if (isEthylBoiling) {             System.out.println("Ethyl Alcohol would be boiling at "                     + "this temperature.");         }         if (isOxygenFreezing) {             System.out.println("Oxygen would be frozen at this temperature.");         }         if (isOxygenBoiling) {             System.out.println("Oxygen would be boiling at this temperature.");         }         if (isWaterFreezing) {             System.out.println("Water would be frozen at this temperature.");         }         if (isWaterBoiling) {             System.out.println("Water would be boiling at this temperature.");         }         return;//return void     }//end of FreezeBoil() method  }//end of main method }//end of class```
• 08-18-2013, 01:13 AM
jim829
Re: Boolean Method Syntax Problem
You didn't place it in correctly. Remember, I said it was just like a method. So the closing curly brace must come before the next method in the class or closing brace of the class.

And classes don't need it unless you want to run the compiled class as a program in the JRE. The main entry point is called and that's where the fun(execution) begins.

Have you checked the tutorials yet? All this (and a whole lot more) is covered in them.

Regards,
Jim
• 08-18-2013, 03:59 AM
SamJava_the_Hut
Re: Boolean Method Syntax Problem
I have just one stubborn little problem left. I'm still getting some red (see /**/ comments):

Code:

```public class FreezingBoilingPoints {     public static void main(String[] args) {}//end of main method (not used)         int temperature;         boolean isEthylFreezing;         boolean isEthylBoiling;         boolean isOxygenFreezing;         boolean isOxygenBoiling;         boolean isWaterFreezing;         boolean isWaterBoiling;         //INPUT:            //Create a Scanner object to read input:         Scanner keyboard = new Scanner(System.in);         //Ask the user to enter a temperature:            System.out.print("Enter a temperature in Fahrenheit: ");         /*error message from System.out.print line of code:         * cannot find symbol         * symbol:  class out         * location: class System            * <identifier> expected                 * illegal start of type         */         temperature = keyboard.nextInt();//error message: <identifier> expected```
• 08-18-2013, 04:42 AM
jim829
Re: Boolean Method Syntax Problem
Your boolean declarations are okay (because they are not inside of main). They are instance variables or instance fields (both terms are used interchangeably). However, the other code where you use your scanner and read input must go inside of a method (in this case main). But you will have more problems such as instantiating an instance of the class and then invoking the various methods, etc. I am not unsympathetic to you plight but you simply cannot learn Java without reading about it. Please get a good book (but not a dummies book) and also read the tutorials. There are many examples that cover everything I have discussed in this thread. If you still have problems, I or someone else will still be ready to help.

Regards,
Jim