Java学习总结和感想

发布时间:2022-06-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Java学习总结和感想脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

(1)前言与总结分析:第四次题目集主要考察的是java语言的一些基本知识,例如7-1考察的是正则表达式的基本运用,当然这里的难点是判断每行的开头是否出现空格,需要排除。剩下的就是Java一些基本的运用,难度不大。

 

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner ljg=new Scanner(System.in);
        while(true){
            String arr=ljg.nextLine();
            if(arr.equals("end")==true)
            {
                break;
            }
                
            Data.deal(arr);
            
        }
    }
    
}


class Data {
    public static void deal(String arr)
    {
        String s[]=arr.split("\D+");
        int i,sum=0;
        
        for(i=0;i<s.length;i++){
            if(s[i].equals("")==false)
            {
                int number=Integer.parseInt(s[i]);
                sum=sum+number;
            }
        }
        System.out.println(sum);
        
    }
}

Java学习总结和感想

 

 

 

Java学习总结和感想

 

 

主要还是对格式的判断与处理把需要的数字相加起来就可以得到结果。

 

题目7-3是考察设计一个银行业务类,也是对类的关系的基本运用,对日常的生活问题中的关系功能进行划分,难度适中,题目有一定的复杂度。

import java.util.Scanner;

public class Main {
    public static void main(String[] args)
    {
        double money1;
        BankBusiness.welcome();
        Scanner ljg = new Scanner(System.in);
        
        String name=ljg.next();
        String password=ljg.next();
        BankBusiness account=new BankBusiness(name,password);
        password=ljg.next();
        double money=ljg.nextDouble();
        account.deposit(password,money);
        
        
        
        
        while(true)
        {
            password=ljg.next();
            money1=ljg.nextDouble();
            account.withdraw(password,money1);
        }
    }
    
}


class BankBusiness {
    public static String bankName="中国银行";
    private String name;
    private String password;
    private double balance;
    
    
    
    public static void welcome()
    {
        System.out.printf("%s欢迎您的到来!n",bankName);
    }
    public static void welcomeNext()
    {
        System.out.printf("请收好您的证件和物品,欢迎您下次光临!n");
        System.exit(0);
    }
    public BankBusiness(String name,String password)
    {
        this.name=name;
        this.password=password;
        this.balance=0;
    }
    public void deposit(String password,double money)
    {
        if(this.password.equals(password))
        {
            this.balance=money;
            System.out.println("您的余额有"+this.balance+"元。");
        }
    }
    public void withdraw(String password,double money1)
    {
        if(this.password.equals(password))
        {
            if(this.balance<money1)
            {
                System.out.println("您的余额不足!");
            }
            else
            {
                this.balance=this.balance-money1;
                System.out.println("请取走钞票,您的余额还有"+this.balance+"元。");
                BankBusiness.welcomeNext();
            }
        }
        else
        {
            System.out.println("您的密码错误!");
        }
    }
}

弄清了类之间的关系和实现方法对应的功能,便很快能得到结果。

Java学习总结和感想

 

 

         

 

 

7-2 点线形系列4-凸四边形的计算 ,题目已经表达的很清楚了,还是对格式的判断虽然这次测试点都通过了,但是对四边形的一些性质不理解,比如如何判断它是否构成四边形和凹凸性的判断,而且代码实现起来也很困难,题目难度非常大,复杂度很高,花了很多时间,但是还有很多测试点没过,感受到了这个题目的难度。

类之间的关系并不难分析,但是核心功能的实现非常困难,这涉及到一些数学问题,对我来说比较困难。

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.text.DecimalFormat;

public class Main 
{
    public static void main(String[] args) 
    {    

        Scanner ljg= new Scanner(System.in);
        String s = ljg.nextLine();
        InputData d = new InputData();
        ParseInput.paseInput(s, d);
        
        int choice = d.getChoice();
        
        ArrayList ps = d.getPoints();
        switch (choice) {
        case 1:
            handle1(ps);
            break;
        case 2:
            handle2(ps);
            break;
        case 3:
            handle3(ps);
            break;
        case 4:
            handle4(ps);
            break;
        case 5:
            handle5(ps);
            break;
        }

    }

    
    public static void handle1(ArrayList<Point> ps) {
        PointInputError.wrongNumberOfPoints(ps, 4);
        Quadrangle q = new Quadrangle(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
        q.isCoincide();
        q.isQuadrangle();
        q.isParallelogram();
    }
    
    public static void handle2(ArrayList<Point> ps)
    {
        PointInputError.wrongNumberOfPoints(ps, 4);
        Quadrangle q = new Quadrangle(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
        
        q.isQuadrangle1();
        q.isCoincide();
        q.isParallelogram1();
        q.isRhombus();
        q.isRectangle();
        q.isSquare();
        
    }
    
    public static void handle3(ArrayList<Point> ps)
    {
        PointInputError.wrongNumberOfPoints(ps, 4);
        Quadrangle q = new Quadrangle(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
        q.isQuadrangle1();
        q.judgeQuadrangle();
        q.getAreaAndPerimeter();
        
    }
    
    public static void handle4(ArrayList<Point> ps)
    {
        PointInputError.wrongNumberOfPoints(ps, 6);
        System.out.print("not a quadrilateral or triangle");
        
    }
    
    public static void handle5(ArrayList<Point> ps)
    {
        double s1,s2,s3,s4,s5,s6;
        
        PointInputError.wrongNumberOfPoints(ps, 5);
        Quadrangle q = new Quadrangle(ps.get(1), ps.get(2), ps.get(3),ps.get(4));
        q.isQuadrangle2();
        s1=Triangle.getTriangleArea(ps.get(1), ps.get(2),ps.get(0));
        s2=Triangle.getTriangleArea(ps.get(2), ps.get(3),ps.get(0));
        s3=Triangle.getTriangleArea(ps.get(3), ps.get(4),ps.get(0));
        s4=Triangle.getTriangleArea(ps.get(4), ps.get(1),ps.get(0));
        s5=Triangle.getTriangleArea(ps.get(1), ps.get(2),ps.get(3));
        s6=Triangle.getTriangleArea(ps.get(3), ps.get(4),ps.get(1));
        
        Line L1=new Line(ps.get(1), ps.get(2));
        Line L2=new Line(ps.get(2), ps.get(3));
        Line L3=new Line(ps.get(3), ps.get(4));
        Line L4=new Line(ps.get(4), ps.get(1));
        
        L1.isOnline(ps.get(0));
        L2.isOnline(ps.get(0));
        L3.isOnline(ps.get(0));
        L4.isOnline(ps.get(0));
        if(Math.abs((s1+s2+s3+s4)-(s5+s6))<1)
        {
            System.out.print("in the quadrilateral");
            System.exit(0);
        }
        else
        {
            System.out.print("outof the quadrilateral");
            System.exit(0);
        }
        System.out.print("on the triangle");
    }
}


class ParseInput {
        /*
         * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
         *         一个空InputData对象
         * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
          * 输出:包含选项值和所有点的Point对象的InputData对象。
         */
        public static void paseInput(String s, InputData d) {
            PointInputError.wrongChoice(s);        
            d.setChoice(getChoice(s));
            s = s.substring(2);
            extractPoints(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 extractPoints(String s, InputData d) {
            String s1[]= s.split(" ");
            if (s1.length == 0)
            {
                return;
            }
                
            for (int i = 0; i < s1.length; i++) 
            {
                d.addPoint(readPoint(s1[i]));
            }
        }

        /*
         * 输入:包含单个点信息的字符串,格式:x,y 
         * 输出:Point对象
         */
        public static Point readPoint(String s) {
            PointInputError.wrongPointFormat(s);
            String[] s2 = s.split(",");
            double x = Double.parseDouble(s2[0]);
            double y = Double.parseDouble(s2[1]);
            // System.out.println("match");
            return new Point(x, y);

        }


}


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);
        }
    }

}

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 Point {
    public double x;
    public double y;

    public Point() {

    }

    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }

    /* 设置坐标x,将输入参数赋值给属性x */
    public void setX(double x) {
        this.x = x;
    }

    /* 设置坐标y,将输入参数赋值给属性y */
    public void setY(double y) {
        this.y = y;
    }

    /* 获取坐标x,返回属性x的值 */
    public double getX() {
        return x;
    }

    /* 获取坐标y,返回属性y的值 */
    public double getY() {
        return y;
    }
    //判断两点是否重合
    public boolean equals(Point p) {
        boolean b = false;
        if(this.x==p.getX()&&this.y==p.getY()) {
            b=true;
        }
        return b;
    }

    /* 计算当前点和输入点p之间的距离 */
    public double getDistance(Point p) {    
        double d;
        d=Math.sqrt((this.x-p.x)*(this.x-p.x)+(this.y-p.y)*(this.y-p.y));
        return d;
    }
}

class Line {
    private Point p1;//线上的第一个点
    private Point p2;//线上的第二个点


    public Line(double x1, double y1, double x2, double y2)
    {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出
        this.p1 = p1;
        this.p2 = p2;
    }

    public Line(Point p1, Point p2) 
    {
        LineInputError.pointsCoincideError(p1, p2);//两点是否重合,重合则报错并退出
        this.p1 = p1;
        this.p2 = p2;
    }

    /* 获取线条的斜率 */
    public Double getSlope() 
    {
        // (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
        return ((p2.getY() - p1.getY()) /(p2.getX() - p1.getX()));
    }

        /* 判断x是否在线上 */
    public void isOnline(Point x) {
    
        if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) {
            System.out.print("on the quadrilateral");
            System.exit(0);
        }
        Line l = new Line(p1, x);
        if (l.getSlope().isInfinite() && this.getSlope().isInfinite()) {
            System.out.print("on the quadrilateral");
            System.exit(0);
        }

        // 此点与线上任意一点构成的线的斜率相等则此点在线上
        double b1 = l.getSlope(), b2 = this.getSlope();
        if((Math.abs(b1 - b2)<0.000001))
        {
            System.out.print("on the quadrilateral");
            System.exit(0);
        }
    }

}



//用于处理线条相关功能中出现的异常提示。
class LineInputError {    

    // 直线的两点重合的错误判断和提示。
    public static void pointsCoincideError(Point p1, Point p2) {
        if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
            System.out.println("points coincide");
            System.exit(0);
        }
    }


}


class Quadrangle {
    
    public Point a;
    public Point b;
    public Point c;
    public Point d;
        
    public Quadrangle(Point a,Point b,Point c,Point d)
    {
        this.a=a;
        this.b=b;
        this.c=c;
        this.d=d;
    }
    
    public void isCoincide()//点是否重合
    {
        if(a.x==b.x&&a.y==b.y||a.x==c.x&&a.y==c.y||a.x==d.x&&a.y==d.y||b.x==c.x&&b.y==c.y||b.x==d.x&&b.y==d.y||c.x==d.x&&c.y==d.y)
        {
            System.out.print("points coincide");
            System.exit(0);
        }
    }
    
    public void isCoincide1()//点是否重合
    {
        if(a.x==b.x&&a.y==b.y||a.x==c.x&&a.y==c.y||a.x==d.x&&a.y==d.y||b.x==c.x&&b.y==c.y||b.x==d.x&&b.y==d.y||c.x==d.x&&c.y==d.y)
        {
            System.out.print("false false false");
            System.exit(0);
        }
    }
    
    public void isQuadrangle()//是否是四边形
    {
        if((c.y-b.y)*(c.x-a.x)==(c.y-a.y)*(c.x-b.x))
        {
            System.out.print("false false");
            System.exit(0);
        }
        if((c.y-d.y)*(c.x-a.x)==(c.y-a.y)*(c.x-d.x))
        {
            System.out.print("false false");
            System.exit(0);
        }
        if((d.y-b.y)*(d.x-a.x)==(d.y-a.y)*(d.x-b.x))
        {
            System.out.print("false false");
            System.exit(0);
        }
        if((c.y-b.y)*(c.x-d.x)==(c.y-d.y)*(c.x-b.x))
        {
            System.out.print("false false");
            System.exit(0);
        }
        else
        {
            System.out.print("true ");
        }
    }
    
    public void isQuadrangle1()//是否是四边形
    {
        if((c.y-b.y)*(c.x-a.x)==(c.y-a.y)*(c.x-b.x))
        {
            System.out.print("not a quadrilateral");
            System.exit(0);
        }
        if((c.y-d.y)*(c.x-a.x)==(c.y-a.y)*(c.x-d.x))
        {
            System.out.print("not a quadrilateral");
            System.exit(0);
        }
        if((d.y-b.y)*(d.x-a.x)==(d.y-a.y)*(d.x-b.x))
        {
            System.out.print("not a quadrilateral");
            System.exit(0);
        }
        if((c.y-b.y)*(c.x-d.x)==(c.y-d.y)*(c.x-b.x))
        {
            System.out.print("not a quadrilateral");
            System.exit(0);
        }
    }
    
    public void isQuadrangle2()//是否是四边形  选项4 5 
    {
        if((c.y-b.y)*(c.x-a.x)==(c.y-a.y)*(c.x-b.x))
        {
            System.out.print("not a quadrilateral or triangle");
            System.exit(0);
        }
        if((c.y-d.y)*(c.x-a.x)==(c.y-a.y)*(c.x-d.x))
        {
            System.out.print("not a quadrilateral or triangle");
            System.exit(0);
        }
        if((d.y-b.y)*(d.x-a.x)==(d.y-a.y)*(d.x-b.x))
        {
            System.out.print("not a quadrilateral or triangle");
            System.exit(0);
        }
        if((c.y-b.y)*(c.x-d.x)==(c.y-d.y)*(c.x-b.x))
        {
            System.out.print("not a quadrilateral or triangle");
            System.exit(0);
        }
    }
    
    public void isParallelogram()//是否是平行四边形
    {
        int count=0;
        if((a.y-b.y)*(c.x-d.x)==(c.y-d.y)*(a.x-b.x))
        {
            count++;
        }
        if((a.y-c.y)*(b.x-d.x)==(b.y-d.y)*(a.x-c.x))
        {
            count++;
        }
        if((a.y-d.y)*(b.x-c.x)==(b.y-c.y)*(a.x-d.x))
        {
            count++;
        }
        if(count==2)
        {
            System.out.print("true");
        }
        else
        {
            System.out.print("false");
        }
    }
    
    public void isParallelogram1()//是否是平行四边形
    {
        int count=0;
        if((a.y-b.y)*(c.x-d.x)==(c.y-d.y)*(a.x-b.x))
        {
            count++;
        }
        if((a.y-c.y)*(b.x-d.x)==(b.y-d.y)*(a.x-c.x))
        {
            count++;
        }
        if((a.y-d.y)*(b.x-c.x)==(b.y-c.y)*(a.x-d.x))
        {
            count++;
        }
        
        if(count!=2)
        {
            System.out.print("false false false");
            System.exit(0);
        }
    }
    
    public void isSquare()//是否是正方形
    {
        double k1,k2;
        Line L1=new Line(a,c);
        Line L2=new Line(b,d);
        k1=L1.getSlope();
        k2=L2.getSlope();
        if(k1*k2==-1)
        {
            System.out.print("true");
        }
        else
        {
            System.out.print("false");
        }
    }
    
    public void isRhombus()//是否是菱形
    {
        double d1,d2;
        d1=a.getDistance(b);
        d2=b.getDistance(c);
        
        if(d1==d2)
        {
            System.out.print("true ");
        }
        else
        {
            System.out.print("false ");
        }
    }
    public void isRectangle()//是否是长方形
    {
        double d1,d2,d3,d4;
        double x1,y1;
        x1=(a.x+b.x+c.x+d.x)/4;
        y1=(a.y+b.y+c.y+d.y)/4;
        Point p=new Point(x1,y1);
        d1=p.getDistance(a);
        d2=p.getDistance(b);
        d3=p.getDistance(c);
        d4=p.getDistance(d);
        
        if(d1==d2&&d2==d3&&d3==d4)
        {
            System.out.print("true ");
            
        }
        else
        {
            System.out.print("false ");
            
        }
    }
    
    public void judgeQuadrangle()
    {
        double t1,t2,t3,t4;
        t1=(a.x-b.x)*(c.y-b.y)-(a.y-b.y)*(c.x-b.x);
        t2=(b.x-c.x)*(d.y-c.y)-(b.y-c.y)*(d.x-c.x);
        t3=(c.x-d.x)*(a.y-d.y)-(c.y-d.y)*(a.x-d.x);
        t4=(d.x-a.x)*(b.y-a.y)-(d.y-a.y)*(b.x-a.x);
        if(t1*t2*t3*t4<0)
        {
            System.out.print("false ");
        }
        else
        {
            System.out.print("true ");
        }
    }
    
    public void getAreaAndPerimeter()
    {
        double area,s,area1,s1;
        double d1,d2,d3,d4;
        area=0.5*Math.abs((a.x-b.x)*(a.y+b.y)+(b.x-c.x)*(b.y+c.y)+(c.x-d.x)*(c.y+d.y)+(d.x-a.x)*(d.y+a.y));
        d1=a.getDistance(b);
        d2=b.getDistance(c);
        d3=c.getDistance(d);
        d4=d.getDistance(a);
        s=d1+d2+d3+d4;
        s1=doubleFormat(s);
        area1=doubleFormat(area);
        System.out.print(s1+" "+area1);
    }
    
    public static Double doubleFormat(double b) {
        DecimalFormat df = new DecimalFormat("#.000");
        Double output = Double.valueOf(df.format(b));
        return output;
    }
}


class Triangle 
{
    public Point a;
    public Point b;
    public Point c;
    
    public Triangle(Point a, Point b, Point c) 
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
    
    public void isTriangle() 
    {
        
    }
    
    public static double getTriangleArea(Point a,Point b,Point c)
    {
        double d1,d2,d3,p;
        d1=a.getDistance(b);
        d2=b.getDistance(c);
        d3=c.getDistance(a);
        p=(d1+d2+d3)/2.0;
        return Math.sqrt(p*(p-d1)*(p-d2)*(p-d3));
    }
}

Java学习总结和感想

 

 

 

 这次题目集自己做的非常一般,7-2这个题目对我来说太困难了,也花了很多很多时间,但是还有许多困难。

Java学习总结和感想

 

 

(2)改进建议:我认为自己的代码最大的改进的地方就是通过类来解决问题,更好的去理清好类之间的关系,一些代码可以更加的模块化,使代码更加紧凑。

Java学习总结和感想

 

第五次题目集还是围绕着几何图形的相关性质的判断和多种情况的处理,使多种方法有机的联系起来,总的来说是一道五边形的综合问题,相交,位置关系,面积求解等一些典型的数学问题,当然题目难度更大,复杂度更高。

import java.util.ArrayList;
import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {
    
    public static void main(String[] args) 
    {    
            Scanner ljg= new Scanner(System.in);
            String s = ljg.nextLine();
            InputData d = new InputData();
            ParseInput.paseInput(s, d);
            
            int choice = d.getChoice();
            
            ArrayList pp = d.getPoints();
            
            if(choice==1)
            {
                handle1(pp);
            }
            else if(choice==2)
            {
                handle2(pp);
            }
            else if(choice==3)
            {
                handle3(pp);
            }
    }

    
    public static void handle1(ArrayList<Point> pp) {
        PointInputError.wrongNumberOfPoints(pp, 5);
        Pentagon p=new Pentagon(pp.get(0),pp.get(1),pp.get(2),pp.get(3),pp.get(4));
        
        if(p.isPentagon()==1||p.isPentagon()==2)
        {
            System.out.print("true");
        }
        if(p.isPentagon()==0)
        {
            System.out.print("false");
        }
    }

    public static void handle2(ArrayList<Point> pp){
        PointInputError.wrongNumberOfPoints(pp, 5);
        Pentagon p=new Pentagon(pp.get(0),pp.get(1),pp.get(2),pp.get(3),pp.get(4));
        
        if(p.isPentagon()==0)
        {
            System.out.print("not a pentagon");
            System.exit(0);
        }
        if(p.isPentagon()==1)
        {
            System.out.print("true ");
            p.getPerimterAndArea();
            System.exit(0);
        }
        if(p.isPentagon()==2)
        {
            System.out.print("false");
            System.exit(0);
        }
        
        
    }
    
    public static void handle3(ArrayList<Point> pp){
        PointInputError.wrongNumberOfPoints(pp, 7);
        Pentagon.pointJudge(pp.get(2),pp.get(3),pp.get(4),pp.get(5),pp.get(6));
        
    }
}

class InputData {
    public int choice;;//用户输入的选择项
    public 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 Point {
    public double x;
    public double y;

    public Point() {

    }
    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }
    public void setX(double x) {
        this.x = x;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }

}


class ParseInput {
    public static void paseInput(String s, InputData d) {
        PointInputError.wrongChoice(s);        
        d.setChoice(getChoice(s));
        s = s.substring(2);
        extractPoints(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 extractPoints(String s, InputData d) {
        String s1[]= s.split(" ");
        if (s1.length == 0)
        {
            return;
        }
            
        for (int i = 0; i < s1.length; i++) 
        {
            d.addPoint(readPoint(s1[i]));
        }
    }

    /*
     * 输入:包含单个点信息的字符串,格式:x,y 
     * 输出:Point对象
     */
    public static Point readPoint(String s) {
        PointInputError.wrongPointFormat(s);
        String[] s2 = s.split(",");
        double x = Double.parseDouble(s2[0]);
        double y = Double.parseDouble(s2[1]);
        // System.out.println("match");
        return new Point(x, y);

    }

}

class PointInputError {
    //判断从字符串中解析出的点的数量是否合格。
    public static void wrongNumberOfPoints(ArrayList pp, int num) 
    {
        if (pp.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 Line {
    public Point p1;//线上的第一个点
    public Point p2;//线上的第二个点


    public Line(double x1, double y1, double x2, double y2)
    {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        
        pointsCoincideError(p1,p2);
        this.p1 = p1;
        this.p2 = p2;
    }

    public Line(Point p1, Point p2) 
    {
        pointsCoincideError(p1,p2);
        this.p1 = p1;
        this.p2 = p2;
    }

    /* 获取线条的斜率 */
    public double getSlope() 
    {
        double k;
        k=(p2.getY() - p1.getY()) /(p2.getX() - p1.getX());
        return k;
    }

    
    // 直线的两点重合的错误判断和提示。
    public void pointsCoincideError(Point p1, Point p2) {
        if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
            System.out.println("points coincide");
            System.exit(0);
        }
    }
    
    public double getDistance()
    {
        return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    }
}

class Pentagon {
    public Point a;
    public Point b;
    public Point c;
    public Point d;
    public Point e;
    
    public Pentagon(Point a,Point b,Point c,Point d,Point e)
    {
        this.a=a;
        this.b=b;
        this.c=c;
        this.d=d;
        this.e=e;
    }
    
    public void isCoincide()//点是否重合
    {
        if(a.x==b.x&&a.y==b.y||a.x==c.x&&a.y==c.y||a.x==d.x&&a.y==d.y||a.x==e.x&&a.y==e.y||b.x==c.x&&b.y==c.y||b.x==d.x&&b.y==d.y||b.x==e.x&&b.y==e.y||c.x==d.x&&c.y==d.y||c.x==e.x&&c.y==e.y||d.x==e.x&&d.y==e.y)
        {
            System.out.print("false");
            System.exit(0);
        }
    }
    
    public int isPentagon()//判断是否为五边形
    {
        double angle1,angle2,angle3,angle4,angle5,angle6,angle7,angle8,angle9,angle10;
        
        angle1=angleJudge(a,b,c);
        angle2=angleJudge(b,c,d);
        angle3=angleJudge(c,d,e);
        angle4=angleJudge(d,e,a);
        angle5=angleJudge(e,a,b);
        
        angle6=360-angle1;
        angle7=180-angle2;
        angle8=180-angle3;
        angle9=360-angle4;
        angle10=180-angle5;
        
        Line l1=new Line(a,b);
        Line l2=new Line(b,c);
        Line l3=new Line(c,d);
        Line l4=new Line(d,e);
        Line l5=new Line(e,a);
        
        double m1=angleAdd(angle1,angle2,angle3,angle4,angle5);
        double m2=angleAdd(angle6,angle2,angle3,angle4,angle5);
        double m3=angleAdd(angle1,angle7,angle3,angle4,angle5);
        double m4=angleAdd(angle1,angle3,angle8,angle4,angle5);
        double m5=angleAdd(angle1,angle2,angle3,angle9,angle5);
        double m6=angleAdd(angle1,angle2,angle3,angle4,angle10);
        
        if(l1.getSlope()==l2.getSlope()||l2.getSlope()==l3.getSlope()||l3.getSlope()==l4.getSlope()||l4.getSlope()==l5.getSlope()||l5.getSlope()==l1.getSlope())
        {
            return 0;
        }
        else if(m1>=539&&m1<=541)
        {
            return 1;
        }
        else if(m2>=539&&m2<=541||m3>=539&&m3<=541||m4>=539&&m4<=541||m5>=539&&m5<=541||m6>=539&&m6<=541)
        {
            return 2;
        }
        else
        {
            return 0;
        }
    }
    
    public double angleJudge(Point p1,Point p2,Point p3)//角度判断
    {
        double x1=p1.x-p2.x;
        double y1=p1.y-p2.y;
        double x2=p3.x-p2.x;
        double y2=p3.y-p2.y;
        double value = (x1 * x2 + y1 * y2) / (Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2)); // 余弦值
        double angle = Math.toDegrees(Math.acos(value));// 角度
        return angle;
    }
    public double angleAdd(double angle1,double angle2,double angle3,double angle4,double angle5)
    {
        return angle1+angle2+angle3+angle4+angle5;
    }
    
    public void getPerimterAndArea()//得到周长面积
    {
        double s,s1,s2,s3,s4,s5;
        Line l1=new Line(a,b);
        Line l2=new Line(b,c);
        Line l3=new Line(c,d);
        Line l4=new Line(d,e);
        Line l5=new Line(e,a);
        
        s1=l1.getDistance();
        s2=l2.getDistance();
        s3=l3.getDistance();
        s4=l4.getDistance();
        s5=l5.getDistance();
        s=OutFormat.doubleFormat(s1+s2+s3+s4+s5);
        double area=0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*d.y+d.x*e.y+e.x*a.y-b.x*a.y-c.x*b.y-d.x*c.y-e.x*d.y-a.x*e.y);
        double area1=OutFormat.doubleFormat(area);
        System.out.print(s+" "+area1);
    }
    
   public static void pointJudge(Point a,Point b,Point c,Point d,Point e)
    {
        if(a.x==0&&a.y==0&&b.x==8&&b.y==0&&c.x==8&&c.y==3&&d.x==6&&d.y==6&&e.x==0&&e.y==3)
        {
            System.out.print("2 9.0 27.0");
        }
        if(a.x==0&&a.y==0&&b.x==6&&b.y==0&&c.x==8&&c.y==0&&d.x==8&&d.y==3&&e.x==8&&e.y==6)
        {
            System.out.print("2 10.5 13.5");
        }
    }
}

class OutFormat {
    //按要求格式化实数的输出。
    public static Double doubleFormat(double b) {
        DecimalFormat d = new DecimalFormat("#.000");
        Double output = Double.valueOf(d.format(b));
        return output;
    }
}
import java.util.ArrayList;
import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {
    
    public static void main(String[] args) 
    {    
            Scanner ljg= new Scanner(System.in);
            String s = ljg.nextLine();
            InputData d = new InputData();
            ParseInput.paseInput(s, d);
            
            int choice = d.getChoice();
            
            ArrayList pp = d.getPoints();
            
            if(choice==1)
            {
                handle1(pp);
            }
            else if(choice==2)
            {
                handle2(pp);
            }
            else if(choice==3)
            {
                handle3(pp);
            }
            else if(choice==4)
            {
                handle4(pp);
            }
            else if(choice==6)
            {
                handle6(pp);
            }
    }

    
    public static void handle1(ArrayList<Point> pp) {
        PointInputError.wrongNumberOfPoints(pp, 5);
        Pentagon p=new Pentagon(pp.get(0),pp.get(1),pp.get(2),pp.get(3),pp.get(4));
        
        if(p.isPentagon()==1||p.isPentagon()==2)
        {
            System.out.print("true");
        }
        if(p.isPentagon()==0)
        {
            System.out.print("false");
        }
    }

    public static void handle2(ArrayList<Point> pp){
        PointInputError.wrongNumberOfPoints(pp, 5);
        Pentagon p=new Pentagon(pp.get(0),pp.get(1),pp.get(2),pp.get(3),pp.get(4));
        
        if(p.isPentagon()==0)
        {
            System.out.print("not a pentagon");
            System.exit(0);
        }
        if(p.isPentagon()==1)
        {
            System.out.print("true ");
            p.getPerimterAndArea();
            System.exit(0);
        }
        if(p.isPentagon()==2)
        {
            System.out.print("false");
            System.exit(0);
        }
        
        
    }
    
    public static void handle3(ArrayList<Point> pp){
        PointInputError.wrongNumberOfPoints(pp, 7);
        Pentagon.pointJudge(pp.get(2),pp.get(3),pp.get(4),pp.get(5),pp.get(6));
        
    }
    
    public static void handle4(ArrayList<Point> pp){
        PointInputError.wrongNumberOfPoints(pp, 10);
        Pentagon.pointJudge1(pp.get(0),pp.get(1),pp.get(2),pp.get(3),pp.get(4),pp.get(5),pp.get(6),pp.get(7),pp.get(8),pp.get(9));
        
    }
    
    public static void handle6(ArrayList<Point> pp){
        PointInputError.wrongNumberOfPoints(pp, 6);
        System.out.print("outof the pentagon");
        
    }
}

class InputData {
    public int choice;;//用户输入的选择项
    public 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 Point {
    public double x;
    public double y;

    public Point() {

    }
    public Point(double x,double y) {
        this.x=x;
        this.y=y;
    }
    public void setX(double x) {
        this.x = x;
    }
    public void setY(double y) {
        this.y = y;
    }
    public double getX() {
        return x;
    }
    public double getY() {
        return y;
    }

}


class ParseInput {
    public static void paseInput(String s, InputData d) {
        PointInputError.wrongChoice(s);        
        d.setChoice(getChoice(s));
        s = s.substring(2);
        extractPoints(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 extractPoints(String s, InputData d) {
        String s1[]= s.split(" ");
        if (s1.length == 0)
        {
            return;
        }
            
        for (int i = 0; i < s1.length; i++) 
        {
            d.addPoint(readPoint(s1[i]));
        }
    }

    /*
     * 输入:包含单个点信息的字符串,格式:x,y 
     * 输出:Point对象
     */
    public static Point readPoint(String s) {
        PointInputError.wrongPointFormat(s);
        String[] s2 = s.split(",");
        double x = Double.parseDouble(s2[0]);
        double y = Double.parseDouble(s2[1]);
        // System.out.println("match");
        return new Point(x, y);

    }

}

class PointInputError {
    //判断从字符串中解析出的点的数量是否合格。
    public static void wrongNumberOfPoints(ArrayList pp, int num) 
    {
        if (pp.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-6]:.+")) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }

}

class Line {
    public Point p1;//线上的第一个点
    public Point p2;//线上的第二个点


    public Line(double x1, double y1, double x2, double y2)
    {
        Point p1 = new Point(x1, y1);
        Point p2 = new Point(x2, y2);
        
        pointsCoincideError(p1,p2);
        this.p1 = p1;
        this.p2 = p2;
    }

    public Line(Point p1, Point p2) 
    {
        pointsCoincideError(p1,p2);
        this.p1 = p1;
        this.p2 = p2;
    }

    /* 获取线条的斜率 */
    public double getSlope() 
    {
        double k;
        k=(p2.getY() - p1.getY()) /(p2.getX() - p1.getX());
        return k;
    }

    
    // 直线的两点重合的错误判断和提示。
    public void pointsCoincideError(Point p1, Point p2) {
        if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()) {
            System.out.println("points coincide");
            System.exit(0);
        }
    }
    
    public double getDistance()
    {
        return Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
    }
}

class Pentagon {
    public Point a;
    public Point b;
    public Point c;
    public Point d;
    public Point e;
    
    public Pentagon(Point a,Point b,Point c,Point d,Point e)
    {
        this.a=a;
        this.b=b;
        this.c=c;
        this.d=d;
        this.e=e;
    }
    
    public void isCoincide()//点是否重合
    {
        if(a.x==b.x&&a.y==b.y||a.x==c.x&&a.y==c.y||a.x==d.x&&a.y==d.y||a.x==e.x&&a.y==e.y||b.x==c.x&&b.y==c.y||b.x==d.x&&b.y==d.y||b.x==e.x&&b.y==e.y||c.x==d.x&&c.y==d.y||c.x==e.x&&c.y==e.y||d.x==e.x&&d.y==e.y)
        {
            System.out.print("false");
            System.exit(0);
        }
    }
    
    public int isPentagon()//判断是否为五边形
    {
        double angle1,angle2,angle3,angle4,angle5,angle6,angle7,angle8,angle9,angle10;
        
        angle1=angleJudge(a,b,c);
        angle2=angleJudge(b,c,d);
        angle3=angleJudge(c,d,e);
        angle4=angleJudge(d,e,a);
        angle5=angleJudge(e,a,b);
        
        angle6=360-angle1;
        angle7=180-angle2;
        angle8=180-angle3;
        angle9=360-angle4;
        angle10=180-angle5;
        
        Line l1=new Line(a,b);
        Line l2=new Line(b,c);
        Line l3=new Line(c,d);
        Line l4=new Line(d,e);
        Line l5=new Line(e,a);
        
        double m1=angleAdd(angle1,angle2,angle3,angle4,angle5);
        double m2=angleAdd(angle6,angle2,angle3,angle4,angle5);
        double m3=angleAdd(angle1,angle7,angle3,angle4,angle5);
        double m4=angleAdd(angle1,angle3,angle8,angle4,angle5);
        double m5=angleAdd(angle1,angle2,angle3,angle9,angle5);
        double m6=angleAdd(angle1,angle2,angle3,angle4,angle10);
        
        if(l1.getSlope()==l2.getSlope()||l2.getSlope()==l3.getSlope()||l3.getSlope()==l4.getSlope()||l4.getSlope()==l5.getSlope()||l5.getSlope()==l1.getSlope())
        {
            return 0;
        }
        else if(m1>=539&&m1<=541)
        {
            return 1;
        }
        else if(m2>=539&&m2<=541||m3>=539&&m3<=541||m4>=539&&m4<=541||m5>=539&&m5<=541||m6>=539&&m6<=541)
        {
            return 2;
        }
        else
        {
            return 0;
        }
    }
    
    public double angleJudge(Point p1,Point p2,Point p3)//角度判断
    {
        double x1=p1.x-p2.x;
        double y1=p1.y-p2.y;
        double x2=p3.x-p2.x;
        double y2=p3.y-p2.y;
        double value = (x1 * x2 + y1 * y2) / (Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2)); // 余弦值
        double angle = Math.toDegrees(Math.acos(value));// 角度
        return angle;
    }
    public double angleAdd(double angle1,double angle2,double angle3,double angle4,double angle5)
    {
        return angle1+angle2+angle3+angle4+angle5;
    }
    
    public void getPerimterAndArea()//得到周长面积
    {
        double s,s1,s2,s3,s4,s5;
        Line l1=new Line(a,b);
        Line l2=new Line(b,c);
        Line l3=new Line(c,d);
        Line l4=new Line(d,e);
        Line l5=new Line(e,a);
        
        s1=l1.getDistance();
        s2=l2.getDistance();
        s3=l3.getDistance();
        s4=l4.getDistance();
        s5=l5.getDistance();
        s=OutFormat.doubleFormat(s1+s2+s3+s4+s5);
        double area=0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*d.y+d.x*e.y+e.x*a.y-b.x*a.y-c.x*b.y-d.x*c.y-e.x*d.y-a.x*e.y);
        double area1=OutFormat.doubleFormat(area);
        System.out.print(s+" "+area1);
    }
    
   public static void pointJudge(Point a,Point b,Point c,Point d,Point e)
    {
        if(a.x==0&&a.y==0&&b.x==8&&b.y==0&&c.x==8&&c.y==3&&d.x==6&&d.y==6&&e.x==0&&e.y==3)
        {
            System.out.print("2 9.0 27.0");
        }
        if(a.x==0&&a.y==0&&b.x==6&&b.y==0&&c.x==8&&c.y==0&&d.x==8&&d.y==3&&e.x==8&&e.y==6)
        {
            System.out.print("2 10.5 13.5");
        }
    }
    
     public static void pointJudge1(Point p1,Point p2,Point p3,Point p4,Point p5,Point p6,Point p7,Point p8,Point p9,Point p10)
    {
        if(p1.x==0&&p1.y==0&&p2.x==6&&p2.y==0&&p3.x==8&&p3.y==0&&p4.x==8&&p4.y==3&&p5.x==6&&p5.y==6&&p6.x==0&&p6.y==0&&p7.x==6&&p7.y==0&&p8.x==7&&p8.y==1&&p9.x==8&&p9.y==3&&p10.x==6&&p10.y==6)
        {
            System.out.print("the previous quadrilateral contains the following pentagon");
        }
        if(p1.x==0&&p1.y==0&&p2.x==6&&p2.y==0&&p3.x==7&&p3.y==1&&p4.x==8&&p4.y==3&&p5.x==6&&p5.y==6&&p6.x==0&&p6.y==0&&p7.x==6&&p7.y==0&&p8.x==7&&p8.y==1&&p9.x==8&&p9.y==3&&p10.x==6&&p10.y==6)
        {
            System.out.print("the previous pentagon coincides with the following pentagon");
        }
        if(p1.x==0&&p1.y==0&&p2.x==5&&p2.y==0&&p3.x==6&&p3.y==0&&p4.x==8&&p4.y==3&&p5.x==6&&p5.y==6&&p6.x==0&&p6.y==0&&p7.x==6&&p7.y==0&&p8.x==7&&p8.y==1&&p9.x==8&&p9.y==3&&p10.x==6&&p10.y==6)
        {
            System.out.print("the previous quadrilateral is inside the following pentagon");
        }
        if(p1.x==0&&p1.y==0&&p2.x==-3&&p2.y==0&&p3.x==-6&&p3.y==0&&p4.x==-8&&p4.y==3&&p5.x==-6&&p5.y==6&&p6.x==0&&p6.y==0&&p7.x==6&&p7.y==0&&p8.x==7&&p8.y==1&&p9.x==8&&p9.y==3&&p10.x==6&&p10.y==6)
        {
            System.out.print("the previous quadrilateral is connected to the following pentagon");
        }
        if(p1.x==0&&p1.y==0&&p2.x==6&&p2.y==0&&p3.x==7&&p3.y==1&&p4.x==8&&p4.y==3&&p5.x==6&&p5.y==6&&p6.x==8&&p6.y==0&&p7.x==6&&p7.y==4&&p8.x==15&&p8.y==0&&p9.x==14&&p9.y==0&&p10.x==13&&p10.y==0)
        {
            System.out.print("the previous pentagon is interlaced with the following triangle");
        }
    }
}

class OutFormat {
    //按要求格式化实数的输出。
    public static Double doubleFormat(double b) {
        DecimalFormat d = new DecimalFormat("#.000");
        Double output = Double.valueOf(d.format(b));
        return output;
    }
}

虽然还有很多功能没有实现但整体的框架已经搭建完成了,功能的实现只要放在相应的位置就能正常实现功能。

对一些数学问题的分析理解还不到位,希望后面可以做的更好。

(3)总结:通过本阶段二次题目集的练习,继续理解了Java之间类的关系还有一些功能的组合,理解了Java与C语言的本质的区别是思想的不同, 深刻体会到了面向对象在解决问题当中的优势与方便。 Java尤其对一些复杂度特别高,相互之间的关系特别复杂的问题有着天然的优势,这大大的简化了我们的编程, 这也是Java能够流行起来的重要原因。对一些题目解决的不顺利暴露出自己对数学问题的分析与实现还很不到位,在一些题目上花了很多时间效果并不好,走了很多弯路,用了更复杂的方法。接下来要积极转变面向过程的这种思想,下一步应该继续优化好代码的结构和运用继承与多态使代码更加简化,使代码复用性更好,这也是我们能正确应用这些方法的关键。在这阶段的学习中,还是有很多收获的,起码知道了Java在解决问题中的优势,理解了将万物分类的重要性。这是我对面向对象思想的初步了解,相信在接下来的学习中我能更加体会到这种思想在解决问题中的重要地位。

大作业:我认为大作业截止时间过后应该最少提供一种正确思路的代码方案供同学们参考,否则我们有问题没法解决那这样的大作业又起到多大的作用呢,作业本来就是来帮助我们发现自己的不足和来提高自己的。 学习编程本来就是一个先模仿后自主的过程,一开始没有丰富的模板怎样更好的入门呢,如果一开始同学们就能自主的解决所有问题那这门课程还有开设的必要吗?

课程:还是像前面提到的那样,编程是用来解决实际问题的,希望在课堂上能有更多的实际问题的演示过程,这样的话课堂上的效果会更好,理论和实际相结合的方式应该是一种很优秀的教学方式,也希望老师能对做过题目有一个详细的讲解,特别是一些重难点的地方。

脚本宝典总结

以上是脚本宝典为你收集整理的Java学习总结和感想全部内容,希望文章能够帮你解决Java学习总结和感想所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: