Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes More help on Java problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "More help on Java problem." Watch "More help on Java problem." New topic
Author

More help on Java problem.

Troy Johnson
Greenhorn

Joined: Sep 24, 2003
Posts: 20
Hello. I think that I am pretty close on the solution to my java problem. However, I think that I might still be missing something. Any more suggestions? Thanks again. I have posted the problem and the solution that I came up with. But I think that I might still be missing something?
Here is my problem:
Write a class named Cube to represent cubes. The member fields are width, height, length and color. Use a double for width, height, length, and a String for color.

Use the following class outline.

public class Cube{

private double width = 1.0;

private double height = 1.0;

private double length = 1.0;

private static String color = "white";

public Cube(){}

public Cube(String newColor){}

public Cube(double newWidth, double newHeight, double newLength){}

public Cube(double newWidth, double newHeight, double newLength, String newColor){}

public double getVolume(){}

public double getWidth(){}

public double getHeight(){}

public double getLength(){}

public String getColor(){}
}

Write a test program to verify all methods and constructors. Note: you need to "construct" more than one instance to adequately test this class.
Here is the solution I have come up with:
// This class name is Cube
public class Assignment6_4 {
public static void main(String[] args) {
Cube cube = new Cube();
double newWidth = 1.0;
double newHeight = 1.0;
double newLength = 1.0;
String newColor = "white";
System.out.println(cube.toString());
}
}
class Cube{

private double width = 1.0;

private double height = 1.0;

private double length = 1.0;

private static String color = "white";
public Cube(){ }

public Cube(String newColor) {
this.color = newColor;
}

public Cube(double newWidth, double newHeight, double newLength){

this.width = newWidth;
this.height = newHeight;
this.length = newLength;
}
public Cube(double newWidth, double newHeight, double newLength, String newColor){

this.width = newWidth;
this.height = newHeight;
this.length = newLength;
this.color = newColor;
}
/* public double getVolume() {
return volume;
}*/
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
public double getLength() {
return length;
}
public String getColor() {
return color;
}
public String toString() {
return "width" + width + "\n"
+ "height " + height + "\n"
+ "length " + length + "\n"
+ "color " + color + "\n";

}
}
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
I think people will be more willing to read your code and help if you use code tags, thus making it easier for them to read it.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Troy,
Base on the description and your code, you have achieved


one more note : for color to be an instance member, it should NOT be static.

HTH
[ October 06, 2003: Message edited by: chi Lin ]

not so smart guy still curious to learn new stuff every now and then
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm curious why a cube has three dimension variables. By definition all three must be identical, no? Can we get by with one number? Or is this a more generalized rectangular solid of some sort?
Why not start with the test program? There's a movement called Test Driven Development that works that way. It can help you envision the thing you are testing, too. Maybe:

Now, if you wrote that first, you'd have a really good picture in mind of what Cube has to do to pass the test. Then you could add tests for color, etc and build Cube to pass one new test at a time.
If you get into this approach, see JUnit.org for a test framework that automates reporting success or failure.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Just abit programming advise. In your assignment, your Cube class should be public instead of the other way around, since Cube is your main interface and your Assignment class is a test class. String color variable should not be static. Otherwise, everything looks good.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: More help on Java problem.
 
Similar Threads
inheritance problem
Rectangle error
Can I pass character array to new method?
Help with compiler error
How to fix error in Rectangle Class problem in Java?