Results 1 to 2 of 2
  1. #1
    Nexgen23's Avatar
    Join Date
    Jul 2013
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0

    Getting started with objects

    SOLVED! Not sure how to delete threads :/


    Hi everyone,

    First of all I'd like to say what up? Feeling fresh to be a new member to this forum ^_^

    To the request!

    So a small task I've been assigned with as homework for my course is to

    Code:
    Create a class that represents a rectangle. A rectangle is specied by the coordinates (x; y) of its
    top-left corner and its width and height. This class will be used to create many rectangle objects,
    and it must feature:
    { dynamic elds (variables) to store x, y, the width and the height as fractional numbers;
    { a constructor with parameters that provide x, y, the width and the height;
    { a function that returns the area of the rectangle;
    { a function that returns the length of the perimeter of the rectangle; and
    { a function that returns a string representation of the rectangle, the four numbers, within a pair
    of parentheses, separated by commas. This function must be named toString().
     Write, in a separate class, a main method that accepts the four numbers as command line arguments,
    instantiates a rectangle object, and prints the string representation of the rectangle, the area and
    the perimeter.
    So I created 2 classes, TestRect and Rectangle.

    Rectangle.java

    Code:
    package lab3;
    
    public class Rectangle {
        private double x;
        private double y;
        private double width;
        private double height;
        public double area;
        public double perimeter;
        public String rectangle;
    
        public Rectangle(double newX, double newY, double newWidth, double newHeight){
           x = newX;
           y = newY;
           width = newWidth;
           height = newHeight;
        }
        
        public double area(){
            area = width*height;
            return area;
        }
        
        public double perimeter(){
           perimeter = (width*2)+(height*2);
           return perimeter;
        }
        
        public String toString(){
           rectangle = "(" + x + ", " + y + ", " + width + ", " + height + ")";
           return rectangle;
        }
    }
    TestRect.java

    Code:
    package lab3;
    
    public class TestRect {
    
        private Rectangle rectangle;
        
        public void main(String[] args){
           rectangle = new Rectangle( Integer.parseInt(args[0]), 
                                      Integer.parseInt(args[1]), 
                                      Double.parseDouble(args[2]), 
                                      Double.parseDouble(args[3]));
           rectangle.area();
           rectangle.perimeter();
           rectangle.toString();
           System.out.println("Rectangle: " + rectangle.rectangle);
           System.out.println("Area: " + rectangle.area);
           System.out.println("Perimeter: " + rectangle.perimeter);
           
        }
    }
    So everything compiles all nice but I get no output when I execute the program.

    I'm a bit stumped at the moment.

    Any help would be so greatly appreciated!
    Last edited by Nexgen23; 08-08-2013 at 06:57 AM. Reason: Solved

  2. #2
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    edit: removed. looks good.

    Why did you make 3 of the member variables public and 3 private?

    Also, you should put
    Code:
           rectangle.area();
           rectangle.perimeter();
           rectangle.toString();
    inside the constructor of the rectangle class. Otherwise you're forcing anyone who uses your class to *remember* to call the functions before using the properties.

    -Actually, since you're storing all the data anyway (and not allowing x,y,width,height to be changed: ie. size is constant), you should really only calculate it all once! Inside the constructor, calculate perimeter,area, and tostring. Then make the functions simply return the private members - no calculations needed. And you avoid the above issue about remembering to call the functions to 'initialize' the values before use.
    Last edited by abuckau907; 08-08-2013 at 10:25 AM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

Similar Threads

  1. [C/C++ Tutorial] Getting Started With Visual C++/CLI
    By Hassan in forum Programming Tutorials
    Replies: 44
    Last Post: 04-02-2021, 07:16 PM
  2. [Help] Getting Started With C++ [Solved]
    By Cehk! in forum C++/C Programming
    Replies: 6
    Last Post: 11-29-2011, 04:39 AM
  3. [Help] Getting started with Uniform Spatial Subdivision.
    By bubblesppf in forum C++/C Programming
    Replies: 0
    Last Post: 07-25-2011, 01:49 PM
  4. Getting started with Mw 2 MPGH
    By lolbie in forum Call of Duty Modern Warfare 2 Private Servers
    Replies: 27
    Last Post: 07-31-2010, 08:51 PM
  5. [Tutorial] Getting Started With a On Screen KeyBoard
    By CoderNever in forum Visual Basic Programming
    Replies: 11
    Last Post: 12-02-2009, 05:02 PM