Blog4-5月总结

在本月的学习中我们主要进行了继承与多态,接口和链表的练习,在此期间老师发布了多次相关练习,通过这些练习我逐渐开始对这些方法有说了解,现开始对这些问题进行分析:

1.正则表达式训练

举例1.

接受给定的字符串,判断该字符串是否属于验证码。验证码是由四位数字或者字母(包含大小写)组成的字符串。

输入格式:

在一行内输入一个字符串。

输出格式:

判断该字符串是否符合验证码规则,若是验证码,输出字符串是验证码,否则输出字符串不是验证码。

代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    String nameRegex = "[a-zA-Z_0-9]{4}";
    String user = input.nextLine();
    if(user.matches(nameRegex)){
        System.out.println(user+"属于验证码");
    }
    else
        System.out.println(user+"不属于验证码");
    }
}

设计策略

通过自学正则表达式学会了match的匹配运用从而让输入的字符串进行相应的匹配,最后判断输入的数字是否符合要求;

复杂性低,题目较为简单;

优点

简单方便,易看懂;

不足之处

总结

正则表达式的基础,以后会用上;

2.第二题  

输入连个点的坐标,计算两点之间的距离

输入格式:

4个double类型的实数,两个点的x,y坐标,依次是x1、y1、x2、y2,两个点的坐标之间以空格分隔,每个点的x,y坐标以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
若输入格式非法,输出"Wrong Format"。
若输入格式合法但坐标点的数量超过两个,输出“wrong number of points”。

输出格式:

计算所得的两点之间的距离。例如:1.4142135623730951

代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        String[] str = scan.nextLine().split(",| "); //字符串以空格分割
        scan.close();
int t=0,j=0;
double n=0;
        double[] num = new double[str.length];
        for(int i = 0; i < str.length; i++)
        {
            String M=str[i];
            for (int x=0;x<M.length();x++)
            {
                if(M.charAt(x)=='0'&&M.charAt(x+1)!='.')
                {j=1;break;}
            }
        }
        try {
            for (int i = 0; i < str.length; i++)
                 n = Double.parseDouble(String.valueOf(str[i]));
        }catch(Exception e){
                t = 1;
            }
        if(t==0&&j==0){
        for (int i = 0; i < str.length; i++)
            num[i] =  Double.parseDouble(String.valueOf(str[i]));
        double result=0;
        result=Math.sqrt(Math.abs((num[0]-num[2])*(num[0]-num[2]))+Math.abs((num[1]-num[3])*(num[1]-num[3])));
        if(str.length==4)
        System.out.println(result);
        else
            System.out.println("wrong number of points");
    }
    else
        System.out.println("Wrong Format");
    }

}

设计策略

首先得获取正确的数字,如果输入非数字的话得输出woring out对此我将从控制台得到的指令按字符串形式得到后转化成数字进行输出,如果无法转化成数字就输出我wrong out,若能转化成数字就进行下一步,通过split函数对字符串进行分割后获取对应数字,通过数学计算获取2点间距离。 

3.第三题

用户输入一组选项和数据,进行与直线有关的计算。选项包括:
1:输入两点坐标,计算斜率,若线条垂直于X轴,输出"Slope does not exist"。
2:输入三个点坐标,输出第一个点与另外两点连线的垂直距离。
3:输入三个点坐标,判断三个点是否在一条线上,输出true或者false。
4:输入四个点坐标,判断前两个点所构成的直线与后两点构成的直线是否平行,输出true或者false.
5:输入四个点坐标,计算输出前两个点所构成的直线与后两点构成的直线的交点坐标,x、y坐标之间以英文分隔",",并输出交叉点是否在两条线段之内(不含四个端点)的判断结果(true/false),判断结果与坐标之间以一个英文空格分隔。若两条线平行,没有交叉点,则输出"is parallel lines,have no intersection point"。

输入格式:

基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。
例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出"points coincide",

代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String[] str = input.nextLine().split(",| |:");
        double[] num = new double[str.length];
        double n=0;
        int t=0;
        try {
            for (int i = 0; i < str.length; i++)
                n = Double.parseDouble(String.valueOf(str[i]));
        }catch(Exception e){
            t = 1;
        }
        if(t==0){
            for (int i = 0; i < str.length; i++)
                num[i] =  Double.parseDouble(String.valueOf(str[i]));}
          else
              System.out.println("Wrong Format");
        if(num[0]==1)
        {
            Slop sl=new Slop(num[1],num[2],num[3],num[4]);
            if(str.length!=5)
                System.out.println("wrong number of points");
            else if(num[1]==num[3]&&num[2]==num[4])
                System.out.println("points coincide");
           else if(num[1]==num[3]){
            System.out.println("Slope does not exist");
        }
            else
            System.out.println(sl.getSlop(num[1],num[2],num[3],num[4]));
        }
        if(num[0]==2)
        {
           Distance distance=new Distance(num[1],num[2],num[3],num[4],num[5],num[6]);
            if(str.length!=7)
                System.out.println("wrong number of points");
            else if(num[1]==num[3]&&num[2]==num[4]||num[1]==num[3]&&num[5]==num[6]||num[5]==num[6]&&num[2]==num[4]||num[1]==num[3]&&num[2]==num[4]&&num[1]==num[3]&&num[5]==num[6])
                System.out.println("points coincide");
            else
                System.out.println(distance.getDistance(num[1],num[2],num[3],num[4],num[5],num[6]));
        }
        if(num[0]==3)
        {
          Sdian sd=new Sdian(num[1],num[2],num[3],num[4],num[5],num[6]);
            if(str.length!=7)
                System.out.println("wrong number of points");
            else if(num[1]==num[3]&&num[2]==num[4]||num[1]==num[5]&&num[2]==num[6]||num[5]==num[3]&&num[6]==num[4]||num[1]==num[3]&&num[2]==num[4]&&num[1]==num[3]&&num[5]==num[6])
                System.out.println("points coincide");
            else
                System.out.println(sd.pd(num[1],num[2],num[3],num[4],num[5],num[6]));
        }
        if(num[0]==4){
            Whetherpx whetherpx=new Whetherpx(num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8]);
            if(str.length!=9)
                System.out.println("wrong number of points");
            else if(num[1]==num[3]&&num[2]==num[4]||num[5]==num[7]&&num[6]==num[8]||num[1]==num[3]&&num[2]==num[4]&&num[5]==num[7]&&num[6]==num[8])
                System.out.println("points coincide");
            else
                System.out.println(whetherpx.pd(num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8]));
        }
        if(num[0]==5){
           Jiaod jiaod=new Jiaod(num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8]);
            if(str.length!=9)
                System.out.println("wrong number of points");
            else if(num[1]==num[3]&&num[2]==num[4]||num[5]==num[7]&&num[6]==num[8]||num[1]==num[3]&&num[2]==num[4]&&num[5]==num[7]&&num[6]==num[8])
                System.out.println("points coincide");
            else
                jiaod.getJiaod(num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8]);
        }
    }
}
class Slop<x1, x2,y1,y2> {
    private double x2;
    private double x1;
    private double y1;
    private double y2;
    public Slop(){}
    public Slop(double x1,double y1,double x2,double y2){
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
    }
    public void setX1(double x1)
    {
        this.x1=x1;
    }
    public void setX2(double x2)
    {
        this.x2=x2;
    }
    public void setY1(double y1)
    {
        this.y1=y1;
    }
    public void setY2(double y2)
    {
        this.y2=y2;
    }
    public double getX1() {
        return x1;
    }
    public double getX2() {
        return x2;
    }
    public double getY2() {
        return y2;
    }
    public double getY1() {
        return y1;
    }
    public Object getSlop(double x1, double y1, double x2, double y2){
    double result=0;
        if(x1==x2){
        System.out.println("Slope does not exist");
        return this;
    }
    else
    {
       result=(y1-y2)/(x1-x2);
        return result;
    }
    }
}
class Distance<x1,y1,x2,y2,x3,y3>{
    private double x2;
    private double x1;
    private double y1;
    private double y2;
    private double y3;
    private double x3;
    public Distance(){}
    public  Distance(double x1,double y1,double x2,double y2,double x3,double y3){
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
    }
    public void setX1(double x1)
    {
        this.x1=x1;
    }
    public void setX2(double x2)
    {
        this.x2=x2;
    }
    public void setX3(double x3)
    {
        this.x3=x3;
    }
    public void setY1(double y1)
    {
        this.y1=y1;
    }
    public void setY2(double y2)
    {
        this.y2=y2;
    }
    public void setY3(double y3)
    {
        this.y3=y3;
    }
    public double getX1() {
        return x1;
    }
    public double getX2() {
        return x2;
    }
    public double getX3() {
        return x3;
    }
    public double getY2() {
        return y2;
    }
    public double getY1() {
        return y1;
    }
    public double getY3() {
        return y3;
    }
    public Object getDistance(double x1,double y1,double x2,double y2,double x3,double y3)
    {
        double space = 0;
        double a, b, c;
        a = Math.sqrt(Math.abs((x2-x3)*(x2-x3))+Math.abs((y2-y3)*(y2-y3)));// 线段的长度
        b = Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2)));// (x1,y1)到点的距离
        c = Math.sqrt(Math.abs((x1-x3)*(x1-x3))+Math.abs((y1-y3)*(y1-y3)));// (x2,y2)到点的距离
        double p = (a + b + c) / 2;// 半周长
        double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积
        space = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)
        return (float)space;
    }
}
class Sdian<x1,y1,x2,y2,x3,y3>{
    private double x2;
    private double x1;
    private double y1;
    private double y2;
    private double y3;
    private double x3;
    public void setX1(double x1)
    {
        this.x1=x1;
    }
    public void setX2(double x2)
    {
        this.x2=x2;
    }
    public void setX3(double x3)
    {
        this.x3=x3;
    }
    public void setY1(double y1)
    {
        this.y1=y1;
    }
    public void setY2(double y2)
    {
        this.y2=y2;
    }
    public void setY3(double y3)
    {
        this.y3=y3;
    }
    public double getX1() {
        return x1;
    }
    public double getX2() {
        return x2;
    }
    public double getX3() {
        return x3;
    }
    public double getY2() {
        return y2;
    }
    public double getY1() {
        return y1;
    }
    public double getY3() {
        return y3;
    }
    public Sdian(){};
    public  Sdian(double x1,double y1,double x2,double y2,double x3,double y3){
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
    }
    public boolean pd(double x1,double y1,double x2,double y2,double x3,double y3)
    {
        double a,b,c;
        a = Math.sqrt(Math.abs((x2-x3)*(x2-x3))+Math.abs((y2-y3)*(y2-y3)));
        b = Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2)));
        c = Math.sqrt(Math.abs((x1-x3)*(x1-x3))+Math.abs((y1-y3)*(y1-y3)));
        if(a==b+c||b==a+c||c==a+b)
            return true;
        else
            return false;
    }
}
class Whetherpx<x1,y1,x2,y2,x3,y3,x4,y4>{
    private double x2;
    private double x1;
    private double y1;
    private double y2;
    private double y3;
    private double x3;
    private double x4;
    private double y4;
    public void setX1(double x1)
    {
        this.x1=x1;
    }
    public void setX2(double x2)
    {
        this.x2=x2;
    }
    public void setX3(double x3)
    {
        this.x3=x3;
    }
    public void setX4(double x4) { this.x4=x4; }
    public void setY1(double y1)
    {
        this.y1=y1;
    }
    public void setY2(double y2)
    {
        this.y2=y2;
    }
    public void setY3(double y3)
    {
        this.y3=y3;
    }
    public void setY4(double y4)
    {
        this.y4=y4;
    }
    public double getX1() {
        return x1;
    }
    public double getX2() {
        return x2;
    }
    public double getX3() {
        return x3;
    }
    public double getX4() {
        return x4;
    }
    public double getY2() {
        return y2;
    }
    public double getY1() {
        return y1;
    }
    public double getY3() {
        return y3;
    }
    public double getY4() {
        return y4;
    }
    public Whetherpx(){}
    public  Whetherpx(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
        this.x4=x4;
        this.y4=y4;
    }
    public boolean pd(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {
        double r1,r2;
        Slop sl = new Slop(x1, y1, x2, y2);
        if(x1==x2)
        {
            r1=0;
        }
        else
            r1= (double) sl.getSlop(x1,y1,x2,y2);
        if(x3==x4)
        {
            r2=0;
        }
        else
            r2=(double) sl.getSlop(x3,y3,x4,y4);
        if(r1==r2)
            return true;
        else
            return false;
    }
}
class Jiaod<x1,y1,x2,y2,x3,y3,x4,y4>{
    private double x2;
    private double x1;
    private double y1;
    private double y2;
    private double y3;
    private double x3;
    private double x4;
    private double y4;
    public void setX1(double x1)
    {
        this.x1=x1;
    }
    public void setX2(double x2)
    {
        this.x2=x2;
    }
    public void setX3(double x3)
    {
        this.x3=x3;
    }
    public void setX4(double x4) { this.x4=x4; }
    public void setY1(double y1)
    {
        this.y1=y1;
    }
    public void setY2(double y2)
    {
        this.y2=y2;
    }
    public void setY3(double y3)
    {
        this.y3=y3;
    }
    public void setY4(double y4)
    {
        this.y4=y4;
    }
    public double getX1() {
        return x1;
    }
    public double getX2() {
        return x2;
    }
    public double getX3() {
        return x3;
    }
    public double getX4() {
        return x4;
    }
    public double getY2() {
        return y2;
    }
    public double getY1() {
        return y1;
    }
    public double getY3() {
        return y3;
    }
    public double getY4() {
        return y4;
    }
    public Jiaod(){}
    public Jiaod(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
        this.x4=x4;
        this.y4=y4;
    }
    public Object getJiaod(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
    {
        Slop sl = new Slop(x1, y1, x2, y2);
        double r1=1,r2=1,a1 = 0,a2=0,b1=0,b2=0,x=0,y=0;

        if(x1==x2){
            a1=x1;
            r1=0;
        }
        else if(r1!=0)
        {
            a1= (double) sl.getSlop(x1,y1,x2,y2);
            b1=y1-a1*x1;
        }
        if(x3==x4){
            a2=x3;
            r2=0;
        }
        else if(r2!=0)
        {
            a2= (double) sl.getSlop(x3,y3,x4,y4);
            b2=y3-a2*x1;
        }
        if(r1==0&&r2==0||a1==a2)
            System.out.println("is parallel lines,have no intersection point");
        else if(x1==x2)
        {
            x=x1;
            y = (a2 * x1) + b2;
        }
        else if(x3==x4)
        {
            x=x3;
            y=(a1*x3)+b1;
        }
        else
        {
        x=(b2-b1)/(a1-a2);
        y=a1*x+b1;
        }
        if(r1==0&&r2==0||a1==a2)
            r1=0;
        else if(x>=x1&&x<=x2||x<=x1&&x>=x2||x>=x3&&x<=x4||x<=x3&&x>=x4)
            System.out.println((float)x+","+(float)y+" ture");
        else
            System.out.println((float)x+","+(float)y+" false");
        return this;

    }

}

设计思路

在开始输入的判断上和上体一样后开始进行数字上的判断后建立Line类含有distance用于对于功能

类图

 

 

缺点

在许多方面有所不组,许多测试的没查到,

4.用户输入一组选项和数据,进行与三角形有关的计算。选项包括:
1:输入三个点坐标,判断是否是等腰三角形、等边三角形,判断结果输出true/false,两个结果之间以一个英文空格符分隔。
2:输入三个点坐标,输出周长、面积、重心坐标,三个参数之间以一个英文空格分隔,坐标之间以英文","分隔。
3:输入三个点坐标,输出是钝角、直角还是锐角三角形,依次输出三个判断结果(true/false),以一个英文空格分隔,
4:输入五个点坐标,输出前两个点所在的直线与三个点所构成的三角形相交的交点数量,如果交点有两个,则按面积大小依次输出三角形被直线分割成两部分的面积。若直线与三角形一条线重合,输出"The point is on the edge of the triangle"
5:输入四个点坐标,输出第一个是否在后三个点所构成的三角形的内部(输出in the triangle/outof triangle)。
必须使用射线法,原理:由第一个点往任一方向做一射线,射线与三角形的边的交点(不含点本身)数量如果为1,则在三角形内部。如果交点有两个或0个,则在三角形之外。若点在三角形的某条边上,输出"on the triangle"

代码

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String[] str = input.nextLine().split(",| |:");
        double[] num = new double[str.length];
        double n=0;
        int t=0;
        try {
            for (int i = 0; i < str.length; i++)
                n = Double.parseDouble(String.valueOf(str[i]));
        }catch(Exception e){
            t = 1;
        }
        if(t==0){
            for (int i = 0; i < str.length; i++)
                num[i] =  Double.parseDouble(String.valueOf(str[i]));}
        else
            System.out.println("Wrong Format");
        if(num[0]==1)
        {
           Shangjx sj =new Shangjx(num[1],num[2],num[3],num[4],num[5],num[6]);
           if(sj.pd1(num[1],num[2],num[3],num[4],num[5],num[6])) {
               if (str.length != 7)
                   System.out.println("wrong number of points");
               else {
                   System.out.println(sj.pd2(num[1],num[2],num[3],num[4],num[5],num[6])+" "+sj.pd3(num[1],num[2],num[3],num[4],num[5],num[6]));
               }
           }
           else
               System.out.println("data error");
    }
        if(num[0]==2)
        {
            Shangjx sj =new Shangjx(num[1],num[2],num[3],num[4],num[5],num[6]);
            if(sj.pd1(num[1],num[2],num[3],num[4],num[5],num[6])) {
                if (str.length != 7)
                    System.out.println("wrong number of points");
                else {
                    sj.getMath(num[1],num[2],num[3],num[4],num[5],num[6]);
                }
            }
            else
                System.out.println("data error");
        }
        if(num[0]==3)
        {
            Shangjx sj =new Shangjx(num[1],num[2],num[3],num[4],num[5],num[6]);
            if(sj.pd1(num[1],num[2],num[3],num[4],num[5],num[6])) {
                if (str.length != 7)
                    System.out.println("wrong number of points");
                else {
                    System.out.println(sj.pd4(num[1],num[2],num[3],num[4],num[5],num[6])+" "+sj.pd5(num[1],num[2],num[3],num[4],num[5],num[6])+" "+sj.pd6(num[1],num[2],num[3],num[4],num[5],num[6]));
                }
            }
            else
                System.out.println("data error");
        }
        if(num[0]==5)
        {
            Shangjx sj =new Shangjx(num[1],num[2],num[3],num[4],num[5],num[6]);
            Sfin sf=new Sfin(num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8]);
            if(sj.pd1(num[3],num[4],num[5],num[6],num[7],num[8])) {
                if (str.length != 9)
                    System.out.println("wrong number of points");
                else if(!sf.getSf(num[1],num[2],num[3],num[4],num[5],num[6],num[7],num[8]))
                {
                    double s1,s2,s3,s4;
                    s1=sf.getSin(num[3],num[4],num[5],num[6],num[7],num[8]);
                    s2=sf.getSin(num[1],num[2],num[5],num[6],num[7],num[8]);
                    s3=sf.getSin(num[1],num[2],num[3],num[4],num[7],num[8]);
                    s4=sf.getSin(num[1],num[2],num[5],num[6],num[3],num[4]);
                    if(s1==s2+s3+s4)
                        System.out.println("in the triangle");
                    else
                        System.out.println("outof triangle");
                }
                else
                    System.out.println("on the triangle");
            }
            else
                System.out.println("data error");
        }
}
public static class Shangjx<x1,y1,x2,y2,x3,y3> {
    private double x2;
    private double x1;
    private double y1;
    private double y2;
    private double y3;
    private double x3;
    public Shangjx(){}
    public  Shangjx(double x1,double y1,double x2,double y2,double x3,double y3){
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
    }
    public void setX1(double x1)
    {
        this.x1=x1;
    }
    public void setX2(double x2)
    {
        this.x2=x2;
    }
    public void setX3(double x3)
    {
        this.x3=x3;
    }
    public void setY1(double y1)
    {
        this.y1=y1;
    }
    public void setY2(double y2)
    {
        this.y2=y2;
    }
    public void setY3(double y3)
    {
        this.y3=y3;
    }
    public double getX1() {
        return x1;
    }
    public double getX2() {
        return x2;
    }
    public double getX3() {
        return x3;
    }
    public double getY2() {
        return y2;
    }
    public double getY1() {
        return y1;
    }
    public double getY3() {
        return y3;
    }
    public boolean pd1(double x1,double y1,double x2,double y2,double x3,double y3) {
        double a, b, c;
        a = Math.sqrt(Math.abs((x2 - x3) * (x2 - x3)) + Math.abs((y2 - y3) * (y2 - y3)));
        b = Math.sqrt(Math.abs((x1 - x2) * (x1 - x2)) + Math.abs((y1 - y2) * (y1 - y2)));
        c = Math.sqrt(Math.abs((x1 - x3) * (x1 - x3)) + Math.abs((y1 - y3) * (y1 - y3)));
        if(a==b+c||b==a+c||c==a+b||a==0||b==0||c==0||x1==x2&&y1==y2||x1==x3&&y3==y1||x2==x3&&y3==y2)
            return false;
        else
            return true;
    }
    public boolean pd2(double x1,double y1,double x2,double y2,double x3,double y3) {
        double a, b, c;
        a = Math.sqrt(Math.abs((x2 - x3) * (x2 - x3)) + Math.abs((y2 - y3) * (y2 - y3)));
        b = Math.sqrt(Math.abs((x1 - x2) * (x1 - x2)) + Math.abs((y1 - y2) * (y1 - y2)));
        c = Math.sqrt(Math.abs((x1 - x3) * (x1 - x3)) + Math.abs((y1 - y3) * (y1 - y3)));
        if(a==b||a==c||b==c||a==b&&b==c)
            return true;
        else
            return false;
    }
    public boolean pd3(double x1,double y1,double x2,double y2,double x3,double y3) {
        double a, b, c;
        a = Math.sqrt(Math.abs((x2 - x3) * (x2 - x3)) + Math.abs((y2 - y3) * (y2 - y3)));
        b = Math.sqrt(Math.abs((x1 - x2) * (x1 - x2)) + Math.abs((y1 - y2) * (y1 - y2)));
        c = Math.sqrt(Math.abs((x1 - x3) * (x1 - x3)) + Math.abs((y1 - y3) * (y1 - y3)));
        if(a==b&&b==c)
            return true;
        else
            return false;
    }
    public Object getMath(double x1,double y1,double x2,double y2,double x3,double y3){
        double a, b, c;
        a = Math.sqrt(Math.abs((x2-x3)*(x2-x3))+Math.abs((y2-y3)*(y2-y3)));
        b = Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2)));
        c = Math.sqrt(Math.abs((x1-x3)*(x1-x3))+Math.abs((y1-y3)*(y1-y3)));
        double p = (a + b + c) / 2;
        double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));
        double x4,y4;
        x4=(x1+x2+x3)/3;
        y4=(y1+y2+y3)/3;
        String f =String.format("%.6f",2*p);
        p=Double.parseDouble(f)/2;
        String k =String.format("%.6f",s);
        s=Double.parseDouble(k);
        String n =String.format("%.6f",x4);
        x4=Double.parseDouble(n);
        String m =String.format("%.6f",y4);
        y4=Double.parseDouble(m);
        System.out.println(2*p+" "+s+" "+x4+","+y4);
        return this;
    }
    public boolean pd4(double x1,double y1,double x2,double y2,double x3,double y3){
        double a, b, c;
        a = Math.sqrt(Math.abs((x2-x3)*(x2-x3))+Math.abs((y2-y3)*(y2-y3)));
        b = Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2)));
        c = Math.sqrt(Math.abs((x1-x3)*(x1-x3))+Math.abs((y1-y3)*(y1-y3)));
        if(a*a>b*b+c*c||b*b>a*a+c*c||c*c>a*a+b*b)
            return true;
        else
            return false;
    }
    public boolean pd5(double x1,double y1,double x2,double y2,double x3,double y3){
        double a, b, c;
        a = Math.sqrt(Math.abs((x2-x3)*(x2-x3))+Math.abs((y2-y3)*(y2-y3)));
        b = Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2)));
        c = Math.sqrt(Math.abs((x1-x3)*(x1-x3))+Math.abs((y1-y3)*(y1-y3)));
        if(a*a==b*b+c*c||b*b==(a*a+c*c)||c*c==a*a+b*b)
            return true;
        else
            return false;
    }
    public boolean pd6(double x1,double y1,double x2,double y2,double x3,double y3){
        double a, b, c;
        a = Math.sqrt(Math.abs((x2-x3)*(x2-x3))+Math.abs((y2-y3)*(y2-y3)));
        b = Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2)));
        c = Math.sqrt(Math.abs((x1-x3)*(x1-x3))+Math.abs((y1-y3)*(y1-y3)));
        if(a*a<b*b+c*c&&b*b<a*a+c*c&&c*c<a*a+b*b)
            return true;
        else
            return false;
    }
}
}
class Sfin<x1,y1,x2,y2,x3,y3,x4,y4>{
    private double x2;
    private double x1;
    private double y1;
    private double y2;
    private double y3;
    private double x3;
    private double x4;
    private double y4;
    public void setX1(double x1)
    {
        this.x1=x1;
    }
    public void setX2(double x2)
    {
        this.x2=x2;
    }
    public void setX3(double x3)
    {
        this.x3=x3;
    }
    public void setX4(double x4) { this.x4=x4; }
    public void setY1(double y1)
    {
        this.y1=y1;
    }
    public void setY2(double y2)
    {
        this.y2=y2;
    }
    public void setY3(double y3)
    {
        this.y3=y3;
    }
    public void setY4(double y4)
    {
        this.y4=y4;
    }
    public double getX1() {
        return x1;
    }
    public double getX2() {
        return x2;
    }
    public double getX3() {
        return x3;
    }
    public double getX4() {
        return x4;
    }
    public double getY2() {
        return y2;
    }
    public double getY1() {
        return y1;
    }
    public double getY3() {
        return y3;
    }
    public double getY4() {
        return y4;
    }
    public Sfin(){}
    public Sfin(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
        this.x1=x1;
        this.x2=x2;
        this.x3=x3;
        this.y1=y1;
        this.y2=y2;
        this.y3=y3;
        this.x4=x4;
        this.y4=y4;
    }
    public boolean getSf(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4){
        double r1=1,r2=1,r3=0,a1 = 0,a2=0,a3=0,b1=0,b2=0,b3=0,x=0,y=0;
        if(x3==x2){
            a1=x2;
            r1=0;
        }
        else if(r1!=0)
        {
            a1= (y3-y2)/(x3-x2);
            b1=y2-a1*x2;
        }
        if(x3==x4){
            a2=x3;
            r2=0;
        }
        else if(r2!=0)
        {
            a2=(y3-y4)/(x3-x4);
            b2=y3-a2*x3;
        }
        if(x2==x4){
            a3=x2;
            r3=0;
        }
        else if(r3!=0)
        {
            a3=(y2-y4)/(x2-x4);
            b3=y2-a2*x2;
        }
        if(y1==a1*x1+b1||y1==a2*x1+b2||y1==a3*x1+b3)
            return true;
        else
            return false;
    }
    public Double getSin(double x1,double y1,double x2,double y2,double x3,double y3){
        double a, b, c;
        a = Math.sqrt(Math.abs((x2-x3)*(x2-x3))+Math.abs((y2-y3)*(y2-y3)));
        b = Math.sqrt(Math.abs((x1-x2)*(x1-x2))+Math.abs((y1-y2)*(y1-y2)));
        c = Math.sqrt(Math.abs((x1-x3)*(x1-x3))+Math.abs((y1-y3)*(y1-y3)));
        double p = (a + b + c) / 2;
        float s = (float) Math.sqrt(p * (p - a) * (p - b) * (p - c));
        return  (double)s;
    }
}

设计分析

输入判断与上体一样,整体考验数学计算;

类图

5.

  • 设计一个类表示平面直角坐标系上的点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:长度值
  • 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**

    • 以下情况为无效作业
      • 无法运行
      • 设计不符合所给类图要求
      • 未通过任何测试点测试
      • 判定为抄袭           

代码

 

import java.util.Scanner;
public class Main {
    public static  void main(String[] args){
        Scanner input = new Scanner(System.in);
        double x1,x2,y1,y2;
        x1=input.nextDouble();
        y1= input.nextDouble();
        x2=input.nextDouble();
        y2=input.nextDouble();
        String color=input.next();
        Point point1=new Point(x1,y1);
        Point point2=new Point(x2,y2);
        Line line=new Line(point1,point2,color);
        if(point1.yes(x1,y1)&& point2.yes(x2,y2)){
        line.display();}
        else
            System.out.println("Wrong Format");
    }
}
class Line {
    private Point point1;
    private Point point2;
    private String color;

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
    public Line(){}
    public Line(Point point1,Point point2,String color)
    {
        this.color=color;
        this.point1=point1;
        this.point2=point2;
    }
    public double GetDistance(Point point1,Point point2)
    {
            double x=Math.abs(point1.getX()- point2.getX());
            double y=Math.abs(point1.getY()- point2.getY());
        double z=Math.sqrt(x*x+y*y);
            return z;
    }
    public void display(){
        System.out.println(
                "The line's color is:"+color+"\n" +
                "The line's begin point's Coordinate is:\n"+"("+String.format("%.2f", point1.getX())+","+String.format("%.2f", point1.getY())+")"+"\n"+
                "The line's end point's Coordinate is:\n" +
                "("+String.format("%.2f", point2.getX())+","+String.format("%.2f", point2.getY())+")\n" +
                "The line's length is:"+String.format("%.2f", GetDistance(point1,point2)));
    }
}
class Point {
    private double x;
    private double 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 Point(){}
    public Point(double x,double y){
        this.x=x;
        this.y=y;
    }
    public void Display(double x,double y){
        if(x>0&&x<=200&&y>0&&y<=200)
            System.out.println("("+x+","+y+")");
        else
            System.out.println("Wrong Format");
    }
    public boolean yes(double x,double y){
        if(x>0&&x<=200&&y>0&&y<=200)
            return true;
        else
            return false;
    }
}

 

设计思路

 

类图

 

 

 

6.

在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。

  • 对题目中的点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);
                double x1,x2,y1,y2;
                x1=input.nextDouble();
                y1= input.nextDouble();
                x2=input.nextDouble();
                y2=input.nextDouble();
                String color=input.next();
                Point point1=new Point(x1,y1);
                Point point2=new Point(x2,y2);
                Line line=new Line(point1,point2,color);
                Plane plane=new Plane(color);
                if(point1.yes(x1,y1)&& point2.yes(x2,y2)){
                point1.display(x1,y1);
                point2.display(x2,y2);
                line.display();
                plane.display();
                }
                else
                    System.out.println("Wrong Format");
            }
        }
        class Point extends Elemen{
            private double x;
            private double 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 Point(){}
            public Point(double x,double y){
                this.x=x;
                this.y=y;
            }
            public void display(double x,double y){
                if(x>0&&x<=200&&y>0&&y<=200)
                    System.out.println("("+String.format("%.2f",x)+","+String.format("%.2f",y)+")");
                else
                    System.out.println("Wrong Format");
            }
            public boolean yes(double x,double y){
                if(x>0&&x<=200&&y>0&&y<=200)
                    return true;
                else
                    return false;
            }
        }
        class Line extends Elemen{
            private Point point1;
            private Point point2;
            private String color;
        
            public Point getPoint1() {
                return point1;
            }
        
            public void setPoint1(Point point1) {
                this.point1 = point1;
            }
        
            public Point getPoint2() {
                return point2;
            }
        
            public void setPoint2(Point point2) {
                this.point2 = point2;
            }
        
            public String getColor() {
                return color;
            }
        
            public void setColor(String color) {
                this.color = color;
            }
            public Line(){}
            public Line(Point point1,Point point2,String color)
            {
                this.color=color;
                this.point1=point1;
                this.point2=point2;
            }
            public double GetDistance(Point point1,Point point2)
            {
                    double x=Math.abs(point1.getX()- point2.getX());
                    double y=Math.abs(point1.getY()- point2.getY());
                double z=Math.sqrt(x*x+y*y);
                    return z;
            }
            public void display(){
                System.out.println(
                        "The line's color is:"+color+"\n" +
                                "The line's begin point's Coordinate is:\n"+"("+String.format("%.2f", point1.getX())+","+String.format("%.2f", point1.getY())+")"+"\n"+
                                "The line's end point's Coordinate is:\n" +
                                "("+String.format("%.2f", point2.getX())+","+String.format("%.2f", point2.getY())+")\n" +
                                "The line's length is:"+String.format("%.2f", GetDistance(point1,point2)));
            }
        }
        abstract class Elemen {
                public void display(){
                    System.out.println();
                }
        }
        class Plane extends Elemen{
            private String color;
        
            public String getColor() {
                return color;
            }
        
            public void setColor(String color) {
                this.color = color;
            }
            public Plane(){}
            public Plane(String color){this.color=color;};
            public void display(){
                System.out.println("The Plane's color is:"+color);
            }
        }

        类图

      • 优化
      • 添加父类可以使代码更加简洁,运行更加迅速。
    •  

       

posted @ 2022-05-01 21:07  长驱直入  阅读(80)  评论(0)    收藏  举报