首页 > 编程语言 >南昌航空大学 软件学院 pta Java 期中考试 蔡珂

南昌航空大学 软件学院 pta Java 期中考试 蔡珂

时间:2022-10-29 19:46:40浏览次数:49  
标签:case index 蔡珂 Java Point pta new line display

两节课的期中考试啊
题目难度不高
基本就是按图说话
如果不错太多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();
        }
    }
}

标签:case,index,蔡珂,Java,Point,pta,new,line,display
From: https://www.cnblogs.com/dapiQAQ/p/16839460.html

相关文章

  • 从新开始学JAVA - 标识符5
    标识符Java的所有组成部分都需要名字,类名、变量名、方法名等统称为标识符。所有的标识符的首个字符必须是字母(A-Z或者a-z)或者美元符号($)或者下划线(_)首字符后,必须是字母(A......
  • Eclipse 给 Java 应用创建 Run configuration 时找不到 main type 的错误消息
    我在Eclipse里选中com文件夹下选中一个.java文件,这个Java文件实现了​​publicstaticvoidmain(String[]args)​​方法,我想作为一个Java应用直接运行它。选......
  • java常用API--->Math数学工具
    介绍Math类是java.lang包中的类,它支持算术运算如平方根,计算绝对值等。算术计算Math.sqrt(Number);//计算Number的平方根Math.cbrt(Number);//计算Number的立方根Math.......
  • 使用JMeter的条件--安装Java
    首先下载JMeter,网址:https://jmeter.apache.org/dow...下载Binaries版,即可执行版,已编译好,下载解压就可以用。Source版是源代码版,需要自己编译成可执行文件。tgz压缩格式......
  • JavaScript 获取和设置剪贴板中的内容
    通过JavaScript在浏览器中获取或设置剪贴板中的内容,常用于一键复制或使用网页油猴复制限制文本使用execCommand(已弃用)写入文本到剪贴板document.onclick=func......
  • javaweb期中考试
     校园社团活动管理系统(20分)1、项目需求:校园社团作为高校课外活动的重要组成部分,发展十分迅速,也受到越来越多学生的欢迎,社团规模、数量等都在日益增长,社团活动也更为多......
  • JAVA-Random猜数字游戏
    packagecom.itheima;importjava.util.Random;importjava.util.Scanner;publicclassScanner04{publicstaticvoidmain(String[]args){//使用......
  • JAVA项目中的常用的异常处理情况总结
    java常见的异常都在如下列表中,以及相应的处理方式1.空指针异常(java.lang.nullpointerexception)发生该情况一般是字符串变量未初始化,数组未初始化,类对象未初始化等。还有......
  • JAVA-Random随机整数
    packagecom.itheima;importjava.util.Random;/*Random的基本使用*/publicclassScanner03{publicstaticvoidmain(String[]args){//Random......
  • JAVA-对键盘录入的两个数求和
    packagecom.itheima;importjava.util.Scanner;publicclassScanner02{publicstaticvoidmain(String[]args){//创建Scanner对象录入数据对象......