Java语言程序设计--实验:类的设计与对象使用

实验目的:

通过编写Java应用程序,熟悉JDK开发环境和开发过程,能够使用JDK命令对Java程序进行编译、调试和运行;掌握应用程序的典型结构,熟悉运算符、表达式、基本数据类型和基本控制语句的使用;熟悉类的定义、对象的创建、对象的属性的引用和方法的调用。

实验内容:

  1. 问题描述:编写一个java程序计算三角形、矩形和圆形的周长、面积,可以从键盘输入三角形的三条边长或者矩形的长和宽的长度或者圆的半径,并可以输出其对应三角形、矩形和圆形的周长、面积并输出其形状。
  2. 基本要求:声明一个父类,包括两个方法,分别用于计算形状的周长和面积;定义三个子类来分别描述三角形、矩形和圆形,包括:

(1)所需的属性,及其setter和getter方法

(2)覆盖父类的求周长、面积方法

(3)覆盖toString方法,可返回描述对应形状的字符串,包括周长和面积的值

(4)设计方法,用于比较两个形状的周长,结果可以是大于、等于和小于

(5)设计方法,用于比较两个形状的面积,结果可以是大于、等于和小于

代码实现:

package jimujiami;//包名随便起一个名字
import java.util.Scanner;

//父类 Shape
abstract class Shape {
 abstract double calculateArea();//计算面积
 abstract double calculatePerimeter();//计算周长
 abstract String ReturnShape();//返回形状
}

//子类 Triangle
class Triangle extends Shape {
 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;
 }

 
 double calculatePerimeter() {
     return a + b + c;
 }

 double calculateArea() {
     double p = (a + b + c) / 2;
     return Math.sqrt(p * (p - a) * (p - b) * (p - c));//海伦公式求三角形面积
 }
 
 public String toString() {
   return "Triangle: perimeter = " + calculatePerimeter() + ", area ="+calculateArea();//覆盖toString方法,可返回描述对应形状的字符串,包括周长和面积的值
 }

 String ReturnShape()
 {
	 return "Triangle";
 }
}

//子类 Rectangle
class Rectangle extends Shape {
 private double length;
 private double width;

 public Rectangle(double length, double width) {
     this.length = length;
     this.width = width;
 }

 
 double calculatePerimeter() {
     return 2 * (length + width);
 }

 
 double calculateArea() {
     return length * width;
 }

 
 String ReturnShape()
 {
	 return "Rectangle";
 }
 public String toString() {
     return "Rectangle: perimeter = " + calculatePerimeter() + ", area = " + calculateArea();
 }
}

//子类 Circle
class Circle extends Shape {
 private double radius;

 public Circle(double radius) {
     this.radius = radius;
 }

 double calculatePerimeter() {
     return 2 * Math.PI * radius;
 }

 
 double calculateArea() {
     return Math.PI * radius * radius;
 }

 String ReturnShape()
 {
	 return "Circle";
 }
 public String toString() {
     return "Circle: perimeter = " + calculatePerimeter() + ", area = " + calculateArea();
 }
}


public class Main {
 public static void main(String[] args) {
     Scanner scanner = new Scanner(System.in);
     // 输入三角形的三条边长
     System.out.println("请输入三角形三条边长:");
     double side1 = scanner.nextDouble();
     double side2 = scanner.nextDouble();
     double side3 = scanner.nextDouble();
     Triangle triangle = new Triangle(side1, side2, side3);
     System.out.println(triangle);

     // 输入矩形的长和宽
     System.out.println("请输入矩形的长和宽:");
     double length = scanner.nextDouble();
     double width = scanner.nextDouble();
     Rectangle rectangle = new Rectangle(length, width);
     System.out.println(rectangle);

     // 输入圆形的半径
     System.out.println("请输入圆的半径:");
     double radius = scanner.nextDouble();
     Circle circle = new Circle(radius);
     System.out.println(circle);

     // 比较周长和面积
     comparePerimeter(triangle, rectangle);
     comparePerimeter(triangle,circle);
     comparePerimeter(rectangle,circle);
     compareArea(triangle, rectangle);
     compareArea(triangle,circle);
     compareArea(rectangle,circle);
 }
// 比较周长
 public static void comparePerimeter(Shape s1, Shape s2) {
     double perimeter1 = s1.calculatePerimeter();
     double perimeter2 = s2.calculatePerimeter();
     if (perimeter1 > perimeter2) {
         System.out.println("The perimeter of "+s1.ReturnShape() +" is greater than "+s2.ReturnShape());
     } else if (perimeter1 < perimeter2) {
         System.out.println("The perimeter of "+s1.ReturnShape()+" is less than "+s2.ReturnShape());
     } else {
         System.out.println("The perimeter of "+s1.ReturnShape()+" is euqal to "+s2.ReturnShape());
     }
 }

 // 比较面积
 public static void compareArea(Shape s1, Shape s2) {
     double area1 = s1.calculateArea();
     double area2 = s2.calculateArea();
     if (area1 > area2) {
         System.out.println("The area of "+s1.ReturnShape()+" is greater than "+s2.ReturnShape());
     } else if (area1 < area2) {
         System.out.println("The area of "+s1.ReturnShape()+" is less than "+s2.ReturnShape());
     } else {
         System.out.println("The area of "+s1.ReturnShape()+" is equal to "+s2.ReturnShape());
     }
 }
}

实验结果:

武汉理工大学计算机专业 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值