考试分析:个人感觉本次考试难度偏简单,也无需注意些什么,只要仔细读题问题应该都不大;
1.创建一个圆形类(Circle),私有属性为圆的半径,从控制台输入圆的半径,输出圆的面积
输入格式:
输入圆的半径,取值范围为(0,+∞)
,输入数据非法,则程序输出Wrong Format
,注意:只考虑从控制台输入数值的情况
输出格式:
输出圆的面积(保留两位小数,可以使用String.format(“%.2f”,输出数值)控制精度)
import java.util.Scanner; public class Main { private double radius; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double radius = scanner.nextDouble(); Main circle = new Main(); circle.setRadius(radius); double area = circle.calculateArea(); if (area == -1) { System.out.println("Wrong Format"); } else { System.out.println(String.format("%.2f", area)); } } public void setRadius(double radius) { if (radius > 0) { this.radius = radius; } else { this.radius = -1; } } public double calculateArea() { if (radius == -1) { return -1; } else { return Math.PI * radius * radius; } } }
2.设计一个矩形类,其属性由矩形左上角坐标点(x1,y1)及右下角坐标点(x2,y2)组成,其中,坐标点属性包括该坐标点的X轴及Y轴的坐标值(实型数),求得该矩形的面积。类设计如下图:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); double area = calculateRectangleArea(x1, y1, x2, y2); System.out.println( String.format("%.2f", area)); } public static double calculateRectangleArea(double x1, double y1, double x2, double y2) { double width = Math.abs(x2 - x1); double height = Math.abs(y2 - y1); return width * height; } }
3.
将测验1与测验2的类设计进行合并设计,抽象出Shape父类(抽象类),Circle及Rectangle作为子类,类图如下所示:
其中,printArea(Shape shape)
方法为定义在Main类中的静态方法,体现程序设计的多态性。
输入格式:
输入类型选择(1或2,不考虑无效输入)
对应图形的参数(圆或矩形)
输出格式:
图形的面积(保留两位小数)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int shapeType = scanner.nextInt(); double area = 0.0; if (shapeType == 1) { double radius = scanner.nextDouble(); if(radius<=0){ System.out.println("Wrong Format"); return; } area = calculateCircleArea(radius); } else if (shapeType == 2) { double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); double length = Math.abs(x1-x2); double width = Math.abs(y1-y2); area = calculateRectangleArea(length, width); } else { System.out.println("Wrong Format"); return; } System.out.println(String.format("%.2f", area)); } public static double calculateCircleArea(double radius) { return Math.PI * radius * radius; } public static double calculateRectangleArea(double length, double width) { return length * width; } }
4.
在测验3的题目基础上,重构类设计,实现列表内图形的排序功能(按照图形的面积进行排序)。
输入图形类型(1:圆形;2:矩形;0:结束输入)
输入图形所需参数
输出格式:
按升序排序输出列表中各图形的面积(保留两位小数),各图形面积之间用空格分隔。、
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List<Double> areas = new ArrayList<>(); int shapeType; do { shapeType = scanner.nextInt(); if (shapeType == 1) { double radius = scanner.nextDouble(); if(radius<=0){ System.out.println("Wrong Format"); return; } double area = calculateCircleArea(radius); areas.add(area); } else if (shapeType == 2) { double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); double length = Math.abs(x1-x2); double width = Math.abs(y1-y2); double area = calculateRectangleArea(length, width); areas.add(area); } else if (shapeType != 0) { System.out.println("Wrong Format"); } } while (shapeType != 0); Collections.sort(areas); for (double area : areas) { System.out.print(String.format("%.2f", area) + " "); } } public static double calculateCircleArea(double radius) { return Math.PI * radius * radius; } public static double calculateRectangleArea(double length, double width) { return length * width; } }
标签:util,scanner,double,BLOG,radius,public,Scanner From: https://www.cnblogs.com/wang365129291/p/17516832.html