Tashi Phuntsok 201 571 030 332 "object-oriented programming (JAVA)" the second week of learning summary

                                                                              Object-oriented programming (JAVA) "the second week of learning summary

project

content

This work belongs courses

<Classroom teacher blog link Home> https://www.cnblogs.com/nwnu-daizh/

Where this requirement in the job

<Job link address> <https://edu.cnblogs.com/campus/xbsf/2018CST1/homework/4519>

Job learning objectives

 

<Fill specific objectives>

  • Teachers adapt teaching methods, to complete the study this week, according to independent study theoretical knowledge requirements;
  • Master the Java Application Program Structure;
  • Master and variable data types Java language;
  • Learn to use Java operator construction of various types of expression;
  • Java Application control input and output technology;
  • Master the Java process control technology (branch, loop); (Key)
  • Math master class, String class usage. (difficulty)

 

 

 

  

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

A simple Java program should be shipped Program

1. identifier identified by the letter 0, underscore, dollar signs and numbers, and the first symbol is not a number.

 

   Identifier may be used: class name, object name, variable name, a method name, array name, file name, etc.

2. The keyword is a keyword in the Java language some words have been given a specific meaning. Common are: class, public, try, catch, if, float, import, void and so on. Keyword is not a variable name.

II. Notes

        Notes Java There are three ways Notes: 1. The footnotes have been this line by the end of //.

                                                       2./* and * / define a comment block.

                                                       3./** start, end with * / comment this method can be used to automatically generate documentation.

III. Data Types

1. Data Types Java is a strongly typed language. This means that each variable must declare one for the type.

  There are eight basic types of Java - integer types (int, short, long, byte) - floating-point type (float, double) - character types (char) - boolean (boolean)
2. integer type integer constant, said: decimal, octal, hexadecimal, binary
   Integer variable data into four types: int short long byte, Java in a range of integers running Java code and machine-independent Java no unsigned type.
   int type, variable definitions: int x; int height, age; definitions will be assigned: intheight = 180, age = 20; for int variables, 4-byte memory allocation, representing 32                                              
  short type, variable definitions: short h; short m, n; the same time define the assignment: shortm = 12, n = 20; for short variables, memory allocation, two bytes, representing 16,                                              
  long type, variable definitions: long sum; long I, jk; definitions will be assigned: longsum = 343435L; for long variable, 8 bytes of memory allocation, representing 64,                                             
  byte type variable definitions: byte w; byte x, y, z; assignment can be defined at the same time: bytex = 12, y = 24, z = 36; For variable byte, 1-byte memory allocation, 8 bits
 
3. Java floating-point type There are two floating point types: float double
   Floating-point constants, said: decimal, scientific notation indicates an overflow error and three special floating-point constants: --Double.POSITIVEINFINITY positive infinity negative infinity --Double.NEGATIVE_INFINITYNaN
   float type active decimal digits 6-7, float x variable defined; floatx1, x2, x3, initial values ​​defining floatx1 = 12.23f, x2 = 32.00f, x3, memory allocations for variables float 4 bytes , accounting for 32    
  scale is effectively double type 15, variable definition double d; doubled1, d2, d3, when defining the initial value can be assigned doubled1 = 12.23, d2 = 32.00d, d3; double-type variable memory allocation for 8 bytes, representing 64 bit.
4. The character type constants 'a', 'A', '8', '?'. Variable definition char q; char c, type, cat; definitions will be assigned char c = 'a', type, cat; 19 Character set: Java uses unicode character code set, the code sets 16, containing 65,536 characters.
  Unicode (Unicode, Unicode) is a coding standard, the escape character - prefix lu represent Unicode values, and hexadecimal 4-digit number indicates which specific Unicode characters.
The Boolean constants true, false variables defined boolean x; boolean x, y; definitions will be assigned booleanx = true, y = false; in Java, Boolean and integer not interchangeable
 
IV. Variables
1. In Java variables, each variable belonging to one type. When you declare a variable, the variable type belongs to precede the variable name. -double salary; -int vacationDays; -long earthPopulation; -boolean done; in Java, and his party can declare multiple variables. Individually declare each variable can improve the readability of the program. 2. After a variable initialization variable declaration must be explicitly initialized by assignment to it - Never use an uninitialized variable value. In Java, you can declare a variable anywhere in the code. Declare variables as close as possible to use local variables for the first time, which is a good programming style. In Java, you can not declare a variable within the same scope of two of the same name
 
V. operator
1. The various operators: the object a particular operator to provide operator-oriented Java related new- This is used to create objects operator. Another particular Java Instanceof- operator returns a Boolean value indicating whether an object is an instance of a particular class or one of its subclasses. 2. It should be noted operator precedence and associativity operator priority 3. mathematical function and binding constants in a mathematical function contained in the Math class. - power function - trigonometric functions - exponential function and its inverse function such as Java also provides two constants. -Math.PI-Math.E If not want to add the prefix in front of mathematics Math method name and constant name. ", You can add the following code to the top of the source file. 4. casts to force type conversion syntax :( target type) variable .
 
VI. String
1. Java string is a sequence of Unicode characters string, which is the basic character of the data structure of the organization, similar to the use of an array of characters. There are built-in string type, provides a Java String predefined classes in the standard Java class library.
   Strings need to use the program can be divided into two categories: - do not modify and change the immutable string String class after creation; - allows to change and do change after the creation of the build string StringBuilder class.
 
VII. O
1. reads input input through the console, a Scanner need to construct the object, and with a "standard input stream" the System.in association. -Scannerin = newScanner (System.in); - Example 3-2InputTest.javap55 Task: Test InputTest.javaScanner java.util package in the class definition, and therefore need to load the appropriate package file incoming 2. To use the input and output read the file, you need to construct a Scanner objects with a file object. To write to a file, you need to construct a PrintWriter object in the constructor, we need to need to provide the file name.
 

Eight control statements (program control flow is determined by these control statements)

1.if、switch、for、while、do-while

2.Package statement and import statements

IX. Large value

    a) If the basic integer and floating point data type can not achieve the required accuracy, the package may be used java.math two classes, a BigInteger and BigDecimal.

    b) these two classes can be operated for any length of numbers: BigInteger class implements arbitrary-precision integer arithmetic, BigDecimal achieve arbitrary precision floating point

X. Array

1. The array is a data structure that is an ordered set of data. The data type of each element in the array are the same.

2. declare the array: one-dimensional array format array element type array name []; array element type [] array name;   

                        Two-dimensional array format: array element type array name [] []; array element type [] [] array name; type wherein data can be any type of java, including basic and complex types. Class may also be defined to declare an array;

  not allocate memory for an array of java in the array declaration, in java, arrays are separate classes, have their own way.

3. Creating an array

After the array is declared, the new operator to use its allocated memory space, the length of the array must be specified when allocating memory.

Format: Array = new array element type name [number]

 

Part II: Experimental part

Experiment name: Experiment two basic Java programming (1)

1. Purpose:

(1) become more familiar with the basic steps of the java program development and IDE command line in two ways;

(2) master the process of importing Java source code under the Eclipse integrated development environment;

(3) Data structure of the Java language type master basic programs, variables, operators, various types of expression, the input and output, the basic syntax of flow control;

(4) master the use of the Math class, String class, StringBuilder class.

2. Experimental procedure and content:

Experiment 1 peer review procedure (10 minutes)

        Rating Requirements: binding score operation program, performance program given score, out of 5, 1 point for each error, until the deduction to 0;

        Each rater find a correct error, experiment a score of 1 point, maximum 5 points.

Experiment 2: java applications written contains the following code fragment, the value of the output string class object s3.

                         String s1=“Hello!”;

                         String s2=“World”;

                         String s3=s1+s2;

code show as below:

public class cld {

 public static void main(String[] args) {  

        // TODO Auto-generated method stub  

        String s1 = "Hello!"; // define a String, called s1, s1 and assign string Hello

        String s2 = "World"; // define a String, named s2, and the string assigned to s2 World

        String s3 = s1 + s2; // define a String, that is named, and the value s1 + s2 s3 assign

        System.out.println (s3); // output value of s3

    }

    }

 

Results are as follows:

 

Experiment 3 (5 minutes) Change Experiment 2 s1, s2, s3 for the StringBuilder class object observation result of the program and 2 with the experimental results, and to understand the differences String class object StringBuilder class object.

Code is as follows:

package message;

 
public  class  Message {
 
     public  static  void  main(String[] args) {
         StringBuilder s1= new  StringBuilder( "Hello!" );    //在字符串构建器StringBuilder中创建一个新的StringBuilder
         StringBuilder s2= new  StringBuilder( "world" );
         StringBuilder s3= new  StringBuilder();
         s3.append(s1);
         s3.append(s2);        //用StringBuilder将s1和s2拼接起来
         System. out .println(s3);
         
     }
    }
Screenshot below:
Results are as follows:

2) String class object StringBuilder difference object classes:

1.String class

      1) is a constant string;

      2) String itself is immutable, it can only be assigned once, the contents of each change occurs, it will generate a new String class object, then the original object reference to the new object. (Their values can not be changed after creation)

      3) String string achieve addition, it creates a lot of memory for a string in memory, taking up more memory, the lower the efficiency of the program.

2.StringBuilder class

      1) Each time operations are subject to their own operation, rather than create new objects, the space will increase its share of the content and expansion;

      2) string buffer, can improve the efficiency of the string, the string support variable;

      3) StringBuilder adder implemented string is always an array, small footprint, so efficient.

      4) If you need to build many small pieces of string with a string, then the first build an empty string builder:

                StringBuilder builder = new StringBuilder();

             When each part of the content needs to be added, it is calling the append method.

                builder.append(ch); // appends a single character

                builder.append(str); // appends a string

             ToString method is called when the need to build a string, can be a String object that contains the character sequence in the builder.

                String completedString = builder.toString();

Experiment 4 (5 minutes) to run the following debugging program in the command line, is understood java application command line parameters usage.

 a) code is as follows:

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(“!”);
   }
  }

b) which results are as follows:

 

 

 

c) Java program in the main method in the presence of "String [] args" parameters: 

    1. "String [] args" parameter is the command line (which receives operating parameter)

          Java program running at the same time, two ways can be assigned to the command line parameter "String [] args":

One is to bring back the Java command line parameters, and the other is input by the compiler (This article will demonstrate using eclipse), after the virtual machine directly to their store to "String [] args" array.

    2. args is a command-line parameter, because parameter can be multiple, so use an array to access the parameter value our input.

 3. For the above procedures, debugging and running the command line:

        Enter the Java Message -h (or Java Message -g) enter, will soon be assigned to args parameter array, run the program.

    4. If in eclipse, the right to find run as, select Run Configurations, Message in a file, find it Arguments, input parameter -h (-g) click to run the program arguments in.

Experiment 5 to import Java (5 minutes)

         Chapter 3 sample introduction step procedure InputTest.java Eclipse environment:

  1) New java project

 

 2) Select File-> import-> File ystem-> Next, click Browse to select the top import source and select, select Browse to import the source location for the new project InputTest / src location, click finish to complete the import.

 

 3) Open the default package InputTest project src folder, double-click to open the file in InputTest.java IDE source code editing area.

 4) Right-click the file name InputTest.java open the shortcut menu, select Run as-> java application to run this program. Understand the code Scanner class object usage, master java console input method.

 

 

A.Scanner class object usage:

      1) Scanner steps of:

    a: the leader packet must import java.util.Scanner; shortcut keys can be used ctril + shift + o

    b: = new new Create Object SC Scanner Scanner ( the System.in );

    c: calling the method  sc.xxx ()

B.Scanner methods of the class

    1) public int nextInt (): read the value of type int

    2) public String nextLine (): read the value of type String

    3) public boolean hasNextXxx (): determines whether a certain type of element

C.Scanner methods of the class Summary:

    1) next (): Only read input until a space. It can not read the two separated by spaces or symbols words. Further, next () after reading the input cursor on the same line. (Read-only data prior to the space next (), and the cursor is pointing to the Bank)

    2) nextLine (): read the input, including spaces between words and the symbols other than the transport of all (i.e., it reads the line). After reading the inputs, nextLine () to locate the cursor on the next line.

    3) nextline (): read only values, remaining "\ n" not yet been read, and place the cursor in the Bank.

    4) hasNextInt (): determining whether an element of type int, then get the element.

    5) next () method does not return the spaces, Tab key, a carriage return character string, and can nextLine.

D. the procedure described above, Scanner in = new Scanner (System.in);

               System.in a keyboard input stream is obtained, monitor keyboard input data, Therefore, Scanner in = new Scanner (System.in); it has been acquired real keyboard input column.

    Scanner is a class, in a column or a real object, new method is to create an object of a class performance in access to ... Scanner console input, which is equivalent to a declaration of human (Scanner), the humans who are (in), The newly created human behind a new Scanner (system.in), system.in indicate what his characteristics. In other words, the newly created Scanner object input, and then assigned to in. When used to close the flow, otherwise there will be alarm. 

Experiment 6 Test file write program (10 minutes)

       5 following the experimental procedure introduced WriteReadFileTest.java example, run the program and understand the program code, project folder observed myfile.txt file contents, the file input and output control operation.

  1. Import WriteReadFileTest.java sample program, the program runs as follows:

 

Experiment 7 (5 minutes)

       5 following the experimental procedure introduced Chapter 33-3--3-4 sample program, the use of two master program loop control structure.

1) introducing the sample 3-3 following procedure:

 

 

 2) introducing 3-4 exemplary procedure is as follows:

 

Experiment 8 (5 minutes)

       5 following the experimental procedure of introducing the sample program Chapter 33-5, understood lottery probability calculation algorithm.

1) introducing the sample program 3-5 as follows:

 

 

 

       3-5 The procedure used for the loop to calculate the probability of winning the lottery this procedure is used, if the K numbers extracted from the n numbers, calculated using the following formula results.

  compute binomial coefficient n*(n-1)*(n-2)*...*(n-k+1)/(1*2*3*...*k)

That statement is:

 

3. Experimental Summary: (10 points)

       By week's study, I become more familiar with the use of eclipse, there is the command basic steps Java programs developed under the line and IDE in two ways. The completion of teacher assignments in the process, I once again encountered new problems, new challenges, but I will study hard to overcome these difficulties. Experiment 7 and Experiment VI eight experiments are done on the basis of five experiments, by operating five experiments for the import file, there has been more familiar. During the experiment, we mastered the basic data types Java program language constructs, variables, operators, all kinds of expressions, input and output, basic grammar process control, hope in the future of learning, to better grasp the Java programming language .

 

Guess you like

Origin www.cnblogs.com/zhaxipingcuo/p/11495057.html