• 201871010131张兴盼《面向程序设计(java)》第四周学习总结 201871010131


    项目

    内容

    这个作业属于哪个课程

    https://www.cnblogs.com/nwnu-daizh/

    这个作业的要求在哪里

    https://www.cnblogs.com/nwnu-daizh/p/11552848.html

    作业学习目标

    1. 掌握类与对象的基础概念,理解类与对象的关系;
    2. 掌握对象与对象变量的关系;
    3. 掌握预定义类DateLocalDate类的常用API
    4. 掌握用户自定义类的语法规则,包括实例域、静态域、构造器方法、更改器方法、访问器方法、静态方法、main方法、方法参数的定义要求;(重点、难点)
    5. 掌握对象的构造方法、定义方法及使用要求;(重点);
    6. 理解重载概念及用法;
    7. 掌握包的概念及用法;

    第一部分:总结第四章理论知识

    1、面向对象程序设计概述:面向对象程序设计(OOP)是一种新的程序设计思维,这种方法更接近人类处理现实世界问题的自然表示方法。

      (1)对象是面向对象编程的核心,是具体实体,具有明确定义的状态和行为。

      (2)对象的三个主要特征:

        1)行为:可以对对象施加哪些操作或者可以使用哪些方法;

        2)状态:当施加那些操作或者方法时,对象应该如何应对;

        3)标识:如何辨别具有相同行为和状态的不同对象。

      2、类(class

       (1)类是具有相同属性和行为的一组对象的集合;

      (2)类是描述对象的模板,它定义一类对象所拥有的数据和能完成的操作,在面向对象的程序设计中,类是构造程序的基本单位;

      (3)每个类由一组结构化的数据(称作实例域)和在其上的一组操作构成。

        3、如何识别类:

            1)过程化程序设计,必须从顶部的main函数开始编写程序;

         2oop的思路,首先从设计类开始,然后在每个类里面添加方法;

      (3)识别类简单规则是在问题分析过程中寻找名词,而类的方法则对应动词。

      4、类和对象的关系:

      (1)类是对象的原型,对象是类的实例,类是同类对象的抽象;

           2)所有属于同一个类的对象都具有相同的特征和操作。

        5、类之间的关系:

      (1 依赖:如果一个类中的方法操作了另一个类的对象,那么这个类就依赖于另一个类;

      (2)聚合:类A的对象包含类B的对象;

      (3)继承:表示一个特定类和一个 一般类之间的关系。

                                 一般来说, 如果类A继承了类B,那么类A 不仅有类B的方法与状态,还有属于自己的 状态与方法。

      6、表达类关系的UML符号:

         7、预定义类的使用:

      (1)已学过的预定义类:Math类、math类、String类、Scanner类;

      (2)要使用预定义类的方法,只需知道方法名和参数即可,无需了解它的内在实现过程;

      (3)使用预定义类需要在程序开始处用import命令导入该类所在的包路经。

        8、对象与对象变量:

      (1)在oop中,要想使用对象,就必须首先构造对象,并初始化对象的状态,然后通过对象调用类中的方法;

      (2Java中,用构造器构造并初始化对象;

      (3)构造器是类中一个特殊的方法,该方法与类名相同。不需要提供返回值;

      (4)构造并初始化对象的格式:

        new 构造器名(参数)

        9、对象的初始化实例:

      (1Date类,定义在Java标准类库的Java.util包中;

      (2)初始化Date类对象的实例:

        new   Date();

        System.out.println(new   Date())

      (3)可以在类中一个方法用于新创建的对象:String   s = new  Date().toString()

        10、对象变量:

      (1)如果要多次使用初始化的变量,可将初始化后的变量放在一个对象变量中;

        格式:  Date birthday = new Date();

      (2)对象变量保存对象后,可用对象变量引用对象;

        System.out.println(birthday.toString());

      (3)可将一个对象变量设置为NULL,表示该对象变量未引用任何对象。

      11、更改器方法和访问器方法:

      (1)一个类中对实例域进行修改的方法,更改器前面加set

      (2)一个类中对实例域进行访问的方法,前缀get

     12、用户自定义类:

      (1)类的定义包括两部分内容:声明和类体

      (2)类体由两部分构成:

                一为实体域(或成员变量)定义;二为方法定义。

      (3)域的定义:

     1)实例域:类定义时实例域部分所定义的变量;

     2)局部变量:方法体中定义的变量和方法的参数;

     3)实例域的隐藏性:局部变量与实例域名字相同时,则实例域被隐藏,即在这个方法内暂时失效。

     4)实例域的有效性:在整个类内部有效,而局部变量只在定义它的方法内有效。

     5)私有实例域的更改器和访问器:有时需要获得或设置私有实例域的值,此时需提供下面三项内容:

               a)一个私有的数据库;

               b)一个共有的域访问器方法;

               c)一个共有的域访问器方法;

    6final实例域:

         可以将实例域定义为final,此类域构建对象时时必须进行初始化;即必须确保在构造器执行之前,这个域的值被设置,并且在后面的操作中,不能再对它的值进行修改,即该域无修改器。

    final修饰符多用于基本数据类型域,或不可变类的域;

    7)方法定义:

      a)包括方法声明和方法体;

      b)方法的声明:名字,类型和参数等属性的说明:方法的类型描述的数返回值类型;

      c)方法体由局部变量和Java语句构成;一个类中可以有多个方法具有相同的名字,不同的类型,不同的参数,这种情况叫重载;

     13、构造器:用来构造并初始化对象。

      (1)构造器的名字必须要与它所在类名字相同;

      (2)每个包可以由一个以上的构造器;

      (3)构造器可以由一个,一个以上的参数;

      (4)构造器没有返回值;

      (5)构造器总是被new运算符调用;

    14、(1)静态域:绝大多数面向对象程序设计语言中,静态域被称为类域。      

        如果将域定义为static,每个类中只有一个这样的域。而每个对象对于所有的实例域却都有自己的一份拷贝。

           2main方法:main方法不对任何对象进行操作。静态的main方法将执行并创建程序所需要的对象。

      (3)静态常量:静态变量用的少,但是静态常量用的多。   

             (4).静态方法:用修饰的方法叫静态方法或类方法,静态方法可通过类名或对象来调用,而其他方法只能通过对象来调用。静态方法不能操作对象,不能在静态方法中访问实例域。但静态方法可以访问自身类中的静态域。可以认为静态方法是没有this参数的方法,而在一个非静态方法中,this参数是表示这个对象/对象构造器的隐式参数

    15、对象的定义:使用一个类创建一个对象时,即创建了一个类的实例域。只有通过对象才可以访问该域的公共实例域,调用类中共有的方法;

    16、类的导入:一个类可以直接使用它所在的包中的所有类,也可以使用来自其他包中的所有public类。

      (1)静态导入:import语句不仅可以导入类,还增加了静态导入方法和静态域的功能;

      (2)将类放如包中:首先用package语句指明报的名字,且该语句应为程序的第一条语句,然后才是定义类的语句;

      (3)如果源文件不使用package语句指明包名,源文件的类将属于默认包。

      (4)当一个源文件中使用了package语句时,那么这个包中所有的类文件的都必须放在与包名相匹配的目录中;

      (5)当程序中所使用的类位于不同包时,起源问价和类文件必须如下组织;

    17、包作用域:

      (1)类中标记为public的部分可以被任意类使用;

      (2)类中标记为private的部分只能在类中使用;

      (3)如果没有为类、方法或实例域指定访问控制修饰符publicprivate,这部分可以被同一包中的所有方法访问;

      (4)如果实例域不标记为private,将默认为包可见,这样做会破坏类的封装性。

    18、类路径:

      (1)类存储在文件系统的子目录中,类得路径必须与包名匹配;

      (2)在命令行方式下,设置类路径,使用-classpath选项指定类路径。

    第二部分:实验部分

    实验名称:实验三 类与对象的定义及使用

    1.  实验目的:

    (1) 熟悉PTA平台线上测试环境;

    (2) 理解用户自定义类的定义;

    (3) 掌握对象的声明;

    (4) 学会使用构造函数初始化对象;

    (5) 使用类属性与方法的使用掌握使用;

    (6) 掌握package和import语句的用途。

    3. 实验步骤与内容:

    实验

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

    实验1.1

    公民身份证号码按照GB11643—1999《公民身份证号码》国家标准编制,由18位数字组成:前6位为行政区划分代码,第7位至14位为出生日期码,第15位至17位为顺序码,第18位为校验码。从键盘输入1个身份证号,将身份证号的年月日抽取出来,按年-月-日格式输出。注意:输入使用Scanner类的nextLine()方法,以免出错。

    输入样例:

    34080019810819327X

    输出样例:

    1981-08-19

    程序代码:

     

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    import java.util.Scanner;

    public class test1 {

      

        public static void main(String[] args) {

            // TODO Auto-generated method stub

            Scanner in = new Scanner(System.in);

            System.out.println("please input your ID:");

            String s1 = in.nextLine();

            String s2,s3,s4;

            s2 = s1.substring(6, 10);

            s3 =s1.substring(10, 12);

            s4 = s1.substring(12, 14);

            System.out.println(s2+"-"+s3+"-"+s4);

        }

    }

     

    运行结果如图:

    实验1.2

    studentfile.txt文件内容是某班同学的学号与姓名,利用此文件编制一个程序,将studentfile.txt文件的信息读入到内存,并提供两类查询功能:(1)输入姓名查询学号;(2)输入学号查询姓名。要求程序具有友好人机交互界面。

    编程建议:

    1)从文件中读入学生信息,可以编写如下函数:

    public static void StudentsFromFile(String fileName))

    2)输入姓名查找学生学号,可以编写如下函数:

    public static String findStudent(String name)

    3)输入学号查找学生姓名,可以编写如下函数:

    public static String findStudent(String ID)

    程序代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    118

    119

    120

    import java.io.BufferedReader;

    import java.io.FileReader;

    import java.io.IOException;

    import java.util.ArrayList;

    import java.util.Scanner;

    public class Main {

        // private static Student students[];

        private static ArrayList<Student> list;

        public static void main(String[] args) {

            list = new ArrayList<>();

            Scanner in = new Scanner(System.in);

            try {

                readFile("studentfile.txt");

                System.out.println("请选择操作,1按姓名,2按学号,3退出");

                int i;

                while ((i = in.nextInt()) != 3) {

                    switch (i) {

                    case 1:

                        System.out.println("请输入姓名");

                        String name = in.next();

                        Student student = findStudentByName(name);

                        if (student == null) {

                            System.out.println("没找到");

                        } else {

                            System.out.println(student.toString());

                        }

                        System.out.println("请选择操作,1按姓名,2按学号,3退出");

                        break;

                    case 2:

                        System.out.println("请输入学号");

                        String id = in.next();

                        Student student1 = findStudentById(id);

                        if (student1 == null) {

                            System.out.println("没找到");

                        } else {

                            System.out.println(student1.toString());

                        }

                        System.out.println("请选择操作,1按姓名,2按学号,3退出");

                        break;

                    default:

                        System.out.println("输入有误");

                        System.out.println("请选择操作,1按姓名,2按学号,3退出");

                        break;

                    }

                }

            } catch (IOException e) {

                // TODO 自动生成的 catch 块

                e.printStackTrace();

            }finally {

                in.close();

            }

        }

        public static void readFile(String path) throws IOException {

            FileReader reader = new FileReader(path);

            BufferedReader br = new BufferedReader(reader);

            String result;

            while ((result = br.readLine()) != null) {

                Student student = new Student();

                student.setName(result.substring(13));

                student.setID(result.substring(0,12));

                list.add(student);

            }

            br.close();

        }

        public static Student findStudentByName(String name) {

            for (Student student : list) {

                if (student.getName().equals(name)) {

                    return student;

                }

            }

            return null;

        }

        public static Student findStudentById(String Id) {

            for (Student student : list) {

                if (student.getID().equals(Id)) {

                    return student;

                }

            }

            return null;

        }

    }

    class Student {

        private String name;

        private String ID;

        public String getName() {

            return name;

        }

        public void setName(String name) {

            this.name = name;

        }

        public String getID() {

            return ID;

        }

        public void setID(String iD) {

            ID = iD;

        }

        @Override

        public String toString() {

            // TODO 自动生成的方法存根

            return "姓名是:" + name + "学号是:" + ID;

        }

    }

    运行结果如下:

    实验2 导入第4章示例程序并测试。

    测试程序1

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

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

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

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

    姓名    性别   java成绩

    程序代码如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    import java.time.*;

    /**

     * This program tests the Employee class.

     * @version 1.13 2018-04-10

     * @author Cay Horstmann

     */

    public class EmployeeTest

    {

       public static void main(String[] args)

       {

          // fill the staff array with three Employee objects

          Employee[] staff = new Employee[3];        //构造Employee数组,并有三个雇员对象;

          staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);

          staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);

          staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

          // raise everyone's salary by 5%

          for (Employee e : staff)

             e.raiseSalary(5);                    //调用raiseSalary的方法提高雇员薪水15%;

          // print out information about all Employee objects

          for (Employee e : staff)

             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="

                + e.getHireDay());                 //打印每个雇员的信息;

       }

    }

    class Employee                             //定义Employee类;

    {

       private String name;

       private double salary;

       private LocalDate hireDay;            //类的实例域定义来存放的需要操作的数据;  

       public Employee(String n, double s, int year, int month, int day)

       {

          name = n;

          salary = s;

          hireDay = LocalDate.of(year, month, day);   //根据参数设置日期,参数分别为年月日;

       }                                          //构造Employee类的对象,并声明局部变量name,salary,hireday;

       public String getName()

       {

          return name;                            //取得name属性的值;

       }

       public double getSalary()

       {

          return salary;                          //取得salsry属性的值;

       }

       public LocalDate getHireDay()

       {

          return hireDay;                        //取得hireday属性的值;

       }

       public void raiseSalary(double byPercent)

       {

          double raise = salary * byPercent / 100;

          salary += raise;                       //调用方法的对象salary实例域设置为新值;

       }

    运行结果如下:

    测试程序2:

    编辑、编译、调试运行程序4-3(教材116);

    结合程序运行结果,理解程序代码,掌握静态域(netxtId)与静态方法(getNextId)的用法,在相关代码后添加注释;

    理解Java单元(类)测试的技巧。

    4-3程序代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    /**

     * 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 用三个employee对象填充staff数组

          var staff = new Employee[3];

          staff[0] = new Employee("Tom", 40000);

          staff[1] = new Employee("Dick", 60000);        //构造Employee数组,并有三个雇员对象;

          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                      //定义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;                        //构造Employee类的对象,并声明局部变量name,salary,hireday;

       }

       public String getName()

       {

          return name;                 //实例域name的访问器方法

       }

       public double getSalary()

       {

          return salary;                     //实例域salary的访问器方法

       }

       public int getId()

       {

          return id;                       //实例域id的访问器方法

       }

       public void setId()

       {

          id = nextId; // set id to next available id

          nextId++;

       }

       public static int getNextId()                       //实例域NextId的访问方法

       {

          return nextId; // returns static field     返回静态字段

       }

       public static void main(String[] args) // unit test   测试

       {

          var e = new Employee("Harry", 50000);

          System.out.println(e.getName() + " " + e.getSalary());

       }

    }

    运行结果:

    测试程序2:

    编辑、编译、调试运行程序4-3(教材116);

    结合程序运行结果,理解程序代码,掌握静态域(netxtId)与静态方法(getNextId)的用法,在相关代码后添加注释;

    理解Java单元(类)测试的技巧。

    4-3程序代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    /**

     * 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 用三个employee对象填充staff数组

          var staff = new Employee[3];

          staff[0] = new Employee("Tom", 40000);

          staff[1] = new Employee("Dick", 60000);        //构造Employee数组,并有三个雇员对象;

          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                      //定义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;                        //构造Employee类的对象,并声明局部变量name,salary,hireday;

       }

       public String getName()

       {

          return name;                 //实例域name的访问器方法

       }

       public double getSalary()

       {

          return salary;                     //实例域salary的访问器方法

       }

       public int getId()

       {

          return id;                       //实例域id的访问器方法

       }

       public void setId()

       {

          id = nextId; // set id to next available id

          nextId++;

       }

       public static int getNextId()                       //实例域NextId的访问方法

       {

          return nextId; // returns static field     返回静态字段

       }

       public static void main(String[] args) // unit test   测试

       {

          var e = new Employee("Harry", 50000);

          System.out.println(e.getName() + " " + e.getSalary());

       }

    }

    运行结果:

    测试程序3

    编辑、编译、调试运行程序4-4(教材121);

    结合程序运行结果,理解程序代码,掌握Java方法参数的用法,在相关代码后添加注释;

      

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    /**

     * 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);                  //调用 tripleValue

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

          System.out.println("Before: salary=" + harry.getSalary());

          tripleSalary(harry);                   //调用tripleSalary

          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);    //定义类型为var的a,b,并进行初始化

          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);               //x的调用

          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());  //x和y进行交换并输出

       }

    }

    class Employee // simplified Employee class

    {

       private String name;

       private double salary;          //类的实例域定义来存放的需要操作的数据;  

       public Employee(String n, double s)   //构造器调用

       {

          name = n;

          salary = s;

       }

       public String getName()

       {

          return name;              //实例域name的访问器方法

       }

       public double getSalary()

       {

          return salary;              //实例域salary访问器方法

       }

       public void raiseSalary(double byPercent)

       {

          double raise = salary * byPercent / 100;

          salary += raise;

       }

    }

    截图如下:

    EmployeeTest)程序代码:

    运行结果如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    public class EmployeeTest

    {

       public static void main(String[] args)

       {

          // 用三个employee对象填充staff数组

          Employee[] staff = new Employee[3];    

          staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);

          staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);

          staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

          // raise everyone's salary by 5%    给每人涨5%的工资

          for (Employee e : staff)     //进行foreach循环

             e.raiseSalary(5);

          // print out information about all Employee objects

          for (Employee e : staff)

             System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="

                + e.getHireDay());

       }

    }

    运行结果如下:

    1. 设计StudentTest程序代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    package student;

    import java.util.Scanner;

    public class student {

        String name;

        String sex;

        double javascore;

        public static void main(String[] args) {

            System.out.println("请输入学生人数");

            Scanner xi = new Scanner(System.in);

            int totalStudent = xi.nextInt();

            student[] stus= new student[totalStudent];

            for(int i=0;i<totalStudent;i++) {

                student s =new student();

                stus[i]=s;

                System.out.println("请输入第i个学生的姓名");

                s.name = xi.next();

                System.out.println("请输入第i个学生的性别");

                s.sex = xi.next();

                System.out.println("请输入第i个学生的java成绩");

                s.javascore = xi.nextDouble();

             

            }

            printstudent(stus);

            xi.close();

        }

        public static void printstudent(student[] s) {

            System.out.println("姓名\t性别\tjava成绩");

            for(int i=0;i<s.length;i++) {

                System.out.println(s[i].name+"\t"+s[i].sex+"\t"+s[i].javascore);

             }

        }

    }

    运行结果如下:

     

     

    测试程序4

    编辑、编译、调试运行程序4-5(教材129);

    结合程序运行结果,理解程序代码,掌握Java用户自定义类的用法,掌握对象构造方法及对象使用方法,在相关代码后添加注释。

    代码如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    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  用三个employee对象填充staff数组

          var staff = new Employee[3];

          staff[0] = new Employee("Harry", 40000);

          staff[1] = new Employee(60000);               //构造Employee数组,并有三个雇员对象;

          staff[2] = new Employee();

          // 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设置为0到999之间的随机值

          nextId = generator.nextInt(10000);

       }

       // object initialization block  对象 initialization块

       {

          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);      //this用来引用当前对象

       }

       // the default constructor      错误的构造器

       public Employee()

       {

          // name initialized to ""--see above 名称初始化为同上

          // salary not explicitly set--initialized to 0 工资未显示设置,初始化为0

          // id initialized in initialization block id初始化

       }

       public String getName()

       {

          return name;            //实例域name的访问器方法

       }

       public double getSalary()

       {

          return salary;    //实例域salary的访问器方法

       }

       public int getId()

       {

          return id;     //实例域id的访问器方法

       }

    }

    运行结果如下:

    测试程序5

    编辑、编译、调试运行程序4-64-7(教材135);

    结合程序运行结果,理解程序代码,掌握Java包的定义及用法,在相关代码后添加注释;

    4.6代码如下:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    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      因为import语句,不需要使用com.horstmann.corejava

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

       }                                //由于使用了静态导入语句,在这里不需要使用System.out

    }

    运行结果如下:

    4-7程序代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    package com.horstmann.corejava;

    // the classes in this file are part of this package 这个文件中的类就是这个包中的一部分

    import java.time.*;

    // import statements come after the package statement import语句位于package语句之后

    /**

     * @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);//根据参数设置日期,参数分别为年月日;

       }                              //构造Employee类的对象,并声明局部变量name,salary,hireday;

       public String getName()

       {

          return name;               //实例域name的访问器方法

       }

       public double getSalary()

       {

          return salary;                      //实例域salary的访问器方法

       }

       public LocalDate getHireDay()

       {

          return hireDay;               //实例域hireDay的访问器方法

       }

       public void raiseSalary(double byPercent)

       {

          double raise = salary * byPercent / 100;

          salary += raise;                  //调用方法的对象salary实例域设置为新值;

       }

    }

    运行结果如下:

    第三部分:实验总结

            

           本次实验首先是编译运行程序了解文件的输入输出,在实验三的基础上,进一步使用student类编写了一个Java程序。通过本次实验,我进一步了解到对象与类的定义以及各自的特点。

    在之前的学习中,已经了解了几种预定义类,在这周的学习中,掌握了预定义类的使用。在实验过程中,还是遇到了很多问题,比如误将float型相加,而float型加在一起要用强制转换

    类型,这是因为基础知识学习不够的原因。而对于新学习的知识,有很多地方还是不明白,做实验时,也是在同学和学长的帮助下,最终才完成了实验。在以后的学习过程中,自己一

    一定要多练习写代码,提高自己的学习能力。

     

     

  • 相关阅读:
    Android Developers:使ListView滑动流畅
    Nexus 搭建maven 私有仓库
    Eclipse 配置Maven以及修改默认Repository
    maven常用命令介绍
    maven 相关
    session机制详解以及session的相关应用
    正确理解web交互中的cookie与session
    前端开发中Cookie那些事儿
    html转义表
    常用的Linux命令
  • 原文地址:https://www.cnblogs.com/zxp19990614/p/11574439.html
Copyright © 2020-2023  润新知