1、实验目的

(1)预定义类:(不是所有类都具有面向对象特征)

构造器的类名和方法名相同,是一种特殊的方法,用来构造并初始化对象。

(2)用户自定义类:(各种主力类,没有main方法,却有实例域和实例方法)

(3) 静态域:将域定义为static,每个类中只有一个这样的域,每一个对象对于所有的实例域都有一份拷贝)

静态常量:在程序中可以采用Math.PI的形式获得这个常量(多次使用的静态常量System.out)

(4) 对象构造

重载:如果多个方法有相同的名字、不同的参数,便产生了重载;

重载解析:编译器找不到匹配的参数,就会产生编译时错误,因为根本不存在匹配,或者没有一个比其它更好的过程称作重载解析。

显示域初始化:通过重载类的构造器方法,可以采用多种形式设置类的实例域的初始状态,确保不管怎样调用构造器,每个实例域都可以被设置为一个有意义的初值。

(5)Java允许使用包将类组织起来;借助包可以方便地组织自己的代码,并将自己的代码与别人提供的代码库分开管理,而且使用包可以确定类名的唯一性。

2、实验内容和步骤

实验1 测试以下程序,掌握文件输入输出程序设计技术(文件输入输出,教材61-62).

 1 import java.io.*;2 import java.util.*;3 public class FileWriteReadTest {4     public static void main(String[] args) throws IOException{5         //写入文件演示6         PrintWriter out = new PrintWriter("myfile.txt");7         out.println("姓名 高数 Java 数据结构 平均成绩 总成绩");8         out.println("张三 20 30 40 0 0");9         out.println("李四 50 60 70 0 0");
10         out.close();//输出完毕,需要close
11         //读入文件演示
12         Scanner in = new Scanner(new File("myfile.txt"));//为myfile.txt这个File创建一个扫描器in
13         int number = 1;//行号
14         System.out.println(in.nextLine());
15         while(in.hasNextLine()){//判断扫描器是否还有下一行未读取,该循环把文件的每一行都读出
16             String line = in.nextLine();//读出myfile.txt的下一行
17             System.out.print("第"+(++number)+"行的内容: ");
18             Scanner linescanner = new Scanner(line);//行内容建立扫描器
19             linescanner.useDelimiter(" ");//使用空格作为分隔符
20             String name = linescanner.next();
21             String math = linescanner.next();
22             String java = linescanner.next();
23             String ds = linescanner.next();
24             String avg = linescanner.next();
25             String total = linescanner.next();
26             System.out.println("name="+name+"  math="+math+"  java="+java+"  ds="+ds+"  avg"+avg+"  total="+total);
27         }
28         in.close();//读入完毕,最后需要对其进行close。
29     }
30 }

测试结果截图如下:

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

测试程序1:

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

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

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

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

姓名      性别 java成绩

(1)源代码:

(2)Employee.java、 EmployeeTest.java 运行结果截图:

 1 import java.time.*;2 3 /**4  * This program tests the Employee class.5  * @version 1.12 2015-05-086  * @author Cay Horstmann7  */8 public class EmployeeTest9 {
10    public static void main(String[] args)
11    {
12       // fill the staff array with three Employee objects
13       Employee[] staff = new Employee[3];
14
15       staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
16       staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
17       staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
18
19       // raise everyone's salary by 5%
20       for (Employee e : staff)
21          e.raiseSalary(5);
22
23       // print out information about all Employee objects
24       for (Employee e : staff)
25          System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay="
26                + e.getHireDay());
27    }
28 }
29
30 class Employee
31 {
32    private String name;
33    private double salary;
34    private LocalDate hireDay;
35
36    public Employee(String n, double s, int year, int month, int day)
37    {
38       name = n;
39       salary = s;
40       hireDay = LocalDate.of(year, month, day);
41    }
42
43    public String getName()
44    {
45       return name;
46    }
47
48    public double getSalary()
49    {
50       return salary;
51    }
52
53    public LocalDate getHireDay()
54    {
55       return hireDay;
56    }
57
58    public void raiseSalary(double byPercent)
59    {
60       double raise = salary * byPercent / 100;
61       salary += raise;
62    }
63 }

(3)EmployeeTest.java源代码及运行结果截图如下

 1 import java.time.LocalDate;2 import java.util.Scanner;3 /**4  * This program tests the Employee class.5  * @version 1.12 2018-09-256  * @author yangxiaoxiao7  */8 public class StudentTest9 {
10     String name;
11     String sex;
12     double score;
13    public static void main(String[] args)
14     //public static void ScannerTest(){
15    {
16
17
18        int i = 0;
19
20         System.out.print("numer:");
21         Scanner sc= new Scanner(System.in);
22        int number = sc.nextInt();
23        StudentTest Stu[] = new StudentTest[number];
24         for (i = 0; i < Stu.length; i++) {
25             Stu[i] = new StudentTest();
26             System.out.print( (i + 1)+" "+"姓名:" );
27             Stu[i].name = sc.next();
28             System.out.print( (i + 1)+" " +"性别:");
29             Stu[i].sex = sc.next();
30             System.out.print( (i + 1)+" " +"java成绩:");
31             Stu[i].score = sc.nextDouble();
32         }
33
34
35       System.out.println("\t姓名\t性别\t成绩");
36         for (StudentTest StudentTest : Stu) {
37             System.out.println("\t" + StudentTest.name+ "\t" + StudentTest.sex + "\t" + StudentTest.score);
38         }
39
40
41    }
42 }
43
44
45     

测试程序2:

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

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

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

 1 /**2  * This program demonstrates static methods.3  * @version 1.01 2004-02-194  * @author Cay Horstmann5  */6 public class StaticTest7 {8    public static void main(String[] args)9    {
10       // fill the staff array with three Employee objects
11       Employee[] staff = new Employee[3];
12
13       staff[0] = new Employee("Tom", 40000);
14       staff[1] = new Employee("Dick", 60000);
15       staff[2] = new Employee("Harry", 65000);
16
17       // print out information about all Employee objects
18       for (Employee e : staff)
19       {
20          e.setId();
21          System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
22                + e.getSalary());
23       }
24
25       int n = Employee.getNextId(); // calls static method
26       System.out.println("Next available id=" + n);
27    }
28 }
29
30 class Employee
31 {
32    private static int nextId = 1;
33
34    private String name;
35    private double salary;
36    private int id;
37
38    public Employee(String n, double s)
39    {
40       name = n;
41       salary = s;
42       id = 0;
43    }
44
45    public String getName()
46    {
47       return name;
48    }
49
50    public double getSalary()
51    {
52       return salary;
53    }
54
55    public int getId()
56    {
57       return id;
58    }
59
60    public void setId()
61    {
62       id = nextId; // set id to next available id
63       nextId++;
64    }
65
66    public static int getNextId()
67    {
68       return nextId; // returns static field
69    }
70
71    public static void main(String[] args) // unit test
72    {
73       Employee e = new Employee("Harry", 50000);
74       System.out.println(e.getName() + " " + e.getSalary());
75    }
76 }

测试程序

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

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

 1 /**2  * This program demonstrates parameter passing in Java.3  * @version 1.00 2000-01-274  * @author Cay Horstmann5  */6 public class ParamTest7 {8    public static void main(String[] args)9    {
10       /*
11        * Test 1: Methods can't modify numeric parameters
12        */
13       System.out.println("Testing tripleValue:");
14       double percent = 10;
15       System.out.println("Before: percent=" + percent);
16       tripleValue(percent);
17       System.out.println("After: percent=" + percent);
18
19       /*
20        * Test 2: Methods can change the state of object parameters
21        */
22       System.out.println("\nTesting tripleSalary:");
23       Employee harry = new Employee("Harry", 50000);
24       System.out.println("Before: salary=" + harry.getSalary());
25       tripleSalary(harry);
26       System.out.println("After: salary=" + harry.getSalary());
27
28       /*
29        * Test 3: Methods can't attach new objects to object parameters
30        */
31       System.out.println("\nTesting swap:");
32       Employee a = new Employee("Alice", 70000);
33       Employee b = new Employee("Bob", 60000);
34       System.out.println("Before: a=" + a.getName());
35       System.out.println("Before: b=" + b.getName());
36       swap(a, b);
37       System.out.println("After: a=" + a.getName());
38       System.out.println("After: b=" + b.getName());
39    }
40
41    public static void tripleValue(double x) // doesn't work
42    {
43       x = 3 * x;
44       System.out.println("End of method: x=" + x);
45    }
46
47    public static void tripleSalary(Employee x) // works
48    {
49       x.raiseSalary(200);
50       System.out.println("End of method: salary=" + x.getSalary());
51    }
52
53    public static void swap(Employee x, Employee y)
54    {
55       Employee temp = x;
56       x = y;
57       y = temp;
58       System.out.println("End of method: x=" + x.getName());
59       System.out.println("End of method: y=" + y.getName());
60    }
61 }
62
63 class Employee // simplified Employee class
64 {
65    private String name;
66    private double salary;
67
68    public Employee(String n, double s)
69    {
70       name = n;
71       salary = s;
72    }
73
74    public String getName()
75    {
76       return name;
77    }
78
79    public double getSalary()
80    {
81       return salary;
82    }
83
84    public void raiseSalary(double byPercent)
85    {
86       double raise = salary * byPercent / 100;
87       salary += raise;
88    }
89 }

测试程序

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

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

 1 import java.util.*;2 3 /**4 * This program demonstrates object construction.5 * @version 1.01 2004-02-196 * @author Cay Horstmann7 */8 public class ConstructorTest9 {
10 public static void main(String[] args)
11 {
12 // fill the staff array with three Employee objects
13 Employee[] staff = new Employee[3];
14
15 staff[0] = new Employee("Harry", 40000);
16 staff[1] = new Employee(60000);
17 staff[2] = new Employee();
18
19 // print out information about all Employee objects
20 for (Employee e : staff)
21 System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary="
22 + e.getSalary());
23 }
24 }
25
26 class Employee
27 {
28 private static int nextId;
29
30 private int id;
31 private String name = ""; // instance field initialization
32 private double salary;
33
34 // static initialization block
35 static
36 {
37 Random generator = new Random();
38 // set nextId to a random number between 0 and 9999
39 nextId = generator.nextInt(10000);
40 }
41
42 // object initialization block
43 {
44 id = nextId;
45 nextId++;
46 }
47
48 // three overloaded constructors
49 public Employee(String n, double s)
50 {
51 name = n;
52 salary = s;
53 }
54
55 public Employee(double s)
56 {
57 // calls the Employee(String, double) constructor
58 this("Employee #" + nextId, s);
59 }
60
61 // the default constructor
62 public Employee()
63 {
64 // name initialized to ""--see above
65 // salary not explicitly set--initialized to 0
66 // id initialized in initialization block
67 }
68
69 public String getName()
70 {
71 return name;
72 }
73
74 public double getSalary()
75 {
76 return salary;
77 }
78
79 public int getId()
80 {
81 return id;
82 }
83 }

测试程序5:

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

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

4-6

 1 import com.horstmann.corejava.*;2 // the Employee class is defined in that package3 4 import static java.lang.System.*;5 6 /**7  * This program demonstrates the use of packages.8  * @version 1.11 2004-02-199  * @author Cay Horstmann
10  */
11 public class PackageTest
12 {
13    public static void main(String[] args)
14    {
15       // because of the import statement, we don't have to use
16       // com.horstmann.corejava.Employee here
17       Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
18
19       harry.raiseSalary(5);
20
21       // because of the static import statement, we don't have to use System.out here
22       out.println("name=" + harry.getName() + ",salary=" + harry.getSalary());
23    }
24 }

4-7

 1 package com.horstmann.corejava;2 3 // the classes in this file are part of this package4 5 import java.time.*;6 7 // import statements come after the package statement8 9 /**
10  * @version 1.11 2015-05-08
11  * @author Cay Horstmann
12  */
13 public class Employee
14 {
15    private String name;
16    private double salary;
17    private LocalDate hireDay;
18
19    public Employee(String name, double salary, int year, int month, int day)
20    {
21       this.name = name;
22       this.salary = salary;
23       hireDay = LocalDate.of(year, month, day);
24    }
25
26    public String getName()
27    {
28       return name;
29    }
30
31    public double getSalary()
32    {
33       return salary;
34    }
35
36    public LocalDate getHireDay()
37    {
38       return hireDay;
39    }
40
41    public void raiseSalary(double byPercent)
42    {
43       double raise = salary * byPercent / 100;
44       salary += raise;
45    }
46 }

实验3  编写长方形类Rectangle与圆形类Circle,其中Rectangle类设置私有属性:width,length;Circle类设置私有属性radius。编写Rectangle类的带参构造函数Rectangle(int width,int length), Circle类的带参构造函数Circle(int radius),编写两个类的toString方法(Eclipse可自动生成)。上述2个类均定义以下方法:

求周长的方法public int getPerimeter()

求面积的方法public int getArea()

在main方法中完成以下任务:

(1) 输入1行长与宽,创建一个Rectangle对象;

(2) 输入1行半径,创建一个Circle对象;

(3) 将两个对象的周长加总输出,将两个对象的面积加总输出

(3) 将两个对象的周长加总输出,将两个对象的面积加总输出。

package 实验三;
import java.util.Scanner;
public class 实验3 {
public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入矩形的x y:"); int x = sc.nextInt();int y = sc.nextInt();System.out.println("矩形的c: "+Perimeter.getPerimeter(x,y));System.out.println("矩形的area:"+Area.getArea(x,y));System.out.println("请输入圆的半径r:"); int r = sc.nextInt();System.out.println("圆c:"+Perimeter.getPerimeter(r));System.out.println("圆area:"+Area.getArea(r));double c = Perimeter.getPerimeter(x,y)+Perimeter.getPerimeter(r);double s = Area.getArea(x,y)+Area.getArea(r);System.out.println("矩形与圆的c之和:"+c);System.out.println("矩形与圆的area之和:"+s);}
}
class Perimeter{public static double getPerimeter(double width, double height) {return 2*(width + height);}public static double getPerimeter(int r) {return 2*3.14*r;}}
class Area{public static double getArea(double width, double height){return width * height;}public static double getArea(int r) {return 3.14*r*r;}
}

实验结果如下图所示:

实验总结

通过本章《对象与类》的学习 ,首先对于本章的理论知识有了初步的了解。如使用预定义类,用户自定义类,静态域与静态方法,还有方法参数,包以及文档注释等等。另外,通过实验测试程序也对本章知识有了更多的认识。

转载于:https://www.cnblogs.com/zd0421/p/9706266.html

2017711010137 赵栋 《面向对象程序设计》第四章学习总结相关推荐

  1. C++程序设计教程(钱能)第四章 学习笔记

    C++程序设计教程(钱能)第四章 学习笔记 4.1 名词解释与操作符 4.1.1 名词解释 4.1.2 操作符汇总 4.1.3 操作符的说明 4.2 算数运算问题 4.2.1 周而复始的整数 4.2. ...

  2. JavaScript高级程序设计第四版学习--第二十四章

    title: JavaScript高级程序设计第四版学习–第二十四章 date: 2021-5-31 10:46:01 author: Xilong88 tags: JavaScript 本章内容: ...

  3. 面向对象程序设计第四单元总结(UML系列)

    2019面向对象程序设计第四单元总结 前言 ​ 本单元是面向对象程序设计课程的最后一个单元了,本单元是和UML模型相关,也就是说,我们需要正确理解UML模型的基础上,对构建出的UML模型进行解析,但是 ...

  4. 我要翻译《Think Python》- 006 第四章 学习案例:接口设计

    本文翻自:Allen B. Downey --<Think Python> 原文链接:http://www.greenteapress.com/thinkpython/html/think ...

  5. Educoder--Java面向对象(第四章)String类

    ***号外!号外! 新的一期已经更新啦!链接Java面向对象(第二章)封装.继承和多态 号外!号外!新的一期更新啦! Java面向对象(第三章)- 综合练习 以后还会第一时间更新! 喜欢博主的博客还可 ...

  6. JavaScript 高级程序设计第四章解读,总结。

    第四章 变量,作用域与内存 通过变量使用原始值 - 1. 原始值与引用值+ 原始值: 最简单的数据+ 引用值: 多个值构成的对象 - 2. 原始值有哪些+ Undefined Null Boolean ...

  7. JavaScript高级程序设计 第四章---变量 作用域 内存

    第四章-变量 作用域 内存 关键字:变量 作用域 内存 本章内容 通过变量使用原始值与引用值 理解执行上下文 理解垃圾回收 4.1 原始值与引用值 ECMAScript 变量可以包含两种不同类型的数据 ...

  8. 数字图像处理与MATLAB 第四章学习笔记

    第四章 图像复原与重建 图像复原技术主要目的是以预先确定的目标来改善图像,大部分属于客观处理,面向退化模型,并采用相反的过程进行处理,以便恢复出原图像. 图像增强技术基本上是一种探索性过程,即根据人类 ...

  9. 《鲁棒控制——线性矩阵不等式处理方法》(俞立)第二、三、四章学习笔记

    第二章   线性矩阵不等式  :非零向量,  或者的最大特征值小于0. 是凸集.(设V是数域P上的线性空间,W是V的一个非空子集,如果对W中任意两个向量a,b以及任意0<=c<=1,都有c ...

  10. 计算机网络第四章学习通题目及答案

    目录 分类的 IP 地址 IP地址与硬件地址 ARP与RARP协议 IP数据报的格式 划分子网 ​ 第七次练兵 分组转发算法 ​ CIDR(构造超网) ICMP报文及应用 内部网关协议RIP 分类的 ...

最新文章

  1. 国家航天局:中国空间站预计到2022年前后建成
  2. 【android API】 ListView api 翻译
  3. LeetCode 873. 最长的斐波那契子序列的长度 题目详解
  4. 《一个程序员的奋斗史》正式上架~
  5. iOS10 CallKit简单开发
  6. 浅析微信支付:申请退款、退款回调接口、查询退款
  7. MathCAD求解方程组
  8. python读取数据库数据、并保存为docx_Python - 爬取博客园某一目录下的随笔 - 保存为docx...
  9. stm32手册_STM32的GPIO概念简介
  10. linux c中字符替换函数,Linux C 支持正则表达式的字符串替换函数
  11. [Active Learning] Multi-Criteria-based Active Learning
  12. 大数据可视化的意义在哪
  13. shell脚本使用getopts自定义传入参数选项
  14. webpack4.0版本中的js压缩问题
  15. SSO —— 单点登录CAS与OAuth2
  16. Aspenone.hysys.V7.1多国语言包(含中文)
  17. ubuntu下bitcoin core的安装和编译
  18. TSC 打印机开发TSPL黑底白字的打印以及一些问题
  19. 英语前缀 2011年8月16日15:55:43
  20. 【儿童节】2018 下半年 Java 后端工程师的书单推荐

热门文章

  1. nginx rua代码同步非阻塞
  2. Python实现仿射密码
  3. 【QT 基础教程 九】QVector类详解
  4. KubernetesDatabase-k8s中helm方式安装postgresql及pgadmin
  5. 组播地址MAC的计算
  6. 关于Topic设计的思考
  7. VS2019制作DLL文件
  8. 乖乖不得了,这款数字机器人竟然能够识别发票扫描信息!
  9. 数据同步利器之Tapdata Cloud
  10. springboot项目扫描不到controller中的解决方法