201871010102- often cloudy "object-oriented programming (java)" fourth week of learning summary

Bowen text at the beginning:

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/11552848.html

Job learning objectives

1. master the basic concepts of classes and objects, understand the relationship between classes and objects;

2. grasp the relationship between the object and the object variable;

3. Grasp common API predefined classes Date, LocalDate class;

4. The master user defined class syntax rules, including fields defined in claim example, static fields, constructor method, the method changer, accessor methods, static methods, main process, process parameters; (major and difficult)

The master object constructor is defined using the methods and requirements (key);

6. overloaded understand the concept and usage;

7. grasp the concept and usage of the package;

 

Bowen body content:

Part I: Chapter IV summarizes theoretical knowledge (20 points)

1. master the basic concepts of classes and objects, understand the relationship between classes and objects;

2. grasp the relationship between the object and the object variable;

3. Grasp common API predefined classes Date, LocalDate class;

4. The master user defined class syntax rules, including fields defined in claim example, static fields, constructor method, the method changer, accessor methods, static methods, main process, process parameters; (major and difficult)

The master object constructor is defined using the methods and requirements (key);

6. overloaded understand the concept and usage;

7. grasp the concept and usage of the package;

Part II: Experimental part

Experiment name: the definition and use of three types of experiments with the object

1. Purpose:

(1) familiar with the PTA platform for online testing environment;

(2) The user-defined appreciated class;

(3) grasp the object declaration;

(4) to learn to use a constructor initializes the object;

(5) using the class attributes and methods of using the master;

(6) to master the use of package and import statements.

3. Experimental procedure and content:

Experiment 1 Task 1 (10 points)

采用个人账号登录https://pintia.cn/使用绑定码620781加入PTA平台NWNU-2019CST1教学班(西北师范大学 计算机科学与工程学院 2018级计算机科学与技术),完成《2019秋季西北师范大学面向对象程序设计程序设计能力测试1》,测试时间50分钟。

实验课已做。

实验1  任务2(25分)

实验课已做。

实验2  测试程序1(10分)

1>、 编辑、编译、调试运行程序4-2(教材104页);

2>、结合程序运行结果,掌握类的定义与类对象的用法,并在程序代码中添加类与对象知识应用的注释;

3>、尝试在项目中编辑两个类文件(Employee.java、 EmployeeTest.java ),编译并运行程序。

将Employee包中的两个类拆开,分别新建两个类装进去

4>、参考教材104页EmployeeTest.java,设计StudentTest.java,定义Student类,包含name(姓名)、sex(性别)、javascore(java成绩)三个字段,编写程序,从键盘输入学生人数,输入学生信息,并按以下表头输出学生信息表:

  姓名    性别    java成绩

 代码如下

import java.util.*;


public class StudentTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in=new Scanner(System.in);
        System.out.println("请输入学生人数:");
        int number=in.nextInt();
        Student[] infro=new Student[number];
        for(int i=0;i<number;i++)
        {
            System.out.println("请输入第"+(i+1)+"个人的姓名:");
            String n=in.next();
            System.out.println("请输入第"+(i+1)+"个人的性别:");
            String s=in.next();
            System.out.println("请输入第"+(i+1)+"个人的java成绩:");
            int java=in.nextInt();
            infro[i]=new Student(n,s,java);
        }
        System.out.println("姓名\t性别\tjava成绩\t");
        for (Student e : infro)
             System.out.println(e.getName() + "\t" + e.getSex() + "\t" 
                + e.getJavaScore()+"\t");
    }

}

class Student{
    
    private String name;
    private String sex;
    private int javascore;
    
    public Student(String n,String s,int java){
        this.name=n;
        this.sex=s;
        this.javascore=java;
    }
    
    public String getName()
    {
        return name;
    }
    
    public String getSex()
    {
        return sex;
    }
    
    public int getJavaScore()
    {
        return javascore;
    }
    
}

运行结果如下

实验2  测试程序2(5分)

代码如下

/**
 * This program demonstrates static methods.
 * @version 1.02 2008-04-10
 * @author Cay Horstmann
 */
public class StaticTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      var staff = new Employee[3];
      //创建Employee类型的数组

      staff[0] = new Employee("Tom", 40000);
      staff[1] = new Employee("Dick", 60000);
      staff[2] = new Employee("Harry", 65000);
      //初始化数组

      // print out information about all Employee objects
      for (Employee e : staff)
      {
         e.setId();
         System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
            + e.getSalary());
         //遍历数组并输出
      }

      int n = Employee.getNextId(); // calls static method
      System.out.println("Next available id=" + n);
   }
}

class Employee
{
   private static int nextId = 1;

   private String name;
   private double salary;
   private int id;
   //定义了三个私有变量

   public Employee(String n, double s)
   {
      name = n;
      salary = s;
      id = 0;
   }
   //构造函数有两个参数

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public int getId()
   {
      return id;
   }
//三个函数分别返回姓名,薪水和ID;
   public void setId()
   {
      id = nextId; // set id to next available id
      nextId++;
   }

   public static int getNextId()
   {
      return nextId; // returns static field
   }

   public static void main(String[] args) // unit test
   {
      var e = new Employee("Harry", 50000);
      //构造了一个e对象,并对Employee构造函数进行传参
      System.out.println(e.getName() + " " + e.getSalary());
   }
}

运行结果如下

实验2  测试程序3(5分)

代码如下

 

package four;
/**
 * This program demonstrates parameter passing in Java.
 * @version 1.01 2018-04-10
 * @author Cay Horstmann
 */
public class ParamTest
{
   public static void main(String[] args)
   {
      /*
       * Test 1: Methods can't modify numeric parameters
       */
      System.out.println("Testing tripleValue:");
      double percent = 10;
      System.out.println("Before: percent=" + percent);
      tripleValue(percent);
      //将percent的值传入函数
      System.out.println("After: percent=" + percent);

      /*
       * Test 2: Methods can change the state of object parameters
       */
      System.out.println("\nTesting tripleSalary:");
      var harry = new Employee("Harry", 50000);
      //创建了一个Employee类型的对象
      System.out.println("Before: salary=" + harry.getSalary());
      tripleSalary(harry);
      System.out.println("After: salary=" + harry.getSalary());
      //输入在函数内部改变后的结果

      /*
       * Test 3: Methods can't attach new objects to object parameters
       */
      System.out.println("\nTesting swap:");
      var a = new Employee("Alice", 70000);
      var b = new Employee("Bob", 60000);
    //创建了两个Employee类型的对象
      System.out.println("Before: a=" + a.getName());
      System.out.println("Before: b=" + b.getName());
      swap(a, b);
      //将a,b进行交换后输出
      System.out.println("After: a=" + a.getName());
      System.out.println("After: b=" + b.getName());
   }

   public static void tripleValue(double x) // doesn't work
   {
      x = 3 * x;
      System.out.println("End of method: x=" + x);
   }

   public static void tripleSalary(Employee x) // works
   {
      x.raiseSalary(200);
      System.out.println("End of method: salary=" + x.getSalary());
   }

   public static void swap(Employee x, Employee y)
   {
      Employee temp = x;
      x = y;
      y = temp;
      System.out.println("End of method: x=" + x.getName());
      System.out.println("End of method: y=" + y.getName());
      //交换两数
   }
}

class Employee // simplified Employee class
{
   //Employee类用于对主类传入的值进行修改后传出
   private String name;
   private double salary;

   public Employee(String n, double s)
   {
      name = n;
      salary = s;
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
      //薪水等于工资加奖金
   }
}

运行结果如下

实验2  测试程序4(5分

代码如下

import java.util.*;

/**
 * This program demonstrates object construction.
 * @version 1.02 2018-04-10
 * @author Cay Horstmann
 */
public class ConstructorTest
{
   public static void main(String[] args)
   {
      // fill the staff array with three Employee objects
      var staff = new Employee[3];

      staff[0] = new Employee("Harry", 40000);
      staff[1] = new Employee(60000);
      staff[2] = new Employee();
      //创建了三个Employeelei类型的数组元素对象,并对其进行初始化
      // print out information about all Employee objects
      for (Employee e : staff)
         System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
            + e.getSalary());
      //遍历数组进行输出值
   }
}

class Employee
{
   private static int nextId;

   private int id;
   private String name = ""; // instance field initialization
   private double salary;
  //创建了三个私有元素变量
   // static initialization block
   static
   {
      var generator = new Random();
      // set nextId to a random number between 0 and 9999
      nextId = generator.nextInt(10000);
   }

   // object initialization block
   {
      id = nextId;
      nextId++;
   }

   // three overloaded constructors
   public Employee(String n, double s)
   {
      name = n;
      salary = s;
   }

   public Employee(double s)
   {
      // calls the Employee(String, double) constructor
      this("Employee #" + nextId, s);
   }

   // the default constructor
   public Employee()
   {
      // name initialized to ""--see above
      // salary not explicitly set--initialized to 0
      // id initialized in initialization block
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public int getId()
   {
      return id;
   }
   //返回各个元素的值
}

运行结果如下

实验2  测试程序5(5分)

 代码如下

package four;

// the classes in this file are part of this package

import java.time.*;

// import statements come after the package statement

/**
 * @version 1.11 2015-05-08
 * @author Cay Horstmann
 */
public class Employee
{
   private String name;
   private double salary;
   private LocalDate hireDay;

   public Employee(String name, double salary, int year, int month, int day)
   {
      this.name = name;
      this.salary = salary;
      hireDay = LocalDate.of(year, month, day);
   }

   public String getName()
   {
      return name;
   }

   public double getSalary()
   {
      return salary;
   }

   public LocalDate getHireDay()
   {
      return hireDay;
   }

   public void raiseSalary(double byPercent)
   {
      double raise = salary * byPercent / 100;
      salary += raise;
   }
   //返回薪水,姓名,和雇佣年月
}
package four;

//import com.horstmann.corejava.*;
// the Employee class is defined in that package

import static java.lang.System.*;

/**
 * This program demonstrates the use of packages.
 * @version 1.11 2004-02-19
 * @author Cay Horstmann
 */
public class PackageTest
{
   public static void main(String[] args)
   {
      // because of the import statement, we don't have to use 
      // com.horstmann.corejava.Employee here
      var harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);

      harry.raiseSalary(5);

      // because of the static import statement, we don't have to use System.out here
      out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
   }
}

 

运行结果如下

第三部分  实验总结

通过这周的学习,我基本了解了预定义类的基本使用方法,如Math类、String类、math类、Scanner类、LocalDate类等;大致掌握了用户自定义类的语法规则,如实例域、静态域、构造器方法等。但还是有些不足,概念理解不够深刻。在运行示例代码的过程中遇到了很多障碍,这都是我的学习经验,说明我在学习中还有一些漏洞和疑惑,在以后的学习中我会更加努力。

Guess you like

Origin www.cnblogs.com/xiaobeike/p/11564816.html