Define a rectangle class Rectangle: (knowledge point: creation and use of objects)
1 Define three methods: getArea () for area, getPer () for perimeter, showAll () output length, width, area, perimeter in the console respectively long.
2 There are 2 attributes: long length, wide width
3 Create a Rectangle object, and output related information
1 package dfishf; 2 3 public class homework { 4 int length; 5 int width; 6 7 public void getArea() { 8 System.out.println(length * width); 9 } 10 11 public void getPer() { 12 System.out.println((length + width) * 2); 13 } 14 15 public void showAll() { 16 System.out.println("长是" + length); 17 System.out.println ("Width is" + width); 18 System.out.println ("Area is" ); 19 getArea (); 20 System.out.println ("Perimeter is" ); 21 getPer ( ); 22 } 23 }
1 package dfishf; 2 3 public class homework { 4 5 public static void main(String[] args) { 6 7 Rectangle r1=new Rectangle(); 8 r1.width=10; 9 r1.length=50; 10 r1.getArea(); 11 r1.getPer(); 12 r1.showAll(); 13 } 14 }