Results 1 to 17 of 17
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default 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:

    Java 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?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default 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?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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.

    Additionally, your conditionals are unnecessarily verbose.
    Java Code:
    // if(temperature <= -173){
            // isEthylFreezing = true;
    //  }
    isEthylFreezing = temperature <= -173;
    And outdents should equal indents.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default 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

  5. #5
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Boolean Method Syntax Problem

    Quote Originally Posted by DarrylBurke View Post
    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.

    Additionally, your conditionals are unnecessarily verbose.
    Java Code:
    // if(temperature <= -173){
            // isEthylFreezing = true;
    //  }
    isEthylFreezing = temperature <= -173;
    And outdents should equal indents.

    db
    I've updated my code:

    Java 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?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default Re: Boolean Method Syntax Problem

    Quote Originally Posted by SamJava_the_Hut View Post
    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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default Re: Boolean Method Syntax Problem

    Where do you declare your boolean values?

    Example:

    Java 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.
    Java Code:
    The following two code examples are equivalent:
    
    if (temperature >= 172) {
        isEthylBoiling = true;
    }
    // or
    isEthyBoiling = temperature >= 172;
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default 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?:

    Java 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.
    Last edited by SamJava_the_Hut; 08-17-2013 at 11:17 PM.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default 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
    Step4: return to Step1 or quit

    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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Boolean Method Syntax Problem

    Quote Originally Posted by jim829 View Post
    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
    Step4: return to Step1 or quit

    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:

    Java 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.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default Re: Boolean Method Syntax Problem

    Change your method declarations to this:

    Java 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default 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){ ?

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default 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):

    Java 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

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Boolean Method Syntax Problem

    I have just one stubborn little problem left. I'm still getting some red (see /**/ comments):

    Java 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
    Last edited by SamJava_the_Hut; 08-18-2013 at 04:02 AM.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Method Declaration Syntax Help
    By ETBunce in forum New To Java
    Replies: 8
    Last Post: 12-02-2012, 01:29 AM
  2. Syntax Error w/ my method
    By Track13 in forum New To Java
    Replies: 2
    Last Post: 11-13-2012, 07:44 PM
  3. Boolean Method Problem
    By Bonfy in forum New To Java
    Replies: 32
    Last Post: 01-23-2012, 01:21 AM
  4. problem on expressing a boolean method
    By b.m in forum New To Java
    Replies: 9
    Last Post: 12-12-2010, 06:17 PM
  5. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 02:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •