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

• September 30th, 2010, 05:57 PM
JavaBeginner123
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.

Code java:

```  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(); } }```
• September 30th, 2010, 08:08 PM
Norm
Re: Need help with what I believe is Boolean & add branching
Quote:

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.
• September 30th, 2010, 08:59 PM
JavaBeginner123
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>
• September 30th, 2010, 09:08 PM
Norm
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.
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:
Quote:

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)
• September 30th, 2010, 09:29 PM
JavaBeginner123
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.security.AccessController.doPrivileged(Native Method)
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.security.AccessController.doPrivileged(Native Method)
Could not find the main class: Area.java. Program will exit.

C:\Users\Owner\Desktop\Stuff>
• September 30th, 2010, 09:41 PM
Norm
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.
• October 1st, 2010, 01:06 AM
Darryl.Burke
Re: Need help with what I believe is Boolean & add branching
Quote:

Originally Posted by JavaBeginner123
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
• October 1st, 2010, 01:13 AM
Darryl.Burke
Re: Need help with what I believe is Boolean & add branching
Quote:

Originally Posted by Norm
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
• October 1st, 2010, 01:15 PM
JavaBeginner123
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?
• October 1st, 2010, 01:25 PM
aussiemcgr
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.
• October 1st, 2010, 01:28 PM
Brt93yoda
Re: Need help with what I believe is Boolean & add branching
EDIT: wow... aussiemcgr beat me haha
• October 1st, 2010, 01:51 PM
Norm
Re: Need help with what I believe is Boolean & add branching
See post#4 for info re setting Window's PATH variable.
• October 1st, 2010, 01:55 PM
aussiemcgr
Re: Need help with what I believe is Boolean & add branching
Quote:

Originally Posted by Brt93yoda
EDIT: wow... aussiemcgr beat me haha

pwnd :cool: