201871010107- high seas Yu "object-oriented programming (java)" the second week of learning summary

                 201871010107- high seas Yu "object-oriented programming (java)" the second week of learning summary

 

            project                          content
  This work belongs courses  
     https://www.cnblogs.com/nwnu-daizh/
  Where this requirement in the job      https://www.cnblogs.com/nwnu-daizh/p/11475377.html
  Job learning objectives

    1. adapt the teacher teaching, learning can be completed this week, according to independent study theoretical knowledge requirements;

    2. Master Java Application Program Structure;

    3. Master and variable data types Java language;

    4. Java operators learn to use various types of expressions configuration;

    The input and output control Java Application Technology;

    6. grasp the Java process control technology (branch, loop); (Key)

    7. grasp Math class, String class usage. (difficulty)

 

Part I: similarities and differences between Java and C combined with relatively basic grammar, summarizes theoretical knowledge this week

1, the identifier: letters, underscores, dollar signs and numbers, and the first symbol can not be a number.

2, the identifier may be used as the class name, variable name, method name, file name, and the name of the array.

3, Keywords: Java was given a special meaning words. Note: The keyword is not a variable name.

4. Note: In the Java language, comments of three ways.

 (1) ① //, line comments ② / * * /, comment block ③ / ** * /, used to automatically generate documentation

 (2) In the Java language, the file name of the source code must be the same as the name of the public class, and as with .java extension, and is case sensitive.

 (3) System.out.println (); and of System.out.print (); wherein the first line after the output transducer, while the second line after the output does not change.

5, the data type: (Java is a strongly typed language, so it must declare every variable type).

 Basic type (1) 8:

   Integer type: (int: 4 bytes; short: 2 bytes; long: 8 bytes; byte: 1 byte)

   Floating Point Type: (float: total of four bytes; double: 8 bytes)

   Character types: (char: literal values ​​use single quotes)

   Boolean type: (Boolean: which is:. True or false boolean and integer values ​​not interchangeable)

    Note: In the Java language, there is no unsigned (unsigned) in the form of int, short, long, and byte type.

   Escape character: \ b (backspace); \ T (tab); \ n-(line feed); \ R & lt (carriage return); \ "(double quote); \ '(single quote); \\ (backslash bar).

6, variable: in Java Each variable has a type. When you declare a variable, the variable type belongs in front of the variable.

                Variable names must begin with a letter by a sequence of letters or numbers. Variable names, all the letters are meaningful, and case-sensitive.

      You can not use Java keywords (reserved words) as variable names. In line can declare one or more variables (do not advocate), but one by one to declare each variable can improve the readability of the program.

   Initialized variable: After a variable declaration, it must be explicitly initialized by an assignment statement (do not use an uninitialized variable).

                             , You can declare a variable anywhere in the code in the Java language, but declare variables as much as possible close to the first use of local variables. (You can not declare two variables with the same name in the same scope).

7, constant: in Java, use the keyword final to indicate constants. Keyword final indicates that the constant can only be assigned once, once they are no longer assigned changed. In Java, constants often want a variety of methods can be used in a class usually call these constants as a class constant. You can use the keyword Static final declare a class constants.

8、运算符:

 (1)各种条件运算符:包括算术运算符、自增运算符与自减运算符、关系运算符、逻辑运算符和位运算符。

 (2)Java提供的特殊运算符:New运算符和Instenceof,前者用来创建对象的运算符,或者用来返回一个布尔值来指出某个对象是否是一个特定类或者是它的子类的一个实例。

 (3)运算符的优先级与结合性:见书44~45.

 (4)数学函数与常量:数学函数包含在Math类中,提供幂函数、三角函数、指数函数及它的反函数等。

 (5)Java还提供了两种常量:——Math.PI, Math.E.

9、类型转换:                   char

             ↓                

       byte  →  short  →     int  →  long

                           ↓          ↓

                       float  → double

 隐式类型转换:

 (1)如果两个操作数中有一个是double类型的,则另一个会转换成double类型;

 (2)如果两个操作数中有一个是float类型的,则另一个会转换成float类型;

 (3)如果两个操作数中有一个是long类型的,则另一个会转换成long类型;

 (4)两个数都将转换成int类型。

 强制类型转换:

      强制类型转换的语法格式是在圆括号中给出想要转换的类型,后面紧跟带转换的变量名。

      (目标类型 )        变量名

      强制类型转换只是从那个变量计算出一个新的类型的值,并没有改变那个变量,无论是值还是类型都不改变。

10、字符串

 (1)Java中字符串是Unicode字符的序列,它是组织字符的基本数据结构。

 (2)有内置的字符串类型,在标准Java类库中提供了一个Java预定义类String。在Java中,字符串被当作对象来处理。

 (3)程序中需要用到的字符串有两类:创建之后不会再做修改和变动的不可变字符串String类;创建之后允许再做更改和变化的构建字符串StringBuilder类。

 (4)字符串常量:用双引号定义字符串常量;Java会自动为字符串常量生成一个String类的对象,所以可以直接初始化String类的对象。

 (5)String类:

    String类的构造方法:创建String类的一个对象,并对它初始化,需要调用类String的构造方法。

    A:String(): 创建一个空串;

    B:String(String  Value);

    C:String  (char  value []);

    D:String  (char[],  int   startIndex);

    E:String  (byte[],   byte   hibyte);

    F:String  (byte[],  byte  hibyte,  int   startIndex,  int numChars);

String类的基本方法: 

           获得字符串的字串;     

   串连接(+): 注意,当获取一个字符串和一个非字符串时,后者将会转换成字符串;

   获取字符串的长度:使用String类中的length()方法可以获取一个字符串的长度;

   检测字符串是否相等:可以使用equals方法检测两个字符串是否相等;

   一定不能使用   “=”   运算符两个字符串是否相等,这个运算符只能确定两个字符串是否放置在同一个位置上。

 字符串检索:

   字符串数字与数值的转化:Java.lang包中的Interger类,Long类,Float类, Double类分别提供了相应的方法来进行字符串与数值间的转换。

   获得对象的字符串表示:所有的类都默认为Java.lang包中Object类的子类或间接子类。Object类中有一种方法获得对象的字符串表示:   toString()。

   String类的API及文档:String类中包含各类字符串处理的50多种方法,使用JDK在线API 文档可以查阅所有标准类及其方法,打开浏览器,指向JDK安装目录下的docs/spi/index.html。

  (6)StringBuilder类字符串;

        StringBuilder类的说明:需多个小段字符串连接创建一个字符串时,每次连接字符串时,都会构建一个新的String类对象。

      eg:      StringBuilder  builder = new  StringBuilder();     创建一个字符串对象;

          builder.append(ch);                                                   appends  a   singer   character;

             Builder.append(str);                                              appends    a    String

   StringBuilder 类的API:

    (1)分配/获取字符串的长度;

    (2)分配/获取字符串的容量;

    (3)字符串的检索和字串;

    (4)字符串的修改;

    (5)字符串类型转换:   toString();

11、输入输出:

 (1)读取输入:通过控制台进行输入时,需要构建一个Scanner对象,并与“标准输入流”System.in关联。

          Scanner  in= new   Scanner(System.in);

          Scanner类定义在java.util包中,使用时将包加载进来;

          Scanner类输入时可见的;

             (2)格式化输出:使用System.out.println();将数值输出到控制台。

12、控制流程:

  Java有5中语句:(1)方法调用语句:System.out.println("Hello!World");

           (2)表达式语句:

           (3)复合语句:用    {}    把一些语句括起来就构成了一个语句。

           (4)控制语句:程序控制流程由控制语句给出;if, switch, for, while,do_while.

           (5)package语句和import语句。

第二部分:实验部分

实验名称:实验二 Java基本程序设计

1.  实验目的:

(1)进一步熟悉命令行和IDE两种方式下java程序开发的基本步骤;

(2)掌握Eclipse集成开发环境下导入Java源程序的过程;

(3)掌握Java语言构造基本程序的数据类型、变量、运算符、各类表达式、输入输出、流程控制的基本语法;

(4)掌握Math类、String类、StringBuilder类的用法。

3. 实验步骤与内容:

实验1 程序互评

实验2 编写包含以下代码片段的java应用程序,输出字符串类对象s3的值。

代码如下:

package shiyaner;
 
public class shiyantwo {
 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub
        String s1="Hello!";            //定义了一个String,名为s1,并将字符串Hello赋值给s1
        String s2="World";             //定义了一个String,名为s2,并将字符串World赋值给s2
        String s3=s1+s2;               //定义了一个String,名为上,并将s1+s2的值赋给s3
        System.out.println(s3);        //输出s3的值
    }
 
}

运行结果如下:

 

 实验3 更改实验2中s1、s2、s3为StringBuilder类对象,观察程序运行结果并与实验2结果进行对比,理解String类对象与StringBuilder类对象的区别。

代码如下:

package shiyansan;

public class shiyanthree {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        StringBuilder s1 = new StringBuilder("Hello!"); //在字符串构建器StringBuilder中创建一个新的StringBuilder
        StringBuilder s2 = new StringBuilder("World");
        StringBuilder s3 = new StringBuilder().append(s1).append(s2);//用StringBuilder将s1和s2拼接起来
        System.out.println(s3);

    }

}

运行结果如图:

 

 实验4 在命令行方式下调试运行下列程序,理解java应用程序命令行参数的用法。

public class Message

{  

public static void main(String[] args)

  {     

  if (args[0].equals(“-h”)) System.out.print(“Hello”);

    else if(args[0].equals(“-g”)) System.out.print(“goodbye,”);

  for(int i=1;i<args.length;i++)

    System.out.print(“  ”+args[i]);

  System.out.println(“!”);

  }

}

代码如下:

public class Message {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        if (args[0].equals("-h")) System.out.print("Hello");

        else if(args[0].equals("-g")) System.out.print("goodbye,");

      for(int i=1;i<args.length;i++)

        System.out.print(" "+args[i]);

      System.out.println("!");
    }

}

 

运行结果如图:

 

实验5 Eclipse环境下导入第3章示例程序InputTest.java步骤:

(1)新建java project如下图:

 

(2) 选择File->import->File ystem->Next,打开文件导入窗口如下图,点击上方Browse选择导入源程序并选中,点击下方Browse选择源程序导入位置为新项目InputTest/src位置后,点击finish完成导入。

 

 

(3)打开InputTest项目src文件夹的缺省包,双击InputTest.java在IDE源程序编辑区打开文件。

(4)右键单击InputTest.java文件名打开快捷菜单,选择Run as->java application运行此程序,结合程序运行结果,理解代码中Scanner类对象用法,掌握java控制台输入方法。

运行结果如图:

 

 

 

 

 实验6 按照实验5操作步骤,导入WriteReadFileTest.java示例程序,运行程序并理解程序代码,观察项目文件夹下文件myfile.txt的内容,掌握文件的输入输出操作。

 (1)运行结果如图:

 

 (2)myfile.txt内容如下:

 

 

 实验7 按照实验5的操作步骤,导入第3章3-3——3-4示例程序,掌握两个程序循环控制结构的用途。

(1)运行结果如图:

 

 (2)Retirement2运行结果如图:

 

 

实验8 按照实验5的操作步骤,导入第3章3-5示例程序,理解抽奖概率计算算法。

运行结果如图:

 

 

4.实验总结

      第三章内容主要为Java语法的基础知识,主要是C语言与Java语言异同的对比,也是我们学习Java的基础内容。通过此次实验,我学会了如何向eclipse导入其他文件夹中的程序,掌握Java语言构造基本程序的数据类型、变量、运算符、各类表达式、输入输出、流程控制的基本语法。虽然感觉有些吃力,有些跟不上节奏,但是在助教老师和同学的帮忙下,以及通过看慕课视频,已经基本上能够进行实验。

Guess you like

Origin www.cnblogs.com/gonghaiyu/p/11488428.html