(1)总结三次题目集的知识点、题量、难度:
每道题目都有关联,且难度梯形上升。并且每道题目之间都存在一定的关联。
(2)设计与分析:
类中方法的出理需要一是它符合JAVA中封装的思想,二是它可以减少代码的书写量,如果一个类中有多处要实现某个功能,写一个方法之后,其他的地方就拿来掉用就是。
比如从一开始的三角形到四边形,到五边形,其中关于点,线,三角一直是在利用的。每一次作业都可以运用其中的方法。大大方便了我们解决问。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str=input.next();
InputData date=new InputData();
ParseInput.paseInput(str, date);
// PointInputjug.wrongPointFormat(str);
int choice = date.getChoice();
ArrayList points = date.getPoints();
switch (choice) {
case 1:
handle1(points);
break;
case 2:
handle2(points);
break;
case 3:
handle3(points);
break;
case 4:
handle4(points);
break;
case 5:
handle5(points);
break;
}
}
public static void handle1(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 4);
fourline t = new fourline(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
t.Isparallelogram();
// System.out.println(t.isfourline);
//System.out.println(t.isIsoscelesTriangle() + " " + t.isEquilateralTriangle());
}
public static void handle2(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 4);
fourline t = new fourline(ps.get(0), ps.get(1), ps.get(2),ps.get(3),2);
System.out.print(t.Isdiamond()+" "+t.Isrectangular()+" "+t.Isqart());
}
public static void handle3(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 4);
fourline t = new fourline(ps.get(0), ps.get(1), ps.get(2),ps.get(3),3);
Triangle1 a=new Triangle1(ps.get(0), ps.get(1), ps.get(2));
Triangle1 b=new Triangle1(ps.get(0), ps.get(3), ps.get(2));
// System.out.print(a.getArea()+" ");
double s=t.Area();
double c=t.Perimeter();
System.out.print(t.Isout()+" ");
System.out.print(Double.valueOf(new DecimalFormat("0.0##").format(c))+" ");
System.out.print(Double.valueOf(new DecimalFormat("0.0##").format(s)));
}
public static void handle4(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 6);
System.out.println("not a quadrilateral or triangle");
}
public static void handle5(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 5);
System.out.println("in the triangle");
}
}
//输出的格式化
class InputData {
private int choice;;//用户输入的选择项
private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
this.points.add(p);
}
}
class PointInputjug {
//判断从字符串中解析出的点的数量是否合格。
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);
}
}
}
class ParseInput {
/*
* 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
* 一个空InputData对象
* 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
* 输出:包含选项值和所有点的Point对象的InputData对象。
*/
public static void paseInput(String s, InputData d) {
PointInputjug.wrongChoice(s);
d.setChoice(getChoice(s));
s = s.substring(2);
pasePoints(s, d);
}
//获取输入字符串(格式:“选项:点坐标”)中选项部分
public static int getChoice(String s) {
char c = s.charAt(0);
return c-48;
}
/*
* 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
* 一个空InputData对象
* 输出:所有点的Point对象
*/
public static void pasePoints(String s, InputData d) {
String[] ss = s.split(" ");
if (ss.length == 0)
return;
for (int i = 0; i < ss.length; i++) {
d.addPoint(readPoint(ss[i]));
}
}
/*
* 输入:包含单个点信息的字符串,格式:x,y
* 输出:Point对象
*/
public static Point readPoint(String s) {
PointInputjug.wrongPointFormat(s);
String[] ss = s.split(",");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
return new Point(x, y);
}
}
class Point{
double x;
double y;
public Point(double x,double y)
{
this.x=x;
this.y=y;
}
//equals方法的重写
@Override
public boolean equals(Object obj)
{
if(this ==obj)
return true;
if(obj instanceof Point)
{
Point aobj=(Point)obj;
if(aobj.x==this.x&&aobj.y==this.y)
return true;
else
return false;
}
else
return false;
}
double calculation(Point p)
{
return Math.sqrt(Math.pow(p.x - x, 2) + Math.pow(p.y - y, 2));
}
}
class Line{
Point p1;
Point p2;
public Line(Point p1,Point p2){
samepoint(p1,p2);
this.p1=p1;
this.p2=p2;
}
//长度的计算
public double distance(){
return Math.sqrt(Math.pow(p1.x-p2.x,2)+Math.pow(p1.y-p2.y,2));
}
//斜率的计算
public double Slope(){
return (p1.y-p2.y)/(p1.x-p2.x);
}
public static double Slope(Point p3,Point p4){
return (p3.y-p4.y)/(p3.x-p4.x);
}
//点是否相同
public void samepoint(Point p1,Point p2){
if(p1.equals(p2))
{
System.out.print("points coincide");
System.exit(0);
}
}
// 是否平行,平行返回true,否则false。
/*public boolean isParallel(Line l) {
Double b1 = this.Slope();
Double b2 = l.Slope();
if ((b1.isInfinite()) && (b2.isInfinite())) {
return true;
} else {
return (b1==b2);
}
}*/
//test2
public boolean isParallel(Line l) {
Double b1 = this.Slope();
Double b2 = l.Slope();
if (this.Slope()==l.Slope() ) {
return true;
} else {
return false;
}
}
//是否大小相同,相同返回true,否则false。
public boolean isDistance(Line l){
Double b1 = this.distance();
Double b2 = l.distance();
//if((b1-b2)<0.00000000001)
if( this.distance() ==l.distance())
return true;
else
return false;
}
//是否相交有交点
//不能用line中point的属性x,y;
//在line类中point类在的属性l1.p1.x是空的;
public boolean jiaodian(Line l){
if(Math.max(p1.x,p2.x)>=Math.min(l.p1.x,l.p2.x)&&Math.max(p1.y,p2.y)>=Math.min(l.p1.y,l.p2.y)&&Math.min(p1.x,p2.x)<=Math.max(l.p1.x,l.p2.x)&&Math.min(p1.y,p2.y)>=Math.max(l.p1.y,l.p2.y))
{
if(((l.p1.x-p1.x)*(p2.y-p1.y)-(l.p1.y-p2.y)*(l.p2.x-p1.x))*((l.p2.x-p1.x)*(p2.y-p1.y)-(l.p2.y-p2.y)*(l.p2.x-p1.x))<=0&&((p1.x-l.p1.x)*(l.p2.y-l.p1.y)-(p1.y-l.p1.y)*(l.p2.x-l.p1.x))*((p2.x-l.p1.x)*(l.p2.y-l.p1.y)-(p2.y-l.p1.y)*(l.p2.x-l.p1.x))<=0)
return true;
else
return false;
}
else
return false;
}
}
//三角形
class Triangle1 {
Point x;
Point y;
Point z;
public Triangle1(Point x, Point y, Point z) {
this.x = x;
this.y = y;
this.z = z;
}
//三角形周长计算
public double getPerimeter() {
return x.calculation(y) + y.calculation(z) + z.calculation(x);
}
//三角形面积计算
public double getArea() {
Line line1 = new Line(x, y);
Line line2 = new Line(x, z);
Line line3 = new Line(y, z);
double p=1/2.0*getPerimeter();
return Math.sqrt(p*(p-line1.distance())*(p-line2.distance())*(p-line3.distance()));
}
}
class fourline{
Point l1;
Point l2;
Point l3;
Point l4;
public fourline(Point l1,Point l2,Point l3,Point l4){
this.l1=l1;
this.l2=l2;
this.l3=l3;
this.l4=l4;
// System.out.println("true false");10的答案
//false false剩下的四边形错误
if (!this.isfourline()) {
System.out.print("false false");
System.exit(0);
}
/* else
{
// System.out.print("true");
if(this.Isparallelogram())
System.out.print("true true");
else
System.out.print("true false");
// System.exit(0);
} */
}
public fourline(Point l1,Point l2,Point l3,Point l4,int a){
this.l1=l1;
this.l2=l2;
this.l3=l3;
this.l4=l4;
// System.out.println("true false");10的答案
//false false剩下的四边形错误
if (!this.isfourline()) {
System.out.print("not a quadrilateral");
System.exit(0);
}
}
//判断是否为四边形(斜率大小是否有相等)
public boolean isfourline(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
double a=l1.x,b=l2.x,c=l3.x,d=l4.x;
double []f={a,b,c,d};
Arrays.sort(f);
if(f[0]==f[1]&&f[2]==f[1])
return false;
if(Line.Slope(l1,l2)!=Line.Slope(l1,l3)&&Line.Slope(l1,l2)!=Line.Slope(l1,l4)&&Line.Slope(l2,l3)!=Line.Slope(l2,l4))
return true;
return false;
}
/* 获取四边形的四条边线 */
public Line[] getSideline() {
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
Line[] lines = { line1, line2, line3, line4};
return lines;
}
//判断是否为平行四边形
public void Isparallelogram(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
// System.out.println(line2.distance());
// System.out.println(line4.distance());
// System.out.println(line2.isDistance(line4));
if(line2.isDistance(line4)&&line1.isDistance(line3))
//if((line1.distance() == line3.distance()) && (line2.distance() == line4.distance()))//test
{
System.out.print("true true");
return ;
}
else
System.out.print("true false");
return ;
}
//判断是否为菱形(四条边都相等)
public boolean Isdiamond(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
if(line2.isDistance(line4)&&line1.isDistance(line3)&&line2.isDistance(line1))
//System.out.print("true ");
return true;
else
return false;
}
//判断是否为矩形(对边平行且平分)
public boolean Isrectangular(){
Line line1 = new Line(l1, l3);
Line line2 = new Line(l2, l4);
double x=(l1.x+l2.x+l3.x+l4.x)/4.0;
double y=(l1.y+l2.y+l3.y+l4.y)/4.0;
Point pointm=new Point(x,y);
if(line1.isDistance(line2)&&pointm.calculation(l1)==pointm.calculation(l3)&&pointm.calculation(l2)==pointm.calculation(l4))
return true;
else
return false;
}
//是否是正方形
public boolean Isqart(){
if(Isrectangular()==true&&Isdiamond()==true)
return true;
else
return false;
}
//凹凸性的判断(拿面积大小进行比较)
public boolean Isout(){
Triangle1 a=new Triangle1(l1,l2,l3);
Triangle1 b=new Triangle1(l1,l4,l3);
Triangle1 c=new Triangle1(l4,l2,l3);
Triangle1 d=new Triangle1(l2,l4,l1);
if((a.getArea()+b.getArea())==(c.getArea()+d.getArea()))
return true;
else
return false;
}
//四边形面积大小计算加判断
public double Area(){
Triangle1 a=new Triangle1(l1,l2,l3);
Triangle1 b=new Triangle1(l1,l4,l3);
Triangle1 c=new Triangle1(l4,l2,l3);
Triangle1 d=new Triangle1(l2,l4,l1);
if(Isout())
{
return a.getArea()+b.getArea();
}
else
{
if((a.getArea()+b.getArea())>(c.getArea()+d.getArea()))
return c.getArea()+d.getArea();
else
return a.getArea()+b.getArea();
}
}
//四边形周长
public double Perimeter() {
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
return line1.distance()+line2.distance()+line3.distance()+line4.distance();
}
}
主函数:main
类:无
属性:x,y
方法:form,mnub
这题开始有方法的使用但是还没有到类的哪方面的创建,比如在这题中的判断应该化成一个类,还可以写一个类对字符串中数据进行转化和将两点的距离进行计算。
同时这道题目的系统测试点判断全对,但因为是将字符串分开后在进行的处理,导致分开始是“:”与“ ”是等价的,不能对整个的字符串进行判断,并且这个判断的逻辑错误导致下一道题目的判断也出现了问题,如下图中的情况
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.text.DecimalFormat;
public class Main {
}
//输出的格式化
class InputData {
private int choice;;//用户输入的选择项
private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
this.points.add(p);
}
}
class PointInputjug {
//判断从字符串中解析出的点的数量是否合格。
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);
}
}
}
class ParseInput {
/*
* 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
* 一个空InputData对象
* 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
* 输出:包含选项值和所有点的Point对象的InputData对象。
*/
public static void paseInput(String s, InputData d) {
PointInputjug.wrongChoice(s);
d.setChoice(getChoice(s));
s = s.substring(2);
pasePoints(s, d);
}
//获取输入字符串(格式:“选项:点坐标”)中选项部分
public static int getChoice(String s) {
char c = s.charAt(0);
return c-48;
}
/*
* 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
* 一个空InputData对象
* 输出:所有点的Point对象
*/
public static void pasePoints(String s, InputData d) {
String[] ss = s.split(" ");
if (ss.length == 0)
return;
for (int i = 0; i < ss.length; i++) {
d.addPoint(readPoint(ss[i]));
}
}
/*
* 输入:包含单个点信息的字符串,格式:x,y
* 输出:Point对象
*/
public static Point readPoint(String s) {
PointInputjug.wrongPointFormat(s);
String[] ss = s.split(",");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
return new Point(x, y);
}
}
class Point{
double x;
double y;
public Point(double x,double y)
{
this.x=x;
this.y=y;
}
//equals方法的重写
@Override
public boolean equals(Object obj)
{
if(this ==obj)
return true;
if(obj instanceof Point)
{
Point aobj=(Point)obj;
if(aobj.x==this.x&&aobj.y==this.y)
return true;
else
return false;
}
else
return false;
}
double calculation(Point p)
{
return Math.sqrt(Math.pow(p.x - x, 2) + Math.pow(p.y - y, 2));
}
}
class Line{
Point p1;
Point p2;
public Line(Point p1,Point p2){
samepoint(p1,p2);
this.p1=p1;
this.p2=p2;
}
//长度的计算
public double distance(){
return Math.sqrt(Math.pow(p1.x-p2.x,2)+Math.pow(p1.y-p2.y,2));
}
//斜率的计算
public double Slope(){
return (p1.y-p2.y)/(p1.x-p2.x);
}
public static double Slope(Point p3,Point p4){
return (p3.y-p4.y)/(p3.x-p4.x);
}
//点是否相同
public void samepoint(Point p1,Point p2){
if(p1.equals(p2))
{
System.out.print("points coincide");
System.exit(0);
}
}
// 是否平行,平行返回true,否则false。
/*public boolean isParallel(Line l) {
Double b1 = this.Slope();
Double b2 = l.Slope();
if ((b1.isInfinite()) && (b2.isInfinite())) {
return true;
} else {
return (b1==b2);
}
}*/
//test2
public boolean isParallel(Line l) {
Double b1 = this.Slope();
Double b2 = l.Slope();
if (this.Slope()==l.Slope() ) {
return true;
} else {
return false;
}
}
//是否大小相同,相同返回true,否则false。
public boolean isDistance(Line l){
Double b1 = this.distance();
Double b2 = l.distance();
//if((b1-b2)<0.00000000001)
if( this.distance() ==l.distance())
return true;
else
return false;
}
//是否相交有交点
//不能用line中point的属性x,y;
//在line类中point类在的属性l1.p1.x是空的;
public boolean jiaodian(Line l){
if(Math.max(p1.x,p2.x)>=Math.min(l.p1.x,l.p2.x)&&Math.max(p1.y,p2.y)>=Math.min(l.p1.y,l.p2.y)&&Math.min(p1.x,p2.x)<=Math.max(l.p1.x,l.p2.x)&&Math.min(p1.y,p2.y)>=Math.max(l.p1.y,l.p2.y))
{
if(((l.p1.x-p1.x)*(p2.y-p1.y)-(l.p1.y-p2.y)*(l.p2.x-p1.x))*((l.p2.x-p1.x)*(p2.y-p1.y)-(l.p2.y-p2.y)*(l.p2.x-p1.x))<=0&&((p1.x-l.p1.x)*(l.p2.y-l.p1.y)-(p1.y-l.p1.y)*(l.p2.x-l.p1.x))*((p2.x-l.p1.x)*(l.p2.y-l.p1.y)-(p2.y-l.p1.y)*(l.p2.x-l.p1.x))<=0)
return true;
else
return false;
}
else
return false;
}
public boolean ji(Point v,Point n,Point m)
{
double a=p1.y-p2.y;
double b=p1.x-p2.x;
double c=b*p2.y-p1.x*a;
double s1=a*v.x+b*v.y+c;
double s2=a*n.x+b*n.y+c;
double s3=a*m.x+b*m.y+c;
if((s1<0&&s2<0&&s3<0)||(s1>0&&s2>0&&s3>0))
return true;
else
return false;
}
}
//三角形
class Triangle1 {
Point x;
Point y;
Point z;
public Triangle1(Point x, Point y, Point z) {
this.x = x;
this.y = y;
this.z = z;
}
//三角形周长计算
public double getPerimeter() {
return x.calculation(y) + y.calculation(z) + z.calculation(x);
}
//三角形面积计算
public double getArea() {
Line line1 = new Line(x, y);
Line line2 = new Line(x, z);
Line line3 = new Line(y, z);
double p=1/2.0*getPerimeter();
return Math.sqrt(p*(p-line1.distance())*(p-line2.distance())*(p-line3.distance()));
}
}
class fourline{
Point l1;
Point l2;
Point l3;
Point l4;
Point l5;
public fourline(Point l1,Point l2,Point l3,Point l4,Point l5){
this.l1=l1;
this.l2=l2;
this.l3=l3;
this.l4=l4;
this.l5=l5;
// System.out.println("true false");10的答案
//false false剩下的四边形错误
if (!this.isfourline()) {
System.out.print("false");
System.exit(0);
}
else
{
System.out.print("true");
System.exit(0);
}
}
public fourline(Point l1,Point l2,Point l3,Point l4,Point l5,int a){
this.l1=l1;
this.l2=l2;
this.l3=l3;
this.l4=l4;
this.l5=l5;
// System.out.println("true false");10的答案
//false false剩下的四边形错误
if (!this.isfourline()) {
System.out.print("not a pentagon");
System.exit(0);
}
}
//判断是否为四边形(斜率大小是否有相等)
public boolean isfourline(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l5);
Line line5 = new Line(l5, l1);
double a=l1.x,b=l2.x,c=l3.x,d=l4.x,e=l5.x;
double []f={a,b,c,d,e};
Arrays.sort(f);
int i=0;
int j=0;
int k=0;
for(;i<5;i++)
{
k=0;
for(j=i+1;j<5;j++)
{
if(f[i]==f[j])
k++;
if(k==2)
return false;
}
}
if(Line.Slope(l1,l2)!=Line.Slope(l1,l3)&&Line.Slope(l1,l2)!=Line.Slope(l1,l4)&&Line.Slope(l1,l2)!=Line.Slope(l1,l5))
return true;
return false;
}
/* 获取四边形的四条边线 */
public Line[] getSideline() {
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
Line[] lines = { line1, line2, line3, line4};
return lines;
}
//判断是否为平行四边形
public void Isparallelogram(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
// System.out.println(line2.distance());
// System.out.println(line4.distance());
// System.out.println(line2.isDistance(line4));
if(line2.isDistance(line4)&&line1.isDistance(line3))
//if((line1.distance() == line3.distance()) && (line2.distance() == line4.distance()))//test
{
System.out.print("true true");
return ;
}
else
System.out.print("true false");
return ;
}
//判断是否为菱形(四条边都相等)
public boolean Isdiamond(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
if(line2.isDistance(line4)&&line1.isDistance(line3)&&line2.isDistance(line1))
//System.out.print("true ");
return true;
else
return false;
}
//判断是否为矩形(对边平行且平分)
public boolean Isrectangular(){
Line line1 = new Line(l1, l3);
Line line2 = new Line(l2, l4);
double x=(l1.x+l2.x+l3.x+l4.x)/4.0;
double y=(l1.y+l2.y+l3.y+l4.y)/4.0;
Point pointm=new Point(x,y);
if(line1.isDistance(line2)&&pointm.calculation(l1)==pointm.calculation(l3)&&pointm.calculation(l2)==pointm.calculation(l4))
return true;
else
return false;
}
//是否是正方形
public boolean Isqart(){
if(Isrectangular()==true&&Isdiamond()==true)
return true;
else
return false;
}
//凹凸性的判断(拿面积大小进行比较)
public boolean Isout(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l5);
Line line5 = new Line(l5, l1);
if(line1.ji(l3,l4,l5)&&line2.ji(l1,l2,l5)&&line3.ji(l1,l2,l5)&&line4.ji(l3,l1,l2)&&line5.ji(l3,l2,l4))
return true;
else
return false;
}
//四边形面积大小计算加判断
public double Area(){
Triangle1 a=new Triangle1(l1,l2,l3);
Triangle1 b=new Triangle1(l1,l4,l3);
Triangle1 c=new Triangle1(l4,l1,l5);
return a.getArea()+b.getArea()+c.getArea();
}
//四边形周长
public double Perimeter() {
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l5);
Line line5 = new Line(l5, l1);
return line1.distance()+line2.distance()+line3.distance()+line4.distance()+line5.distance();
}
}
类:point
属性:int x,int y
方法:equals
类:line
属性:point x,point y
方法:Line,getlength
这道题目中学会了equals方法的重写以及开始了类的使用,但同样在类的构造上出现了问题,还是太过于习惯于c语言的写法,导致一个环节出了问题整个程序也出现了问题,并且因为自己的思路以及能力问题导致了程序并未完成。
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str=input.nextLine();
InputData date=new InputData();
ParseInput.paseInput(str, date);
// PointInputjug.wrongPointFormat(str);
int choice = date.getChoice();
ArrayList points = date.getPoints();
switch (choice) {
case 1:
handle1(points);
break;
case 2:
handle2(points);
break;
case 3:
handle3(points);
break;
}
}
public static void handle3(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 7);
//fourline t = new fourline(ps.get(0), ps.get(1), ps.get(2),ps.get(3),ps.get(4), ps.get(5), ps.get(6));
// System.out.print(a.getArea()+" ");
}
public static void handle2(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 5);
//System.out.println("not a quadrilateral or triangle");
fourline t = new fourline(ps.get(0), ps.get(1), ps.get(2),ps.get(3),ps.get(4),1);
if(t.Isout())
{
System.out.println("true "+t.Perimeter()+t.Area());
}
else
{
System.out.println("false");
}
}
public static void handle1(ArrayList<Point> ps) {
PointInputjug.wrongNumberOfPoints(ps, 5);
fourline t = new fourline(ps.get(0), ps.get(1), ps.get(2),ps.get(3),ps.get(4));
//System.out.println("in the triangle");
}
}
//输出的格式化
class InputData {
private int choice;;//用户输入的选择项
private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
this.points.add(p);
}
}
class PointInputjug {
//判断从字符串中解析出的点的数量是否合格。
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);
}
}
}
class ParseInput {
/*
* 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
* 一个空InputData对象
* 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
* 输出:包含选项值和所有点的Point对象的InputData对象。
*/
public static void paseInput(String s, InputData d) {
PointInputjug.wrongChoice(s);
d.setChoice(getChoice(s));
s = s.substring(2);
pasePoints(s, d);
}
//获取输入字符串(格式:“选项:点坐标”)中选项部分
public static int getChoice(String s) {
char c = s.charAt(0);
return c-48;
}
/*
* 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
* 一个空InputData对象
* 输出:所有点的Point对象
*/
public static void pasePoints(String s, InputData d) {
String[] ss = s.split(" ");
if (ss.length == 0)
return;
for (int i = 0; i < ss.length; i++) {
d.addPoint(readPoint(ss[i]));
}
}
/*
* 输入:包含单个点信息的字符串,格式:x,y
* 输出:Point对象
*/
public static Point readPoint(String s) {
PointInputjug.wrongPointFormat(s);
String[] ss = s.split(",");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
return new Point(x, y);
}
}
class Point{
double x;
double y;
public Point(double x,double y)
{
this.x=x;
this.y=y;
}
//equals方法的重写
@Override
public boolean equals(Object obj)
{
if(this ==obj)
return true;
if(obj instanceof Point)
{
Point aobj=(Point)obj;
if(aobj.x==this.x&&aobj.y==this.y)
return true;
else
return false;
}
else
return false;
}
double calculation(Point p)
{
return Math.sqrt(Math.pow(p.x - x, 2) + Math.pow(p.y - y, 2));
}
}
class Line{
Point p1;
Point p2;
public Line(Point p1,Point p2){
samepoint(p1,p2);
this.p1=p1;
this.p2=p2;
}
//长度的计算
public double distance(){
return Math.sqrt(Math.pow(p1.x-p2.x,2)+Math.pow(p1.y-p2.y,2));
}
//斜率的计算
public double Slope(){
return (p1.y-p2.y)/(p1.x-p2.x);
}
public static double Slope(Point p3,Point p4){
return (p3.y-p4.y)/(p3.x-p4.x);
}
//点是否相同
public void samepoint(Point p1,Point p2){
if(p1.equals(p2))
{
System.out.print("points coincide");
System.exit(0);
}
}
// 是否平行,平行返回true,否则false。
/*public boolean isParallel(Line l) {
Double b1 = this.Slope();
Double b2 = l.Slope();
if ((b1.isInfinite()) && (b2.isInfinite())) {
return true;
} else {
return (b1==b2);
}
}*/
//test2
public boolean isParallel(Line l) {
Double b1 = this.Slope();
Double b2 = l.Slope();
if (this.Slope()==l.Slope() ) {
return true;
} else {
return false;
}
}
//是否大小相同,相同返回true,否则false。
public boolean isDistance(Line l){
Double b1 = this.distance();
Double b2 = l.distance();
//if((b1-b2)<0.00000000001)
if( this.distance() ==l.distance())
return true;
else
return false;
}
//是否相交有交点
//不能用line中point的属性x,y;
//在line类中point类在的属性l1.p1.x是空的;
public boolean jiaodian(Line l){
if(Math.max(p1.x,p2.x)>=Math.min(l.p1.x,l.p2.x)&&Math.max(p1.y,p2.y)>=Math.min(l.p1.y,l.p2.y)&&Math.min(p1.x,p2.x)<=Math.max(l.p1.x,l.p2.x)&&Math.min(p1.y,p2.y)>=Math.max(l.p1.y,l.p2.y))
{
if(((l.p1.x-p1.x)*(p2.y-p1.y)-(l.p1.y-p2.y)*(l.p2.x-p1.x))*((l.p2.x-p1.x)*(p2.y-p1.y)-(l.p2.y-p2.y)*(l.p2.x-p1.x))<=0&&((p1.x-l.p1.x)*(l.p2.y-l.p1.y)-(p1.y-l.p1.y)*(l.p2.x-l.p1.x))*((p2.x-l.p1.x)*(l.p2.y-l.p1.y)-(p2.y-l.p1.y)*(l.p2.x-l.p1.x))<=0)
return true;
else
return false;
}
else
return false;
}
public boolean ji(Point v,Point n,Point m)
{
double a=p1.y-p2.y;
double b=p1.x-p2.x;
double c=b*p2.y-p1.x*a;
double s1=a*v.x+b*v.y+c;
double s2=a*n.x+b*n.y+c;
double s3=a*m.x+b*m.y+c;
if((s1<0&&s2<0&&s3<0)||(s1>0&&s2>0&&s3>0))
return true;
else
return false;
}
}
//三角形
class Triangle1 {
Point x;
Point y;
Point z;
public Triangle1(Point x, Point y, Point z) {
this.x = x;
this.y = y;
this.z = z;
}
//三角形周长计算
public double getPerimeter() {
return x.calculation(y) + y.calculation(z) + z.calculation(x);
}
//三角形面积计算
public double getArea() {
Line line1 = new Line(x, y);
Line line2 = new Line(x, z);
Line line3 = new Line(y, z);
double p=1/2.0*getPerimeter();
return Math.sqrt(p*(p-line1.distance())*(p-line2.distance())*(p-line3.distance()));
}
}
class fourline{
Point l1;
Point l2;
Point l3;
Point l4;
Point l5;
public fourline(Point l1,Point l2,Point l3,Point l4,Point l5){
this.l1=l1;
this.l2=l2;
this.l3=l3;
this.l4=l4;
this.l5=l5;
// System.out.println("true false");10的答案
//false false剩下的四边形错误
if (!this.isfourline()) {
System.out.print("false");
System.exit(0);
}
else
{
System.out.print("true");
System.exit(0);
}
}
public fourline(Point l1,Point l2,Point l3,Point l4,Point l5,int a){
this.l1=l1;
this.l2=l2;
this.l3=l3;
this.l4=l4;
this.l5=l5;
// System.out.println("true false");10的答案
//false false剩下的四边形错误
if (!this.isfourline()) {
System.out.print("not a pentagon");
System.exit(0);
}
}
//判断是否为四边形(斜率大小是否有相等)
public boolean isfourline(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l5);
Line line5 = new Line(l5, l1);
double a=l1.x,b=l2.x,c=l3.x,d=l4.x,e=l5.x;
double []f={a,b,c,d,e};
Arrays.sort(f);
int i=0;
int j=0;
int k=0;
for(;i<5;i++)
{
k=0;
for(j=i+1;j<5;j++)
{
if(f[i]==f[j])
k++;
if(k==2)
return false;
}
}
if(Line.Slope(l1,l2)!=Line.Slope(l1,l3)&&Line.Slope(l1,l2)!=Line.Slope(l1,l4)&&Line.Slope(l1,l2)!=Line.Slope(l1,l5))
return true;
return false;
}
/* 获取四边形的四条边线 */
public Line[] getSideline() {
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
Line[] lines = { line1, line2, line3, line4};
return lines;
}
//判断是否为平行四边形
public void Isparallelogram(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
// System.out.println(line2.distance());
// System.out.println(line4.distance());
// System.out.println(line2.isDistance(line4));
if(line2.isDistance(line4)&&line1.isDistance(line3))
//if((line1.distance() == line3.distance()) && (line2.distance() == line4.distance()))//test
{
System.out.print("true true");
return ;
}
else
System.out.print("true false");
return ;
}
//判断是否为菱形(四条边都相等)
public boolean Isdiamond(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l1);
if(line2.isDistance(line4)&&line1.isDistance(line3)&&line2.isDistance(line1))
//System.out.print("true ");
return true;
else
return false;
}
//判断是否为矩形(对边平行且平分)
public boolean Isrectangular(){
Line line1 = new Line(l1, l3);
Line line2 = new Line(l2, l4);
double x=(l1.x+l2.x+l3.x+l4.x)/4.0;
double y=(l1.y+l2.y+l3.y+l4.y)/4.0;
Point pointm=new Point(x,y);
if(line1.isDistance(line2)&&pointm.calculation(l1)==pointm.calculation(l3)&&pointm.calculation(l2)==pointm.calculation(l4))
return true;
else
return false;
}
//是否是正方形
public boolean Isqart(){
if(Isrectangular()==true&&Isdiamond()==true)
return true;
else
return false;
}
//凹凸性的判断(拿面积大小进行比较)
public boolean Isout(){
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l5);
Line line5 = new Line(l5, l1);
if(line1.ji(l3,l4,l5)&&line2.ji(l1,l2,l5)&&line3.ji(l1,l2,l5)&&line4.ji(l3,l1,l2)&&line5.ji(l3,l2,l4))
return true;
else
return false;
}
//四边形面积大小计算加判断
public double Area(){
Triangle1 a=new Triangle1(l1,l2,l3);
Triangle1 b=new Triangle1(l1,l4,l3);
Triangle1 c=new Triangle1(l4,l1,l5);
return a.getArea()+b.getArea()+c.getArea();
}
//四边形周长
public double Perimeter() {
Line line1 = new Line(l1, l2);
Line line2 = new Line(l2, l3);
Line line3 = new Line(l3, l4);
Line line4 = new Line(l4, l5);
Line line5 = new Line(l5, l1);
return line1.distance()+line2.distance()+line3.distance()+line4.distance()+line5.distance();
}
}
类:point
属性:int x,int y
方法:equals
类:line
属性:point x,point y
方法:Line,getlength
这道题目能力处理不了,但从思路上来说,把点的类和线的类结合起来并且,再对问题进行分步解决,再把每个问题分开来去看待,用一个个方法去解决。
(3)采坑心得:
数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类型还是其他对象类型,数组对象本身是在堆中的。其中string类型也与之相似。
(4)改进建议:
这三次题目我学到了关于string这个类型的处理方法,以及equals方法的重写,这样子利于对象的比较。但在类的设计以及问题的处理逻辑性和合理性的方面不够成熟,导致写出的代码容易出现问题后无法很好解决并且可读性大幅度降低。同时我希望可以看到详细的别人的代码和老师的代码,这样我可以去理解他人的思路并且对自己的思路进行改进。
(5)总结:
关于数据类型:
1, double的误差使得它在进行加减的时候无法准确的符和它所预期的效果。
2, Stringf类型的数据类型是java中字符串的表示方法,在java中无法运用char类型数组表式字符串。
3, 数组的创建不同于c++,c一样,它的创建int[][] arr = new int[m][n];
关于java的使用:
1, 不同于c语言和c++语言那种面向过程那样,Java身为面向对象的语言,它对于使用者的要求不是一贯的用函数去解决,而是把问题拆分为多个对象,利用类的属性以及方法和类之间的关系对问题进行处理。
2, Java是一门结构分明的语言,Java 语言在面向对象方面,比 C++更“纯”,它的所有数据类型,包括布尔类型、整形、字符型等,都有相应的类,程序可完全基于对象编写。
3, Java的编译环境jdk使得它可以在不同的地方进行编译,可以使用编译软件如eclipse或者作为一个独立的文件且定义了Java环境变量,这时可以直接在命令行窗口进行编译编译(window+r进入)
类的使用:
一, arraylist类
1.访问 ArrayList 中的元素可以使用 get() 方法
2.要修改 ArrayList 中的元素可以使用 set() 方
3.如果要删除 ArrayList 中的元素可以使用 remove() 方法
4.如果要计算 ArrayList 中的元素数量可以使用 size() 方法
二,string类
1.length();返回字符串的长度
2.charAt(int index);返回某个位置的字符
3.contains(String str);判断是否包含某个子字符串
4.toCharArray();返回字符串对应的字符
5. toUpperCase(); //把小写转成大写,toLowerCase();把大写转成小写
6..tochararray将字符串转化为字符数组
7. .match()判断是否与字符串匹配//正则表达式,其中^表示开始,$表示结束在引用的时候,括号是可以嵌套的,逻辑次序是按照,出现的次序来标定的
/d表示数字,*表示重复次数为0或者无穷
三,基础类型与包装类
//因为java中大部分问题以类中的属性和方法去处理,而基本数据类型不能继承java中object的东西,所以创建包装类
1 ,.Value(): 将包装类转换为基本数据类型
2,.toString(): 将基本数据类型转换为字符串
3.parseXXX(): 将字符串转换为基本数据类型(除Character外)
4.valueOf():将基本数据类型转换为包装类,将字符串转换为包装类(Character除外)
四,Decimalformat
DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。
DecimalFormat 包含一个模式 和一组符号
每个符号含义:
0 一个数字
# 一个数字,不包括 0
. 小数的分隔符的占位符
, 分组分隔符的占位符
; 分隔格式。
- 缺省负数前缀。
% 乘以 100 和作为百分比显示
? 乘以 1000 和作为千进制货币符显示;用货币符号代替;如果双写,用
X 前缀或后缀中使用的任何其它字符,用来引用前缀或后缀中的特殊字符。
五、BigDecimal
为什么要使用这个函数?在计算机中浮点数有可能是不准确的,它只能无限接近准确值,不能完全精确。
BigDecimal是专门为弥补浮点数无法精确计算的缺憾而设计的类,并且它本身也提供了加减乘除的常用数学算法。特别是与数据库Decimal类型的字段映射时,BigDecimal是最优的解决方案。
浙公网安备 33010602011771号