/*定义一个矩形类Rectangle:(知识点:对象的创建和使用) 1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。 2 有2个属性:长length、宽width 3 创建一个Rectangle对象,并输出相关信息*/ package homework5; public class Rectangle { double length; //定义长 double width; //定义宽 public void getArea() { //定义getArea方法 double area = length * width; //定义面积 } public void getPer() { //定义getPer方法 double per = (length + width) * 2;//定义周长 } public void showAll() { //定义showAll方法 double area = length * width; double per = (length + width) * 2; System.out.print ("length=" + length + " "); System.out.println("width=" + width + " "); System.out.println("area=" + area + " "); System.out.println("per=" + per); } }
package homework5; import java.util.Scanner; public class test { public static void main(String[] args) { // TODO Auto-generated method stub Rectangle R = new Rectangle(); Scanner input = new Scanner(System. in); System.out.print("input length="); R.length = input.nextInt(); System.out.print("input width="); R.width = input.nextInt(); R.showAll(); } }