201871010104- Chen Yuanyuan "object-oriented programming (java)" the second week of learning summary

                                                                                                         201871010104- Chen Yuanyuan "object-oriented programming (java)" the second week of learning summary

project content
This work belongs courses ttps: //www.cnblogs.com/nwnu-daizh/
Where the job requires https://www.cnblogs.com/lily-2018/p/11441372.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)

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

1, the identifier

     Java identifier are letters, underscores, dollar signs and numbers, and the first symbol is not a number. Identifier may be used as the class name, object name , variable name, a method name, array name, file name.

2, Keyword

     Keyword is the java language has been given a specific meaning of some words. Common are: class, public, the try, the catch, Import, IF, float, void and so on. Keyword is not a variable name.

3 comments

    java comments in three ways:

    1) // // footnotes have been made to this end of the line.

    2) / * and * / define a comment block

    3) / * start * / End    This method can be used to automatically generate annotated document.

4, the data type

    Integer type (int, Short, Long, byte )

   Floating-point type (float, double)

   Character type (char)

   Boolean ( boolean )

   java no unsigned types. In java, Boolean and integer not interchangeable .

5, the character set: java using unicode character code set, the code sets 16, containing 65,536 characters. Unicode (Unicode, Unicode) is a coding standard, produced in order to solve the limitations of traditional character encoding scheme. It set a unified and unique binary encoding for each language for each character, in order to meet the cross-language, cross-platform text conversion processing requirements.

6, the escape character

7, variable

     In java, each of the variables belonging to one type. When you declare a variable, the variable type belongs to precede the variable name. His party can declare multiple variables, individually declare each variable can improve the readability of the program.

     After a variable declaration must be explicitly initialized by assignment to it. 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.

8, constant

    In java, use the keyword final to denote a constant. Traditionally constant names are capitalized. Keyword final representation can only assign a value to a variable, its value once set, it can not be changed. Use the keyword static final declare a class constants.

9, various operators

   Be operator, operator domain increment decrement operator, in conjunction with the priority of relational operators, logic operators, bitwise operators, operator.

   java provides two special operators, new (object to create the operator), insteadof (returns a Boolean value indicating whether an object is an instance of a particular class or one of its subclasses).

 

 10, classes, fields, methods and global variables, functions

 

 1) Class C is not in, Java classes are defined as follows:

 

 [Modifier] class class name [extends parent class name] [the implements interface name]

 

Where modifier can be one or more of the following access modifiers:

 

 abstract: an abstract class.

 

 final: The final class.

 

 public:公共类。

 

 2)域(成员变量)和全局变量类比:

 

 Java中域的定义如下:

 

 [修饰符] 类型 成员变量名;

 

 修饰符可选以下一个或多个关键字:

 

 public:公共成员。

 

 protected:本类或同一个包的其他类以及其它包该类的子类可访问。

 

 private:私有成员。

 

 final:常量,确定后不能改变。

 

 static:静态变量。

 

 transient:临时变量。

 

 volatile:备份变量。

 

 各类型成员变量默认初始化为:

 

整型变量:0

 

浮点型变量:0.0

 

布尔型变量:false

 

字符型变量:空格

 

类变量:null

 

C中全局变量定义同一般变量:

 

[存储类别] 数据类型 变量表列;

 

其中存储类别可选:

 

auto:自动变量,当不申明存储类别时隐式默认该值。

 

static:静态变量。

 

register:寄存器变量。

 

extern:外部变量。

 

3)方法和函数类比:

 

Java中方法的定义如下:

 

[修饰符] 返回类型 方法名([参数表列])

 

修饰符可选以下一个或多个:

 

public:公共方法。

 

protected:本类或同一个包的其他类以及其它包该类的子类可访问。

 

private:私有方法。

 

abstract:抽象方法,只有方法头没有方法体。

 

static:静态方法。

 

存储类别可选:

 

extern:外部函数。

 

static:静态函数。

 

11、数学函数与常量

    数学函数包含在Math类中(幂函数、三角函数。指数函数以及它的反函数)等。

12、数组

 

Java中数组定义有两种方式:

 

数据类型数组名[];或 数据类型 []数组名;

 

定义和初始化可同时进行,如:int []a = {0,1,2,3,4,5,6,7,8,9};

 

注意Java中数组如果在定义时没有进行初始化,在进行初始化的时候需要先分配内存,即:

 

数组名 = new 数据类型[常量表达式];

 

也可在定义同时进行内存分配:

 

数据类型数组名[] = new 数据类型[常量表达式];

 

C和Java都不支持变长数组,引用的时候都是 数组名[下标]。区别是:Java的下标范围为0~数组长度-1,不在该范围会抛出数组下标越界异常,而C有效范围也是0~数组长度-1,但下标超出此界不会报错。

 

 

13、类、域、方法和全局变量、函数

 

 1)类是C中没有的,Java中类定义如下:

 

 [修饰符] class 类名[extends 父类名][implements 接口名]

 

其中修饰符可以为以下一个或多个访问修饰符:

 

 abstract:抽象类。

 

 final:最终类。

 

 public:公共类。

 

 2)域(成员变量)和全局变量类比:

 

 Java中域的定义如下:

 

 [修饰符] 类型 成员变量名;

 

 修饰符可选以下一个或多个关键字:

 

 public:公共成员。

 

 protected:本类或同一个包的其他类以及其它包该类的子类可访问。

 

 private:私有成员。

 

 final:常量,确定后不能改变。

 

 static:静态变量。

 

 transient:临时变量。

 

 volatile:备份变量。

 

 各类型成员变量默认初始化为:

 

整型变量:0

 

浮点型变量:0.0

 

布尔型变量:false

 

字符型变量:空格

 

类变量:null

 

C中全局变量定义同一般变量:

 

[存储类别] 数据类型 变量表列;

 

其中存储类别可选:

 

auto:自动变量,当不申明存储类别时隐式默认该值。

 

static:静态变量。

 

register:寄存器变量。

 

extern:外部变量。

 

第二部分:实验部分

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

1.  实验目的:

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

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

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

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

3. 实验步骤与内容:

实验1 java风格九九乘法表程序互评。

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

String  s1="Hello!";

String s2="World";

String s3=s1+s2;

代码如下:

package cyy;
public class cl {
	public static void main(String[] args) {
		String  s1="Hello!";
		String s2="World";
		String s3=s1+s2;
		s3=s2.concat(s1);
		System.out.println(s3);
	}

}

 

运结结果如下:

 

 

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

代码如下:

package cyy;
public class cl {
	public static void main(String[] args) {
		StringBuilder s1=new StringBuilder("Hello!");
		StringBuilder s2=new StringBuilder("World");
		StringBuilder builder=new StringBuilder();
		builder.append(s1);
		builder.append(s2);
		System.out.println(builder);
	}
}

 

运行结果如下:

 

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

运行结果如下:

实验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 文件读写程序测试

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

2)myfile.txt内容如下

 

 

实验7  

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

3-3实例

代码如下:

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

  运行结果如下:

 

 3-4实例

代码如下:

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);   //通过控制台进行输入时,需要构建一个Scanner对象,并与“标准输入流”System.in关联。
 
      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;
      int year = 0;
 
      String input;
 
      // update account balance while user isn't ready to retire
      do                                    //do-while循环
      {
         // add this year's payment and interest
         balance += payment;                 //balance=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"));  //循环满足的条件,当输入“Y”的时候结束循环语句;
   }
}

运行结果如下:

 

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

代码如下:

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

  

运行结果如下:

 

 四.实验总结

  本周的实验主要是在c语言的基础之上,对java作了学习,通过将两者作对比将java的基础知识加以巩固与学习,此外,还通过实例中的代码慢慢地走进java,慢慢理解java中的各种数学算法,虽然看着不太懂。但是我相信以后可能会慢慢得到升华,努力为以后的学习打下一个基础。

 

Guess you like

Origin www.cnblogs.com/chanyeol1127/p/11483986.html