We have a programming assignment for my Java Programming class and I can't figure out to fix my output. Here's the code:

   class Box {

            double width;
            double height;
            double depth;
            double w2, h2, d2;
            double v2;
            Box() {
            }
            Box(double w, double h, double d) {
                    width = w;
                    height = h;
                    depth = d;
            }
            void getVolume() {
                    System.out.println("Volume is : " + width * height * depth);
            }
            void Match(double a, double b, double c) {
                    w2 = a;
                    h2 = b;
                    d2 = c;
            }
            void getNumber() {
                    System.out.println("# of Matches is : " + width * height * depth / v2);
            }
    }

    public class fep2 extends Box {

            double weight;
            fep2() {
            }
            fep2(double w, double h, double d, double m) {
                    super(w, h, d);
                    weight = m;
            }
            fep2(double a, double b, double c) {
                    super(a, b, c);
                    double v2 = a * b * c;
            }

            public static void main(String args[]) {
                    fep2 mb1 = new fep2(10, 10, 10, 10);
                    fep2 mb2 = new fep2(2, 2, 2);
                    mb1.getVolume();
                    mb2.getNumber();
                    System.out.println("width of MatchBox 1 is " + mb1.width);
                    System.out.println("height of MatchBox 1 is " + mb1.height);
                    System.out.println("depth of MatchBox 1 is " + mb1.depth);
                    System.out.println("weight of MatchBox 1 is " + mb1.weight);
            }
    }

And here's my output:

Volume is : 1000.0
# of Matches is : Infinity
width of MatchBox 1 is 10.0
height of MatchBox 1 is 10.0
depth of MatchBox 1 is 10.0
weight of MatchBox 1 is 10.0

The number of matches in the box shouldn't be infinity. By my math it should've came out ot 125. Can anyone please help?

Recommended Answers

All 6 Replies

Probably becuase v2 is zero on line 24. Perhaps you should be setting v2 somewhere?

Print out the values of all the variables used to compute the value that is being printed just before the expression that does the computation. That should show you where the problem is.

if the volume output part works fine and the # of matches doesn't and te only thing different is division by v2 then check to see where you are using the variable. see if you properly intialized it or not and print out it's value to check

Scratch this code and start again from zero. Your code is completely bad design, sorry to say. Also the basic logic of finding number of matches fitting in matchbox is wrong. Number of matches is not volume of box divided by volume of match (which I think you are aiming at) in general case, but only by "chance". By chance I mean that match size must be "fixed" to fit exactly the width of the box and height of the box and the length of the match would be the length of the box exactly. Only liquid could fill the box exactly. I hate the maths not matching the reality, even I understand this to be artificial example for teaching. But for me it teaches bad thinking, which is even worse that bad programming (which is worse than bad coding) :(

number_of_matches = number_of_matches_per_level * number_of_levels //assuming matches are never end to end

Probably becuase v2 is zero on line 24. Perhaps you should be setting v2 somewhere?

He is right. You set a new v2 in your fep2 class. What you want to do is:

EXCLotto(double a, double b, double c) {
    super(a, b, c);
    **this.v2** = a * b * c;
}

Thanks for all the help guys but, come to find out my professor took down the turn in files early yesterday morning so I couldn't turn it in anyway, but I'm still going to try and solve it just for personal knowledge gain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.