Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Making a rectangle

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making a rectangle

    So I am working on a hw assignment for a Java programming class and there is some slight confusion.

    I am suppose to create a rectangle and I have created two classes; Rectangle.java and RectangleTester.Java.

    So far my code for the class Rentangle.java is:

    package edu.sbcc.hw2;

    public class Rectangle {

    private int width = 25;
    private int height = 25;

    public rectangle(int xcoord, int ycoord, int thewidth, int theheight) {
    this.width = width;
    this.height = height;

    }

    public int getWidth() {

    return width;

    So for my assignment I need two instance variables for height and width for which I have, but it says in the assignment I need methods (settings and getters /mutators and accessors that allow manipulation of my instance variables which is a little confusing. Do I put these methods on Rectangle.java or RectangleTester.java.

    The same goes for the calculateArea, where am I suppose to put this?

    Am I even on the right track with this assignment or am I way off? I DO NOT want anyone to right anything for me, I want to do it myself, I just need it explained a little better.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Making a rectangle

    Please post your code correctly using code or highlight tags which are explained near the top of this link.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Making a rectangle

    Setters and Getters are created for private member variables. You could google them to read more information on how these methods usually look like. A simple example would be:
    public class Coord {
     
    	private int x;
    	private int y;
     
    	public Coord(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
     
    	public void setX(int value) {
    		x = value;
    	}
     
    	public int getX() {
    		return x;
    	}
     
    	public void setY(int value) {
    		y = value;
    	}
     
    	public int getY() {
    		return y;
    	}
     
    }
    This class has 2 private member variables and the apropriate Getters and Setters.

    You would add them to the body of your Rectangle class.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a rectangle

    Alright thanks!

    --- Update ---

    Okay right now I have two classes, Rectangle Java and RectangleTester.Java.

    Here is Rectangle:

    public class Rectangle {
    private int getArea();
    {
    	Area=(length * width);
    	return Area;
    }
    private int getPerimeter();
    {
    	Perimerter = 2*(length) +
    			2*(width);
    	return Perimeter;
    }
    private int getLength();
    }
    private int getWidth();

    And here is my RectangleTester:

    public class RectangleTester {
    	public static void main(String[] args) {
    		Rectangle rectangle = new Rectangle(75, 60);
    		int x = rectangle.getLength();
    		int y = rectangle.getWidth();
    		int area = rectangle.getArea();
    		int perimeter = rectangle.getPerimeter();
                    System.out.print("For rectangle with length " + x);
    		System.out.println(" and width " + y);
    		System.out.println(" Area = " + area);
    		System.out.println("Perimeter = " + perimeter);

    I keep getting these errors, and I am trying to figure out why.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The constructor Rectangle(int, int) is undefined
    The method getLength() from the type Rectangle is not visible
    The method getWidth() from the type Rectangle is not visible
    The method getArea() from the type Rectangle is not visible
    The method getPerimeter() from the type Rectangle is not visible
    Syntax error, insert "}" to complete ClassBody
    Last edited by Jhop86; September 11th, 2014 at 04:36 PM.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Making a rectangle

    You should really go over the basics once again before trying this program, there are countless errors in the program. Starting with misplaced semicolon and variables that have no type defined. You also removed your private member variables entirely and have closing curly brackets at wrong places.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a rectangle

    That's why I am asking for help... if someone can explain to me where I am making mistakes I can fix them. Saying you need to go over the basics again does not help... and countless errors? I count 7... I am new to this and just learning.
    Last edited by Jhop86; September 11th, 2014 at 05:19 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Making a rectangle

    Quote Originally Posted by Jhop86 View Post
    Saying you need to go over the basics again does not help...
    Yes it does. If you go back and read and reread things until you understand you will be able to fix your own problems and you learn better that way. Just expecting us to tell you how to fix the code you do not learn plus it would be our work and not yours. Or we could waste our time and type up an explanation for you which would be exactly the same as in any text book.
    Improving the world one idiot at a time!

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Making a rectangle

    Quote Originally Posted by Jhop86 View Post
    That's why I am asking for help... if someone can explain to me where I am making mistakes I can fix them. Saying you need to go over the basics again does not help... and countless errors? I count 7... I am new to this and just learning.
    We would just be repeating what all the other tutorials and textbooks say because the errors you make are so basic there is not much more to say about them. I dont feel like repeating textbooks.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Making a rectangle

    Did you review the links Norm provided in Post #7? Do you have any questions about the material presented there? Did you find an answer to the error you posted in Post #4?

  11. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginning Java- Making a Rectangle Class (help)

    So I was having a lot of problems earlier with going with the advice of many I read up and gained a little bit more knowledge about this. I went for something extremely basic so that I a) wouldn't get too confused, and b) would have a solid foundation for this assignment. So far I have the following:

    My Rectangle.java Class:

    public class Rectangle {
     
    	int iLength;
    	int iWidth;
     
    	int calcArea() {
    		return (iLength * iWidth);
    	}
    }

    My RectangleTester.java Class:

    package tutorial;
    import tutorial.Rectangle;
     
    public class RectangleTester {
    	private static void main(String[] args) {
     
    		Rectangle rectFirstRectangle = new Rectangle();
     
    		rectFirstRectangle.iLength = 20;
    		rectFirstRectangle.iWidth = 40;
     
    		 int iAreaofFirstRect = rectFirstRectangle.calcArea();
     
    		System.out.println("area of my first rectangle is:" + iAreaofFirstRect);
     
    	}
    }

    I know I am not finished yet, but I am off to an amazing start (only 2 errors instead of 7 WOOHOO)

    The problem I am having now is I keep getting an error in the RectangleTester.java associated with "package tutorial" and "import tutorial.Rectangle". These two errors are the only things that are holding me back. I thought I entered the syntax correctly, but that doesn't appear to be the case. When I delete it, the code will not run because it says it is missing an import syntax.

    Whats going on here?
    Last edited by Jhop86; September 12th, 2014 at 02:01 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a rectangle

    Threads merged.

    I keep getting an error
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Also please edit your post and format the code. Nested statements should be indented. See post#3 for an example.
    Also the code is missing some }s.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a rectangle

    Error: Main method not found in class edu.sbcc.hw2.RectangleTester, please define the main method as:
    public static void main(String[] args)

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a rectangle

    Do you read and understand English? What don't you understand about that error message? Compare its recommendation with what you have coded.

    Please edit the code and fix its formatting and provide the missing }s
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Making a rectangle

    Your code is missing closing curly brackets. Whenever you open them you also have to close them later on.
    Besides that the error message is pretty much self-explanatory, is it not?

  16. #16
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a rectangle

    Figured it out, thanks for the help. If I have anymore questions I will ask.

    --- Update ---

    Well so far it is going really good, except I ran into another error while implementing constructors.

    My new code is:

    public class RectangleTester {
    	private static void main(String[] args) {
     
    		//instantiating the class
    		Rectangle myFirstRectangle = new Rectangle(20, 50);
     
    		//variable initialization
    		myFirstRectangle.setLength (20);
    		myFirstRectangle.setWidth (50);
     
     
    		//print out
    	System.out.println("Hey David! The area of my first rectangle with the dimensions:"+myFirstRectangle.getLength()+"*"+myFirstRectangle.getWidth());;
    	System.out.println("is:"+myFirstRectangle.calcArea());
    	}
    }

    The error is: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor Rectangle(int, int) is not visible

    at edu.sbcc.hw2.RectangleTester.main(RectangleTester. java:9)

    When it says the constructor is not visible what does this mean?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Making a rectangle

    the constructor is not visible
    That refers to the private attribute. For most constructors you want to use public.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Making a rectangle

    If you write:
    private Rectangle(int width, int length) {
    ...
    }
    Then the constructor has been declared "private" and can not be used by any other classes other then the Rectangle class.
    You can use "public" instead to make the constructor visible to other classes.

    For more information read over some of the very basic tutorials, it is explained there in great detail.

  19. #19
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a rectangle

    Wow, dumb mistake, thanks!

    Now I am wrapping up the assignment, but I have one quick question.

    The assignment says that I have to set all instance variables to private, how do I this and make them visible in the RectangleTester?

  20. #20
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Making a rectangle

    That's why it has been suggested that you improve your familiarity with THE BASICS. Most (if not all) of your errors are due to misapplying BASIC concepts. You have either not been paying attention to the details, or you haven't been practicing enough. Programming in any language is a skill and must be practiced before it can be confidently, accurately, and repeatably performed.

  21. #21
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Making a rectangle

    Quote Originally Posted by Jhop86 View Post
    The assignment says that I have to set all instance variables to private, how do I this and make them visible in the RectangleTester?
    Thats what the Setters are for.

  22. #22
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making a rectangle

    I had to change Width to Height and I had to change Length to Width, in doing so everything was fine except on my RectangleTester, on line 13 I have myFirstRectangle.setHeight(20);

    It gives me these errors:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method setHeight(int) is undefined for the type Rectangl.

    It also says this method is undefined for the type of rectangle. Can someone please elaborate?

    --- Update ---

    Figured it out

Similar Threads

  1. Replies: 2
    Last Post: June 9th, 2014, 04:06 PM
  2. Rectangle keeps getting faster
    By cs218087 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 14th, 2014, 11:41 AM
  3. Drawing a rectangle
    By new_user in forum Android Development
    Replies: 1
    Last Post: February 16th, 2014, 05:07 AM
  4. getPerimeter of a rectangle
    By JlovesJava in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2013, 03:12 PM
  5. Rectangle + Origo
    By NeewBieGeKo in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2012, 01:51 PM