Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Boolean code question!

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Boolean code question!

    Hello!

    My question that I'm creating a code for is as follows:

    Write the following methods and provide a program to test them:
    boolean sorted(double x, double y, double z),returning true if the arguments are sorted, with the smallest one coming first.

    This is my code:
    import java.util.Scanner;
    public class a3q1
    {
            public static void main ( String args[])
       {
    Scanner in = new Scanner(System.in);
     
    double x = in.nextDouble();
    double y = in.nextDouble();
    double z = in.nextDouble();
       }
    public static boolean inorder(double x, double y, double z) {
    if (x < y && y < z) {
    System.out.println("Correct!");
    }
     
    else {
    System.out.println("incorrect. Try again!");
       }
    }
    }


    For some reason however, it won't compile. My question is, how do I fix the compiling error, and do you think this code is suitable for the question asked?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Boolean code question!

    What is the compiler error?

    By the way, you flubbed up your code tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Boolean code question!

    Quote Originally Posted by KevinWorkman View Post
    What is the compiler error?

    By the way, you flubbed up your code tags.
    Heh, yeah. Fixed that.

    The compiler error is:


    a3q1.java:20: missing return statement
    }
    ^
    1 error

  4. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Boolean code question!

    This code compiles with no syntax errors. This program calls your inorder() method and returns true or false in the main method.

    import java.util.Scanner;
     
    public class a3q1
    {
    public static void main ( String args[])
       {
           Scanner in = new Scanner(System.in);
     
           double x = in.nextDouble();
           double y = in.nextDouble();
           double z = in.nextDouble();
           boolean myb = inorder( x, y, z );    //call method inorder() and pass variables x, y, and z to the argument. Return value is stored in boolean
     
           if( myb = true )
                System.out.println("Correct!");
           else
                System.out.println("incorrect. Try again!");
     
    }//end of main method
     
    public static boolean inorder(double x, double y, double z) {
        if (x < y && y < z) 
            return true;
        else 
            return false;
     
        }//end of inorder method
    }//end of cloass

  5. The Following User Says Thank You to Zyrion For This Useful Post:

    Scorks (March 1st, 2013)

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Boolean code question!

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #6
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Boolean code question!

    Quote Originally Posted by KevinWorkman View Post
    I understand.

Similar Threads

  1. Boolean Input Question.
    By CommisarDave in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: August 2nd, 2012, 12:25 PM
  2. beginner question--boolean expressions
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 17th, 2012, 03:12 PM
  3. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  4. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM

Tags for this Thread