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