长方形 长:width, 宽:width;
圆的半径 r;
长方形周长方法:
public double zc(double width, double length) {
return 2 * (length + width);
}
长方形面积方法:
public double pre(double length, double width) {
return length * width;
}
圆的面积
public double area(double r) {
double s = Math.PI * r * r;
return s;
}
总的代码:
package wxt.zyy.demo;
import java.util.Scanner;
public class Rec {
private double length;
private double width;
public Rec(double length, double width) {
super();
this.length = length;
this.width = width;
}
public Rec() {
}
// 长方形周长方法
public double zc(double width, double length) {
return 2 * (length + width);
}
// 长方形面积方法
public double pre(double length, double width) {
return length * width;
}
// 圆的面积
public double area(double r) {
double s = Math.PI * r * r;
return s;
}
// 主函数
public static void main(String[] args) {
Rec rc = new Rec();
// 长方形的长
System.out.println("请输入长度:");
Scanner sc = new Scanner(System.in);
double length = sc.nextDouble();
// 长方形的宽
System.out.println("请输入宽度:");
Scanner sc1 = new Scanner(System.in);
double width = sc1.nextDouble();
System.out.println("长方形周长:" + rc.zc(width, length));
System.out.println("长方形面积:" + rc.pre(width, length));
System.out.println("=====================");
// 输入圆的半径
System.out.println("请输入圆的半径r:");
Scanner sc2 = new Scanner(System.in);
double r = sc2.nextDouble();
System.out.println("圆的面积:" + rc.area(r));
}
}
运行结果:
请输入长度:
12
请输入宽度:
32
长方形周长:88.0
长方形面积:384.0
=====================
请输入圆的半径r:
3
圆的面积:28.274333882308138