aspose file tools*
The moose likes Beginning Java and the fly likes Testing classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Testing classes" Watch "Testing classes" New topic
Author

Testing classes

Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
Hi I am trying to write a program that calculates volume in a class called box. I am using a class called testbox to test the box class. I am having diffuculty writing the code to test the box. I am creating two boxes to calculate.This is a copy of my box code it compiles:
import javax.swing.*;
public class Box {
// Instance Variables
int length ;
int width ;
int height ;
// Constructors
public Box ( int width, int height, int length ){

width = 0 ;
height = 0 ;
length = 0 ;
}
public void setValues() {
String heightString,lengthString,widthString;
do
{

heightString = JOptionPane.showInputDialog(null,
"Please enter the height of your box with a positive number:");

height = Integer.parseInt(heightString);
}
while (height > 0 );
do
{

widthString = JOptionPane.showInputDialog(null,
"Please enter the width of your box with a positive number:");

width = Integer.parseInt(widthString);
}
while ( width > 0 );

do
{

lengthString = JOptionPane.showInputDialog(null,
"Please enter the length of your box with a positive number:");

length = Integer.parseInt(lengthString);
}
while (length > 0 );
}

// Methods
public String toString() {

return "(" +length +"," + width + "," + height +")" ;
}

}
Now this is a copy of my testbox code it doesn't. What am I doing wrong?
mport javax.text.*;
public class testbox {
public static void main ( String [] arg) {
Box boxone = new Box(setValues);
Box boxtwo = new Box(setValues);
System.out.println("Box 1 Dimensions = " + (boxone.toString(0));
System.out.println("Box 1 Dimensions = " + (boxtwo.toString(0));
System.out.println("Box 1 VOlume is = " + (boxone.volume);
System.out.println("Box 2 VOlume is = " +(boxtwo.volume);
}
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Lisa,
You had a few little problems with both classes.
Here is the fix:
Box.java

testbox.java

Go thru that and if you have any questions, post a reply.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
Well I put the volume formula in a pubblic class and I compiled and received 2 errors stating cannot resolce symbol at new Box. I was also wondering how can i write a class that lets me test to test if either of the boxes fits into the other and report whether the first fits into the second, the second fits into the first, or neither one fits into the other. I would like to use a loop with this.
Again here is a copy of my code
import javax.swing.*;
public class Box {
// Instance Variables
int length ;
int width ;
int height ;
// Constructors
public Box ( int width, int height, int length ){

width = 0 ;
height = 0 ;
length = 0 ;
}
public void setValues() {
String heightString,lengthString,widthString;
do
{

heightString = JOptionPane.showInputDialog(null,
"Please enter the height of your box with a positive number:");

height = Integer.parseInt(heightString);
}
while (height > 0 );
do
{

widthString = JOptionPane.showInputDialog(null,
"Please enter the width of your box with a positive number:");

width = Integer.parseInt(widthString);
}
while ( width > 0 );

do
{

lengthString = JOptionPane.showInputDialog(null,
"Please enter the length of your box with a positive number:");

length = Integer.parseInt(lengthString);
}
while (length > 0 );

}

// Methods
public String toString() {

return "(" +length +"," + width + "," + height +")" ;
}
public int volume() {

return length * width * height ;
}
}
public class testbox
{ public static void main ( String [] arg)
{ Box boxone = new Box();
Box boxtwo = new Box();
boxone.setValues();
boxtwo.setValues();
System.out.println("Box 1 Dimensions = " + boxone.toString());
System.out.println("Box 1 Dimensions = " + boxtwo.toString());
System.out.println("Box 1 Volume is = " + boxone.volume());
System.out.println("Box 2 Volume is = " + boxtwo.volume());
System.exit(0); }
}
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Lisa,
You are calling a parameterless (default) constructor and you haven't defined one. Here is the only constructor you have defined:

Now passing the three values into this constuctor and then setting them to 0 doesn't do much for our class. As a matter of fact, you probably don't realize it, but you're not even setting Box.width, Box.height and Box.length here; those variables are hidden by their namesakes in the constructor, so all you are doing is changing whatever the programmer passed in to the constructor to all zeros. This is probably what you meant to do:

Now you can make a default constructor that calls the above constructor and zeros everything:

Another problem you are going to find is your conditional for terminating the do loops are doing the opposite of what you want. Instead of:

You need to do this:

That demands a positive integer greater than zero. If you wish to allow zero then change the conditional to while (height < 0); You'll have to do the same for the other two do loops.
Hope this helps,
Michael Morris
[ February 18, 2003: Message edited by: Michael Morris ]
Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
I was also wondering how can i write a class that lets me test to test if either of the boxes fits into the other and report whether the first fits into the second, the second fits into the first, or neither one fits into the other. I would like to use a loop with this. I would like to call the class public boolean fit() and pass the parameter of one box to compare to the other, but I don't know where to begin.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Lisa,
You probably want to do something like this:

Just put the fit method into the Box class. This tests whether the Box b will fit into this.
It assumes that the wall thickness is zero and therefore if the tested box has identical dimensions it will fit into this. You can change that behavior by changing all the <=s to <s. If any of this is confusing, post another reply and I will try to answer it.
Hope this helps,
Michael Morris
Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
I tried to use the example micheal gave but I did not understand the array concept. I used a if statement and got a data imcompatible error stating cannot use integer with boolean. here is the code
public boolean Fitsin(Box b) {
boolean result = false;
if ( length = b.length &&
width = b.length &&
height =b.height)
result = true;
return result;
}
}
what m i doing wrong. Also in ending my program i would like to prompt the user to contiune or quit. how can i send the user back to entering the dimensions of the box. Oh yeah i did have a successful compilatiom with the box without the fitsin class.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Lisa,
You're using the wrong operator. = is an assignment operator. What you want is the comparison operator ==. Your code is legal in C but probably wouldn't do what you wanted.
Let me explain why I put the three sides into an array and then sorted it with Arrays.sort(). In order to guarantee that Box b will fit into this, both boxes need to be normalized. Whose to say whether b.height > b.length and this.height < this.length? So we need to compare the sides in order of their respective magnitudes, hence the sort.
Hope this clears it up,
Michael Morris
Lisa Smith
Ranch Hand

Joined: Sep 24, 2002
Posts: 57
I compiled your way and tried to print the result in testbox. Fit box in box cannotbe applied to <> this is my code.
public class testbox

{ public static void main ( String [] arg)
{ Box boxone = new Box();
Box boxtwo = new Box();
boxone.setValues();
boxtwo.setValues();
System.out.println("Box 1 Dimensions = " + boxone.toString());
System.out.println("Box 1 Dimensions = " + boxtwo.toString());
System.out.println("Box 1 Volume is = " + boxone.volume());
System.out.println("Box 2 Volume is = " + boxtwo.volume());
System.out.println("Will Box 2 fit into box 1 " + boxtwo.fits());
System.exit(0); }
}
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Lisa,
The problem is that your fits() method requires a Box parameter. Change that line to:

One further point, the code that I provided is the inverse of what you are requesting above. So you probably want to do this instead:

Hope this helps,
Michael Morris
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
Hi Lisa,
Is this a homework assignment you are working on? You mentioned that you did not understand an array concept you were trying to apply, and indicated that you don't know how to show the user an input box in a loop. I strongly suggest that you practice on using arrays and loops, before you tackle Object Oriented Programming concepts.
The things I mentioned will pop up all over the place when you are working with OOP, and it will be assumed that you have the necessary programming fundamentals background before tackling OOP. Please don't take my suggestions the wrong way, I just don't want to see you struggle more than you have to
Thank you,
Thomas


if you don't know, then ask. if you do know, then share. love is knowledge.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing classes