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 2 of 2

Thread: Java Simple Rectangle Program Error

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Simple Rectangle Program Error

    public class RectangleObjects {
    	public static void main(String[] args) {
    		//Create a rectangle with width of 4 and height of 40
    		SimpleRectangle rectangle1 = new SimpleRectangle();
    		System.out.println("The width of the rectangle is " + rectangle1.width + ", height is " + rectangle1.height + ", area is " + rectangle1.getArea() + ", and perimeter is " + rectangle1.getPerimeter());
     
    		//Create a rectangle with width of 3.5 and height of 35.9
    		SimpleRectangle rectangle2 = new SimpleRectangle(3.5, 35.9);
    		System.out.println("The width of the rectangle is " + rectangle2.width + ", height is " + rectangle2.height + ", area is " + rectangle2.getArea() + ", and perimeter is " + rectangle2.getPerimeter());
     
    }
    }
     
    	class SimpleRectangle {
    		double width;
    		double height;
     
    		//Construct a rectangle with width of 4 and height of 40
    		SimpleRectangle() {
    			width = 4;
    			height = 40;
    		}
     
    		SimpleRectangle(double newWidth, double newHeight) {
    			width = newWidth;
    			height = newHeight;
    		}
    		//Return the area for rectangle objects
    		double getArea() {
    			return 2 * width * 2 * height;
    		}
    		//Return the perimeter for rectangle objects
    		double getPerimeter() {
    			return 2 * width + 2 * height;
    		}
     
    		//Set a new width and height for this rectangle
    		void setWidth(double newWidth, double newHeight) {
    			width = newWidth;
    			height = newHeight;
    		}	
    }

    Output:
    The width of the rectangle1 is 4.0, height is 40.0, area is 160.0, and perimeter is 88.0
    The width of the rectangle2 is 3.5, height is 35.9, area is 125.64999999999999, and perimeter is 78.8

    Can someone show me how to limit the output to 2 decimal places for 125.64999999999999 to 125.65?

    --- Update ---

    Never mind. I got everything fixed. Please close and/or delete this post, thank you.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Simple Rectangle Program Error

    You can mark your threads as SOLVED by using the "Thread Tools" directly above the first post in a thread.
    This thread marked SOLVED for you.

Similar Threads

  1. Better solution for moving my rectangle? (simple game)
    By JohnZ in forum Java Theory & Questions
    Replies: 5
    Last Post: May 24th, 2012, 09:54 AM
  2. Rectangle/Ellipse program help
    By ja3n in forum AWT / Java Swing
    Replies: 1
    Last Post: March 1st, 2012, 05:51 AM
  3. Replies: 3
    Last Post: November 10th, 2011, 07:11 AM
  4. Simple I/O Java Error
    By Prox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2011, 04:48 PM
  5. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM