java计算前n项积_java类求N个图形的面积和

展开全部

楼主,程序如下:

测试62616964757a686964616fe4b893e5b19e31333332636430类:

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class ShapeSystem {

static List sList = new ArrayList();

static float square = 0;

public static void main(String[] args) {

while(true)

{

show();

}

}

public static void show()

{

print("***图形类对象的管理***", 1);

print("-------------------", 1);

print("1.创建一个圆", 1);

print("2.创建一个矩形", 1);

print("3.创建一个长方体", 1);

print("4.创建一个圆柱体", 1);

print("5.显示已经创建的图形", 1);

print("6.求各图形的面积之和", 1);

print("7.退出系统", 1);

print("请选择操作项:", 2);

Scanner scanner = new Scanner(System.in);

int action = 0;

try {

action = scanner.nextInt();

} catch (RuntimeException e) {

print("输入必须是整数!", 1);

}

float pi = 3.14f;

String name = "";

float x;

float y;

float h;

float r;

switch (action) {

case 1:

print("请输入圆的名称:", 2);

name = scanner.next();

print("请输入圆心x坐标:", 2);

x = scanner.nextInt();

print("请输入圆心y坐标:", 2);

y = scanner.nextInt();

print("请输入圆的半径:", 2);

r = scanner.nextFloat();

Circle c = new Circle(name,x,y,r);

sList.add(c);

square += pi*r*r;

break;

case 2:

print("请输入矩形的名称:", 2);

name = scanner.next();

print("请输入矩形的长:", 2);

x = scanner.nextInt();

print("请输入矩形的宽:", 2);

y = scanner.nextInt();

Rectangle ra = new Rectangle(name,x,y);

sList.add(ra);

square += x*y;

break;

case 3:

print("请输入长方体的名称:", 2);

name = scanner.next();

print("请输入长方体的长:", 2);

x = scanner.nextInt();

print("请输入长方体的宽:", 2);

y = scanner.nextInt();

print("请输入长方体的高:", 2);

h = scanner.nextInt();

Cube cb = new Cube(name,x,y,h);

sList.add(cb);

square += 2*x*y + 2*x*h +2*h*y;

break;

case 4:

print("请输入圆柱体的名称:", 2);

name = scanner.next();

print("请输入圆柱体的底圆心x坐标:", 2);

x = scanner.nextInt();

print("请输入圆柱体的底圆心y坐标:", 2);

y = scanner.nextInt();

print("请输入圆柱体的底圆的半径:", 2);

r = scanner.nextFloat();

print("请输入圆柱体的高:", 2);

h = scanner.nextFloat();

Clinder ccd = new Clinder(name,x,y,r,h);

sList.add(ccd);

square += 2*pi*r*h;

break;

case 5:

for(Shape s : sList)

{

print(s.getName(), 1);

}

break;

case 6:

print("各图形的面积和为:"+square, 1);

break;

case 7:

print("系统退出...", 1);

System.exit(0);

break;

default:

print("无效的操作项...", 1);

break;

}

}

public static void print(String info,int flag)

{

if(flag == 1)

{

System.out.println(info);

}

else

{

System.out.print(info);

}

}

}

=========================华丽的分割线=============================

图形Shape类:

public abstract class Shape {

private String name;

public Shape(String name)

{

this.name = name;

}

public abstract void show();

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

=========================华丽的分割线=============================

矩形类:

public class Rectangle extends Shape {

private float x;

private float y;

public Rectangle(String name,float x,float y) {

super(name);

this.x = x;

this.y = y;

}

@Override

public void show() {

System.out.println(this.getName());

}

public String toString()

{

return this.getName() +  ":长=" + this.x + " 宽=" + this.y;

}

public float getX() {

return x;

}

public void setX(float x) {

this.x = x;

}

public float getY() {

return y;

}

public void setY(float y) {

this.y = y;

}

}

}

=========================华丽的分割线=============================

长方体为:

public class Cube extends Rectangle {

private float h;

public Cube(String name,float x,float y,float h) {

super(name,x,y);

this.h = h;

}

@Override

public void show() {

System.out.println(this.getName());

}

public String toString()

{

return this.getName() +  ":长=" + this.getX() + " 宽=" + this.getY() + " 高=" + this.h;

}

}

=========================华丽的分割线=============================

圆:

public class Circle extends Shape {

private float x;

private float y;

private float r;

public Circle(String name, float x, float y, float r) {

super(name);

this.x = x;

this.y = y;

this.r = r;

}

@Override

public void show() {

System.out.println(this.getName());

}

public float getX() {

return x;

}

public void setX(float x) {

this.x = x;

}

public float getY() {

return y;

}

public void setY(float y) {

this.y = y;

}

public float getR() {

return r;

}

public void setR(float r) {

this.r = r;

}

public String toString()

{

return this.getName() +  ":圆心x=" + this.x + " y=" + this.y + " 半径=" + this.r;

}

}

=========================华丽的分割线=============================

圆柱体:

public class Clinder extends Circle {

private float h;

public Clinder(String name, float x, float y, float r,float h) {

super(name, x, y, r);

this.h = h;

}

@Override

public void show() {

System.out.println(this.getName());

}

public String toString()

{

return this.getName() +  ":圆心x=" + this.getX() + " y=" + this.getY() + " 半径=" + this.getR() + " 高=" + this.h;

}

}

程序运行效果图:

3e0a5a2f6a268b2dabb570d9b4bc7b73.png

具体请自行运行程序操作,细节部分需自己完善。

有问题欢迎提问,满意请采纳!有分就加点,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: Java代码如下: ``` // 定义TIwDShape类 class TIwDShape { double width; // 宽度 double height; // 高度 String name; // 名称 // 构造方法 TIwDShape() { width = 0; height = 0; name = ""; } // 构造方法 TIwDShape(double w, double h, String n) { width = w; height = h; name = n; } // 计算面积方法 double calculateArea() { System.out.println("必须指定图形的类型才能计算面积。"); return 0; } } // 定义Circle类,继承TIwDShape类 class Circle extends TIwDShape { // 构造方法 Circle(double r, String n) { super(r, r, n); // 调用父类构造方法初始化宽度、高度和名称 } // 计算面积方法 double calculateArea() { return Math.PI * width * height; } } ``` 解析: 1. 定义TIwDShape类,包括属性width(宽度)、height(高度)和name(名称),以及构造方法和成员方法。 2. 定义子类Circle,继承TIwDShape类,包括一个计算面积方法和一个使用super初始化部分的构造方法。 3. 在Circle类中的构造方法中,使用super关键字调用父类构造方法初始化width、height和name属性。 4. 在Circle类中的计算面积方法中,使用Math.PI求出圆的面积,并返回结果。 使用示例: 下面是一个使用示例,用于创建一个圆对象并计算面积: ``` public static void main(String[] args) { Circle circle = new Circle(3, "圆形"); double area = circle.calculateArea(); System.out.println(circle.name + "的面积为:" + area); } ``` 输出结果: ``` 圆形的面积为:28.274333882308138 ``` 说明: 1. 在main方法中,创建一个Circle对象,指定半径为3、名称为"圆形"。 2. 调用Circle对象的calculateArea方法,计算圆的面积,并将结果保存在变量area中。 3. 输出圆形的名称和面积

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值