201871010115-- Mabei "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. Teachers adapt teaching methods, to complete the study this week, according to independent study theoretical knowledge requirements;
  2. Master the Java Application Program Structure;
  3. Master and variable data types Java language;
  4. Learn to use Java operator construction of various types of expression;
  5. Java Application control input and output technology;
  6. Master the Java process control technology (branch, loop); (Key)
  7. Math master class, String class usage. (difficulty)

Essay Bowen text content:

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

For basic grasp of words, both are relatively similar. Because all object-oriented languages, most of the syntax is similar. However, relatively easier to use JAVA, JAVA because there is no concept of pointers, are completed through the java virtual machine memory recovery, so that it is easier for a programmer. The C ++ programmers need to master the application and release of memory, if handled badly, the program crashes and other issues are likely to arise. JAVA language and C ++ language in order to learn better, then the third-party libraries is essential, as the swing JAVA, etc., the ACE C ++ class libraries, and so on are very good, must master these skills. Ranking from the language point of view, both of which it is the top 3. JAVA slight advantage. Both employment prospects have development, but first learn C ++ JAVA sub relatively somewhat difficult, and C ++ to JAVA transition relatively easy. Personal understanding of the above, if the proposed self-learning java bar, in general, C ++ will be relatively easier. Difference: ------ JAVA and C ++ is an object-oriented language. That is, they are able to achieve the object-oriented thinking (encapsulation, inheritance, polymorphism). And because c ++ to take care of a large number of C language users, and is compatible with the C, making itself become the only language C with Classes, more or less affected the thoroughness of its object-oriented! JAVA is a fully object-oriented language that the syntax clearer, smaller and easier to learn. It is a variety of programming languages ​​has been the basis of intensive studies, according to abandon the shortcomings of other languages ​​to solve the inherent defects in c ++ fundamentally. Java and c ++ are more similarities than differences, but asked several major two different languages ​​make Java easier to learn and easier programming environment. Lists only the more significant differences:

1. Pointer JAVA language allows programmers can not find no pointer to directly access memory pointer, and adds automatic memory management functions, effectively preventing the c / c ++ language pointer operational errors, such as system crashes caused by wild pointer. But it does not mean that there is no pointer JAVA virtual machine inside or use a pointer, but outsiders can not use it. This facilitates secure Java programs.

2. Multiple inheritance c ++ supports multiple inheritance, which is a feature of c ++, which allows multiple parent classes derive a class. Although multiple inheritance function is very strong, but the use of complex and will cause a lot of trouble, compiler, it is not easy to achieve. Java does not support multiple inheritance, but allows a class to inherit more than one interface (extends + implement), to achieve a c ++ multiple inheritance function, but also to avoid bringing multiple inheritance implementation c ++ in a lot of inconvenience.

3. Java data types and classes are fully object-oriented languages, functions and variables portion must be part of a class. In addition to basic data types, as are the rest of the class object, including arrays. Object and method for combining data, encapsulate them in the class, each object can be realized so that their characteristics and behavior. And c ++ allows to define the global variables and functions. In addition, Java canceled in c / c ++ in structures and unions, eliminating unnecessary trouble.

4. Automatic memory management program in Java all objects are using the new operator based on the memory stack, this operator is similar to c ++ new operator. The following statement creates an object class by a Read, then call the object's methods work: Read r = new Read (); r.work (); statement Read r = new Read (); established on the stack structure one example of Read. Java automatically useless memory recovery operation, the programmer does not need to be removed. And c Shi Shi Bei by the program must release the memory resources, an increase of negative throwing program designers. When Java, when an object is not to be reused, memory garbage collector it will be tagged to show deleted. JAVA in memory recovery program is useless threads run in the background, use of free time to work.

5. Operator overloading Java does not support operator overloading. Operator overloading is considered to be a prominent feature c ten ten, although in Java class in general can achieve this functionality, but the ease of operator overloading are still missing a lot. Java language does not support operator overloading is to maintain the Java language as simple as possible.

6. Java does not support pre-processing functions preprocessing functions. c / c has a pre-compiled ten ten stages of the compilation process, known as pre-processor. Preprocessor provides convenience for developers, but it increases the complexity of Ding compiled. JAVA virtual machine does not pre-processor, but it provides the import statement (import) and Shi Shi c preprocessor function is similar.

7. Java does not support the default function parameters, and support c c Shi Shi, the code organization in the function, the function can access the global variables program. c Shi Shi adds class, providing class algorithm, which is a function of the type connected, c ten ten class methods with the Java class methods are very similar, however, due to the c Shi Shi is still supported c, they can not stop c ten ten development personnel functions, methods and results of the function program so that the use of chaotic mixing. Java does not function as a language more pure than ten ten c OO, Java developers to force all routines included in the class, in fact, the method may be implemented by routines encourages developers to better organize the coding .

C 8 and c ten ten strings do not support the string variable, using the Null terminator string representing the end of the ten ten c and c program in Java class object is a string (String and the StringBuffer) to achieve, these classes are the core of the Java language objects, implemented by the string class object has the following advantages: (1) the method of establishing access strings and string element in the entire system is the same; (2) as string class part of the definition of the Java language, rather than as an extension of the external part; (. 3) Java runtime execution check empty string, can help eliminate some of the run-time error occurs; (4) on the string "ten" connection operating.

9. "goto" statement "horrible" goto statement is c and c ++ the "relic", which is technically legitimate part of the language, references goto statement caused confusion in program structure, easy to understand, goto statement to be used for child unconditional jump subroutine technology and multi-branched structure. Given the extensive grounds with, Java does not provide a goto statement, although it specifies goto as a keyword, but it does not support the use of the program simple and easy to read.

l0. Type conversion in c and c Shi Shi sometimes appear implicit data type conversion, which involves a mandatory class automatic conversion problems. For example, in the ten ten c may be a floating point value to an integer variable, and remove the mantissa. Java does not support automatic c cast of ten ten, if required, must be cast by the program explicitly. 11. The exception JAVA abnormal exceptions for capturing mechanism, enhance the fault tolerance of the system may try {// an exception code} catch (exceptionType name) {//} where exceptionType indicates an abnormal process type. The C ++ is not so convenient mechanism.

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.

3. Experimental procedure and content:

Experiment 1 peer review procedure (10 minutes)

Experiment 2 (5 minutes)

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

1> code is as follows:

package JavaTest;

public class zxxx {

public static void main(String[] args) {

// TODO Auto-generated method stub

String s1 = "Hello!";

String s2 = "World" ;

String s3 = s1 + s2;

System.out.println(s3);

}

}

2> 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.

1> code is as follows:

package JavaTest;

public class zxxx {

public static void main(String[] args) {

// TODO Auto-generated method stub

StringBuilder  s1 =new StringBuilder( "Hello!");

StringBuilder  s2 =new StringBuilder("World");

        StringBuilder  s3 =new StringBuilder();

S3.append(s1);

        S3.append(s2);

        System.out.println(s3);

}

 

}

2> results are as follows:

 

 

 

 

 

 

 

 

 

 

 

3> String class object like a data type;

The Stringbuilder class object need to create an object, and then call a method of this object to implement certain operations.

Experiment 4 (5 minutes)

The following commissioning procedures at the command line, understanding the use of java application command line arguments.

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

(2) Results FIG Run:

 

 

 

 

Experiment 4: commissioning program in the following command line, understanding the use of java application command line arguments.

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

  }

}

 

 

 

 

 

 

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

(1)   新建java project如下图:

 

 

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

 

 

 

 

 

 

 

 

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

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

(5)代码如下:

import java.util.*;

 

/** 

* This program demonstrates console input. 

* @version 1.10 2004-02-10 

* @author Cay Horstmann 

*/

public class InputTest

{   

public static void main(String[] args)   

{      

Scanner in = new Scanner(System.in);//通过控制台进行输入时,

//需要构建一个Scanner对象,并与“标准输入流”System.in关联。

 get first input      

System.out.print("What is your name? ");//输入名字      

String name = in.nextLine();//定义一个String类,用来显示计算机读入//的字符串“What is your name?”的内容;       

get second input      

System.out.print("How old are you? ");//输入年龄      

int age = in.nextInt();//定一个整形,用来显示计算机读入的字符串

//“How old are you内容;       

 display output on console      

System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));//最后显示Hello,加上你输入的名字,再加

//上.next year, you'll be 在加上输入年龄加一。   

}

}

 

 

 

 

 

 

 

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

(1)代码如下

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

import java.util.Arrays;

 

public class WriteReadFileTest {

 

    /**

     * @param args

     * @throws FileNotFoundException

     */

    public static void main(String[] args) throws FileNotFoundException {

 

        //写入文件演示

        PrintWriter out = new PrintWriter("myfile.txt");

        out.println("姓名 高数 Java 数据结构 平均成绩 总成绩");

        out.println("张三 20 30 40 0 0");

        out.println("李四 50 60 70 0 0");

        out.close();//切记,输出完毕,需要close

         

         

        //读入文件演示

        Scanner in = new Scanner(new File("myfile.txt"));//为myfile.txt这个File创建一个扫描器in

        int number = 1;//行号

        while(in.hasNextLine()){//判断扫描器是否还有下一行未读取,该循环把文件的每一行都读出

            String line = in.nextLine();//读出myfile.txt的下一行

            System.out.println("第"+(number++)+"行的内容" +"="+line);

             

            Scanner linescanner = new Scanner(line);//对每一行的内容建立一个扫描器

            linescanner.useDelimiter(" ");//使用空格作为分隔符

            String 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);

        }

        in.close();//读入完毕,最后需要对其进行close。   

 

 }

}

(2)运行结果如下

 

 

 

 

 

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

(1)代码如下:

import java.util.*;

 

/**

 * This program demonstrates a <code>while</code> loop.

 * @version 1.20 2004-02-10

 * @author Cay Horstmann

 */

public class Retirement

{

   public static void main(String[] args)

   {

      // read inputs

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

 

      System.out.print("How much money do you need to retire? ");

      double goal = in.nextDouble();//输入"How much money do you need to retire? "的内容;

 

      System.out.print("How much money will you contribute every year? ");

      double payment = in.nextDouble();//输入"How much money will you contribute every year? "的内容;

 

      System.out.print("Interest rate in %: ");

      double interestRate = in.nextDouble();//输入"Interest rate in %: "的内容;

 

      double balance = 0; //定义双精度型 balance,并进行初始化;

      int years = 0;      //定义整型years,并进行初始化;

 

      // update account balance while goal isn't reached

      while (balance < goal)                   //while循环

      {

         // add this year's payment and interest

         balance += payment;                   //balance=balance + payment;

         double interest = balance * interestRate / 100;

         balance += interest;

         years++;

      }

 

      System.out.println("You can retire in " + years + " years.");

   }

}

 

 

 

 

 

 

(2.1)代码如下:

import java.util.*;

 

/**

 * This program demonstrates a <code>do/while</code> loop.

 * @version 1.20 2004-02-10

 * @author Cay Horstmann

 */

public class Retirement2

{

   public static void main(String[] args)

   {

      Scanner in = new Scanner(System.in);

 

      System.out.print("How much money will you contribute every year? ");

      double payment = in.nextDouble();

 

      System.out.print("Interest rate in %: ");

      double interestRate = in.nextDouble();

 

      double balance = 0;

      int year = 0;

 

      String input;

 

      // update account balance while user isn't ready to retire

      do

      {

         // add this year's payment and interest

         balance += payment;

         double interest = balance * interestRate / 100;

         balance += interest;

 

         year++;

 

         // print current balance

         System.out.printf("After year %d, your balance is %,.2f%n", year, balance);

 

         // ask if ready to retire and get input

         System.out.print("Ready to retire? (Y/N) ");

         input = in.next();

      }

      while (input.equals("N"));

   }

}

 

 

 

 

 

实验8:

(1)代码如下:

import java.util.*;

 

/**

 * This program demonstrates a <code>for</code> loop.

 * @version 1.20 2004-02-10

 * @author Cay Horstmann

 */

public class LotteryOdds

{

   public static void main(String[] args)

   {

      Scanner in = new Scanner(System.in);

 

      System.out.print("How many numbers do you need to draw? ");

      int k = in.nextInt();   //输入字符串"How many numbers do you need to draw? "的内容;

 

      System.out.print("What is the highest number you can draw? ");

      int n = in.nextInt();  //输入字符串"What is the highest number you can draw? "的内容;

 

      /*

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

       */

 

      int lotteryOdds = 1;

      for (int i = 1; i <= k; i++)    //for循环;

         lotteryOdds = lotteryOdds * (n - i + 1) / i;

 

      System.out.println("Your odds are 1 in " + lotteryOdds + ". Good luck!");

   }

}

  

 

 

 

 

 

4.实验总结

  第  本次实验的过程中,我掌握了进行java编程所需的最基本的知识,为将来的学习打下了必要的基础。经过实验之后我了解到了Java语言有8种数据类型,其中4种整型、2种浮点类型、1种用于表示Unicode编码的字符单元的字符类型char和1种用于表示真值的boolean类型。我学会了使用Java运算符构造各类表达式,掌握了Java流程控制技术,但是对string类和数组类的用法还是不太熟悉,之后我会通过课后的学习和网上的课程相结合,将它们的用法弄懂,看懂,并且加以运用。

Guess you like

Origin www.cnblogs.com/G19990718/p/11494504.html