Advertisement
fosterbl

Square.java incomplete inheritance code 9.2

Feb 6th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. class Rectangle{
  2.    private int length;
  3.    private int width;
  4.  
  5.    public Rectangle(){
  6.       length = 1;
  7.       width = 1;
  8.    }
  9.  
  10.    public Rectangle(int l, int w){
  11.       length = l;
  12.       width = w;
  13.    }
  14.  
  15.    public void draw(){
  16.       for(int i=0; i < length; i++)
  17.       {
  18.          for(int j=0; j < width; j++)
  19.             System.out.print("* ");
  20.          System.out.println();
  21.       }
  22.       System.out.println();
  23.    }
  24.  
  25. }
  26.  
  27. // 1. Make the class square inherit from Rectangle
  28. public class Square{
  29.    // 2. Add a Square no-argument constructor
  30.  
  31.    // 3. Add a Square constructor with 1 argument for a side
  32.  
  33.    public static void main(String[] args){
  34.       Rectangle r = new Rectangle(3,5);
  35.       r.draw();
  36.       // 4. Uncomment these to test
  37.       // Square s1 = new Square();
  38.       // s1.draw();
  39.       // Square s = new Square(3);
  40.       // s.draw();
  41.    }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement