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

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

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


版权声明:本文为weixin_35651612原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。