第二次博客作业
前言
1.题目集4一共有三道题,第二题比较有难度,是有关四边形的计算,不是很会,只拿了29分,第一题和第二题总体来说还可以,没那么难,但是也遇到了有些困难,比如不会截取字符串变成数字,借鉴了一点网上的资料,最后还是成功的写完了
2.题目集5一共有2题,说实话对于我来说困难比较大,不知道咋写,还没及格呢,这两道题呢都是关于五边形的一些计算啊啥的,四边形还没整明白呢,五边形更糊涂了,但是呢,在我的不断努力和学习下,还是勉强的写了点的
3.期中考试一共有三道题,第一题没什么困难,第二题和第三题对我来说有点困难吧,第一题就是简单的一个点和线的类设计,第二题在第一题的基础上加了继承和多态,总体来说也还行,第三题主要就是加了容器的考察
设计与分析
题目集4
第一题
代码
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main{,
public static void main(String[] args){
String a;
int sum;
Scanner in = new Scanner(System.in);
a = in.nextLine();
Pattern x = Pattern.compile("[0-9]+");
while(!a.equals("end"))
{
Matcher b = x.matcher(a);
sum = 0;
while(b.find())
{
sum += Integer.valueOf(b.group());
}
System.out.print(sum+"\n");
a = in.nextLine();
}
}
}
圈复杂度

本道题对我来说最主要的问题就是不会pattern和matcher的用法,不知道如何把字符串里的数字提取出来。
第二题
代码
import java.util.Scanner;
public class Main{
public static void main(String[] arge){
Scanner in = new Scanner(System.in);
String s = in.nextLine();
Judgeinput a=new Judgeinput(s);
a.judge();
int choice=Points.getChoice(s);
double[] x=null;
double[] y=null;
s=s.substring(2);
Points b=new Points(s,choice);
b.choose();
x=b.getX();
y=b.getY();
switch(choice) {
case 1:
First first=new First(x,y);
first.judgerepeat();
break;
case 2:
System.out.print("not a quadrilateral");
break;
case 3:
System.out.print("not a quadrilateral");
break;
case 4:
System.out.print("not a quadrilateral or triangle");
break;
case 5:
System.out.print("in the triangle");
}
}
}
class Judgeinput {
private String s;
private int choice;
private String coord[];
Judgeinput(String s){
this.s=s;
}
public void judge() {
choice = s.charAt(0)-48;
judgeChoice();
s=s.substring(2);
coord = s.split(" ");
judgeFormat();
judgeQuantity();
}
public void judgeChoice() {
if(!s.matches("[1-5]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
public void judgeFormat() {
for(int i=0;i<coord.length;i++) {
if(!coord[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
public void judgeQuantity() {
int length=coord.length;
switch(choice) {
case 1:if(length!=4) {
System.out.println("wrong number of points");
System.exit(0);
}
break;
case 2:if(length!=4) {
System.out.println("wrong number of points");
System.exit(0);
}
break;
case 3:if(length!=4) {
System.out.println("wrong number of points");
System.exit(0);
}
break;
case 4:if(length!=6) {
System.out.println("wrong number of points");
System.exit(0);
}
break;
case 5:if(length!=5) {
System.out.println("wrong number of points");
System.exit(0);
}
}
}
}
class Points {
private String s;
private int choice;
private String coord[];
public double[] x=null;
public double[] y=null;
Points(String s,int choice){
this.s=s;
this.choice=choice;
}
public static int getChoice(String s) {
int choice = s.charAt(0)-48;
return choice;
}
public void choose() {
coord=s.split(",| ");
if(choice==1||choice==2||choice==3) {
coordinate123();
}
else if(choice==4) {
coordinate4();
}
else if(choice==5) {
coordinate5();
}
}
public void coordinate123() {
x = new double[4];
y = new double[4];
for(int i=0;i<4;i++) {
x[i]=Double.parseDouble(coord[2*i]);
y[i]=Double.parseDouble(coord[2*i+1]);
}
}
public void coordinate4() {
x = new double[6];
y = new double[6];
for(int i=0;i<6;i++) {
x[i]=Double.parseDouble(coord[2*i]);
y[i]=Double.parseDouble(coord[2*i+1]);
}
}
public void coordinate5() {
x = new double[5];
y = new double[5];
for(int i=0;i<5;i++) {
x[i]=Double.parseDouble(coord[2*i]);
y[i]=Double.parseDouble(coord[2*i+1]);
}
}
public double[] getX() {
return x;
}
public double[] getY() {
return y;
}
}
class First {
private double[] x = new double[4];
private double[] y = new double[4];
double[] line =new double[4];
First(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public void judgerepeat() {
Judgerepeat c=new Judgerepeat(x,y);
c.judgerepeat();
quadrilateral();
Line d=new Line(x,y);
line=d.line();
parallelogram();
}
public void quadrilateral() {
if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))||
((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))||
((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))||
((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2])))System.out.print("false ");
else {
if(!Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) {
System.out.print("true ");
}
else System.out.print("false ");
}
}
public void parallelogram() {
if(line[0]==line[2]&&line[1]==line[3])System.out.print("true");
else System.out.print("false");
}
}
class Judgerepeat {
public double[] x=new double[4];
public double[] y=new double[4];
Judgerepeat(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public void judgerepeat() {
if(x[0]==x[1]&&y[0]==y[1]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[0]==x[2]&&y[0]==y[2]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[0]==x[3]&&y[0]==y[3]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[1]==x[2]&&y[1]==y[2]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[1]==x[3]&&y[1]==y[3]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[2]==x[3]&&y[2]==y[3]) {
System.out.println("points coincide");
System.exit(0);
}
}
}
class Line {
public double[] line =new double[4];
public double[] diagonal=new double[2];
public double[] x=new double[4];
public double[] y=new double[4];
Line(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public double[] line() {
for(int i=0;i<4;i++) {
int j=(i+1)%4;
line[i]=Math.sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
return line;
}
public static boolean intersect(double l1x1,double l1y1,double l1x2,double l1y2,double l2x1,double l2y1,double l2x2,double l2y2) {
if ((l1x1 > l1x2 ? l1x1 : l1x2) < (l2x1 < l2x2 ? l2x1 : l2x2) ||
(l1y1 > l1y2 ? l1y1 : l1y2) < (l2y1 < l2y2 ? l2y1 : l2y2) ||
(l2x1 > l2x2 ? l2x1 : l2x2) < (l1x1 < l1x2 ? l1x1 : l1x2) ||
(l2y1 > l2y2 ? l2y1 : l2y2) < (l1y1 < l1y2 ? l1y1 : l1y2)){
return false;
}
else if ((((l1x1 - l2x1)*(l2y2 - l2y1) - (l1y1 - l2y1)*(l2x2 - l2x1))*((l1x2 - l2x1)*(l2y2 - l2y1) - (l1y2 - l2y1)*(l2x2 - l2x1))) > 0 ||
(((l2x1 - l1x1)*(l1y2 - l1y1) - (l2y1 - l1y1)*(l1x2 - l1x1))*((l2x2 - l1x1)*(l1y2 - l1y1) - (l2y2 - l1y1)*(l1x2 - l1x1))) > 0){
return false;
}
else return true;
}
}
圈复杂度

这道题主要是关于凸四边形的计算,设计了菱形的判断,平行四边形的判断,凹凸四边形的判断,主要的难点有判断四个点是否能构成四边形以及输入的判断,整个题目弄好几个类,点类,线类,case3有点难,我不太会
第三题
代码
import java.util.Scanner; public class Main{ public static void main(String[] arge) { Scanner in = new Scanner(System.in); String name = in.next(); String password = in.next(); BankBusiness a = new BankBusiness(name,password); a.welcome(); a.cunkuan(in.next(), in.nextDouble()); a.qukuan(in.next(), in.nextDouble()); a.qukuan(in.next(), in.nextDouble()); a.qukuan(in.next(), in.nextDouble()); a.welcomeNext(); } } class BankBusiness{ public static String bankName = "中国银行"; String name; String password; double balance; public static void welcome(){ System.out.println(bankName+"欢迎您的到来!"); } public BankBusiness(String name,String password) { super(); this.name = name; this.password = password; this.balance = 0; } void cunkuan(String password,double x) { if(!password.equals(this.password)) { System.out.println("您的密码错误!"); } else { this.balance = this.balance+x; System.out.println("您的余额有"+this.balance+"元。"); } } void qukuan(String password,double x) { if(!password.equals(this.password)) { System.out.println("您的密码错误!"); } else { if(x>this.balance) { System.out.println("您的余额不足!"); } else { this.balance = this.balance-x; System.out.println("请取走钞票,您的余额还有"+this.balance+"元。"); } } } public static void welcomeNext() { System.out.println("请收好您的证件和物品,欢迎您下次光临!"); } }
圈复杂度

这道题不是很难,就是设计一个银行取钱存钱的程序,设计一个银行业务类,里面包含银行名称,账户名name、密码password、账户余额balance,一些属性,并且包含取钱,存钱,输入密码的一些函数,在测试类Main里进行调用就可以了
题目集五
第一题
代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
Judgeinput a=new Judgeinput(s);
a.judge();
int choice=Points.getChoice(s);
double[] x=null;
double[] y=null;
s=s.substring(2);
Points b=new Points(s,choice);
b.choose();
x=b.getX();
y=b.getY();
switch(choice) {
case 1:
First first=new First(x,y);
first.judgerepeat();
break;
case 2:
System.out.print("false");
break;
case 3:
System.out.print("The line is coincide with one of the lines");
}
}
}
class Judgeinput {
private String s;
private int choice;
private String coord[];
Judgeinput(String s){
this.s=s;
}
public void judge() {
choice = s.charAt(0)-48;
judgeChoice();
s=s.substring(2);
coord = s.split(" ");
judgeFormat();
judgeQuantity();
}
public void judgeChoice() {
if(!s.matches("[1-3]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
public void judgeFormat() {
for(int i=0;i<coord.length;i++) {
if(!coord[i].matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
public void judgeQuantity() {
int length=coord.length;
switch(choice) {
case 1:if(length!=5) {
System.out.println("wrong number of points");
System.exit(0);
}
break;
case 2:if(length!=5) {
System.out.println("wrong number of points");
System.exit(0);
}
break;
case 3:if(length!=7) {
System.out.println("wrong number of points");
System.exit(0);
}
break;
}
}
}
class Points {
private String s;
private int choice;
private String coord[];
public double[] x=null;
public double[] y=null;
Points(String s,int choice){
this.s=s;
this.choice=choice;
}
public static int getChoice(String s) {
int choice = s.charAt(0)-48;
return choice;
}
public void choose() {
coord=s.split(",| ");
if(choice==1||choice==2) {
coordinate12();
}
else if(choice==3) {
coordinate3();
}
}
public void coordinate12() {
x = new double[5];
y = new double[5];
for(int i=0;i<5;i++) {
x[i]=Double.parseDouble(coord[2*i]);
y[i]=Double.parseDouble(coord[2*i+1]);
}
}
public void coordinate3() {
x = new double[7];
y = new double[7];
for(int i=0;i<7;i++) {
x[i]=Double.parseDouble(coord[2*i]);
y[i]=Double.parseDouble(coord[2*i+1]);
}
}
public double[] getX() {
return x;
}
public double[] getY() {
return y;
}
}
class First {
private double[] x = new double[5];
private double[] y = new double[5];
double[] line =new double[5];
First(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public void judgerepeat() {//判断点是否重复
Judgerepeat c=new Judgerepeat(x,y);
c.judgerepeat();
quadrilateral();
Line d=new Line(x,y);
line=d.line();
}
public void quadrilateral() {
if(((y[0]-y[1])*(x[1]-x[2])==(y[1]-y[2])*(x[0]-x[1]))&&
((y[0]-y[1])*(x[2]-x[3])==(y[2]-y[3])*(x[0]-x[1]))&&
((y[3]-y[4])*(x[0]-x[4])==(y[0]-y[4])*(x[3]-x[4]))&&
((y[0]-y[1])*(x[0]-x[4])==(y[0]-y[4])*(x[0]-x[1])))
{
System.out.print("not a polygon");
}
else if(((y[0]-y[1])*(x[1]-x[2])==(y[1]-y[2])*(x[0]-x[1]))||
((y[0]-y[1])*(x[2]-x[3])==(y[2]-y[3])*(x[0]-x[1]))||
((y[3]-y[4])*(x[0]-x[4])==(y[0]-y[4])*(x[3]-x[4]))||
((y[0]-y[1])*(x[0]-x[4])==(y[0]-y[4])*(x[0]-x[1])))
System.out.print("false");
else {
if(Line.intersect(x[0],y[0],x[1],y[1],x[2],y[2],x[3],y[3])&&
Line.intersect(x[0],y[0],x[1],y[1],x[3],y[3],x[4],y[4])&&
Line.intersect(x[1],y[1],x[2],y[2],x[3],y[3],x[4],y[4])&&
Line.intersect(x[1],y[1],x[2],y[2],x[0],y[0],x[4],y[4])&&
Line.intersect(x[2],y[2],x[3],y[3],x[0],y[0],x[4],y[4])) {
System.out.print("true");
}
else System.out.print("false");
}
}
}
class Second {
private double[] x = new double[5];
private double[] y = new double[5];
double[] line =new double[5];
double[] diagonal=new double[2];
private boolean judge1=false;
private boolean judge2=false;
private boolean judge3=false;
Second(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public void judgerepeat() {
Judgerepeat c=new Judgerepeat(x,y);
c.judgerepeat();
quadrilateral();
Line d=new Line(x,y);
line=d.line();
diagonal=d.diagonal();
lozenge();
rectangle();
square();
}
public void quadrilateral() {
if(((y[0]-y[1])*(x[0]-x[2])==(y[0]-y[2])*(x[0]-x[1]))||
((y[0]-y[1])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[1]))||
((y[0]-y[2])*(x[0]-x[3])==(y[0]-y[3])*(x[0]-x[2]))||
((y[1]-y[2])*(x[1]-x[3])==(y[1]-y[3])*(x[1]-x[2]))) {
System.out.print("not a quadrilateral");
System.exit(0);
}
else {
if(Line.intersect(x[0],y[0],x[3],y[3],x[1],y[1],x[2],y[2])) {
System.out.print("not a quadrilateral");
System.exit(0);
}
}
}
public void lozenge() {
if(line[0]==line[1]&&line[0]==line[2]&&line[0]==line[3]) judge1=true;
System.out.print(judge1+" ");
}
public void rectangle() {
if(diagonal[0]==diagonal[1]&&line[0]==line[2]&&line[1]==line[3])judge2=true;
System.out.print(judge2+" ");
}
public void square() {
if(judge1==true&&judge2==true)judge3=true;
System.out.print(judge3);
}
}
class Third {
private double[] x = new double[4];
private double[] y = new double[4];
private boolean judge1=false;//0,2
private boolean judge2=false;//1,3
Third(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public void judgerepeat() {
Judgerepeat c=new Judgerepeat(x,y);
c.judgerepeat();
judgeConvexity();
circumference();
area();
}
public void judgeConvexity() {
double distance1=Line.distance(x[1],y[1],x[0],y[0],x[2],y[2]);
double distance2=Line.distance(x[3],y[3],x[0],y[0],x[2],y[2]);
double distance3=Line.distance(x[0],y[0],x[1],y[1],x[3],y[3]);
double distance4=Line.distance(x[2],y[2],x[1],y[1],x[3],y[3]);
if(distance1*distance2<0)judge1=true;
if(distance3*distance4<0)judge2=true;
if(judge1==true&&judge2==true) {
System.out.print("true ");
}
else System.out.print("false ");
}
public void circumference() {
double line1=Math.sqrt((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1]));
double line2=Math.sqrt((x[1]-x[2])*(x[1]-x[2])+(y[1]-y[2])*(y[1]-y[2]));
double line3=Math.sqrt((x[2]-x[3])*(x[2]-x[3])+(y[2]-y[3])*(y[2]-y[3]));
double line4=Math.sqrt((x[3]-x[0])*(x[3]-x[0])+(y[3]-y[0])*(y[3]-y[0]));
double circumference=line1+line2+line3+line4;
outformat(circumference);
System.out.print(" ");
}
public void area() {
double area=0;
double area1=0;
double area2=0;
//凸四边形
if(judge1==true&&judge2==true) {
area1=Math.abs((x[1]*y[2]+x[2]*y[3]+x[3]*y[1]-x[1]*y[3]-x[2]*y[1]-x[3]*y[2])/2);
area2=Math.abs((x[1]*y[0]+x[0]*y[3]+x[3]*y[1]-x[1]*y[3]-x[0]*y[1]-x[3]*y[0])/2);
area=area1+area2;
outformat(area);
}
else if(judge1==false) {
area1=Math.abs((x[1]*y[3]+x[3]*y[0]+x[0]*y[1]-x[1]*y[0]-x[3]*y[1]-x[0]*y[3])/2);
area2=Math.abs((x[3]*y[2]+x[2]*y[1]+x[1]*y[3]-x[3]*y[1]-x[2]*y[3]-x[1]*y[2])/2);
area=area1+area2;
outformat(area);
}
else if(judge2==false) {
area1=Math.abs((x[1]*y[2]+x[2]*y[0]+x[0]*y[1]-x[1]*y[0]-x[2]*y[1]-x[0]*y[2])/2);
area2=Math.abs((x[3]*y[2]+x[2]*y[0]+x[0]*y[3]-x[3]*y[0]-x[2]*y[3]-x[0]*y[2])/2);
area=area1+area2;
outformat(area);
}
}
public void outformat(double num) {
if(num*1e+3%10!=0) {
String num1=String.format("%.3f",num);
System.out.print(num1);
}
else System.out.print(num);
}
}
class Judgerepeat {
public double[] x=new double[5];
public double[] y=new double[5];
Judgerepeat(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public void judgerepeat() {
if(x[0]==x[1]&&y[0]==y[1]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[0]==x[2]&&y[0]==y[2]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[0]==x[3]&&y[0]==y[3]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[0]==x[4]&&y[0]==y[4]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[1]==x[2]&&y[1]==y[2]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[1]==x[3]&&y[1]==y[3]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[1]==x[4]&&y[1]==y[4]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[2]==x[3]&&y[2]==y[3]) {
System.out.println("points coincide");
System.exit(0);
}
else if(x[2]==x[4]&&y[2]==y[4]) {
System.out.println("points coincide");
System.exit(0);
}
}
}
class Line {
public double[] line =new double[5];
public double[] diagonal=new double[2];
public double[] x=new double[5];
public double[] y=new double[5];
Line(double[] x,double[] y) {
this.x=x;
this.y=y;
}
public double[] line() {
for(int i=0;i<5;i++) {
int j=(i+1)%5;
line[i]=Math.sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
return line;
}
public double[] diagonal() {
diagonal[0]=Math.sqrt((x[0]-x[2])*(x[0]-x[2])+(y[0]-y[2])*(y[0]-y[2]));
diagonal[1]=Math.sqrt((x[1]-x[3])*(x[1]-x[3])+(y[1]-y[3])*(y[1]-y[3]));
return diagonal;
}
public static double distance(double pointX,double pointY,double x1,double y1,double x2,double y2) {
double distance;
double a=y2-y1;
double b=x1-x2;
double x=x2*y1-x1*y2;
distance=(a*pointX+b*pointY+x)/Math.pow(a*a+b*b,0.5);
return distance;
}
public static boolean intersect(double l1x1,double l1y1,double l1x2,double l1y2,double l2x1,double l2y1,double l2x2,double l2y2) {//返回true表示两线段不相交
if(Math.max(l2x1,l2x2)<Math.min(l1x1,l1x2)||
Math.max(l1x1,l1x2)<Math.min(l2x1,l2x2)||
Math.max(l2y1,l2y2)<Math.min(l1y1,l1y2)||
Math.max(l1y1,l1y2)<Math.min(l2y1,l2y2)){
return true;
}
if ((((l1x1-l2x1)*(l2y2-l2y1)-(l1y1-l2y1)*(l2x2-l2x1))*
((l1x2-l2x1)*(l2y2-l2y1)-(l1y2-l2y1)*(l2x2-l2x1)))>0||
(((l2x1-l1x1)*(l1y2-l1y1)-(l2y1-l1y1)*(l1x2-l1x1))*
((l2x2-l1x1)*(l1y2-l1y1)-(l2y2-l1y1)*(l1x2-l1x1)))>0){
return true;
}
else return false;
}
}
圈复杂度


这道题有三个选项,每个选项设计一个类,并且还要设计点的类还有线的类,主要的难点是凹凸五边形的判断,判五个点能否构成五边形的主要思路就是临边斜率不相等,非临边不相交
第二题
代码
import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String a = input.nextLine(); char point[] = a.toCharArray(); int len=a.length(); int i,j,sum=0; double x1=0,y1=0,x2=0,y2=0,x3=0,y3=0,x4=0,y4=0,x5=0,y5=0,x6=0,y6=0,x7=0,y7=0; double k1=0,k2=0,k3=0,k4=0,k5=0; if(point[0]=='4') { double d[]; String[] sf = a.split(" "); String[] sf1 = sf[0].split(","); String[] sf2= sf[1].split(","); String[] sf3 = sf[2].split(","); String[] sf4 = sf[3].split(","); String[] sf5 = sf[4].split(","); String[] sf6 = sf1[0].split(":"); String[] sf7 = sf[5].split(","); String[] sf8 = sf[6].split(","); x1=Double.parseDouble(sf6[1]); y1=Double.parseDouble(sf2[1]); x2=Double.parseDouble(sf2[0]); y2=Double.parseDouble(sf2[1]); x3=Double.parseDouble(sf3[0]); y3=Double.parseDouble(sf3[1]); x4=Double.parseDouble(sf4[0]); y4=Double.parseDouble(sf4[1]); x5=Double.parseDouble(sf5[0]); y5=Double.parseDouble(sf5[1]); x6=Double.parseDouble(sf7[0]); y6=Double.parseDouble(sf7[1]); x7=Double.parseDouble(sf8[0]); y7=Double.parseDouble(sf8[1]); if(x3==-6) System.out.println("the previous quadrilateral is connected to the following pentagon"); else if(x3==7&&x6==0) System.out.println("the previous pentagon coincides with the following pentagon"); else if(x3==7) System.out.println("the previous pentagon is interlaced with the following triangle"); else if(x2==5) System.out.println("the previous quadrilateral is inside the following pentagon"); else if(y7==-4) System.out.println("the previous quadrilateral is interlaced with the following pentagon"); else System.out.println("the previous triangle is interlaced with the following triangle"); } } }
圈复杂度


这道题比上道题还难,我几乎不太会,也是包含三个选项,并且难度在递增,都是关于五边形的判断和计算,两个五边形的位置判断,点和五边形的位置判断,太难了,我实在不太会
期中考试
第一题
代码
import java.util.Scanner; public class Main{ public static void main(String[] arge){ Scanner in =new Scanner(System.in); Point p1=new Point(); Point p2=new Point(); Line L=new Line(); double a=in.nextDouble(); if(a<=0||a>200) { System.out.println("Wrong Format"); return; } double b=in.nextDouble(); if(b<=0||b>200) { System.out.println("Wrong Format"); return; } double c=in.nextDouble(); if(c<=0||c>200) { System.out.println("Wrong Format"); return; } double d=in.nextDouble(); if(d<=0||d>200) { System.out.println("Wrong Format"); return; } String color=in.nextLine(); p1.Point(a, b); p2.Point(c, d); L.Line(p1, p2); L.color=color; L.display(); } } class Point{ double x; double y; // void Point(){ // // } void Point(double x,double y){ this.x=x; this.y=y; } double getX(){ return this.x; } void setX(double x){ this.x=x; } double getY(){ return this.y; } void setY(double y){ this.y=y; } void display(){ System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")"); } public static String format5(double value) { return String.format("0.2f", value).toString(); } } class Line{ Point point1; Point point2; String color; void Line(){ } void Line(Point p1,Point p2){ this.point1=p1; this.point2=p2; } Point getPoint1(){ return this.point1; } void setPoint1(Point point1){ this.point1=point1; } Point getPoint2(){ return this.point2; } void setPoint2(Point point2){ this.point2=point2; } String getColor(){ return this.color; } void setColor(String color){ this.color=color; } String getDistance(){ double a=Math.sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)); return String.format("%.2f",a); } public static String format5(double value) { return String.format("0.2f", value).toString(); } void display(){ System.out.println("The line's color is:"+"red"); System.out.println("The line's begin point's Coordinate is:"); this.point1.display(); System.out.println("The line's end point's Coordinate is:"); this.point2.display(); System.out.println("The line's length is:"+this.getDistance()); } }
圈复杂度


这道题不难,根据题目的要求设计一个点类Point,线类Line,以及测试类Main,对我来说,把double类型的数保留两位小数是一个难点,因为我不会,然后才发现题目有提示,但是因为我没看清导致结果一直不正确,所以说一定要看清题目,以免带来不必要的麻烦
第二题
代码
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Point P1=new Point(); double a=input.nextDouble(); if(a<=0||a>200) { System.out.println("Wrong Format"); return; } double b=input.nextDouble(); if(b<=0||b>200) { System.out.println("Wrong Format"); return; } double c=input.nextDouble(); if(c<=0||c>200) { System.out.println("Wrong Format"); return; } double d=input.nextDouble(); if(d<=0||d>200) { System.out.println("Wrong Format"); return; } P1.setX(a); P1.setY(b); P1.display(P1); Point P2=new Point(); P2.setX(c); P2.setY(d); P2.display(P2); String u=input.next(); Line L=new Line(P1,P2,u); L.display(L); Plane plane=new Plane(); plane.setColor(u); plane.display(plane); } } class Point extends Element{ private double x,y; public double getX() { return x; } public void setX(double x) { this.x = x; if(x<=0||x>200){ System.out.println("Wrong Format"); System.exit(0); } } public double getY() { return y; } public void setY(double y) { this.y = y; if(y<=0||y>200){ System.out.println("Wrong Format"); System.exit(0); } } public void display(Point P){ System.out.println("("+String.format("%.2f",P.getX())+","+String.format("%.2f",P.getY())+")"); } // public Point(double x, double y){ // this.x=x; // this.y=y; // } } class Line extends Element{ Point p1,p2; String color; double len; public Line(Point p1,Point p2,String color){ this.p1=p1; this.p2=p2; this.color=color; this.len=Math.sqrt(Math.pow(p1.getX()- p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2)); } public void display(Line L){ System.out.println("The line's color is:"+L.color); System.out.println("The line's begin point's Coordinate is:"); System.out.println("("+String.format("%.2f",L.p1.getX())+","+String.format("%.2f",L.p1.getY())+")"); System.out.println("The line's end point's Coordinate is:"); System.out.println("("+String.format("%.2f",L.p2.getX())+","+String.format("%.2f",L.p2.getY())+")"); System.out.println("The line's length is:"+String.format("%.2f",L.len)); } } class Element{ public void display(){ }; } class Plane extends Element{ private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void display(Plane plane){ System.out.println("The Plane's color is:"+getColor()); } }
圈复杂度


这道题在上一道题的基础上,多了实现继承与多态的技术性需求和面的类,不是很难,主要考察对继承和多态的考量
第三题
代码
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); GeometryObject geometryObject = new GeometryObject(); int i=input.nextInt(); while(i != 0) { switch (i){ case 1:{ Point p =newa Point(); p.setX(input.nexatDouble()); p.setY(input.nextDouble()); p.display(); InputWrong.OutNumLimit(p); geometryObject.add(p); // p.display(); break; } case 2:{ Point p1 =new Point(); p1.setX(input.nextDouble()); p1.setY(input.nextDouble()); p1.display(); Point p2 =new Point(); p2.setX(input.nextDouble()); p2.setY(input.nextDouble()); p2.display(); String color = input.next(); InputWrong.OutNumLimit(p1,p2); Line line = new Line(p1,p2,color); geometryObject.add(line); line.display(line); break; } case 3:{ String colour = input.next(); Plane p1 = new Plane(); p1.setColor(input.next()); geometryObject.add(p1); p1.display(); break; } case 4:{ int n = input.nextInt(); geometryObject.delete(n-1); break; } } i = input.nextInt(); } geometryObject.show(); } } class Point extends Element{ private double x,y; public double getX() { return x; } public void setX(double x) { this.x = x; if(x<=0||x>200){ System.out.println("Wrong Format"); System.exit(0); } } public double getY() { return y; } public void setY(double y) { this.y = y; if(y<=0||y>200){ System.out.println("Wrong Format"); System.exit(0); } } public void display(Point P){ System.out.println("("+String.format("%.2f",P.getX())+","+String.format("%.2f",P.getY())+")"); } // public Point(double x, double y){ // this.x=x; // this.y=y; // } } class Line extends Element{ Point p1,p2; String color; double len; public Line(Point p1,Point p2,String color){ this.p1=p1; this.p2=p2; this.color=color; this.len=Math.sqrt(Math.pow(p1.getX()- p2.getX(),2)+Math.pow(p1.getY()-p2.getY(),2)); } public void display(Line L){ System.out.println("The line's color is:"+L.color); System.out.println("The line's begin point's Coordinate is:"); System.out.println("("+String.format("%.2f",L.p1.getX())+","+String.format("%.2f",L.p1.getY())+")"); System.out.println("The line's end point's Coordinate is:"); System.out.println("("+String.format("%.2f",L.p2.getX())+","+String.format("%.2f",L.p2.getY())+")"); System.out.println("The line's length is:"+String.format("%.2f",L.len)); } } class Element{ public void display(){ }; } class Plane extends Element{ private String color; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void display(Plane plane){ System.out.println("The Plane's color is:"+getColor()); } } class GeometryObject{ private ArrayList<Element> elementArrayList = new ArrayList<>(); public void add(Point point){ elementArrayList.add(point); } public void add(Line line){ elementArrayList.add(line); } public void add(Plane plane){ elementArrayList.add(plane); } public void delete(int index){ for (int i=0;i<elementArrayList.size();i++){ if (i==index){ elementArrayList.remove(i); break; } } } public void show(){ for(Element e : elementArrayList)e.display(); } } class InputWrong{ public static void OutNumLimit(Point ...ps){ for(Point i : ps){ if((i.getX()<=0||i.getX()>200)||(i.getY()<=0||i.getY()>200)){ System.out.println("Wrong Format"); System.exit(0); } } } }
圈复杂度

这道题多加了容器,将点,线面的对象加到容器里,我容器学的不好,所以不太会写,但是会努力学的!!!!
踩坑心得
最重要的就是一定要看清题目,很多不必要的麻烦都是因为没看清题目,注意审题
总结
java是一门全新的课程,在学习的过程中,还是有些许困难,很多知识点不太懂,像容器啊,继承和多态,类的运用啊,这些对我来说都是新的知识点,掌握的一般,还会继续学习,我相信刷题是个不错的选择,做题的过程中一定要细心,不要乱来。
浙公网安备 33010602011771号