(wL) Forums
print("Need help with the program."); - Printable Version

+- (wL) Forums (https://war-lords.net/forum)
+-- Forum: Resources (https://war-lords.net/forum/forum-31.html)
+--- Forum: Programming (https://war-lords.net/forum/forum-33.html)
+--- Thread: print("Need help with the program."); (/thread-10012.html)



print("Need help with the program."); - Danger255 - Mar 04 2013

Code:
/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}

When executed it doesn't shows up anything (running in Netbeans)


RE: print("Need help with the program."); - Morpheus - Mar 19 2013

Here:
Code:
class Box {
private double width,height,length, volume;
public Box(double w, double l, double h)
{
width = w, height = h, length = l;
volume =  width*height*length;
}
public double getVolume()
{
return volume;
}
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box(10,20,15);
System.out.println("Volume is " + mybox.getVolume());
}
}

Now, I know this isn't the best approach but it does what it is supposed to. You can add more methods such as "getHeight, getLength, getwidth, setHeight, setLength, setwidth". In my opinion, changing the values of the properties like you did... I would use mybox.setHeight(23.3);