Results 1 to 7 of 7
  1. #1
    DanK is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default Help with beginners program

    Hi, i am new to java and my assignment is to:
    Write a class Rectangle that represents a rectangle with integer width
    and height. Include a constructor that builds a rectangle with a given
    width and height and another constructor (with one parameter) that
    builds a rectangle that is actually a square of a given size. Make sure
    these constructors check that the width and height are positive. Add a
    constructor that takes no parameters and builds a square of size 1.


    My code so far is:
    public class Rectangle {

    private int width;
    private int height;

    public Rectangle (){
    width = 1;
    height = 1;
    }


    public Rectangle(int square){

    }

    public Rectangle (int height, int width){
    height = height;
    width = width
    }

    }




    But im not sure if i am doing it right, also can some one give me advice on how to do the sqaure part, and check if it is a square because that is where i think i messed up. thnkx!
    Last edited by DanK; 12-15-2008 at 01:42 AM.

  2. #2
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    Did you run this through a compiler? Also your width = width is missing a ; and use height.this and width.this if your going to use the same name outside of the method and inside of the method

  3. #3
    DanK is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    ok i fixed that but i dont thinks its suppose to return anything yet because i also have to create a client, but my teacher gave me a hint that this file should have two fields, three constuctors, and two methods, but im not sure where the method should go

  4. #4
    gnarly hogie is offline Member
    Join Date
    Oct 2008
    Posts
    34
    Rep Power
    0

    Default

    What do you mean by where the methods are supposed to go? If you mean physically in the program then it probably doesn't matter. Also do you know what the methods are supposed to do? I think one is supposed to check if the numbers given are positive.

  5. #5
    DanK is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    o ok, but yea im not sure what the other method is suppose to do
    , maby return void or something?

  6. #6
    DanK is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    the rest of my directions are:
    Add a boolean method isSquare that returns true if and only if the
    rectangle is a square. Add a method quadratize that converts this
    rectangle into a square with approximately the same area — the closest
    possible for a square with an integer side.


    Test all your constructors and methods in a simple console application.
    Define several rectangles, check which ones among them are squares,
    and print appropriate messages. “Quadratize” one of the rectangles,
    verify that it becomes a square, and print an appropriate message.

  7. #7
    DanK is offline Member
    Join Date
    Dec 2008
    Posts
    10
    Rep Power
    0

    Default

    right now my code is:
    public class Rectangle {

    private int width;
    private int height;

    public Rectangle (){
    width = 1;
    height = 1;
    }


    public Rectangle(int square){

    }

    public Rectangle (int height, int width){
    this.height = height;
    this.width = width;

    }
    boolean isSquare;{

    if (height==width)
    isSquare = true;
    else
    isSquare = false;
    }

    public int quadratize()

    {
    int sideLength = (int) Math.sqrt (width * height);
    int area = sideLength*sideLength;

    return area;



    }



    }

Similar Threads

  1. Replies: 3
    Last Post: 10-23-2008, 05:11 AM
  2. what is d best spring book 4 beginners
    By shikhin_82@yahoo.co.in in forum Web Frameworks
    Replies: 3
    Last Post: 10-17-2008, 04:12 PM
  3. Executing a program within a program
    By gibsonrocker800 in forum New To Java
    Replies: 5
    Last Post: 05-12-2008, 08:24 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

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