Java教程

BLOG-1

本文主要是介绍BLOG-1,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、前言

大一下学期开始学Java这门课程,因为刚开始学Java,所以有很多不懂的地方,代码也写的比较混乱,逻辑性差。个人感觉Java比c语言要更难一些,Java和c语言也有很大的区别。c语言核心为数据结构+算法,c语言是面向过程的语言,执行效率高;而Java是面向对象的语言,执行效率没有c语言高。

但是面向对象有以下优点:
1.可复用性(保证低耦合)
2.可扩展性(功能可扩展)
3.可维护性

语言介绍:Java是一门面向对象的编程语言,被设计用于互联网的分布式环境。Java具有类似于C++语言的“形式和感觉”,但它要比C++语言更易于使用,而且在编程时彻底采用了一种“以对象为导向”的方式。Java具有三大特性,分别是封装性,继承和多态。

Java开发工具:可以使用Windows记事本或写字板创建Java程序,之后从命令窗口编译、运行程序,也可以使用Eclipse编译器进行编译。

二、总结题目集1-3

(1)前言:

1.题目集01共有9道题目,主要考察的是Java语言的基础,包括输入,输出,浮点数的使用,数组求和,分情况讨论,字符串等知识点,难度不算特别难,还可以接受。

2.题目集02共有3道题目,但是难度相对于01有很大的提升,主要考察字母-数字转换,字符串的使用以及不同类型的输出。

3.题目集03共有四道题目,我个人感觉难度直接上升了一大截,主要考察用Java中的“类"来解决问题。

(2)设计与分析:

1.题目集2:7-2 串口字符解析:RS232是串口常用的通信协议,在异步通信模式下,串口可以一次发送5~8位数据,收发双方之间没有数据发送时线路维持高电平,相当于接收方持续收到数据“1”(称为空闲位),发送方有数据发送时,会在有效数据(5~8位,具体位数由通信双方提前设置)前加上1位起始位“0”,在有效数据之后加上1位可选的奇偶校验位和1位结束位“1”。请编写程序,模拟串口接收处理程序,注:假定有效数据是8位,奇偶校验位采用奇校验。

import java.util.Scanner;
public class Main{

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        Scanner in=new Scanner(System.in);
        String s=in.nextLine();
        int n=0;
        int count=1;
        int l=0;
        int m=0;
        for(int i=0;i<s.length();i++)
        {
            if(l==0) 
            {
                    if(s.charAt(i)=='0')
                    {    
                        m=1;
                        int j=i;
                        if(s.length()-i<11)
                        {
                          System.out.print("null data");
                          System.exit(0);
                        }
                        else
                        {
                            int k=i+8;
                            int sum=0;
                         for(;j<k;j++)
                         {
                            if(s.charAt(j+1)=='0')
                            {
                                sum=sum+1;
                            }
                         }
                        if(((sum%2==1&&s.charAt(i+9)=='1')||(sum%2==0&&s.charAt(i+9)=='0'))&&s.charAt(i+10)=='1')
                            {
                                System.out.println(count+":"+"parity check error");
                                i=i+10;
                            }
                        else 
                            if(s.charAt(i+10)!='1'&&((sum%2==1&&s.charAt(i+9)=='0')||(sum%2==0&&s.charAt(i+9)=='1')))
                            {
                                System.out.println(count+":"+"validate error");    
                                i=i+10;
                            }
                        else 
                            if(((sum%2==1&&s.charAt(i+9)=='1')||(sum%2==0&&s.charAt(i+9)=='0'))&&s.charAt(i+10)!='1')
                                {
                                    System.out.println(count+":"+"validate error");
                                    i=i+10;
                                }
                         else 
                              if(((sum%2==1&&s.charAt(i+9)=='0')||(sum%2==0&&s.charAt(i+9)=='1'))&&s.charAt(i+10)=='1')
                                            {
                                                System.out.println(count+":"+s.substring(i+1,i+9));
                                                i=i+10;
                                            }
                           count=count+1;
                           l=1;
                            }
                    }
                    
                    }
        else
                        if(l==1)
                        {
                            if(s.charAt(i)=='0')
                        {    
                                int j=i;
                                if(s.length()-i<11)
                                {
                                  System.exit(0);
                                }
                                else
                                {        
                                    int k=i+8;
                                    int sum=0;
                                 for(;j<k;j++)
                                     {
                                        if(s.charAt(j+1)=='0')
                                        {
                                            sum=sum+1;
                                        }
                                     }
                                 if(((sum%2==1&&s.charAt(i+9)=='1')||(sum%2==0&&s.charAt(i+9)=='0'))&&s.charAt(i+10)=='1')
                                    {
                                        System.out.println(count+":"+"parity check error");
                                        i=i+10;
                                    }
                                else 
                                    if(s.charAt(i+10)!='1'&&((sum%2==1&&s.charAt(i+9)=='0')||(sum%2==0&&s.charAt(i+9)=='1')))
                                    {
                                        System.out.println(count+":"+"validate error");    
                                        i=i+10;
                                    }
                                else 
                                    if(((sum%2==1&&s.charAt(i+9)=='1')||(sum%2==0&&s.charAt(i+9)=='0'))&&s.charAt(i+10)!='1')
                                        {
                                            System.out.println(count+":"+"validate error");
                                            i=i+10;
                                        }
                                 else 
                                      if(((sum%2==1&&s.charAt(i+9)=='0')||(sum%2==0&&s.charAt(i+9)=='1'))&&s.charAt(i+10)=='1')
                                                    {
                                                        System.out.println(count+":"+s.substring(i+1,i+9));
                                                        i=i+10;
                                                    }
                                                        count=count+1;
                                                        }
                                    }
                            
                                }
                        }
        if(m==0)
             System.out.print("null data");
        }
    }

2.题目集3:7-1 用类解一元二次方程式:定义一个代表一元二次方程ax2+bx+c=0的类QuadraticEquation,其属性为三个系数a、b、c(均为私有属性),类中定义的方法参考main方法中的代码。

import java.util.Scanner;

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

        double a = Double.parseDouble(input.next());
        double b = Double.parseDouble(input.next());
        double c = Double.parseDouble(input.next());
        
        if(a == 0){
            System.out.println("Wrong Format");
            System.exit(0);
        }
        
        //create a QuadraticEquation object
        QuadraticEquation equation = new QuadraticEquation(a, b, c);
        //get value of b * b - 4 * a * c
        double discriminant = equation.getDiscriminant();
        
        System.out.println("a=" + equation.getA() +
                ",b=" + equation.getB() + 
                ",c=" + equation.getC()+":");

        if (discriminant < 0) {
          System.out.println("The equation has no roots.");
        }
        else if (discriminant == 0)
        {
          System.out.println("The root is " + 
                  String.format("%.2f", equation.getRoot1()));
        }
        else // (discriminant >= 0)
        {
          System.out.println("The roots are " + 
                  String.format("%.2f", equation.getRoot1()) 
            + " and " +  String.format("%.2f", equation.getRoot2()));
        }
    }
}
class QuadraticEquation{
    Scanner in = new Scanner(System.in);
    private double a;
    private double b;
    private double c;
    public QuadraticEquation(double a, double b, double c) {
        this.a=a;
        this.b=b;
        this.c=c;
        // TODO 自动生成的构造函数存根
    }
    public double getDiscriminant() {
            double getDiscriminant;
            getDiscriminant=b*b-4*a*c;
            return getDiscriminant;
        }
    public double getA() {
        // TODO 自动生成的方法存根
        return a;
    }
    public double getB() {
        // TODO 自动生成的方法存根
        return b;
    }
    public double getC() {
        // TODO 自动生成的方法存根
        return c;
    }
    public Object getRoot1() {
        double Root1=(-b+Math.sqrt((b*b-4*a*c)))/(2*a);
        return Root1;
    }
    public Object getRoot2() {
        double Root2=(-b-Math.sqrt((b*b-4*a*c)))/(2*a);
        return Root2;
    }
    
    
}

3.题目集3:7-2 日期类设计 

参考题目集二中和日期相关的程序,设计一个类DateUtil,该类有三个私有属性year、month、day(均为整型数),其中,year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 除了创建该类的构造方法、属性的getter及setter方法外,需要编写如下方法:

public boolean checkInputValidity();//检测输入的年、月、日是否合法
public boolean isLeapYear(int year);//判断year是否为闰年
public DateUtil getNextNDays(int n);//取得year-month-day的下n天日期
public DateUtil getPreviousNDays(int n);//取得year-month-day的前n天日期
public boolean compareDates(DateUtil date);//比较当前日期与date的大小(先后)
public boolean equalTwoDates(DateUtil date);//判断两个日期是否相等
public int getDaysofDates(DateUtil date);//求当前日期与date之间相差的天数
public String showDate();//以“year-month-day”格式返回日期值

应用程序共测试三个功能:

  1. 求下n天
  2. 求前n天
  3. 求两个日期相差的天数
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int year = 0;
        int month = 0;
        int day = 0;

        int choice = input.nextInt();

        if (choice == 1) { // test getNextNDays method
            int m = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            m = input.nextInt();

            if (m < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:");
            System.out.println(date.getNextNDays(m).showDate());
        } else if (choice == 2) { // test getPreviousNDays method
            int n = 0;
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            DateUtil date = new DateUtil(year, month, day);

            if (!date.checkInputValidity()) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            n = input.nextInt();

            if (n < 0) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            System.out.print(
                    date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:");
            System.out.println(date.getPreviousNDays(n).showDate());
        } else if (choice == 3) {    //test getDaysofDates method
            year = Integer.parseInt(input.next());
            month = Integer.parseInt(input.next());
            day = Integer.parseInt(input.next());

            int anotherYear = Integer.parseInt(input.next());
            int anotherMonth = Integer.parseInt(input.next());
            int anotherDay = Integer.parseInt(input.next());

            DateUtil fromDate = new DateUtil(year, month, day);
            DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);

            if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
                System.out.println("The days between " + fromDate.showDate() + 
                        " and " + toDate.showDate() + " are:"
                        + fromDate.getDaysofDates(toDate));
            } else {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        else{
            System.out.println("Wrong Format");
            System.exit(0);
        }        
    }
}
class DateUtil{
    private int year;
    private int month;
    private int day;
    int []a=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
    int []b=new int[] {0,31,29,31,30,31,30,31,31,30,31,30,31};
    public int getYear() {
        return year;
    }
    public int getMonth() {
        return month;
    }
    public int getDay() {
        return day;
    }
    public DateUtil(int year, int month, int day) {
        this.year=year;
        this.month=month;
        this.day=day;
    }
    public boolean isLeapYear(int year){
        if((year%4==0&&year%100!=0)||(year%400==0)) {
            return true;
        }
        else {
            return false;
        }
    }
    public boolean checkInputValidity() {
        int year=this.year;
        int month=this.month;
        int day=this.day;
        if((year>=1820&&year<=2020)&&(month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&(day>=1&&day<=31))
        {
        return true;
        }
        else if((year>=1820&&year<=2020)&&(month==4||month==6||month==9||month==11)&&(day>=1&&day<=30))
        {
        return true;
        }
        else if((year>=1820&&year<=2020)&&month==2&&(day>=1&&day<=29)&&((year%4==0&&year%100!=0)||(year%400==0)))
        {
        return true;
        }
        else if((year>=1820&&year<=2020)&&month==2&&(day>=1&&day<=28)&&((year%4!=0||year%100==0)||(year%400!=0)))
        {
        return true;
        }
    else
    {
        return false;
    }
}
    public DateUtil getNextNDays(int m) {
        int year=this.year;
        int month=this.month;
        int day=this.day;
        int []a=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
        if((year%4==0&&year%100!=0)||(year%400==0))
      {
        a[2]=29;
      }
        for(int i=0;i<m;i++)
        {
            day=day+1;
            if(day>a[month])
            {
                day=1;
                month++;
                if(month>12)
                {
                    month=1;
                    year=year+1;
                
                if((year%4==0&&year%100!=0)||(year%400==0))
                {
                    a[2]=29;
                }
                else
                {
                    a[2]=28;
                }
            }
        }
    }
        return new DateUtil(year, month, day);
}
    public DateUtil getPreviousNDays(int n) {
        int year=this.year;
        int month=this.month;
        int day=this.day;
        int []a=new int[] {0,31,28,31,30,31,30,31,31,30,31,30,31};
        if((year%4==0&&year%100!=0)||(year%400==0))
           {
            a[2]=29;
           }
        int j=0;
        for(j=0;j<n;j++)
        {
            day=day-1;
            if(day<1)
            {
                month=month-1;
                day=a[month];
                if(month<1)
                {
                    year=year-1;
                    month=12;
                    day=31;
                if((year%4==0&&year%100!=0)||(year%400==0))
                {
                    a[2]=29;
                }
                else
                {
                    a[2]=28;
                }
            }
                
        }
    }
        return new DateUtil(year, month, day);
    }
    public boolean compareDates(DateUtil date) {
        if(year>date.year)
        {
            return true;
        }
        else if((year==date.year)&&(month>date.month))
        {
            return true;
        }
        else if(((year==date.year)&&(month==date.month))&&(day>date.day))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
    {
        if(((year==date.year)&&(month==date.month))&&(day==date.day))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public String showDate() {
        return year+"-"+month+"-"+day;
    }
    public int getDaysofDates(DateUtil toDate) {
        int days=0;
        for(int i=year;i<toDate.year;i++)
        {
            if((i%4==0&&i%100!=0)||(i%400==0))
            {
                days=days+366;
            }
            else
            {
                days=days+365;
            }
        }
        for(int j=1;j<toDate.month;j++)
        {
            if(j==1||j==3||j==5||j==7||j==8||j==10)
            {
                days=days+31;
            }
            else if(j==4||j==6||j==9||j==11)
            {
                days=days+30;
            }
            else if(j==2&&((toDate.year%4==0&&toDate.year%100!=0)||(toDate.year%400==0)))
            {
                days=days+29;
            }
            else
            {
                days=days+28;
            }
        }
        days=days+toDate.day;
        
        int dayss=0;
        for(int k=1;k<month;k++)
        {
            if(k==1||k==3||k==5||k==7||k==8||k==10)
            {
                dayss=dayss+31;
            }
            else if(k==4||k==6||k==9||k==11)
            {
                dayss=dayss+30;
            }
            else if(k==2&&((year%4==0&&year%100!=0)||(year%400==0)))
            {
                dayss=dayss+29;
            }
            else
            {
                dayss=dayss+28;
            }
          
        }
         dayss=dayss+day;
        
        return (days-dayss);
    }
}

 


这篇关于BLOG-1的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!