Advertisement
fosterbl

Rectangle.java COMPLETE

Feb 4th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.77 KB | None | 0 0
  1. public class Rectangle{//class header
  2.    
  3.    //instance variables
  4.    private double length, width;
  5.    
  6.    //constructor
  7.    public Rectangle( double l, double w ){
  8.       length = l;
  9.       width = w;
  10.    }
  11.    
  12.    //methods
  13.    public void printArea(){
  14.       double area = length * width;
  15.       System.out.println( "Area: " + area );
  16.    }
  17.    
  18.    public void printPerimeter(){
  19.       double perim = 2 * length + 2 * width;
  20.       System.out.println( "Perimeter: " + perim );
  21.    }
  22.    
  23.    //main method
  24.    public static void main(String[] args){
  25.       Rectangle r1 = new Rectangle( 26.0, 5.0 );//instantiation
  26.       r1.printArea();
  27.       r1.printPerimeter();
  28.      
  29.       Rectangle r2 = new Rectangle( 10.0, 14.0 );
  30.       r2.printArea();
  31.       r2.printPerimeter();
  32.    }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement