Solved

Composition Cube Problems

Posted on 2003-10-31
4
418 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

813 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

17 Experts available now in Live!

Get 1:1 Help Now