SCAU 计算钢板的利用率
作業(yè)4:計算鋼板的利用率
題目類別: B作業(yè)
關(guān)鍵字: 接口 類 繼承 實現(xiàn) 多態(tài)
內(nèi)容要求:
某工廠需要在矩形的鋼板上截取不同形狀來制作零件。截取形狀過程中需要記錄形狀類型及可以計算形狀面積的必要數(shù)據(jù)。
首先記錄鋼板的大小:寬度和高度
目前的形狀有:
矩形,類型用字符A表示,記錄數(shù)據(jù)為寬度和高度
圓形,類型用字符B表示,記錄數(shù)據(jù)為半徑
梯形,類型用字符C表示,記錄數(shù)據(jù)為上底寬、下底寬和高度
(1) 從鍵盤讀取數(shù)據(jù),鍵盤輸入數(shù)據(jù)格式為:
第一行是兩個實數(shù),表示鋼板的寬度和高度;
以后每一行代表一個形狀的數(shù)據(jù),包括類型及計算面積的數(shù)據(jù)
例如:
2000 1000
A 20 30
B 30
B 45.5
C 10 30.5 15
A 10 40
(2) 根據(jù)輸入的數(shù)據(jù)計算并輸出該鋼板的利用率。
(3) 采用面向抽象的編程方式,基于抽象類或接口進行編程。
(4) 程序具有較好的擴展性,例如增加一種形狀三角形(類型符為D,記錄三角形的三條邊長)時,計算利用率的代碼不會被修改。
總結(jié):
1.這是用了一個Calculation接口
2.這里換行結(jié)束我查了很久的一種辦法就是:
Main
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) //package com.company;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner;public class Main {public Main() {}public static void main(String[] args) throws IOException {System.out.println("請分別輸入鋼板的長和寬");Scanner cin = new Scanner(System.in);BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));SteelPlate s = new SteelPlate(cin.nextDouble(), cin.nextDouble());double using = 0.0D;while(true) {System.out.println("請輸入A/B/C (輸入回車即結(jié)束):");String kinds = "";kinds = bf.readLine();if (kinds.length() == 0) {System.out.println("利用率為:" + using / s.getArea() * 100.0D + "%");return;}if (kinds.charAt(0) == 'A') {System.out.println("請分別輸入矩形的寬和高:");Rectangle r = new Rectangle(cin.nextDouble(), cin.nextDouble());using += r.getArea();} else if (kinds.charAt(0) == 'B') {System.out.println("請輸入圓形的半徑:");Circle c = new Circle(cin.nextDouble());using += c.getArea();} else if (kinds.charAt(0) == 'C') {System.out.println("請輸入梯形的上邊和下邊和高:");Trapezoid t = new Trapezoid(cin.nextDouble(), cin.nextDouble(), cin.nextDouble());using += t.getArea();}}} }Calculation接口
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) //package com.company;public interface Calculation {double PI = 3.14D;double getArea();double getUtilizationRate(double var1); }Circle
package com.company;public class Circle implements Calculation{private double radius;public Circle(double radius) {this.radius = radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;}@Overridepublic double getArea(){return radius*PI*radius;}@Overridepublic double getUtilizationRate(double steelArea) {return getArea()/steelArea;}}Rectangle
package com.company;public class Rectangle implements Calculation{private double width;public Rectangle(double width, double height) {this.width = width;this.height = height;}public double getWidth() {return width;}public void setWidth(double width) {this.width = width;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public double height;@Overridepublic double getArea(){return width*height;}@Overridepublic double getUtilizationRate(double steelArea) {return getArea()/steelArea;}} package com.company;public class Trapezoid implements Calculation{private double top;private double bottom;public double getTop() {return top;}public void setTop(double top) {this.top = top;}public double getBottom() {return bottom;}public void setBottom(double bottom) {this.bottom = bottom;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public Trapezoid(double top, double bottom, double height) {this.top = top;this.bottom = bottom;this.height = height;}@Overridepublic double getArea(){return (top+bottom)*height/2;}@Overridepublic double getUtilizationRate(double steelArea) {return getArea()/steelArea;}public double height; } package com.company;public class SteelPlate {private double width;private double height;public double getWidth() {return width;}public SteelPlate(double width, double height) {this.width = width;this.height = height;}public void setWidth(double width) {this.width = width;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public double getArea(){return this.width*this.height;} }總結(jié)
以上是生活随笔為你收集整理的SCAU 计算钢板的利用率的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 巽风游戏攻略苹果手机免越狱群控
- 下一篇: 思科设备中DHCP 服务的配置