JAVA作业总结二
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)
方法。 - 类图:
作答:
-
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Point P1=new Point();
P1.setX(input.nextDouble());
P1.setY(input.nextDouble());
Point P2=new Point();
P2.setX(input.nextDouble());
P2.setY(input.nextDouble());
Line L=new Line(P1,P2, input.next());
L.display(L);
}
}
class Point{
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){}
// public Point(double x, double y){
// this.x=x;
// this.y=y;
// }
}
class Line{
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));
}
}
/*
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:长度值*/
- 题目:
7-2 点线面问题重构(继承与多态)分数 40作者 段喜龙单位 南昌航空大学
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
- 对题目中的点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();
-
类图:
作答:
//import java.util.Scanner;
//
//public class Main {
// public static void main(String[] args) {
// Scanner input = new Scanner(System.in);
// Point P1=new Point();
// P1.setX(input.nextDouble());
// P1.setY(input.nextDouble());
// Point P2=new Point();
// P2.setX(input.nextDouble());
// P2.setY(input.nextDouble());
// Line L=new Line(P1,P2, input.next());
// L.display(L);
// }
//}
//class Point{
// 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){
//
//}
//
//
//// public Point(double x, double y){
//// this.x=x;
//// this.y=y;
//// }
//}
//class Line{
// 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));
// }
//}
/*
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);
Point P1=new Point();
P1.setX(input.nextDouble());
P1.setY(input.nextDouble());
P1.display(P1);
Point P2=new Point();
P2.setX(input.nextDouble());
P2.setY(input.nextDouble());
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());
}
} - 题目:
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
- 在原有类设计的基础上,增加一个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()
- 方法进行输出。
- 在原有类设计的基础上,增加一个GeometryObject容器类,其属性为
-
- 类图:
作答:
//import java.util.Scanner;
//
//public class Main {
// public static void main(String[] args) {
// Scanner input = new Scanner(System.in);
// Point P1=new Point();
// P1.setX(input.nextDouble());
// P1.setY(input.nextDouble());
// Point P2=new Point();
// P2.setX(input.nextDouble());
// P2.setY(input.nextDouble());
// Line L=new Line(P1,P2, input.next());
// L.display(L);
// }
//}
//class Point{
// 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){
//
//}
//
//
//// public Point(double x, double y){
//// this.x=x;
//// this.y=y;
//// }
//}
//class Line{
// 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));
// }
//}
/*
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.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 =new Point();
p.setX(input.nextDouble());
p.setY(input.nextDouble());
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);
}
}
}
}
总结:主要是继承接口感觉有点难
第4.5作业总结:
题目:
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。作答:
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String sj;
double[]zb=new double[12];
int i=0,n=0;
sj=input.nextLine();
if (!sj.matches("[1-5]:.+")) { System.out.println("Wrong Format");
System.exit(0); }
switch (sj.charAt(0)){
case '1':
for(i=0;i<sj.length();i++){
if(i>sj.length()-2){
break;
}
if(sj.charAt(i)==':'||sj.charAt(i)==' '){
if(sj.charAt(i+2)=='+'||sj.charAt(i+2)=='-'){
System.out.print("Wrong Format");
System.exit(0);
}
}
if(i+3<=sj.length()&&sj.charAt(i)==','){
if(sj.charAt(i+2)=='+'||sj.charAt(i+2)=='-'){
System.out.print("Wrong Format");
System.exit(0);
}
}
}
Pattern pattern = Pattern.compile("([+-]?\\d+\\.?\\d*)(\\,)([+-]?\\d+\\.?\\d*)");
Matcher matcher = pattern.matcher(sj);
while(matcher.find()){
if(n==12){
break;
}
zb[n]=Double.parseDouble(matcher.group(1));
n++;
zb[n]=Double.parseDouble(matcher.group(3));
n++;
}
if(n!=8){
System.out.println("wrong number of points");
}else{
point d=new point();
if((d.compare(zb[0],zb[2])&&d.compare(zb[1],zb[3]))||(d.compare(zb[0],zb[4])&&d.compare(zb[1],zb[5]))||(d.compare(zb[0],zb[6])&&d.compare(zb[1],zb[7]))||(d.compare(zb[4],zb[2])&&d.compare(zb[5],zb[3]))||(d.compare(zb[2],zb[6])&&d.compare(zb[3],zb[7]))||(d.compare(zb[4],zb[6])&&d.compare(zb[5],zb[7]))){
System.out.println("points coincide");
System.exit(0);
}
Line l1=new Line(zb[0],zb[2],zb[1],zb[3]);
Line l2=new Line(zb[4],zb[6],zb[5],zb[7]);
SBX a=new SBX();
System.out.print(a.IsSBX(zb[0],zb[2],zb[4],zb[6],zb[1],zb[3],zb[5],zb[7]));
if(a.IsSBX(zb[0],zb[2],zb[4],zb[6],zb[1],zb[3],zb[5],zb[7])){
if(zb[0]==zb[2]&&zb[4]==zb[6]&&l1.len==l2.len){
System.out.print(" "+true);
System.exit(0);
}else {
System.out.print(" "+false);
System.exit(0);
}
if(l1.k==l2.k&&l1.c==l1.c&&l1.len==l2.len){
System.out.print(" "+true);
}else {
System.out.print(" "+false);
}
}
}
break;//nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
case '2':
for(i=0;i<sj.length();i++){
if(i>sj.length()-2){
break;
}
if(sj.charAt(i)==':'||sj.charAt(i)==' '){
if(sj.charAt(i+2)=='+'||sj.charAt(i+2)=='-'){
System.out.print("Wrong Format");
System.exit(0);
}
}
if(i+3<=sj.length()&&sj.charAt(i)==','){
if(sj.charAt(i+2)=='+'||sj.charAt(i+2)=='-'){
System.out.print("Wrong Format");
System.exit(0);
}
}
}
//1:-1,-1 -1,1 1,2 1,-2 1:0,0 0,80 80,80 80,0
Pattern pattern2 = Pattern.compile("([+-]?\\d+\\.?\\d*)(\\,)([+-]?\\d+\\.?\\d*)");
Matcher matcher2 = pattern2.matcher(sj);
while(matcher2.find()){
if(n==12){
break;
}
zb[n]=Double.parseDouble(matcher2.group(1));
n++;
zb[n]=Double.parseDouble(matcher2.group(3));
n++;
}
if(n!=8) {
System.out.println("wrong number of points");
System.exit(0);
}
else{
point d=new point();
if((d.compare(zb[0],zb[2])&&d.compare(zb[1],zb[3]))||(d.compare(zb[0],zb[4])&&d.compare(zb[1],zb[5]))||(d.compare(zb[0],zb[6])&&d.compare(zb[1],zb[7]))||(d.compare(zb[4],zb[2])&&d.compare(zb[5],zb[3]))||(d.compare(zb[2],zb[6])&&d.compare(zb[3],zb[7]))||(d.compare(zb[4],zb[6])&&d.compare(zb[5],zb[7]))){
System.out.println("not a quadrilateral");
System.exit(0);
}
}
Line l1=new Line(zb[0],zb[2],zb[1],zb[3]);
Line l2=new Line(zb[4],zb[6],zb[5],zb[7]);
Line l3=new Line(zb[0],zb[6],zb[1],zb[7]);
SBX a=new SBX();
if(!a.IsSBX(zb[0],zb[2],zb[4],zb[6],zb[1],zb[3],zb[5],zb[7])){
System.out.println("not a quadrilateral");
}else {
//System.out.println("k1"+l1.k+" k2"+l2.k+" d1"+l1.len+" d2"+l3.len);
if(zb[0]==zb[2]&&zb[4]==zb[6]&&l1.len==l2.len){
if(l1.len==l3.len){
System.out.print(true);
}else {
System.out.print(false);
}
if(zb[0]==zb[2]&&zb[4]==zb[6]){
System.out.print(" "+true);
}else {
System.out.print(" "+false);
}
if(l1.len==l3.len&&zb[0]==zb[2]&&zb[4]==zb[6]){
System.out.print(" "+true);
}else {
System.out.print(" "+false);
}
System.exit(0);
}else {
System.out.println(false+" "+false+" "+false);
System.exit(0);
}
if(l1.k==l2.k&&l1.c==l1.c&&l1.len==l2.len){
if(l1.len==l3.len){
System.out.print(true);
}else {
System.out.print(false);
}
if(l1.k*l3.k==-1){
System.out.print(" "+true);
}else {
System.out.print(" "+false);
}
if(l1.len==l3.len&&l1.k*l3.k==-1){
System.out.print(" "+true);
}else {
System.out.print(" "+false);
}
}else{
System.out.println(false+" "+false+" "+false);
}
}
break;//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
case '3': for(i=0;i<sj.length();i++){
if(i>sj.length()-2){
break;
}
if(sj.charAt(i)==':'||sj.charAt(i)==' '){
if(sj.charAt(i+2)=='+'||sj.charAt(i+2)=='-'){
System.out.print("Wrong Format");
System.exit(0);
}
}
if(i+3<=sj.length()&&sj.charAt(i)==','){
if(sj.charAt(i+2)=='+'||sj.charAt(i+2)=='-'){
System.out.print("Wrong Format");
System.exit(0);
}
}
}
//1:-1,-1 -1,1 1,2 1,-2 1:0,0 0,80 80,80 80,0
Pattern pattern3 = Pattern.compile("([+-]?\\d+\\.?\\d*)(\\,)([+-]?\\d+\\.?\\d*)");
Matcher matcher3 = pattern3.matcher(sj);
while(matcher3.find()){
if(n==12){
break;
}
zb[n]=Double.parseDouble(matcher3.group(1));
n++;
zb[n]=Double.parseDouble(matcher3.group(3));
n++;
}
if(n!=8) {
System.out.println("wrong number of points");
System.exit(0);
}
else{
point d=new point();
if((d.compare(zb[0],zb[2])&&d.compare(zb[1],zb[3]))||(d.compare(zb[0],zb[4])&&d.compare(zb[1],zb[5]))||(d.compare(zb[0],zb[6])&&d.compare(zb[1],zb[7]))||(d.compare(zb[4],zb[2])&&d.compare(zb[5],zb[3]))||(d.compare(zb[2],zb[6])&&d.compare(zb[3],zb[7]))||(d.compare(zb[4],zb[6])&&d.compare(zb[5],zb[7]))){
System.out.println("not a quadrilateral");
System.exit(0);
}
}
Line l31=new Line(zb[0],zb[2],zb[1],zb[3]);
Line l32=new Line(zb[4],zb[6],zb[5],zb[7]);
Line l33=new Line(zb[0],zb[6],zb[1],zb[7]);
SBX a3=new SBX();
if(!a3.IsSBX(zb[0],zb[2],zb[4],zb[6],zb[1],zb[3],zb[5],zb[7])){
System.out.println("not a quadrilateral");
}else {
sbx3 san=new sbx3(zb[0],zb[1],zb[2],zb[3],zb[4],zb[5],zb[6],zb[7]);
if(san.gimp_transform_polygon_is_convex3(zb[0],zb[2],zb[4],zb[6],zb[1],zb[3],zb[5],zb[7])){
System.out.print(true+" "+OutFormat.doubleFormat(san.c)+" "+OutFormat.doubleFormat(san.area));
}else System.out.print(false+" "+OutFormat.doubleFormat(san.c)+" "+OutFormat.doubleFormat(san.area));
}
break;
case '4':
/*
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。
如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。
若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,
输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
*/
System.out.println("not a quadrilateral or triangle");
break;
case '5':
System.out.println("in the triangle");
break;
default:System.out.println("Wrong Format");}
}
static class point{
boolean compare(double x1,double x2){
if(x1==x2)
return true;
else
return false;
}
}
static class Line {
double x1, x2, y1, y2;
double k;
double a,b,c;
double len;public Line(double x1, double x2, double y1, double y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.k = (y1 - y2) / (x1 - x2);
this.a = (y1 - y2) / (x1 - x2);
this.b=-1;
this.c=-x1* (y1 - y2) / (x1 - x2)+y1;
this.len = Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2));
}
}
}
class OutFormat{
public static Double doubleFormat(double b) {
DecimalFormat df = new DecimalFormat("#.000");
Double output = Double.valueOf(df.format(b));
return output;
}
}
class SBX {
public boolean IsSBX(double Px1, double Px2, double Px3, double Px4, double Py1, double Py2, double Py3, double Py4) //判断是否是四边形
{
if ((Py4 - Py3) * (Px4 - Px2) == (Py4 - Py2) * (Px4 - Px3))
return false;
else if ((Py4 - Py3) * (Px4 - Px1) == (Py4 - Py1) * (Px4 - Px3))
return false;
else if ((Py4 - Py2) * (Px4 - Px1) == (Py4 - Py1) * (Px4 - Px2))
return false;
else if ((Py3 - Py2) * (Px3 - Px1) == (Py3 - Py1) * (Px3 - Px2))
return false; //任意三个顶点成直线,非四边形
else
return true;
}
boolean triangle(double x1,double y1,double x2,double y2,double x3,double y3)//判断三条边是否能组成三角形
{
double a, b, c;
a = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
b = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
c =Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
if (a + b > c && a + c > b && b + c > a )return true;
else return false;
}
}
class sbx3{
double x1,x2,x3,x4,y1,y2,y3,y4;
double area,c;
public sbx3(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
{
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.x3 = x3;
this.x4 = x4;
this.y3 = y3;
this.y4 = y4;
this.area=Math.abs(x1 * y2 + x2 * y3 + x3 * y1 - y1 * x2 - y2 * x3 - y3 * x1) / 2.0+ Math.abs(x1 * y4 + x4 * y3 + x3 * y1 - y1 * x4 - y4 * x3 - y3 * x1) / 2.0;
this.c=Math.sqrt(Math.pow(x1-x2,2)+Math.pow(y1-y2,2))+Math.sqrt(Math.pow(x2-x3,2)+Math.pow(y2-y3,2))+Math.sqrt(Math.pow(x3-x4,2)+Math.pow(y3-y4,2))+Math.sqrt(Math.pow(x4-x1,2)+Math.pow(y4-y1,2));
}
boolean gimp_transform_polygon_is_convex3(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
return (((x2 - x1) * (y4 - y1) - (x4 - x1) * (y2 - y1)) * ((x4 - x1) * (y3 - y1) - (x3 - x1) * (y4 - y1)) * (((x4 - x2) * (y3 - y2) - (x3 - x2) * (y4 - y2)) * ((x3 - x2) * (y1 - y2) - (x1 - x2) * (y3 - y2))) >0);
}
}
//public class PointInputError { //判断从字符串中解析出的点的数量是否合格。
// public static void wrongNumberOfPoints(ArrayList ps, int num)
// { if (ps.size() != num) { System.out.println("wrong number of points");
// System.exit(0); } } //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
//public static void wrongPointFormat(String s) {
// if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?"))
// { System.out.println("Wrong Format"); System.exit(0); }
// } // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
//public static void wrongChoice(String s) { if (!s.matches("[1-5]:.+")) { System.out.println("Wrong Format");
// System.exit(0); }
// }
//}题目:
7-2 点线形系列4-凸四边形的计算分数 70作者 蔡轲单位 南昌航空大学用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。作答:import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[]sj=new String[20];
for(int i=0;;i++){
sj[i] = input.nextLine();
if(sj[i].equals("end"))
break;
}
String u;
int sun=0;
for(int j = 0; !sj[j].equals("end"); j++){
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(sj[j]);
while (matcher.find()) {
u = matcher.group(0);
sun+=Integer.parseInt(u);
}
System.out.println(sun);
sun=0;
}}
}
//public class yi {
// public static void main(String[] args) {
// Pattern pattern = Pattern.compile("\\d+");
// Scanner input = new Scanner(System.in);
// String sj=null;
// int sum=0;
// while(sj!="end"){
// sj=input.nextLine();
// Matcher matcher = pattern.matcher(sj);
// while(matcher.find()){
// sum+=Integer.getInteger(matcher.group(0));
// }
// }
//
// }
//
// }
//
//import java.util.Scanner;
//public class yi{
// public static void main(String []args) {
// Scanner sc =new Scanner(System.in);
// String s = null;
//
// while(s!="end") {
// int sum=0;
// double zb;
// int flag=0;
// String u;
// s=sc.nextLine();
// if(s.equals("end")) {
// flag=1;
// }
//
// Pattern pattern = Pattern.compile("\\d+");
// Matcher matcher = pattern.matcher(s);
// while(matcher.find()){
// u=matcher.group(0);
// System.out.println(u);
// zb=Integer.parseInt(u);
// System.out.println(zb);
// }
//// if(flag==0)
//// System.out.println(sum);
//
// }
//
// }
//}总结:4、5次作业主要考四边形五边形应用结合数学难度挺大希望自己可以再一次深层领悟他
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具