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();
}
}
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.
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>
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:
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)
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>
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.
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
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
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?
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.
Re: Need help with what I believe is Boolean & add branching
EDIT: wow... aussiemcgr beat me haha
Re: Need help with what I believe is Boolean & add branching
See post#4 for info re setting Window's PATH variable.
Re: Need help with what I believe is Boolean & add branching
Quote:
Originally Posted by
Brt93yoda
EDIT: wow... aussiemcgr beat me haha
pwnd :cool: