Results 1 to 5 of 5

Thread: help!!!! please

  1. #1
    stacym87 is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default help!!!! please

    i want to test the Square class and instantiate two squares with different length sides and then print the side, area and perimeter (for example, i want one square to be 4 & the other square to be 5 in length). here's what i have but im stuck please help. im using dr. java

    this is my square class:
    public class Square {


    private String mySquare;

    //private instance variables
    //the side of the square
    double getSide;

    // the area of the square
    double myArea;

    //the perimeter of the square
    double myPerimeter;

    double mySide;





    //constructor
    public Square() {
    mySide = 4;
    }
    public Square(String ryan) {
    mySide = 5;
    }


    //methods
    public double getSide(){
    return mySide;}

    public String toString(){
    String tmpString;
    tmpString = "square: " + mySquare +
    " area = " + myArea +
    " perimeter = " + myPerimeter;
    return tmpString;
    }

    public double calculateArea()
    {
    return myArea = (mySide*4);
    }

    public double calculatePerimeter()
    {
    return myPerimeter = (mySide*4);
    }

    }













    And this is my SquareTest:public class SquareTest {
    public static void main(String[] args) {

    //define object variables (local)
    Square stacy, ryan;

    //create/instantiate objects
    stacy = new Square("stacy");
    ryan = new Square("ryan");


    stacy.getSide();
    stacy.calculateArea();
    stacy.calculatePerimeter();
    System.out.println(stacy + " side = ");



    ryan.getSide();
    ryan.calculateArea();
    ryan.calculatePerimeter();
    System.out.println(ryan + " side = ");

    }
    }

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    Is there a question coming any time soon?

  3. #3
    stacym87 is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    well its an implicit question but apparently u didnt notice at the beginning

    but here it is anyways...
    how do i create two squares with different side lengths and print them out to show the area & perimeter?

  4. #4
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    It's not implicit what parts you have problems with. In case you didn't notice.
    1. Do you trouble understanding how constructors work? I'd say so, since they don't really make any sense. At least not the second one. What's the point of passing an argument to the constructor if you're not going to use it?
    2. What is getSide? You don't use it anywhere.
    3. What's the point of myArea? And since when is a square's area "length of side * 4"? And you set myArea in calculateArea(), which means that if you call toString() before calculateArea(), myArea will be 0 (zero).
    4. What does your comments say your variables are private, when in fact they are package private. That's not the same.
    5. What is mySquare? Where do you ever assign that a value?

    Do you want me to go on? As you can see, it's impossible to implicitly understand exactly what you believe the problem is, since your code creates so many of them.

    You asked how to create a square with different side lengths (a rectangle)? Well.. if you need two different side length, it stands to reason you need two instance variables. It also implies you need to assign them both values, either in the constructor (my recommendation) or via set-methods (stupid). Read up on how constructors work, and how you make them take multiple arguments.
    Last edited by ojn; 10-07-2008 at 04:10 AM. Reason: spelling

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A great resource that has helped me and will probably help you is an article entitled How To Ask Questions The Smart Way. It will tell you how to formulate your questions so that the folks here will be better able to answer them. Good luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •