Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************
- *
- *File: TwoNumbers.java
- *
- *Author: Zach Barnett
- *
- *Description: Rectangles!
- *
- *Date 6/28/2017
- *
- *************************************************/
- public class Rectangle extends Object
- {
- /* INITIAL STATE */
- private final int DEFAULT_LENGTH = 2;
- private final int DEFAULT_WIDTH = 1;
- private int length = 0;
- private int width = 0;
- /* BUILDERS */
- public Rectangle ()
- {
- this.length = DEFAULT_LENGTH;
- this.width = DEFAULT_WIDTH;
- }
- public Rectangle ( int length, int width )
- {
- this.setDimensions(length,width);
- }
- /* GETTERS */
- public int getLength ()
- {
- return this.length;
- }
- public int getWidth ()
- {
- return this.width;
- }
- public int getPerimeter()
- {
- int perimeter = 0;
- perimeter = 2*(this.length + this.width);
- return perimeter;
- }
- public int getArea()
- {
- int area;
- area = this.length * this.width;
- return area;
- }
- @Override
- public String toString ()
- {
- return "Length: " + this.getLength () + " " + "Width: " + this.getWidth();
- }
- /* CHANGERS */
- public void setDimensions (int length, int width)
- {
- this.setLength(length);
- this.setWidth(width);
- }
- public void setLength(int length)
- {
- this.length=length;
- }
- public void setWidth (int width)
- {
- this.width=width;
- }
- /* MAIN ROUTINE */
- public static void main( String args[] )
- {
- Rectangle r1 = new Rectangle () ;
- Rectangle r2 = new Rectangle (4, 7) ;
- displayRectangle("1", r1);
- displayRectangle("2", r2);
- System.out.println();
- }
- private static void displayRectangle(String id, Rectangle r)
- {
- final String STRING_FORMAT = "%15s ";
- final String STRING_INTEGER = STRING_FORMAT + "%7d ";
- final String EXPRESSION = "%5d %1s % 5d";
- final String NL = "%n" ;
- final String AREA_EXPRESSION = STRING_INTEGER + "=" + EXPRESSION + NL;
- final String PERIMETER_EXPRESSION = STRING_INTEGER + "=" + EXPRESSION + "%1s" + EXPRESSION;
- final String STRING_STRING = STRING_FORMAT + "%s%n" ;
- System.out.println();
- System.out.printf(STRING_INTEGER + NL, "Length:", r.getLength() );
- System.out.printf(STRING_INTEGER + NL, "Width:", r.getWidth() );
- System.out.println();
- System.out.printf(PERIMETER_EXPRESSION,
- "Perimeter:",
- r.getPerimeter(),
- r.getLength(),
- "+",
- r.getWidth(),
- "+",
- r.getLength(),
- "+",
- r.getWidth()
- );
- System.out.printf(AREA_EXPRESSION,
- "Area:",
- r.getArea(),
- r.getLength(),
- "*",
- r.getWidth()
- );
- System.out.println();
- System.out.printf( STRING_STRING, "toString ():", r.toString() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement