Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More help on Java problem.

 
Troy Johnson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chi Lin
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic