Please help a java newbie
problem is im doing some homework and have no idea what to do.
ive created two files, in one:
publicclass Cube{
privatedouble length;
privatedouble surfaceArea;
privatedouble volume;
public Cube(double lengthIn){
length = lengthIn;
surfaceArea = ((lengthIn * lengthIn) * 6);
volume = (lengthIn * lengthIn * lengthIn);
}
public String toString(){
return("The surface area is: " + surfaceArea +" and the volume is: " + volume);
}
now this one works ok
my problem is here:
import java.io.* ;
publicclass CubeUser{
publicstaticvoid main (String argv[])throws IOException{
BufferedReader input =new BufferedReader (new InputStreamReader ( System.in));
String inputString;
int cube1, cube2, cube3;
System.out.println("We are going to create 3 different cubes");
System.out.println("Please enter the first cubes side:");
inputString = input.readLine();
cube1 = Integer.parseInt(inputString);
System.out.println("Cube1: " + cube1.toString());
System.out.println("Please enter the second cubes side:");
inputString = input.readLine();
cube2 = Integer.parseInt(inputString);
System.out.println("Cube2: " + cube2.toString());
System.out.println("Please enter the third cubes side:");
inputString = input.readLine();
cube3 = Integer.parseInt(inputString);
System.out.println("Cube3: " + cube3.toString());
}
}
now this is my first time ever even using input in a java program, and imkinda loss about what to do. What is suppose to be done is the user inputs the data of the side and the program calculates and displays the results, but im not quiet sure what im doing
[2993 byte] By [
addona] at [2007-10-2 1:39:18]

Hi! Here is the complete program.
import java.io.* ;
public class CubeUser{
//private double length;
private double surfaceArea;
private double volume;
public CubeUser(double lengthIn){
//length = lengthIn;
surfaceArea = ((lengthIn * lengthIn) * 6);
volume = (lengthIn * lengthIn * lengthIn);
}
public String toString(){
return("The surface area is: " + surfaceArea + " and the volume is: " + volume);
}
public static void main (String argv[]) throws IOException {
BufferedReader input = new BufferedReader ( new InputStreamReader ( System.in));
String inputString;
int cube1, cube2, cube3;
System.out.println("We are going to create 3 different cubes");
System.out.println("Please enter the first cubes side:");
inputString = input.readLine();
cube1 = Integer.parseInt(inputString);
CubeUser u1 = new CubeUser(cube1);
System.out.println("Cube1: " + u1.toString());
System.out.println("Please enter the second cubes side:");
inputString = input.readLine();
cube2 = Integer.parseInt(inputString);
CubeUser u2 = new CubeUser(cube2);
System.out.println("Cube2: " + u2.toString());
System.out.println("Please enter the third cubes side:");
inputString = input.readLine();
cube3 = Integer.parseInt(inputString);
CubeUser u3 = new CubeUser(cube3);
System.out.println("Cube3: " + u3.toString());
}
}
I've just combined both of your programs.
In the main method, the line
CubeUser u1 = new CubeUser(cube1);
creates an object of the class.
This calls the constructor which calculates the surface area and volume using the value you pass in cube1.
Subsequently calling
System.out.println("Cube1: " + u1.toString());
would call the toString() method to display the o/p.
See if its clear now or revert.
Ishana at 2007-7-15 19:02:50 >

I'm not totally sure what you are trying to do. You cannot guarantee that the user will enter three identical numbers. Hence, you cannot ensure that the three dimensional shape will in fact be a cube.
However, you can still calculate surface area and volume. Simply modify your constructor to take the three values submitted. Store all three within the class. Then you can calculate surface area or volume on demand. Provide getSurfaceArea() and getVolume() methods within CubeUser.
Eliminate the variables surfaceArea and volume. These are 'dervied' values, meaning that you can re-calculate them at any time. It would be more valuable to store the actual dimensions themselves, IMO.
- Saish
Saisha at 2007-7-15 19:02:50 >
