201771010117 Machind Experiment two Java Basic program design (1)

Source: Internet
Author: User
Tags mathematical functions

Experiment two Java Basic Program Design (1)

First Part the study of theoretical knowledge.

Chapter III Java BASIC program design structure

1 Basic knowledge:

(1) Identifier: identifiers consist of letters, underscores, dollar signs, and numbers, and the first symbol cannot be a number. For example, hello,$1234 are valid characters, and identifiers can also be used as class names, variable names, method names, file names, and so on.

(2) Keywords: keywords are words that are given a particular meaning in the Java language. such as class, public, try, etc., but the keyword cannot be used as a variable name.

(3) Note: Java has three kinds of annotation methods, such as://,/* and *, (define a comment block),/** start, */end.

2 data types

The Java language has 8 types of data: integer type (int, short, byte) floating-point type (float, double) character type (char) Boolean type (Boolean)

3 Initialization of variables

in Java, each variable belongs to a type. When declaring a variable, the type to which the variable belongs is before the variable name.

In Java, a row can declare multiple variables. Declaring each variable individually can improve the readability of the program.

–int I,j;//both isintegers
(1) Initialization of variablesafter a variable declaration, it must be explicitly initialized with an assignment statement-never use the value of an uninitialized variableIn Java, you can make variable declarations anywhere in your code. The declaration of a variable is as close as possible to the place where the variable was first used, which is a gooda good program writing style.  In Java, you cannot declare two variables of the same name within the same scope.

(2) Definition of constants

In Java, use the keyword final to indicate constants. The customary constant names are capitalized.

Final double cm_per_inch=2.54;

The L keyword final indicates that a value can only be assigned once, and once the value is set, it cannot be changed. In Java, you often want a constant to be used in multiple methods within a class, often referred to as class constants. You can declare a class constant (classconstants) with the keyword static final. public static final double cm_per_inch=2.54;

4 operator

Operators have various operators, precedence and binding of operators, and mathematical functions and constants

The various operators include the arithmetic operator, the self-increment decrement operator, the relational operator, the logical operator, and the bitwise operator. Moreover, various operators are related to object-oriented operations.

5 Type conversions

Type conversions are also classified as implicit type conversions and mandatory type conversions, and I'll use a set to illustrate type conversions

In the case of implicit type conversions, most numeric conversions follow a precedence relationship, whereas in a forced type conversion, emphasis is placed on preventing data loss during the conversion process.

6 string

(1) A Java string is a sequence of Unicode characters that is the basic data structure of an organization's characters, similar to a character array.

There is a built-in string type, but a Java-predefined class string is provided in the standard Java class library. In Java, strings are treated as objects.

The strings that need to be used in a program can be divided into two main categories:
Immutable strings that are not modified and changed after the creation of the string class;
The build string StringBuilder class is allowed to make changes and changes after creation.

(2) The most important point in the string is the string class, which is also divided into the basic operations of the string class, construction methods, and APIs and documentation

7 Input and output

It is divided into read input, formatted output and file input and output.

8 Control process

There are 5 types of statements in Java: Java statements (compound statements, control statements, and impothe packege statements), conditional statements (if, switch), loop statements (while, Do-while, for), interrupt control flow statements (break, continue , return statement (return value)

9 Large values

If the basic integer and floating-point data do not meet the required precision, then you can use the two classes in the Java.math package, BigInteger and BigDecimal. These two classes can manipulate arbitrary long numbers.

The BigInteger class realizes the integer operation of arbitrary precision, and BigDecimal realizes the floating-point operation with arbitrary precision.

10 arrays

An array is a data structure that is a collection of ordered data, with the same data type for each element in the array.

The determination of an element is implemented by the array name and its subscript, such as:

A[0] Represents the first element of array A, a[1] represents the second element of array A, and so on

The second part of the experiment

1, the purpose and requirements of the experiment

(1) Further familiar with the basic steps of Java Program Development in command line and IDE two ways ;

(2) Mastering the process of importing Java source program under Eclipse integrated development environment ;

(3) Mastering the basic Grammar of the basic program of Java language construction, such as data type, variables, operators, various expressions, input and output, and process control;

(4) mastering The use of the string class, the StringBuilder class, and the array class.

2. Experiment contents and Steps

Experiment 1: write the Java application and output the values of the following 4 expressions.

int i=1;

Double d=1.0;

(1) 45+45*50%i--

(2) 1.5*3+d++

(3) (true) && (3>4)

(4) (i>0) | | (i<0)

Write the code for the following program on Eclipse, as shown in the output:

Experiment 2: Write a Java application that contains the following code fragment , outputting the value of the string class object S3.

String s1= "hello!";

String s2= "World";

String s3=s1+s2;

The result of the output is as follows:

Experiment 3: Change Experiment 2 s1,S2,S3 as StringBuilder class object, observe the program running results and compare with experiment 2 results, Understand the difference between a string class object and a StringBuilder class object.

The experimental results are as follows:

Experiment 4: Run the following program in command-line mode to understand the use of Java Application command-line parameters.

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 ("!");

}

}

The experimental results are as shown

Lab 5:Import Chapter 3 sample program Inputtest.java steps in Eclipse environment :

(1) new Java project such as:

(2) Select File->import->file ystem->next, open the File Import window like, click on the above browse Select Import source program and select, click Browse below After selecting the source program import location for the new project inputtest/src location, click Finish to complete the import.

(3) open the default package of the Inputtest Project Src folder, and double-click Inputtest.java to open the file in the IDE's source program editing area.

(4) right-click the Inputtest.java file name to open the shortcut menu, choose Run As->java application running the program, in conjunction with the program run results, understand the code Scanner Class object usage, mastering the Java Console input method.

Following the above operation, the output results as shown:

Experiment 6 : follow The procedure of Experiment 5, Import Writereadfiletest.java sample program, understand program code with program running result , observe the contents of file MyFile.txt under project folder, master the input and output operation of file.

Here is the code for the experiment and the results of the experiment:

import java.io.file;import java.io.filenotfoundexception;import java.io.printwriter;import Java.util.Scanner; Import Java.util.Arrays; Public classWritereadfiletest {/** * @param args * @throws filenotfoundexception*/     Public Static voidMain (string[] args) throws FileNotFoundException {//Write file DemoPrintWriter out=NewPrintWriter ("MyFile.txt");  out. println ("name high number Java data structure average score");  out. println ("Sheet 320 0 0");  out. println ("li 450 0 0");  out. Close ();//Remember, the output is complete and requires close//read-in file demoScannerinch=NewScanner (NewFile ("MyFile.txt"));//create a scanner in for myfile.txt this file        intNumber =1;//Line number         while(inch. Hasnextline ()) {//determines whether the next line of the scanner is not read, and the loop reads each line of the fileString line =inch. nextline ();//read out the next line of MyFile.txtSystem. out. println ("Section"+ (number++) +"the contents of the line"+"="+Line ); Scanner Linescanner=NewScanner (line);//Create a scanner for the contents of each rowLinescanner.usedelimiter (" ");//use spaces as separatorsString name =Linescanner.next (); String Math=Linescanner.next (); String Java=Linescanner.next (); String DS=Linescanner.next (); String avg=Linescanner.next (); String Total=Linescanner.next (); System. out. println ("name="+name+"math="+math+"java="+java+"ds="+ds+"avg"+avg+"total="+Total ); }        inch. Close ();//The read-in is complete, and you need to close it finally.  }}

Experiment 7: Follow the procedure of experiment 5, import the 3rd Chapter sample program, each sample program summarizes the learning content from the syntax and algorithm two angles.

(1) test Retirement.java,retirement2.java,lotteryodds.java to master the cyclic control structure;

Retirement.java:

Retirement2.java:

Lotteryodds.java:

(2) test Bigintegertest.java, master the use of large numerical classes;

(3) test Lotterydrawing.java, master the use of arrays;

(4) test Compoundinterest.java, master the use of multidimensional arrays;

(5) test Lotteryarray.java, master the use of irregular arrays.

Experiment Summary:

Learn the basics of identifiers, keywords, and annotations for the Java language this week. With these basic knowledge to pave the way, I will be able to better learn some of the data types, variables, operators, strings, input and output, and some of the relevant knowledge. Especially in the study of the content of the string, the teacher not only gave us a review and C language related knowledge, but also gave us some differences with the C language, although in the class to listen to a bit vague, but in the afternoon of the experiment class teacher has told us some other aspects of knowledge , but also learned some basic knowledge of the control flow, large values and arrays of the Java language, and was able to deepen my memory once again in the experiment.

In the experiment, we also learned the basic steps of Java program development in two ways, and mastered the process of importing Java source program under the Eclipse Integrated development environment, and the help of teacher's assistant students in the process, it is my relative ease to this part of the content study. In the end, I also learned a bit more about the string class, learning about the construction methods of the string class, basic operations and documentation, and some basic methods of StringBuilder classes that were learned during the program's run.

201771010117 Machind Experiment two Java Basic program design (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.