结课项目需求
运用所学的知识,设计计算图形面积和周长的系统,要求用到对象和类,构造方法,继承,抽象类(或接口)和多态。实现如下功能:
- 根据菜单提示选择要计算的图像类型。
- 输入矩形的长和宽,计算矩形的面积和周长。
- 输入圆的半径,计算圆形的面积和周长。(要求保留两位小数)
- 输入三角形的三边,计算三角形的面积和周长。
- 如果根据菜单提示输入有误,有相关提示并重新选择。
- 退出(非右上角x号退出)
框架实现
1.图形父类完成两个抽象方法面积和周长用于子类继承和重写(也可以用接口的方式实现),这里我加了两个shou方法,需要输出面积和周长时直接调用就可以了。
//父类图形
abstract class Shape{
abstract double Area();
abstract double Girth();
abstract void showArea();
abstract void showGirth();
}
2.子类继承并重写父类中的方法
????????????????????????????????????????????????????????????????矩形
//子类矩形
class Rectangle extends Shape{
double length;
double wide;
public Rectangle(double length, double wide) {
this.length = length;
this.wide = wide;
}
@Override
double Area() {
return length*wide;
}
@Override
double Girth() {
return 2*(length+wide);
}
@Override
void showArea() {
System.out.printf("The area of the rectangle:"+"%.2f", Area());
System.out.println();
}
@Override
void showGirth() {
System.out.printf("The girth of the rectangle:" +"%.2f", Girth());
System.out.println();
}
}
????????????????????????????????????????????????????????????????圆
//子类圆形
class Circle extends Shape{
double radius;
final double PI = 3.14;
public Circle(double radius) {
this.radius = radius;
}
@Override
double Area() {
return PI*radius*radius;
}
@Override
double Girth() {
return 2*PI*radius;
}
@Override
void showArea() {
System.out.printf("The girth of the circle:"+"%.2f", Area());
System.out.println();
}
@Override
void showGirth() {
System.out.printf("The girth of the circle:" +"%.2f", Girth());
System.out.println();
}
}
????????????????????????????????????????????????????????????????三角形
//子类三角形
class Triangle extends Shape{
double a,b,c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
double Girth() {
return a + b + c;
}
@Override
double Area() {
return Math.sqrt(this.Girth()*(this.Girth()-a)*(this.Girth()-b)*(this.Girth()-c));
}
@Override
void showArea() {
System.out.printf("The girth of the triangle:" +"%.2f", Area());
System.out.println();
}
@Override
void showGirth() {
System.out.printf("The girth of the triangle:" +"%.2f", Girth());
System.out.println();
}
}
还需要什么图形自行添加就可以。
下面是需要用到的相关方法
1.主菜单:
//主菜单
static void Menu(){
System.out.println("*********图形面积周长计算系统*********");
System.out.println("*********请挑选需要进行的操作*********");
System.out.println("*************1.计算矩形**************");
System.out.println("*************2.计算圆形**************");
System.out.println("*************3.计算三角形************");
System.out.println("*************4.退出******************");
}
2.退出方法,输入y代表确认退出,其他的代表否
static boolean exit(String c,boolean condition){
if ("y".equalsIgnoreCase(c)){//判断是否为y,equalsIgnoreCase不区分大小写
condition = false;
System.out.println("退出成功!");
}
else
System.out.println("继续操作!");
return condition;
}
3.清屏函数。
每一次走到Switch的最后都进行一次清屏,首先我想到的是和c一样用close(),但是发现java并没有close()这个方法,所以这里我并没有想到好的方法,只能用事件来代替人为的操作鼠标和键盘来实现清理控制台,但是这样有不好的地方,首先,当你清屏之后,控制台中什么也没有直接进行到下一次输入需要操作的选项,所以这里清屏之前需要给出一点友好的提示并再次确认;其次如果清屏快捷键不同也不能使用,我这里清理控制台的快捷键设置为Ctrl+R,如果你的清屏快捷键不是这个的话这个函数你将需要做一些修改。当然你也可以选择不用清屏函数,这个看个人需要。
//清屏函数,利用GUI来实现鼠标事件和键盘事件Ctrl+R
//我这里设置的清屏快捷键为Ctrl+R,如果快捷键为其他的该方法体内的内容不同
static void clear()throws AWTException{
Robot r = new Robot();
r.mousePress(InputEvent.BUTTON3_DOWN_MASK);//按下鼠标右键
r.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);//释放鼠标右键
r.keyPress(KeyEvent.VK_CONTROL);//按下ctrl键
r.keyPress(KeyEvent.VK_R);//按下R键
r.keyRelease(KeyEvent.VK_R);//释放R键
r.keyRelease(KeyEvent.VK_CONTROL);//释放ctrl键
}
4.判断输入格式是否正确
//判断输入的格式是否符合矩形
static boolean IsRectangle(double length,double wide){
return length > 0 && wide > 0;
}
//判断输入是否是圆
static boolean IsCircle(double radius){
return radius > 0;
}
//判断输入的三条边能否构成三角形
static boolean IsTriangle(double side[]){
return side[1] + side[0] > side[2] && side[2] - side[0] < side[1] && side[0] > 0 && side[1] > 0 && side[2] > 0;
}
5.是否确认清屏及友好提示
//是否要清屏
static void whether() throws AWTException {
System.out.println("是否清屏(Y),输入其他的代表否");
System.out.println("注意清屏之后控制台什么提示也没有,需要直接输入一次需要选择操作的序号");
Scanner in = new Scanner(System.in);
String s = in.next();
if ("y".equalsIgnoreCase(s))
clear();
}
6.循环体
//循环体
static void CirculatingBody() throws AWTException {
while (isFlag){
Scanner in = new Scanner(System.in);
Menu();
int n = in.nextInt();
switch (n){
case 1:
boolean condition1 = true;
while (condition1){
double length,wide;
do {
System.out.print("请输入矩形的长和宽(数字):");
length = in.nextDouble();
wide = in.nextDouble();
if (!IsRectangle(length,wide)){
System.out.println("输入数据有误,请从新输入");
}
}while (!IsRectangle(length,wide));//输入数据有误循环继续重新输入
Shape rectangle = new Rectangle(length,wide);//多态向下转型
rectangle.showArea();
rectangle.showGirth();
System.out.println("是否返回到上一级(Y),输入其他的代表否");
String s = in.next();
condition1 = exit(s,condition1);
}
break;
case 2:
boolean condition2 = true;
while (condition2){
double radius;
do {
System.out.println("请输入圆的半径(数字):");
radius = in.nextDouble();
if (!IsCircle(radius)){
System.out.println("输入有误,请重新输入!");
}
}while (!IsCircle(radius));//输入有误,重新输入,循环继续
Shape circle = new Circle(radius);//多态向下转型
circle.showArea();
circle.showGirth();
System.out.println("是否返回到上一级(Y),输入其他的代表否");
String s = in.next();
condition2 = exit(s,condition2);
}
break;
case 3:
boolean condition3 = true;
while (condition3){
double []side = new double[3];
do {
System.out.println("请输入三角形的三条边(数字)");
side[0] = in.nextDouble();
side[1] = in.nextDouble();
side[2] = in.nextDouble();
Arrays.sort(side);//将输入的三条边按从小到大排序
if (!IsTriangle(side)){
//三角形的条件是两边之和大于第三边,两边之差小于第三边
//边的长度肯定也不能为0
System.out.println("输入的三条边不能构成一个三角形,请重新输入");
}
}while (!IsTriangle(side));//不能构成三角形重新输入循环继续
Shape triangle = new Triangle(side[0],side[1],side[2]);//多态向下转型
triangle.showGirth();
triangle.showArea();
System.out.println("是否返回到上一级(Y),输入其他的代表否");
String s = in.next();
condition3 = exit(s,condition3);
}
break;
case 4:
System.out.println("是否确认退出(Y),输入其他的代表否");
String s = in.next();
isFlag = exit(s,isFlag);
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
whether();
}
}
}
下面附上完整代码
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Scanner;
public class ClosingProject {
static boolean isFlag = true;//控制循环退出
public static void main(String[] args) throws AWTException {//抛出AWT可能出现的异常,下面同理
CirculatingBody();//调用循环体
}
//主菜单
static void Menu(){
System.out.println("*********图形面积周长计算系统*********");
System.out.println("*********请挑选需要进行的操作*********");
System.out.println("*************1.计算矩形**************");
System.out.println("*************2.计算圆形**************");
System.out.println("*************3.计算三角形************");
System.out.println("*************4.退出******************");
}
//退出方法
static boolean exit(String c,boolean condition){
if ("y".equalsIgnoreCase(c)){//判断是否为y,equalsIgnoreCase不区分大小写
condition = false;
System.out.println("退出成功!");
}
else
System.out.println("继续操作!");
return condition;
}
//清屏函数,利用GUI来实现鼠标事件和键盘事件Ctrl+R
//我这里设置的清屏快捷键为Ctrl+R,如果快捷键为其他的该方法体内的内容不同
static void clear()throws AWTException{
Robot r = new Robot();
r.mousePress(InputEvent.BUTTON3_DOWN_MASK);//按下鼠标右键
r.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);//释放鼠标右键
r.keyPress(KeyEvent.VK_CONTROL);//按下ctrl键
r.keyPress(KeyEvent.VK_R);//按下R键
r.keyRelease(KeyEvent.VK_R);//释放R键
r.keyRelease(KeyEvent.VK_CONTROL);//释放ctrl键
}
//是否要清屏
static void whether() throws AWTException {
System.out.println("是否清屏(Y),输入其他的代表否");
System.out.println("注意清屏之后控制台什么提示也没有,需要直接输入一次需要选择操作的序号");
Scanner in = new Scanner(System.in);
String s = in.next();
if ("y".equalsIgnoreCase(s))
clear();
}
//判断输入的格式是否符合矩形
static boolean IsRectangle(double length,double wide){
return length > 0 && wide > 0;
}
//判断输入是否是圆
static boolean IsCircle(double radius){
return radius > 0;
}
//判断输入的三条边能否构成三角形
static boolean IsTriangle(double side[]){
return side[1] + side[0] > side[2] && side[2] - side[0] < side[1] && side[0] > 0 && side[1] > 0 && side[2] > 0;
}
//循环体
static void CirculatingBody() throws AWTException {
while (isFlag){
Scanner in = new Scanner(System.in);
Menu();
int n = in.nextInt();
switch (n){
case 1:
boolean condition1 = true;
while (condition1){
double length,wide;
do {
System.out.print("请输入矩形的长和宽(数字):");
length = in.nextDouble();
wide = in.nextDouble();
if (!IsRectangle(length,wide)){
System.out.println("输入数据有误,请从新输入");
}
}while (!IsRectangle(length,wide));//输入数据有误循环继续重新输入
Shape rectangle = new Rectangle(length,wide);//多态向下转型
rectangle.showArea();
rectangle.showGirth();
System.out.println("是否返回到上一级(Y),输入其他的代表否");
String s = in.next();
condition1 = exit(s,condition1);
}
break;
case 2:
boolean condition2 = true;
while (condition2){
double radius;
do {
System.out.println("请输入圆的半径(数字):");
radius = in.nextDouble();
if (!IsCircle(radius)){
System.out.println("输入有误,请重新输入!");
}
}while (!IsCircle(radius));//输入有误,重新输入,循环继续
Shape circle = new Circle(radius);//多态向下转型
circle.showArea();
circle.showGirth();
System.out.println("是否返回到上一级(Y),输入其他的代表否");
String s = in.next();
condition2 = exit(s,condition2);
}
break;
case 3:
boolean condition3 = true;
while (condition3){
double []side = new double[3];
do {
System.out.println("请输入三角形的三条边(数字)");
side[0] = in.nextDouble();
side[1] = in.nextDouble();
side[2] = in.nextDouble();
Arrays.sort(side);//将输入的三条边按从小到大排序
if (!IsTriangle(side)){
//三角形的条件是两边之和大于第三边,两边之差小于第三边
//边的长度肯定也不能为0
System.out.println("输入的三条边不能构成一个三角形,请重新输入");
}
}while (!IsTriangle(side));//不能构成三角形重新输入循环继续
Shape triangle = new Triangle(side[0],side[1],side[2]);//多态向下转型
triangle.showGirth();
triangle.showArea();
System.out.println("是否返回到上一级(Y),输入其他的代表否");
String s = in.next();
condition3 = exit(s,condition3);
}
break;
case 4:
System.out.println("是否确认退出(Y),输入其他的代表否");
String s = in.next();
isFlag = exit(s,isFlag);
break;
default:
System.out.println("输入有误,请重新输入");
break;
}
whether();
}
}
}
//父类图形
abstract class Shape{
abstract double Area();
abstract double Girth();
abstract void showArea();
abstract void showGirth();
}
//子类矩形
class Rectangle extends Shape{
double length;
double wide;
public Rectangle(double length, double wide) {
this.length = length;
this.wide = wide;
}
@Override
double Area() {
return length*wide;
}
@Override
double Girth() {
return 2*(length+wide);
}
@Override
void showArea() {
System.out.printf("The area of the rectangle:"+"%.2f", Area());
System.out.println();
}
@Override
void showGirth() {
System.out.printf("The girth of the rectangle:" +"%.2f", Girth());
System.out.println();
}
}
//子类圆形
class Circle extends Shape{
double radius;
final double PI = 3.14;
public Circle(double radius) {
this.radius = radius;
}
@Override
double Area() {
return PI*radius*radius;
}
@Override
double Girth() {
return 2*PI*radius;
}
@Override
void showArea() {
System.out.printf("The girth of the circle:"+"%.2f", Area());
System.out.println();
}
@Override
void showGirth() {
System.out.printf("The girth of the circle:" +"%.2f", Girth());
System.out.println();
}
}
//子类三角形
class Triangle extends Shape{
double a,b,c;
public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
double Girth() {
return a + b + c;
}
@Override
double Area() {
return Math.sqrt(this.Girth()*(this.Girth()-a)*(this.Girth()-b)*(this.Girth()-c));
}
@Override
void showArea() {
System.out.printf("The girth of the triangle:" +"%.2f", Area());
System.out.println();
}
@Override
void showGirth() {
System.out.printf("The girth of the triangle:" +"%.2f", Girth());
System.out.println();
}
}
|