第七周作业
定义一个矩形类Rectangle:(知识点:对象的创建和使用)
1 定义三个方法:getArea()求面积、getPer()求周长,showAll()分别在控制台输出长、宽、面积、周长。
2 有2个属性:长length、宽width
3 创建一个Rectangle对象,并输出相关信息
package test; public class Rectangle { int width; int length; public double getArea(){ return this.width*this.length; } public double getPer(){ return 2*(this.width+this.length); } public void showAll(){ System.out.println("宽="+this.width); System.out.println("长="+this.length); System.out.println("面积="+this.getArea()); System.out.println("周长="+this.getPer()); }
}
package test; import java.util.Scanner; public class Now1 { public static void main(String[] args) { Scanner in=new Scanner(System.in); Rectangle r1=new Rectangle(); System.out.println("请输入长度: "); r1.length=in.nextInt(); System.out.println("请输入宽度: "); r1.width=in.nextInt(); r1.showAll(); } }


浙公网安备 33010602011771号