Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with java code for Box Class

108 views
Skip to first unread message

gumdrop374 via JavaKB.com

unread,
Nov 6, 2005, 5:18:08 PM11/6/05
to
Create a Box class. The program should be able to create three types of boxes,
with 0, 1, or 3 parameters. If the box is a cube you should only have to
provide the length of one side. You should also be able to specify the length,
width, and height of the box. You should also be able to create a box with
zero dimensions, using default values. You should be able to perform the
following calculations on your box: area and volume. Furthermore, you should
be able to make your box larger and smaller in two different ways.

Specify by how much larger (multiplier) you would like to enlarge each
dimension of the object. For example, makeLarger(2) would multiply the length,
width, and height of the box by 2.
Specify the specific amount you would like to add to each dimension of the
box. For example, makeLarger(1, 3, 5) would add 1 to the length, 3 to the
width, and 5 to the height of the box.
Be sure to include methods for the following tasks:

Constructors (three)
Calculate and return the volume
Calculate and return the area
Enlarge the box (two ways)
Output the dimensions
Demonstrate that your program works properly. Display all results to two
decimal places.


I don't fully understand the assignment. I am attaching the code that I have
writen so far. I need help figuring out how to code the parameters. Any help
would be greatly appreciated.

public class box()
{
this.length = 1.0;
this.width = 1.0;
this.height = 1.0;
}

// Method to determine volume

public void calVolume(double vol)
{
vol = length * width * height;
}


// Method for returning volume
public double getVol()
{
return vol;
}


//Method to determine Area
public void calArea(double area)
{

area = 2*length*width + 2*length*height + 2*width*height;
}


//Return Area
public double getArea()
{
return area;
}

Bjorn Abelli

unread,
Nov 6, 2005, 5:49:18 PM11/6/05
to
"gumdrop374 via JavaKB.com" wrote...

> Create a Box class.

[snip]

> I don't fully understand the assignment. I am attaching the
> code that I have writen so far. I need help figuring out how
> to code the parameters. Any help would be greatly appreciated.

It is a beginning, but as you say, you've misinterpreted some of the
assignment, and how OOP works.

What you have done so far is to define the class and the methods how to...

> Calculate and return the volume
> Calculate and return the area

However, you've done the same mistake with them, as those should only be one
method each, not two.

Let's start with some basic OO.
==================================================

Object-orientation means to let each object have its own data, *and* the
methods to use on them.

When we look at the brackets, you've managed to put the methods *outside* of
the class.

You also try to assign some values to instance variables, without having
them *declared*! You can declare them and assign a default value in the same
sentence.

[As a side note: The assignment said "Box" with a capitalized name. Maybe it
will be essential in consecutive assignments that you actually name it that
way. Even if not, it's common practice in Java to use Capitalized names for
classes]


> public class box()
> {
> this.length = 1.0;
> this.width = 1.0;
> this.height = 1.0;
> }

==================================================
Apart from the methods not being inside the class, there's also some minor
flaws in them.

As the assignment said to Calculate *and* return the volume, you should do
just that. put the calculation and return into one single method.

Another small error is that you don't use the in-argument for anything but
to use it for the result. Hence it isn't needed as an argument at all, but
you should declare it *inside* the method as a plain local variable.

> // Method to determine volume
>
> public void calVolume(double vol)
> {
> vol = length * width * height;
> }
>
>
> // Method for returning volume
> public double getVol()
> {
> return vol;
> }

==================================================

The same goes for the task to "Calculate and return the area" as with the
methods above.

> //Method to determine Area
> public void calArea(double area)
> {
>
> area = 2*length*width + 2*length*height + 2*width*height;
> }
>
>
> //Return Area
> public double getArea()
> {
> return area;
> }

==================================================

After correcting the above, I would suggest you to continue with the
following:

> Output the dimensions

In that way you have an output to use in the following task...

> Demonstrate that your program works properly.
> Display all results to two decimal places.

Create a "test class" with just a main-method, in which you instantiate a
Box and use the output of dimensions, and you can also use the results of
the previous methods as well.

After that, you can go on to:

> Constructors (three)

Adapt the "test class" as you go along, to see if the latest changes work.
Do it between each change you make.

> Enlarge the box (two ways)

Same as before, adapt the "test class" as you go along, to see if the latest
changes work. Do it between each change you make.


// Bjorn A

Andrew Thompson

unread,
Nov 6, 2005, 5:53:00 PM11/6/05
to
gumdrop374 via JavaKB.com wrote:


> ..I am attaching the code that I have
> writen so far.

I take it it is not complete enough to compile yet?

// Java nomenclature would suggest a class name
// begins with EachWordUpperCase, so this should
// be 'Box'
> public class box()

/** Length of this Box */
double length;
double width;
double height;

> {

// 'this.length' refers to the 1st double
// declared above. 'this' is the instance
// or object of 'box' that has that 'length'.


> this.length = 1.0;
> this.width = 1.0;
> this.height = 1.0;
> }
>
> // Method to determine volume
>
> public void calVolume(double vol)
> {
> vol = length * width * height;
> }

The way you coded this suggests that you expect 'vol'
to be filled with the correct value while inside
the method. Here is how you should do it instead.

/** Calculates the volume from the current
width/length/height
@param none. requires no parameters
@return the volume */
public double calculateVolume() {
return length*width*height;
}

Then, when you need to call it later...
Box theBox = new Box(1.2,5,6);
double theVolume = theBox.calculateVolume();

I think that gives you enough to get you going again?

HTH

Roedy Green

unread,
Nov 6, 2005, 6:17:51 PM11/6/05
to
On Sun, 06 Nov 2005 22:18:08 GMT, "gumdrop374 via JavaKB.com"
<u15523@uwe> wrote, quoted or indirectly quoted someone who said :

>Create a Box class. The program should be able to create three types of boxes,
>with 0, 1, or 3 parameters. If the box is a cube you should only have to
>provide the length of one side.

http://mindprod.com/jgloss/homework.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Monique Y. Mudama

unread,
Nov 8, 2005, 11:37:15 AM11/8/05
to
On 2005-11-06, Roedy Green penned:

> On Sun, 06 Nov 2005 22:18:08 GMT, "gumdrop374 via JavaKB.com"
><u15523@uwe> wrote, quoted or indirectly quoted someone who said :
>
>>Create a Box class. The program should be able to create three types
>>of boxes, with 0, 1, or 3 parameters. If the box is a cube you
>>should only have to provide the length of one side.
>
> http://mindprod.com/jgloss/homework.html

With all due respect, the OP wasn't trying to get anyone to do their
homework, but rather had questions about how to interpret the
assignment. They provided the code they'd tried and asked a specific
question. Yes, it would have been better to introduce the question
before the assignment transcript, but ... as homework posts go, I
think this one was fairly legit.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Roedy Green

unread,
Nov 8, 2005, 10:48:07 PM11/8/05
to
On Tue, 8 Nov 2005 09:37:15 -0700, "Monique Y. Mudama"
<sp...@bounceswoosh.org> wrote, quoted or indirectly quoted someone who
said :

>> http://mindprod.com/jgloss/homework.html


>
>With all due respect, the OP wasn't trying to get anyone to do their
>homework, but rather had questions about how to interpret the
>assignment.

>monique

have you read the essay? It is not just about the foolishness of
playing helpless.

Andrew Thompson

unread,
Nov 9, 2005, 12:05:38 AM11/9/05
to
Roedy Green wrote:

> On Tue, 8 Nov 2005 09:37:15 -0700, "Monique Y. Mudama"
> <sp...@bounceswoosh.org> wrote, quoted or indirectly quoted someone who
> said :
>
>>>http://mindprod.com/jgloss/homework.html
>>
>>With all due respect, the OP wasn't trying to get anyone to do their

>>homework, ..
...


> have you read the essay?

I did[1].

> ..It is not just about the foolishness of
> playing helpless.

[1] ..because I was about to make a similar comment,
before I read what it contained, and realised the
conclusions you (well, me - anyway) might jump to
from the URL were not really what the page was about.

[ I was prompted to make that comment because I have felt
what it is like to have someone ..question a document to
which I linked, while obviously not having read a single
word of it! It is exasperating. :-( ]

Monique Y. Mudama

unread,
Nov 9, 2005, 11:51:33 AM11/9/05
to
On 2005-11-09, Andrew Thompson penned:

Mea culpa. I thought I recognized the link and knew its contents, but
obviously I was mistaken.

That being said:

1) The link was given no context, not even a one-line "Here are some
pointers on how to approach your homework assigment."

2) Roedy himself confuses his own links frequently, so obviously he
doesn't always check them before posting. Therefore I feel only
mildly bad about mis-recognizing it.

3) Roedy has certainly been known to post links of the sort I thought
his one was.

Anyway, yes, I screwed up. Sorry.

0 new messages