第1关 编写一个代表三角形的类
package step1;
import java.util.Scanner;
public class Triangle {
// 自行设计类的实现
/********** Begin **********/
private double a;
private double b;
private double c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public double getArea() {
double p = (a + b + c) / 2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
public double getPerimeter() {
return a + b + c;
}
//@Override
public String toString() {
return String.format("三角形(%.1f,%.1f,%.1f)", a, b, c);
}
/********** End **********/
public static void main(String[] args) {
/********** Begin **********/
Scanner sc = new Scanner(System.in);
double a,b,c; //定义三条边
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
Triangle t1 = new Triangle(a, b, c);
//输出面积
System.out.println(t1 + "的面积为:" + String.format("%.2f", t1.getArea()));
//输出周长
System.out.println(t1 + "的周长为:" + String.format("%.2f", t1.getPerimeter()));
/********** End **********/
}
}
第2关 编写一个圆环类
package step2;
public class Ring {
/********** Begin **********/
//自行设计类的实现
private double innerRadius; //内半径
private double outerRadius; //外半径
private String color; //颜色
//构造方法
public Ring(double innerRadius, double outerRadius, String color){
this.innerRadius = innerRadius;
this.outerRadius = outerRadius;
this.color = color;
}
//计算圆环面积
public double area() {
return Math.PI * (outerRadius * outerRadius - innerRadius * innerRadius);
}
//计算圆环外圆周长
public double perimeter() {
return 2 * Math.PI * outerRadius;
}
//计算圆环内圆周长
public double innerPerimeter() {
return 2 * Math.PI * innerRadius;
}
//getter和setter方法
public double getInnerRadius() {
return innerRadius;
}
public void setInnerRadius(double innerRadius) {
this.innerRadius = innerRadius;
}
public double getOuterRadius() {
return outerRadius;
}
public void setOuterRadius(double outerRadius) {
this.outerRadius = outerRadius;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
/********** End **********/
public static void main(String[] args) {
Ring ring = new Ring(5, 8, "red");
System.out.printf("圆环的内半径: %.1f\n", ring.getInnerRadius());
System.out.printf("圆环的外半径: %.1f\n", ring.getOuterRadius());
System.out.println("圆环的颜色: " + ring.getColor());
System.out.printf("圆环的面积: %.2f\n", ring.area());
System.out.printf("圆环的外圆周长: %.2f\n", ring.perimeter());
System.out.printf("圆环的内圆周长: %.2f\n", ring.innerPerimeter());
System.out.println();
ring.setInnerRadius(4);
ring.setOuterRadius(6);
ring.setColor("blue");
System.out.printf("圆环的内半径: %.1f\n", ring.getInnerRadius());
System.out.printf("圆环的外半径: %.1f\n", ring.getOuterRadius());
System.out.println("圆环的颜色: " + ring.getColor());
System.out.printf("圆环的面积: %.2f\n", ring.area());
System.out.printf("圆环的外圆周长: %.2f\n", ring.perimeter());
System.out.printf("圆环的内圆周长: %.2f\n", ring.innerPerimeter());
}
}
第3关 编写一个学生类
package step3;
public class Student {
/********** Begin **********/
// 自行设计类的实现
// 成员变量
private int id; // 学号
private int age; // 年龄
private String name; // 姓名
// 构造方法
public Student(int id, int age, String name) {
this.id = id;
this.age = age;
this.name = name;
}
// getter方法
public int getId() {
return id;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
// toString方法,返回学生的字符串表示
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age;
}
/********** End **********/
public static void main(String args[]) {
Student s1 = new Student(1, 18, "小明");
Student s2 = new Student(2, 20, "小张");
Student s3 = new Student(3, 19, "小李");
Student s4 = new Student(4, 18, "小王");
Student s5 = new Student(5, 20, "小赵");
Student s[] = { s1, s2, s3, s4, s5 }; // 给对象数组赋值
System.out.println("班级学生名单如下:");
output(s); // 第1次调用output方法输出数组
/********** Begin **********/
// 将所有学生年龄加 1
for (int i = 0; i < s.length; i++) {
s[i].age++;
}
/********** End **********/
System.out.println("所有学生年龄加 1 后...");
output(s); /// 第2次调用output方法输出数组
int count = 0;
/********** Begin **********/
//统计大于20岁的学生个数
for (int i = 0; i < s.length; i++) {
if (s[i].age > 20) {
count++;
}
}
/********** End **********/
System.out.println("大于 20 岁人数是:" + count);
}
/* 以下方法输岀学生数组的所有元素 */
private static void output(Student s[]) {
for (int i = 0; i < s.length; i++)
System.out.println(s[i]);
}
}