Solved

Composition Cube Problems

Posted on 2003-10-31
4
417 Views
Last Modified: 2006-11-17
I have some problems that I do not know what do with.  If anyone could help me that would be great.  I will post all 4 files here and pray someone can help me!  the compositioncube file is the one that I am having problems with, but I may have problems in the other ones as well.  I will post that at the top and the following ones under.

// Compositioncube


public class compositionCube
{
private double depth;
private compositionSquare square;


public compositionCube( double side, int x, int y )
{

      square = new compositionSquare( x, y );
      setDepth( side );

}

//CALCULATE SURFACE AREA
public double area()
{
      return square.area() * 6;

}

//ACCESS FOR DEPTH
public double getDepth()
{
      return depth;

}

//GET SHAPE NAME
public String getName()
{
      return "Cube";
}

//ACCESSOR TO SQUARE'S VALUE FOR AREA
public double getSquareArea()
{

      return square.area();

}

//ACCESSOR TO SQUARE'S NAME
public String getSquareName()
{
      return square.getName();

}

//ACCESSOR TO SQUARE'S ACCESSOR TO POINT'S NAME
public String getSPointName()
{
         //REMEMBER THE OBJECT square HAS ACCESS TO
         //THE getPointName MEHTOD IN THE CLASS CompositionSquare
}

//ACCESSOR TO SQUARE'S ACCESSOR TO POINT'S STRING VALUE
public String getSPointString()
{
         //REMEMBER THE OBJECT square HAS ACCESS TO
         //THE getPointString MEHTOD IN THE CLASS CompositionSquare
}

//ACCESSOR TO SQUARE'S STRING VALUE
public String getSquareString()
{
      return square.toString();

}

//RETURN A STRING DESCRIBING THE CUBE
public String toString()
{
return square.toString() + "; Depth = " + depth;
}

//CALCULATE VOLUME
public double volume()
{
return square.area() * depth;
}
}


********
// Compositionpoint

public class compositionPoint
{
private int x, y;

public compositionPoint( int xCoordinate, int yCoordinate )
{
setPoint( xCoordinate, yCoordinate );
}

public String getName()
{
return "Point";
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

public void setPoint( int xCoordinate,
int yCoordinate )
{
x = xCoordinate;
y = yCoordinate;
}

public String toString()
{
return "[" + x + ", " + y + "]";
}
}

*******
// Compositionsquare



public class compositionSquare
{
private double side;


private compositionPoint point;


public compositionSquare()
{

this( 0.0, 0, 0 );
}

public compositionSquare( double length, int x, int y )
{

      point = new compositionPoint( x, y );

      setSide( length );
}

//CALCULATES THE AREA OF THE SQUARE
public double area()
{
return side * side;
}

//GET SHAPE NAME
public String getName()
{
return "Square";
}

//THE FOLLOWING TWO METHODS ACCESS POINT'S DATA
public String getPointName()
{
return point.getName();
}

//ACCESSOR TO POINT'S STRING VALUE
public String getPointString()
{
return point.toString();
}

//GET SIDE
public double getSide()
{
return side;
}

//ERROR-CHECKS THEN SETS THE SIDE LENGTH
public void setSide( double length )
{
side = ( length >= 0 ? length : 0 );
}

public String toString()
{
return "Corner = " + point.toString() +
"; Side = " + side;
}
}

*******
// Compositiontest

import java.text.DecimalFormat;

import javax.swing.*;

public class compositionTest
{
//TEST CLASSES POINT, SQUARE AND CUBE
public static void main( String args[] )
{

compositionCube cube = new compositionCube( 3.3, 10, 10 );

DecimalFormat precision2 = new DecimalFormat( "0.00" );

String result = "";

result += "\n" + cube.getSPointName() + ": " + cube.getSPointString();

result += "\n" + cube.getSquareName() + ": " + cube.getSquareString();

result += "\n" + "Area of Square = " + precision2.format( cube.getSquareArea() );

result += "\n" + cube.getName() + ": " + cube.toString();

result += "\n" + "Surface Area of Cube = " + precision2.format( cube.area() );

result += "\n" + "Volume of Cube = " + precision2.format( cube.volume() );

JOptionPane.showMessageDialog( null, result, "Shapes",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );
}
}
0
Comment
Question by:brassmon
  • 2
  • 2
4 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 9659853
It might be more efficient if you indicated what problems you are having. Won't compile? Won't run? Runs but gives wrong results? Don't understand?
0
 
LVL 16

Expert Comment

by:imladris
ID: 9659867
Also, including at least some of any error messages you are getting (if you are) would be helpful.
0
 
LVL 15

Accepted Solution

by:
jimmack earned 500 total points
ID: 9661036
First problems:

It doesn't compile (for the following reasons):

1) In the compositionCube constructor, you are creating a compositionSquare, but this class doesn't have a constructor that only takes two ints.

2) Again the the compositionCube constructor, this class doesn't have a setDepth method.

        depth = side;

3) Same class.  getSPointName() and getSPointString() don't return anything.

I tried some fixes as follows:

1) and 2) Modified the constructor of compositionCube to:

    public compositionCube(double side, int x, int y)
    {
//        square = new compositionSquare(x, y);
//        setDepth(side);
        square = new compositionSquare(side, x, y);
        depth = side;
    }

3) Returned "Dummy" from each of the two methods.

The result was a window, titled "Shape" displaying:

Dummy:Dummy

Square: Corner = [10, 10]; Side = 3.3

Area of Square = 10.89

Cube: Corner = [10, 10]; Side = 3.3; Depth = 3.3

Surface Area of Cube = 65.34

Volume of Cube = 35.94

Is this what you expect?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9661062
If I go one step further and include the code that is implied in the comments of the getSPoint... methods, like so:

    public String getSPointName()
    {
        //REMEMBER THE OBJECT square HAS ACCESS TO
        //THE getPointName MEHTOD IN THE CLASS CompositionSquare
        return (square.getPointName());
    }

    public String getSPointString()
    {
        //REMEMBER THE OBJECT square HAS ACCESS TO
        //THE getPointString MEHTOD IN THE CLASS CompositionSquare
        return (square.getPointString());
    }

The first line in the dialog changes from "Dummy:Dummy" to "Point: [10, 10]"

Again, I have no way of knowing if this is what you actually want.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now