Solved

Composition Cube Problems

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 62
allStar challenge 1 41
mapShare challenge 13 68
advertisement module in core php 4 88
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 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

21 Experts available now in Live!

Get 1:1 Help Now