pta_Blog作业
(1)前言:总结之前所涉及到的知识点、题量、难度等情况
第四次作业到第五次作业贯穿始终的主题是继承与多态,第四次作业的三道题一道是sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)。另一道则是点线形系列4-凸四边形的计算。还有一道题是设计一个银行业务类,题目相对来说就第二题很难,第三题我没有注意题目格式要求。
期中考试则每次只有三道题,内容都是ATM机的类结构设计,期中考试题目在之前的实验的基础上增加了更加的细致的规定。
总的来说,三次作业虽然题目都不算太多,但都是非常具有综合性的考察的很全面。难度相对来说有一点点偏难。
(2)设计与分析:重点对题目的提交源码进行分析,可参考SourceMonitor的生成报表内容以及PowerDesigner的相应类图,要有相应的解释和心得(做到有图有真相),本次Blog必须分析PTA第4、5次大作业中的图形类设计的题目以及期中考试的三道题目
部分代码
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String[] inputs2 = new String[8];
for(int i=0;i<inputs2.length;i++){
inputs2[i]="1";
}
String type = inputs2[0];
String color = inputs2[1];
int speed = Integer.parseInt(inputs2[2]);
int sx = Integer.parseInt(inputs2[3]);
int sy = Integer.parseInt(inputs2[4]);
int n = Integer.parseInt(inputs2[5]);
int tx = Integer.parseInt(inputs2[6]);
int ty = Integer.parseInt(inputs2[7]);
/* 基于builder模式创建汽车对象 */
Builder builder = new CarBuilder();
Car car = builder.Brand(type).Color(color).Speed(speed).Sx(sx).Sy(sy).Frequency(n).Tx(tx).Ty(ty).getCar();
/* 基于adapter模式,实现汽车的移动 */
Target adapter = new AdapterLength(car);
DecimalFormat df = new DecimalFormat("0.0##");
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String str1;
String regex = "[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?(\\s[\\-\\+]?\\d+(\\.\\d+)?,[\\-\\+]?\\d+(\\.\\d+)?\\s?){0,}";
if(str.length()>2) {
str1 = str.substring(0, 1);
str=str.substring(2, str.length());
if(str.matches(regex)) {// 符合格式
String[] s = str.split(",|\\s");
for(int i=0; i<s.length; i++){
if(s[i].matches("0\\d+"))
outPut("Wrong Format");
}
double [] num = new double[12];
for(int i=0; i<12&&i<s.length; i++) {
num[i] = Double.parseDouble(s[i]);
}
if(0<Integer.parseInt(str1)&&Integer.parseInt(str1)<4 ){// 选项1,2,3
if(s.length==8) {
if(isRepetition(num)) outPut("points coincide");
else {
double l1,l2,l3,l4,l;
l1=Math.sqrt(Math.pow(num[0]-num[2], 2)+Math.pow(num[1]-num[3], 2));
l2=Math.sqrt(Math.pow(num[2]-num[4], 2)+Math.pow(num[3]-num[5], 2));
l3=Math.sqrt(Math.pow(num[4]-num[6], 2)+Math.pow(num[5]-num[7], 2));
l4=Math.sqrt(Math.pow(num[0]-num[6], 2)+Math.pow(num[1]-num[7], 2));
l=Math.sqrt(Math.pow(num[0]-num[4], 2)+Math.pow(num[1]-num[5], 2));
switch (Integer.parseInt(str1) ) {
case 1:choice1(num,l1,l2,l3,l4,l);// 选项1
break;
case 2:choice2(num,l1,l2,l3,l4,l);// 选项2
break;
case 3:choice3(num,l1,l2,l3,l4,l);// 选项3
break;
}
}
}
else outPut("wrong number of points");
}
else if(Integer.parseInt(str1)==4) {// 选项4
if(s.length==12) {
if(num[0]==num[2]&&num[1]==num[3]) outPut("points coincide");// 前两个坐标重合
else choice4(num);
}
else outPut("wrong number of points");
}
else if(Integer.parseInt(str1)==5) {// 选项5
if(s.length==10) choice5(num);
else outPut("wrong number of points");
}
else outPut("Wrong Format");
}
else outPut("Wrong Format");// 不符合格式
}
else outPut("Wrong Format"); // 字符串长度不够<2
}
public static boolean isRepetition(double []num) {// 选项1,2,3是否有重复的点
for(int i=0; i<5; i=i+2) {
if((num[i]==num[i+2]&&num[i+1]==num[i+3])||(num[0]==num[6]&&num[1]==num[7])) {
return true;
}
}
return false;
}
public static void outPut(String str) {
System.out.println(str);
System.exit(0);
}
public static int isQuad(double []n) {
double t1,t2,t3,t4;
t1=(n[2]-n[0])*(n[5]-n[3])-(n[4]-n[2])*(n[3]-n[1]);
t2=(n[4]-n[2])*(n[7]-n[5])-(n[6]-n[4])*(n[5]-n[3]);
t3=(n[6]-n[4])*(n[1]-n[7])-(n[0]-n[6])*(n[7]-n[5]);
t4=(n[0]-n[6])*(n[3]-n[1])-(n[2]-n[0])*(n[1]-n[7]);
if( (t1*t2>0)&&(t2*t3>0)&&(t3*t4>0))
return 1;
else if(t1*t2*t3*t4<0)
return 2;
else if((t1==t4&&t4==0&&t2*t3>0)||(t1==t2&&t2==0&&t3*t4>0)||(t2==t3&&t3==0&&t1*t4>0)||(t3==t4&&t4==0&&t1*t2>0)||(t2==t4&&t2==0&&t1*t3!=0)||(t1==0&&t2*t3*t4!=0)
||(t2==0&&t1*t3*t4!=0)||(t3==0&&t2*t1*t4!=0)||(t4==0&&t2*t3*t1!=0))
return 0;
else return -1;
}
public static double getMax(double []n) {
return Math.max(Math.max(n[0], n[2]), Math.max(n[4], n[6]));
}
public static double getMin(double []n) {
return Math.min(Math.min(n[0], n[2]), Math.min(n[4], n[6]));
}
public static double getMax(double n1 ,double n2) {
return Math.max(n1, n2);
}
public static double getMin(double n1,double n2) {
return Math.min(n1, n2);
}
public static double getArea(double l1, double l2, double l3) {
double c=(l1+l2+l3)/2;
return Math.sqrt(c*(c-l1)*(c-l2)*(c-l3));
}
public static void choice1(double[]n,double l1,double l2,double l3,double l4,double l) {
if(isQuad(n)>0) {
if((n[0]-n[2])*(n[1]-n[5])==(n[6]-n[4])*(n[1]-n[3])&&(Math.abs(l1-l3)<0.001)) outPut("true true");
else outPut("true false");
}
else outPut("false false");
}
public static void choice2(double[] n, double l1, double l2, double l3, double l4, double l) {
if(isQuad(n)>0) {// 四边形
if((n[0]-n[2])*(n[7]-n[5])==(n[6]-n[4])*(n[1]-n[3])&&(Math.abs(l1-l3)<0.001)) {// 平行四边形
if(l1!=l2&&Math.abs(l1*l1+l2*l2-l*l)<0.01) outPut("false true false");
else if(l1==l2&&Math.abs(l1*l1+l2*l2-l*l)<0.001) outPut("true true true");
else if(l1==l2) outPut("true false false");
else outPut("false false false");
}
else outPut("false false false");
}
else outPut("not a quadrilateral");
}
public static void choice3(double[] n, double l1, double l2, double l3, double l4, double l) {
DecimalFormat df = new DecimalFormat("0.0##");
if(isQuad(n)==1){
outPut("true "+df.format(l1+l2+l3+l4)+" "+df.format(0.5*Math.abs((n[0]-n[4])*(n[3]-n[7])-(n[2]-n[6])*(n[1]-n[5])) ) );
}
else if(isQuad(n)==2){
outPut("false "+df.format(l1+l2+l3+l4)+" "+df.format(0.5*Math.abs((n[0]-n[4])*(n[3]-n[7])-(n[2]-n[6])*(n[1]-n[5])) ) );
}
else outPut("not a quadrilateral");
}
public static void choice4(double[] n) {
double[] m=new double[8];
for(int i=0; i<8;i++) {
m[i]=n[i+4];
}
if(isQuad(m)<0) outPut("not a quadrilateral or triangle");
else if(isQuad(m)==1){
if(((n[2]-n[0])*(m[3]-m[1])==(m[2]-m[0])*(n[3]-n[1])&&(m[0]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[1]-n[1]))||
((n[2]-n[0])*(m[3]-m[5])==(m[2]-m[4])*(n[3]-n[1])&&(m[2]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[3]-n[1]))||
((n[2]-n[0])*(m[5]-m[7])==(m[4]-m[6])*(n[3]-n[1])&&(m[4]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[5]-n[1]))||
((n[2]-n[0])*(m[7]-m[1])==(m[6]-m[0])*(n[3]-n[1])&&(m[0]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[1]-n[1])))
outPut("The line is coincide with one of the lines");
else
{
if(n[0]==n[2]) {// 垂直x轴
if(n[0]<getMin(m)||n[0]>getMax(m))
outPut("0");
else if(n[0]==getMin(m)||n[0]==getMax(m))
outPut("1");}
else outPut("1");
}
}
else {
int i=0;
double area1,area2,area3;
if(isQuad(m)==0) {
if(((n[2]-n[0])*(m[3]-m[1])==(m[2]-m[0])*(n[3]-n[1])&&(m[0]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[1]-n[1]))||
((n[2]-n[0])*(m[3]-m[5])==(m[2]-m[4])*(n[3]-n[1])&&(m[2]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[3]-n[1]))||
((n[2]-n[0])*(m[5]-m[7])==(m[4]-m[6])*(n[3]-n[1])&&(m[4]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[5]-n[1]))||
((n[2]-n[0])*(m[7]-m[1])==(m[6]-m[0])*(n[3]-n[1])&&(m[0]-n[0])*(n[3]-n[1])==(n[2]-n[0])*(m[1]-n[1])))
outPut("The line is coincide with one of the lines");
else {
if(n[0]==n[2]) {// 垂直x轴
if(n[0]<getMin(m)||n[0]>getMax(m))
outPut("0");
else if(n[0]==getMin(m)||n[0]==getMax(m))
outPut("1");}
outPut("1");
}
}
}
}
public static void choice5(double[]n ) {
double[] m=new double[8];
for(int i=0; i<8;i++) {
m[i]=n[i+2];
}
if(isQuad(m)<0) outPut("not a quadrilateral or triangle");
else {
double t1,t2,t3,t4;
t1=(n[0]-m[0])*(m[3]-m[1])-(m[2]-m[0])*(n[1]-m[1]);
t2=(n[0]-m[2])*(m[5]-m[3])-(m[4]-m[2])*(n[1]-m[3]);
t3=(n[0]-m[4])*(m[7]-m[5])-(m[6]-m[4])*(n[1]-m[5]);
t4=(n[0]-m[6])*(m[1]-m[7])-(m[0]-m[6])*(n[1]-m[7]);
if(isQuad(m)==0) {
if(m[0]==m[2]&&m[1]==m[3]) {
if(t1==0&&t2*t3>0&&t3*t4>0) outPut("in the triangle");
else if(t1==0&&t2*t3*t4==0&&(t2*t3>=0||t2*t4>=0||t3*t4>=0) ) outPut("on the triangle");
else outPut("outof the triangle");
}
else if(m[2]==m[4]&&m[3]==m[5]) {
if(t2==0&&t1*t3>0&&t1*t4>0) outPut("in the triangle");
else if(t2==0&&t1*t3*t4==0&&(t1*t3>=0||t1*t4>=0||t3*t4>=0))outPut("on the triangle");
else outPut("outof the triangle");
}
else if(m[4]==m[6]&&m[5]==m[7]) {
if(t3==0&&t1*t2>0&&t1*t4>0) outPut("in the triangle");
else if(t3==0&&t1*t2*t4==0&&(t2*t1>=0||t2*t4>=0||t1*t4>=0))outPut("on the triangle");
else outPut("outof the triangle");
}
else if(m[0]==m[6]&&m[1]==m[7]) {
if(t4==0&&t1*t3>0&&t1*t2>0) outPut("in the triangle");
else if(t4==0&&t1*t3*t2==0&&(t2*t3>=0||t2*t1>=0||t3*t1>=0))outPut("on the triangle");
else outPut("outof the triangle");
}
else{
if((t1*t2>0)&&(t2*t3>0)&&(t3*t4>0)) outPut("in the triangle");
else if( (t1*t2*t3*t4==0&&(n[0]<=getMax(m[0],m[2])&&n[0]>=getMin(m[0],m[2])&&n[1]<=getMax(m[1],m[3])&&n[1]>=getMin(m[1],m[3])||n[0]<=getMax(m[4],m[2])&&n[0]>=getMin(m[4],m[2])&&n[1]<=getMax(m[5],m[3])&&n[1]>=getMin(m[5],m[3])||
n[0]<=getMax(m[4],m[6])&&n[0]>=getMin(m[4],m[6])&&n[1]<=getMax(m[5],m[7])&&n[1]>=getMin(m[5],m[7])||n[0]<=getMax(m[0],m[6])&&n[0]>=getMin(m[0],m[6])&&n[1]<=getMax(m[1],m[7])&&n[1]>=getMin(m[1],m[7]) ) ) )outPut("on the triangle");
else outPut("outof the triangle");
}
}
else {
if((t1*t2>0)&&(t2*t3>0)&&(t3*t4>0)) outPut("in the quadrilateral");
else if((t1*t2*t3*t4==0&&(n[0]<=getMax(m[0],m[2])&&n[0]>=getMin(m[0],m[2])&&n[1]<=getMax(m[1],m[3])&&n[1]>=getMin(m[1],m[3])||n[0]<=getMax(m[4],m[2])&&n[0]>=getMin(m[4],m[2])&&n[1]<=getMax(m[5],m[3])&&n[1]>=getMin(m[5],m[3])||
n[0]<=getMax(m[4],m[6])&&n[0]>=getMin(m[4],m[6])&&n[1]<=getMax(m[5],m[7])&&n[1]>=getMin(m[5],m[7])||n[0]<=getMax(m[0],m[6])&&n[0]>=getMin(m[0],m[6])&&n[1]<=getMax(m[1],m[7])&&n[1]>=getMin(m[1],m[7]) ) ) ) outPut("on the quadrilateral");//(t1==t2&&t1==0&&t3*t4>0)||(t1==t3&&t1==0&&t2*t4>0)||(t1==t4&&t4==0&&t2*t3>0)||(t2==t3&&t2==0&&t1*t4>0)||(t2==t4&&t2==0&&t1*t3>0)||(t3==t4&&t4==0&&t1*t2>0)||(t1==t2&&t2==t3&&t3==0)||(t1==t2&&t2==t4&&t4==0)||(t1==t4&&t4==t3&&t3==0)||(t4==t2&&t2==t3&&t3==0)
else outPut("outof the quadrilateral");
}
}
}
}
class Car{
private String brand;
private String color;
private int speed;
private int sx;
private int sy;
private int frequency;
private int tx;
private int ty;
public Car(String brand, String color, int speed, int sx, int sy, int frequency, int tx, int ty) {
this.brand = brand;
this.color = color;
this.speed = speed;
this.sx = sx;
this.sy = sy;
this.frequency = frequency;
this.tx = tx;
this.ty = ty;
}
public int getSx() {
return sx;
}
public void setSx(int sx) {
this.sx = sx;
}
public int getSy() {
return sy;
}
public void setSy(int sy) {
this.sy = sy;
}
public int getTx() {
return tx;
}
public void setTx(int tx) {
this.tx = tx;
}
public int getTy() {
return ty;
}
public void setTy(int ty) {
this.ty = ty;
}
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public void turnUp(){
this.setSy(this.getSy()+this.getSpeed());
}
public void turnDown(){
this.setSy(this.getSy()-this.getSpeed());
}
public void turnLeft(){
this.setSx(this.getSx()-this.getSpeed());
}
public void turnRight(){
this.setSx(this.getSx()+this.getSpeed());
}
}
interface Builder {
Builder Brand(String brand);
Builder Color(String color);
Builder Speed(int speed);
Builder Sx(int sx);
Builder Sy(int sy);
Builder Frequency(int frequency);
Builder Tx(int tx);
Builder Ty(int ty);
Car getCar();
}
class CarBuilder implements Builder {
private String brand;
private String color;
private int speed;
private int sx;
private int sy;
private int frequency;
private int tx;
private int ty;
@Override
public Builder Brand(String brand) {
this.brand = brand;
return this;
}
@Override
public CarBuilder Color(String color) {
this.color = color;
return this;
}
@Override
public Builder Speed(int speed) {
this.speed = speed;
return this;
}
@Override
public Builder Sx(int sx) {
this.sx = sx;
return this;
}
@Override
public Builder Sy(int sy) {
this.sy = sy;
return this;
}
@Override
public Builder Frequency(int frequency) {
this.frequency = frequency;
return this;
}
@Override
public Builder Tx(int tx) {
this.tx = tx;
return this;
}
@Override
public Builder Ty(int ty) {
this.ty = ty;
return this;
}
public Car getCar(){
return new Car(brand,color,speed,sx,sy,frequency,tx,ty);
}
}
//目标:判断距离
interface Target{
void fitFor(Car car);
}
//距离适配器
class AdapterLength implements Target{
private Car car;
public AdapterLength(Car car) {
this.car = car;
}
public void fitFor(Car car) {
if(car.getTx() <= car.getSx() && car.getTy() > car.getSy()){//目标点在左上,上
//判断走左还是走上
if(Math.pow(Math.abs(car.getTx()-car.getSx())-8,2)+Math.pow(Math.abs(car.getTy()-car.getSy()),2) <=
Math.pow(Math.abs(car.getTy()-car.getSy())-8,2)+Math.pow(Math.abs(car.getTx()-car.getSx()),2)){
car.turnLeft();
}else{
car.turnUp();
}
}else if(car.getTx() < car.getSx() && car.getTy() <= car.getSy()){//目标点在左下,左
//判断走左还是走下
if(Math.pow(Math.abs(car.getTx()-car.getSx())-8,2)+Math.pow(Math.abs(car.getTy()-car.getSy()),2) <=
Math.pow(Math.abs(car.getTy()-car.getSy())-8,2)+Math.pow(Math.abs(car.getTx()-car.getSx()),2)){
car.turnLeft();
}else{
car.turnDown();
}
}else if(car.getTx() >= car.getSx() && car.getTy() > car.getSy()) {//目标点在右上,右
//判断走右还是走上
if(Math.pow(Math.abs(car.getTx()-car.getSx())-8,2)+Math.pow(Math.abs(car.getTy()-car.getSy()),2) <=
Math.pow(Math.abs(car.getTy()-car.getSy())-8,2)+Math.pow(Math.abs(car.getTx()-car.getSx()),2)){
car.turnRight();
}else{
car.turnUp();
}
}else {//目标点在右下,下
if(Math.pow(Math.abs(car.getTx()-car.getSx())-8,2)+Math.pow(Math.abs(car.getTy()-car.getSy()),2) <=
Math.pow(Math.abs(car.getTy()-car.getSy())-8,2)+Math.pow(Math.abs(car.getTx()-car.getSx()),2)){
car.turnRight();
}else{
car.turnDown();
}
}
}
}
期中考试代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double x1 = input.nextDouble();
if(!judge(x1)){
System.out.println("Wrong Format");
return;
}
double y1 = input.nextDouble();
if(!judge(y1)){
System.out.println("Wrong Format");
return;
}
double x2 = input.nextDouble();
if(!judge(x2)){
System.out.println("Wrong Format");
return;
}
double y2 = input.nextDouble();
if(!judge(y2)){
System.out.println("Wrong Format");
return;
}
input.nextLine();
String color = input.nextLine();
Point start = new Point(x1,y1);
Point end = new Point(x2,y2);
Line line = new Line(start,end,color);
line.display();
}
public static boolean judge(double pos){
if(pos > 200 || pos <= 0){
return false;
}
return true;
}
}
class Point{
private double x;
private double y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void display(){
System.out.printf("(%.2f,%.2f)\n",x,y);
}
}
class Line{
private Point start;
private Point end;
private String color;
public Line() {
}
public Line(Point start, Point end, String color) {
this.start = start;
this.end = end;
this.color = color;
}
public Point getStart() {
return start;
}
public void setStart(Point start) {
this.start = start;
}
public Point getEnd() {
return end;
}
public void setEnd(Point end) {
this.end = end;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance(){
return Math.sqrt(Math.pow(start.getX()-end.getX(),2)+Math.pow(start.getY()-end.getY(),2));
}
public void display(){
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
start.display();
System.out.println("The line's end point's Coordinate is:");
end.display();
System.out.printf("The line's length is:%.2f",getDistance());
}
}
第二题
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double x1 = input.nextDouble();
if(!judge(x1)){
System.out.println("Wrong Format");
return;
}
double y1 = input.nextDouble();
if(!judge(y1)){
System.out.println("Wrong Format");
return;
}
double x2 = input.nextDouble();
if(!judge(x2)){
System.out.println("Wrong Format");
return;
}
double y2 = input.nextDouble();
if(!judge(y2)){
System.out.println("Wrong Format");
return;
}
input.nextLine();
String color = input.nextLine();
Point start = new Point(x1,y1);
Point end = new Point(x2,y2);
Line line = new Line(start,end,color);
Plane plane = new Plane(color);
Element element;
element = start;
element.display();
element = end;
element.display();
element = line;
element.display();
element = plane;
element.display();
}
public static boolean judge(double pos){
if(pos > 200 || pos <= 0){
return false;
}
return true;
}
}
abstract class Element{
public abstract void display();
}
class Point extends Element{
private double x;
private double y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public void display() {
System.out.printf("(%.2f,%.2f)\n",x,y);
}
}
class Line extends Element{
private Point start;
private Point end;
private String color;
public Line() {
}
public Line(Point start, Point end, String color) {
this.start = start;
this.end = end;
this.color = color;
}
public Point getStart() {
return start;
}
public void setStart(Point start) {
this.start = start;
}
public Point getEnd() {
return end;
}
public void setEnd(Point end) {
this.end = end;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance(){
return Math.sqrt(Math.pow(start.getX()-end.getX(),2)+Math.pow(start.getY()-end.getY(),2));
}
@Override
public void display(){
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
start.display();
System.out.println("The line's end point's Coordinate is:");
end.display();
System.out.printf("The line's length is:%.2f\n",getDistance());
}
}
class Plane extends Element{
private String color;
public Plane() {
}
public Plane(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+color);
}
}
第三题:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
GeometryObject object = new GeometryObject();
int choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1:{
double x1 = input.nextDouble();
if(!judge(x1)){
System.out.println("Wrong Format");
return;
}
double y1 = input.nextDouble();
if(!judge(y1)){
System.out.println("Wrong Format");
return;
}
Element start = new Point(x1,y1);
object.getList().add(start);
break;
}
case 2:{
double x1 = input.nextDouble();
if(!judge(x1)){
System.out.println("Wrong Format");
return;
}
double y1 = input.nextDouble();
if(!judge(y1)){
System.out.println("Wrong Format");
return;
}
double x2 = input.nextDouble();
if(!judge(x2)){
System.out.println("Wrong Format");
return;
}
double y2 = input.nextDouble();
if(!judge(y2)){
System.out.println("Wrong Format");
return;
}
input.nextLine();
String color = input.nextLine();
Point start = new Point(x1,y1);
Point end = new Point(x2,y2);
Element line = new Line(start,end,color);
object.getList().add(line);
break;
}
case 3: {
input.nextLine();
String color = input.nextLine();
Element plane = new Plane(color);
object.getList().add(plane);
break;
}
case 4:{
int index = input.nextInt();
object.remove(index);
break;
}
}
choice = input.nextInt();
}
for(Element element : object.getList()){
element.display();
}
}
public static boolean judge(double pos){
if(pos > 200 || pos <= 0){
return false;
}
return true;
}
}
abstract class Element{
public abstract void display();
}
class Point extends Element{
private double x;
private double y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public void display() {
System.out.printf("(%.2f,%.2f)\n",x,y);
}
}
class Line extends Element{
private Point start;
private Point end;
private String color;
public Line() {
}
public Line(Point start, Point end, String color) {
this.start = start;
this.end = end;
this.color = color;
}
public Point getStart() {
return start;
}
public void setStart(Point start) {
this.start = start;
}
public Point getEnd() {
return end;
}
public void setEnd(Point end) {
this.end = end;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance(){
return Math.sqrt(Math.pow(start.getX()-end.getX(),2)+Math.pow(start.getY()-end.getY(),2));
}
@Override
public void display(){
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
start.display();
System.out.println("The line's end point's Coordinate is:");
end.display();
System.out.printf("The line's length is:%.2f\n",getDistance());
}
}
class Plane extends Element{
private String color;
public Plane() {
}
public Plane(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+color);
}
}
class GeometryObject{
private ArrayList<Element> list = new ArrayList<>();
public GeometryObject() {}
public void add(Element element){
list.add(element);
}
public void remove(int index){
if(index > list.size() || index < 0){
return;
}
list.remove(index-1);
}
public ArrayList<Element> getList() {
return list;
}
}
第三题:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
GeometryObject object = new GeometryObject();
int choice = input.nextInt();
while(choice != 0) {
switch(choice) {
case 1:{
double x1 = input.nextDouble();
if(!judge(x1)){
System.out.println("Wrong Format");
return;
}
double y1 = input.nextDouble();
if(!judge(y1)){
System.out.println("Wrong Format");
return;
}
Element start = new Point(x1,y1);
object.getList().add(start);
break;
}
case 2:{
double x1 = input.nextDouble();
if(!judge(x1)){
System.out.println("Wrong Format");
return;
}
double y1 = input.nextDouble();
if(!judge(y1)){
System.out.println("Wrong Format");
return;
}
double x2 = input.nextDouble();
if(!judge(x2)){
System.out.println("Wrong Format");
return;
}
double y2 = input.nextDouble();
if(!judge(y2)){
System.out.println("Wrong Format");
return;
}
input.nextLine();
String color = input.nextLine();
Point start = new Point(x1,y1);
Point end = new Point(x2,y2);
Element line = new Line(start,end,color);
object.getList().add(line);
break;
}
case 3: {
input.nextLine();
String color = input.nextLine();
Element plane = new Plane(color);
object.getList().add(plane);
break;
}
case 4:{
int index = input.nextInt();
object.remove(index);
break;
}
}
choice = input.nextInt();
}
for(Element element : object.getList()){
element.display();
}
}
public static boolean judge(double pos){
if(pos > 200 || pos <= 0){
return false;
}
return true;
}
}
abstract class Element{
public abstract void display();
}
class Point extends Element{
private double x;
private double y;
public Point() {
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
@Override
public void display() {
System.out.printf("(%.2f,%.2f)\n",x,y);
}
}
class Line extends Element{
private Point start;
private Point end;
private String color;
public Line() {
}
public Line(Point start, Point end, String color) {
this.start = start;
this.end = end;
this.color = color;
}
public Point getStart() {
return start;
}
public void setStart(Point start) {
this.start = start;
}
public Point getEnd() {
return end;
}
public void setEnd(Point end) {
this.end = end;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance(){
return Math.sqrt(Math.pow(start.getX()-end.getX(),2)+Math.pow(start.getY()-end.getY(),2));
}
@Override
public void display(){
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
start.display();
System.out.println("The line's end point's Coordinate is:");
end.display();
System.out.printf("The line's length is:%.2f\n",getDistance());
}
}
class Plane extends Element{
private String color;
public Plane() {
}
public Plane(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+color);
}
}
class GeometryObject{
private ArrayList<Element> list = new ArrayList<>();
public GeometryObject() {}
public void add(Element element){
list.add(element);
}
public void remove(int index){
if(index > list.size() || index < 0){
return;
}
list.remove(index-1);
}
public ArrayList<Element> getList() {
return list;
}
}
(3)采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
1.面向对象编程不能注重于按步骤一步一步解决问题,而应该注重于将问题中的部分分成各个对象,各司其职,谁做了什么事,谁又做了什么事,他们共同作用的结果解决了一件事。
可以说,面向对象编程更接近现实世界。程序的对象就像现实世界中的个体,他们通过发送消息告知彼此所要做的(方法的调用)。OO设计的主要任务是将设计尽量和现实世界的事物一致化。尽量使程序设计的思路符合现实世界事物的规律。类和对象是OO编程中最重要最基本的两个元素,在设计时,先将找到要处理的各个对象,再找到各个对象的共同的祖先,逐层抽象。最后确立各个类之间的继承关系。
2.编写类时需要充分利用类的单一职责的原则,通过类图编写类的属性和方法的作用域,类与类之间的关系,也要注意类的继承关系,关注子类对父类方法的重写,子类对父类属性的使用。
(4)改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进
在代码中,还需要更好的对面向对象的理解更加透彻,要不然用的不是很顺手,更应该抓住的更领悟的是所有的关系,而并非一种继承的关系。
(5)总结:对本阶段(10-12周)综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。
学习到的内容以及收获:
1、单一功能原则:认为对象应该仅具有一种单一功能的概念
2、开闭原则:认为软件体应该是对于扩展开放的,但是对于修改封闭的概念
3、里氏替换原则:认为“程序中的对象应该是可以在不改变程序正确性的前提下被它的子类所替换的”概念
4、接口隔离原则:认为“多个特定客户端接口要好于一个宽泛用途的接口”
5、依赖反转原则:认为一个方法应该遵从“依赖于抽象而不是一个实例”的概念,依赖注入所以该原则的一种实现方式
类只能继承extends一个类,但可以实现implements多个接口,子类需要实现抽象类或接口中的所有不完整的方法,也就是说,多个子类继承于一个父类或实现一个接口,这些子类中会有相同的方法,而子类和父类可以在一定条件下相互转化,每个子类都是父类的实例,若为一个父对象赋值不同的子对象,则父对象可使用不同子对象的相同方法,即父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作,也就是面向对象的多态性。
建议:
希望每次可以给同学一定的时间去修改作业,让自己的笔记更加完善。