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
Definition method:
package rectangle; public class Rectangle { double length, width, area, per; // Calculate area method public void getArea () { area = length * width; } // Calculate perimeter method public void getPer () { per = 2 * (length + width); } // Output rectangle length, width, height, area, and perimeter. public void showAll () { System.out.println ( "Length:" + length + "Width is" + width + "Area" + area + "Perimeter" + per); } }
Call method:
package rectangle; import java.util. * ; public class gogogo { public static void main (String [] args) { // TODO Auto-generated method stub Rectangle p = new Rectangle (); Scanner input = new Scanner (System.in ); System.out.println ( "Please enter the length and width of the rectangle" ); System.out.print ( "The length of the rectangle is \ n" ); p.length = input.nextDouble (); System.out.println ( "The width of the rectangle is" ); p.width = input.nextDouble(); p.getArea(); p.getPer(); p.showAll(); } }