南昌航空大学 软件学院 pta Java 期中考试 蔡珂
两节课的期中考试啊
题目难度不高
基本就是按图说话
如果不错太多bug的话时间应该很充足的
很开心的是我没出啥bug
除了第一题之外都是一遍过的
题目列表
7-1 点与线(类设计)
设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format
设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。
** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**
以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭
输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。
输出格式:
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
代码如下
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double []num =new double[5];
for(int i=1;i<=4;i++){
num[i] = input.nextDouble();
if(num[i]>200||num[i]<=0){
System.out.printf("Wrong Format\n");
return;
}
}
String color = input.next();
Point point1 = new Point(num[1],num[2]),point2 = new Point(num[3],num[4]);
Line line = new Line(point1,point2,color);
line.display();
}
}
class Point {
private double x,y;
public Point(){
}
public Point(double x, double y){
setX(x);setY(y);
}
void setX(double x){
this.x =x;
}
void setY(double y){
this.y = y;
}
double getX(){
return this.x;
}
double getY(){
return this.y;
}
void display(){
double numx,numy;
String str1 = String.format("%.3f",getX()),str2 = String.format("%.3f",getY());
numx = Double.parseDouble(str1);numy = Double.parseDouble(str2);
System.out.printf("(%.2f,%.2f)",numx,numy);
}
}
class Line {
Point point1,point2;
String color;
public Line(){}
public Line(Point point1, Point point2,String color){
setColor(color);setPoint1(point1) ;setPoint2(point2);
}
Point getPoint1(){return point1;}
Point getPoint2(){return point2;}
String getColor(){return color;}
void setPoint1(Point point1){
this.point1 = new Point(point1.getX(),point1.getY());
}
void setPoint2(Point point2){
this.point2 = new Point(point2.getX(),point2.getY());
}
void setColor(String color){
this.color = color;
}
double getDistance(){
return Math.sqrt((point1.getX()- point2.getX())*(point1.getX()- point2.getX())+(point1.getY()- point2.getY())*(point1.getY()- point2.getY()));
}
void display(){
System.out.printf("The line's color is:" +getColor()+
"\nThe line's begin point's Coordinate is:\n" );
point1.display();
System.out.printf("\n"+"The line's end point's Coordinate is:\n" );
point2.display() ;
System.out.printf("\n"+"The line's length is:" );
double num;
String str1 = String.format("%.3f",getDistance());
num= Double.parseDouble(str1);
System.out.printf("%.2f",num);
}
}
7-2 点线面问题重构(继承与多态)
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
element = p1;//起点Point
element.display();
element = p2;//终点Point
element.display();
element = line;//线段
element.display();
element = plane;//面
element.display();
其中,所有数值均保留两位小数,建议可用String.format("%.2f", data)方法。
以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭
输入格式:
分别输入线段的起点横坐标、纵坐标、终点的横坐标、纵坐标以及颜色,中间可用一个或多个空格、tab或者回车分隔。
输出格式:
(x1,y1)
(x2,y2)
The line's color is:颜色值
The line's begin point's Coordinate is:
(x1,y1)
The line's end point's Coordinate is:
(x2,y2)
The line's length is:长度值
The Plane's color is:颜色值
代码如下
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double []num =new double[5];
for(int i=1;i<=4;i++){
num[i] = input.nextDouble();
if(num[i]>200||num[i]<=0){
System.out.printf("Wrong Format\n");
return;
}
}
String color = input.next();
Element point1 = new Point(num[1],num[2]),point2 = new Point(num[3],num[4]);
Element line = new Line(new Point(num[1],num[2]),new Point(num[3],num[4]),color);
Element plane = new Plane(color);
point1.display();
point2.display();
line.display();
plane.display();
}
}
class Point extends Element{
private double x,y;
public Point(){
}
public Point(double x, double y){
setX(x);setY(y);
}
void setX(double x){
this.x =x;
}
void setY(double y){
this.y = y;
}
double getX(){
return this.x;
}
double getY(){
return this.y;
}
void display(){
double numx,numy;
String str1 = String.format("%.3f",getX()),str2 = String.format("%.3f",getY());
numx = Double.parseDouble(str1);numy = Double.parseDouble(str2);
System.out.printf("(%.2f,%.2f)\n",numx,numy);
}
}
class Line extends Element{
Point point1,point2;
String color;
public Line(){}
public Line(Point point1, Point point2,String color){
setColor(color);setPoint1(point1) ;setPoint2(point2);
}
Point getPoint1(){return point1;}
Point getPoint2(){return point2;}
String getColor(){return color;}
void setPoint1(Point point1){
this.point1 = new Point(point1.getX(),point1.getY());
}
void setPoint2(Point point2){
this.point2 = new Point(point2.getX(),point2.getY());
}
void setColor(String color){
this.color = color;
}
double getDistance(){
return Math.sqrt((point1.getX()- point2.getX())*(point1.getX()- point2.getX())+(point1.getY()- point2.getY())*(point1.getY()- point2.getY()));
}
void display(){
System.out.printf("The line's color is:" +getColor()+
"\nThe line's begin point's Coordinate is:\n" );
point1.display();
System.out.printf("The line's end point's Coordinate is:\n" );
point2.display() ;
System.out.printf("The line's length is:" );
double num;
String str1 = String.format("%.3f",getDistance());
num= Double.parseDouble(str1);
System.out.printf("%.2f\n",num);
}
}
abstract class Element{
abstract void display();
}
class Plane extends Element{
private String color;
public Plane(){}
public Plane(String color){
setColor(color);
}
void display(){
System.out.printf("The Plane's color is:"+getColor()+"\n");
};
void setColor(String color){this.color = color;}
String getColor(){return color;}
}
7-3 点线面问题再重构(容器类)
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList<Element>类型的对象(若不了解泛型,可以不使用<Element>)
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
1:向容器中增加Point对象
2:向容器中增加Line对象
3:向容器中增加Plane对象
4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
0:输入结束
示例代码如下:
choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1://insert Point object into list
...
break;
case 2://insert Line object into list
...
break;
case 3://insert Plane object into list
...
break;
case 4://delete index - 1 object from list
int index = input.nextInt();
...
}
choice = input.nextInt();
}
输入结束后,按容器中的对象顺序分别调用每个对象的display()方法进行输出。
以下情况为无效作业
无法运行
设计不符合所给类图要求
未通过任何测试点测试
判定为抄袭
输入格式:
switch(choice) {
case 1://insert Point object into list
输入“点”对象的x,y值
break;
case 2://insert Line object into list
输入“线”对象两个端点的x,y值
break;
case 3://insert Plane object into list
输入“面”对象的颜色值
break;
case 4://delete index - 1 object from list
输入要删除的对象位置(从1开始)
...
}
输出格式:
Point、Line、Plane的输出参考题目2
删除对象时,若输入的index超出合法范围,程序自动忽略该操作
代码如下
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static double []num =new double[15];
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int choice = input.nextInt();
GeometryObject geometryObject = new GeometryObject();
String color;
while(choice != 0) {
switch(choice) {
case 1:
getNum(2);
Element point = new Point(num[1],num[2]);
geometryObject.add(point);
break;
case 2://insert Line object into list
getNum(4);
color = input.next();
Element line = new Line(new Point(num[1],num[2]),new Point(num[3],num[4] ),color);
geometryObject.add(line);
break;
case 3://insert Plane object into list
color = input.next();
Element plane = new Plane(color);
geometryObject.add(plane);
break;
case 4://delete index - 1 object from list
int index = input.nextInt();
geometryObject.remove(index);
break;
}
//System.out.printf(choice+"\n");
choice = input.nextInt();
}
geometryObject.display();
}
static void getNum(int x) {
for (int i = 1; i <= x; i++) {
num[i] = input.nextDouble();
}
}
}
class Point extends Element{
private double x,y;
public Point(){
}
public Point(double x, double y){
setX(x);setY(y);
}
void setX(double x){
this.x =x;
}
void setY(double y){
this.y = y;
}
double getX(){
return this.x;
}
double getY(){
return this.y;
}
void display(){
double numx,numy;
String str1 = String.format("%.3f",getX()),str2 = String.format("%.3f",getY());
numx = Double.parseDouble(str1);numy = Double.parseDouble(str2);
System.out.printf("(%.2f,%.2f)\n",numx,numy);
}
}
class Line extends Element{
Point point1,point2;
String color;
public Line(){}
public Line(Point point1, Point point2,String color){
setColor(color);setPoint1(point1) ;setPoint2(point2);
}
Point getPoint1(){return point1;}
Point getPoint2(){return point2;}
String getColor(){return color;}
void setPoint1(Point point1){
this.point1 = new Point(point1.getX(),point1.getY());
}
void setPoint2(Point point2){
this.point2 = new Point(point2.getX(),point2.getY());
}
void setColor(String color){
this.color = color;
}
double getDistance(){
return Math.sqrt((point1.getX()- point2.getX())*(point1.getX()- point2.getX())+(point1.getY()- point2.getY())*(point1.getY()- point2.getY()));
}
void display(){
System.out.printf("The line's color is:" +getColor()+
"\nThe line's begin point's Coordinate is:\n" );
point1.display();
System.out.printf("The line's end point's Coordinate is:\n" );
point2.display() ;
System.out.printf("The line's length is:" );
double num;
String str1 = String.format("%.3f",getDistance());
num= Double.parseDouble(str1);
System.out.printf("%.2f\n",num);
}
}
abstract class Element{
abstract void display();
}
class Plane extends Element{
private String color;
public Plane(){}
public Plane(String color){
setColor(color);
}
void display(){
System.out.printf("The Plane's color is:"+getColor()+"\n");
};
void setColor(String color){this.color = color;}
String getColor(){return color;}
}
class GeometryObject{
ArrayList<Element> elements = new ArrayList<>();
void add(Element element){
elements.add(element);
}
void remove(int x){
if(x>elements.size())
return;
elements.remove(x-1);
}
void display(){
for (Element ele:elements){
ele.display();
}
}
}