- 编程创建一个矩形类,在其中定义两个变量表示一个矩形的长、宽,再定义一个方法setDemo对这两个个变量进行初始化,然后定义两个个方法求矩形得周长和面积。创建一个对象,求给定尺寸的矩形的体积。
import java.util.Scanner;
public class Rectangle {
private int length;
private int width;
private int height;
public void setDemo(int length, int width,int height){
this.length = length;
this.width = width;
this.height = height;
}
public int area(){
return length * width;
}
public int perimeter(){
return 2 * ( length + width );
}
public int volume(){
return length * width * height;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入长:");
int length = scanner.nextInt();
System.out.println("请输入宽:");
int width = scanner.nextInt();
System.out.println("请输入高:");
int heigth = scanner.nextInt();
Rectangle rectangle = new Rectangle();
rectangle.setDemo(length,width,heigth);
System.out.println("周长为:"+rectangle.perimeter());
System.out.println("面积为:"+rectangle.area());
System.out.println("体积为:"+rectangle.volume());
}
}
- 将上题的方法setDemo改用构造函数实现初始化。
package com.company;
import java.util.Scanner;
public class Rectangle1 {
private int length;
private int width;
private int height;
Rectangle1(int a,int b,int c){
length = a;
width = b;
height = c;
}
public int area(){
return length * width;
}
public int perimeter(){
return 2 * ( length + width );
}
public int volume(){
return length * width * height;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入长:");
int length = scanner.nextInt();
System.out.println("请输入宽:");
int width = scanner.nextInt();
System.out.println("请输入高:");
int heigth = scanner.nextInt();
Rectangle1 rectangle = new Rectangle1(length,width,heigth);
System.out.println("周长为:"+rectangle.perimeter());
System.out.println("面积为:"+rectangle.area());
System.out.println("体积为:"+rectangle.volume());
}
}
- 定义一个圆形类,做1、2中得事情。
package com.company;
import java.util.Scanner;
public class Round {
private double radius;
final static double PI=3.1415926;
public void setDemo(double radius){
this.radius = radius;
}
public double area(){
return PI * radius * radius;
}
public double perimeter(){
return 2 * PI * radius;
}
public double volume(){
return 4*PI*radius*radius*radius/3;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入半径:");
double radius = scanner.nextDouble();
Round round = new Round();
round.setDemo(radius);
System.out.println("周长为:"+round.perimeter());
System.out.println("面积为:"+round.area());
System.out.println("体积为:"+round.volume());
}
}
构造函数:
package com.company;
import java.util.Scanner;
public class Round1 {
private double radius;
final static double PI=3.1415926;
public Round1(double a){
radius = a;
}
public double area(){
return PI * radius * radius;
}
public double perimeter(){
return 2 * PI * radius;
}
public double volume(){
return 4*PI*radius*radius*radius/3;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入半径:");
double radius = scanner.nextDouble();
Round1 round = new Round1(radius);
System.out.println("周长为:"+round.perimeter());
System.out.println("面积为:"+round.area());
System.out.println("体积为:"+round.volume());
}
}