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 13 of 13

Thread: Need help with what I believe is Boolean & add branching

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with what I believe is Boolean & add branching

    Alright let me start off by saying I am a beginner. This might sound stupid but I don't even know how to run my java program to try it out. With that being said I have a problem. I am doing an assignment for school that requires me to write a java program that asks the user to enter either a 1 or 2. If its not a 1 or 2 then the program exits and says please enter a one or two. If its a 1 then it asks for the user to enter two non-negative numbers for the base and height so it can figure the area of the triangle. If the user enters a negative number the program exits and says what number was negative. If its a 2 it asks for a number for radius and calculates the area of a circle. Same deal with the negative. If they enter the right numbers it prints Area of Triangle with base=(number), Height=(number): area. And same for the circle. It says I should use calcTriangleArea and calcCircleArea. Now I have written something that I don't know how to test so I don't know if its right. Can someone maybe tell me if its right and if not where I am wrong.

     
    importjava.until.scanner;
    class Area
    {
      Area()
      {
       int number; //Number
       int b;      //Base
       int h;      //Height
       int r;      //Radius
     
      //Read number from user
      scanner in = newScanner(System.in);
      system.out.print("Please enter either 1 or 2");
     
      number = in.nextInt();
     
      if (number = 1) || (number = 2){
     
         if (number = 1){
            system.out.print("Please enter non-negative number for Base");
            b = in.nextInt();     
            system.out.print("Please enter non-negative number for Height");
            h = in.nextInt();
            system.out.print("Base: " + "b", "Height: " + "h", "Area of Triangle: " + "0.5*b*h"); 
         }
         elseif (number = 2){
                 system.out.print("Please enter non-negative number for radius");
                 r = in.nextInt();
                 system.out.print("Radius: " + "r," "Area of Circle: " + "3.14*r*r");
         }  
         else{
           system.out.print("Please enter either 1 or 2! Program exits");
       }
     }
     
       public static void man(String args[])
       {
         //declare and instantiate a new object
         Area obj1 = new Area();
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    I don't know how to test
    How do you compile the program? Do you use the javac command?
    If so, if there are no errors, the javac command will create a .class file: Area.class for your program.

    To execute the program, open a command prompt window, change to the folder with the Area.class file and enter:
    java Area

    If there are problems, copy and paste the contents of the command prompt window here.
    To copy the contents of the Window's command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    C:\Users\Owner\Desktop\Stuff>javac Area
    'javac' is not recognized as an internal or external command,
    operable program or batch file.

    C:\Users\Owner\Desktop\Stuff>javac Area.java
    'javac' is not recognized as an internal or external command,
    operable program or batch file.

    C:\Users\Owner\Desktop\Stuff>java Area.java
    'java' is not recognized as an internal or external command,
    operable program or batch file.

    C:\Users\Owner\Desktop\Stuff>

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    If you have the JDK installed on your computer, you need to tell the OS where it java tools are located.
    If you don't have the JDK, you need to download and install it.

    To tell the OS where the java tools are, you need to add an entry to the OS's PATH variable.
    Here's how on Windows XP:
    The PATH environment variable is set from the Settings | Control Panel | System panel.
    Select the Advanced tab and
    Click on the Environment variable's button.
    At the bottom in System Variables, find the PATH entry and click the Edit button
    Add the new path using ; to separate it
    Its a stupid small text field so be careful.

    If you have the JDK installed you can tell the OS where the command is by using the full path to the command when you use it.
    Something like this. You need the path in "s because of the space in the directory name:

    D:\Norms\Norms Tools>"c:\Program Files\java\jdk1.5.0_04\bin\java" -version
    java version "1.5.0_04"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
    Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode, sharing)

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    C:\Users\Owner\Desktop\Stuff>java Area
    Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value
    1768779887 in class file Area
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: Area. Program will exit.

    C:\Users\Owner\Desktop\Stuff>java Area.java
    Exception in thread "main" java.lang.NoClassDefFoundError: Area/java
    Caused by: java.lang.ClassNotFoundException: Area.java
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: Area.java. Program will exit.

    C:\Users\Owner\Desktop\Stuff>

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    What was the result of the javac step?
    Where did you get the Area.class file?
    The Area.class is not from the compiler. If you open it in an editor it starts with the letters: impo
    Rename it out of the way and execute the compiler:
    javac Area.java
    Then look to see if an Area.class file was created.

  7. #7
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    Quote Originally Posted by JavaBeginner123 View Post
    C:\Users\Owner\Desktop\Stuff>java Area
    Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value
    1768779887 in class file Area
    Your JDK and JRE appear to be of different versions, and the JDK is newer. Since you're new to Java, there's no point in you working with older versions: just download the latest releases of both (or at least the JRE).

    Since you're on Windows, you can easily update your JRE from the Java Control Panel.

    db

  8. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    Quote Originally Posted by Norm View Post
    The Area.class is not from the compiler. If you open it in an editor it starts with the letters: impo
    Oops, missed that. It's not even a Java class file.

    @OP: ignore my remarks on JDK/JRE incompatibility. The suggestion to update to the newest version stands.

    db

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    Ok so I think I might have the code somewhat right but when I go to do javac it comes up with:

    'javac' is not recognized as an internal or external command,
    operable program or batch file.

    How do I get passed this?

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    You have not set your computer to recognize where the PATH for the JAVA JDK is.

  11. #11
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    EDIT: wow... aussiemcgr beat me haha

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    See post#4 for info re setting Window's PATH variable.

  13. #13
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with what I believe is Boolean & add branching

    Quote Originally Posted by Brt93yoda View Post
    EDIT: wow... aussiemcgr beat me haha
    pwnd

Similar Threads

  1. Boolean Value Not Changing
    By bosox960 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 21st, 2010, 02:11 PM
  2. ArrayList Boolean Issue
    By kite98765 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2010, 08:15 AM
  3. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM
  4. [SOLVED] Modification of Boolean function in Java program
    By lotus in forum Java SE APIs
    Replies: 4
    Last Post: July 10th, 2009, 10:15 AM
  5. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM