一、理论知识部分

类是构建造对象的模板和蓝图。类是对一组具有相同属性、行为、关系及语义的对象的描述,是具有相同类型对象的抽象。类中使用变量来表示对象的抽象状态,用方法抽象出对象的行为特征。

封装是将数据和行为组合在一个包内,并对对象的使用者隐藏了数据的实现方法。对象中的数据称为实例域,操作数据的过程称为方法,对于每个特定的类对象都有一组特定的实例域值,这些值的集合就是这个对象地当前状态。

对象的三个主要特性:对象的行为、对象的状态、对象标识。

标准类(预定义类):Math类,math类(大整数,大浮点数),string类,scanner类。

构造器是一种特殊的方法,用来构造并初始化对象。构造器名字应与类名相同;要想构造一个Data对象,需要在构造器前面加上new操作符。

更改器方法:改变对象的状态,前缀为set。

访问器方法:只访问对象不修改对象的方法。前缀为get。

二、实验部分

1、实验目的与要求

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

(2) 掌握对象的声明;

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

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

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

2、实验内容和步骤

实验1 测试以下程序,掌握文件输入输出程序设计技术

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

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

测试程序1:

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

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

package test0;

import java.time.*;

/**
* This program tests the Employee class.
* @version 1.12 2015-05-08
* @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];

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

// 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
{
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);
}

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

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

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

姓名      性别     java成绩

 1 package test05;
 2
 3 import java.util.*;
 4
 5 public class Student {
 6
 7     public static void main(String[] args) {
 8         // TODO 自动生成的方法存根
 9          int x=2;
10           Employee[] staff = new Employee[x];
11           System.out.println("请输入学生:");
12           Scanner in = new Scanner(System.in);
13           for(int i=0;i<staff.length;i++) {
14               staff[i]=new Employee(in.next(),in.next(),in.nextInt());
15           }
16           System.out.println("name"+" "+"sex"+" "+" "+"javascore");
17
18           // 输出二个学生的信息
19           for (Employee e : staff)
20              System.out.println(e.getName() +"   "+e.getSex()+"        "+e.getJavaScore());
21        }
22     }
23
24     class Employee
25     {
26        private String name;
27        private String sex;
28        private int javascore;
29        public Employee(String n, String s, int m)
30        {
31           name = n;
32           sex = s;
33           javascore =m;
34        }
35
36        public String getName()
37        {
38           return name;
39        }
40
41        public String getSex()
42        {
43           return sex;
44        }
45
46        public int getJavaScore()
47        {
48           return javascore;
49        }
50     }

测试程序2:

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

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

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

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

测试程序3:

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

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

package test03;/*** This program demonstrates parameter passing in Java.* @version 1.00 2000-01-27* @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);System.out.println("After: percent=" + percent);/** Test 2: Methods can change the state of object parameters*/System.out.println("\nTesting tripleSalary:");Employee harry = new Employee("Harry", 50000);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:");Employee a = new Employee("Alice", 70000);Employee b = new Employee("Bob", 60000);System.out.println("Before: a=" + a.getName());System.out.println("Before: b=" + b.getName());swap(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
{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;}
}

测试程序4:

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

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

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

测试程序5:

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

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

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

 1 package com.horstmann.corejava;
 2
 3 // the classes in this file are part of this package
 4
 5 import java.time.*;
 6
 7 // import statements come after the package statement
 8
 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对象;

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

 1 package test05;
 2
 3 import java.util.Scanner;
 4
 5 public class add {
 6
 7     public static void main(String[] args) {
 8         // TODO 自动生成的方法存根
 9         Scanner in=new Scanner(System.in);
10         System.out.println("请输入长方形的长与宽:");
11         int length=in.nextInt();
12         int width=in.nextInt();
13         Rectangle  a=new Rectangle(length, width);
14         System.out.println("长方形的周长为:"+a.getPerimeter());
15         System.out.println("长方形的面积为:"+a.getArea());
16         System.out.println("请输入圆的半径:");
17         int r=in.nextInt();
18         Circle b=new Circle(r);
19         System.out.println("圆的周长为:"+b.getPerimeter());
20         System.out.println("圆的面积为:"+b.getArea());
21         System.out.println("长方形和圆的周长之和="+(a.getPerimeter()+b.getPerimeter()));
22         System.out.println("长方形和圆的面积和="+(a.getArea()+b.getArea()));
23     }
24 }
25 class Rectangle
26 {
27     private int length;
28     private int width;
29     public Rectangle(int l,int w)
30     {
31         length=l;
32         width=w;
33     }
34     public int getPerimeter()
35     {
36         int Perimeter=(length+width)*2;
37         return Perimeter;
38     }
39
40     public int getArea()
41     {
42         int Area=length*width;
43         return Area;
44     }
45 }
46 class Circle
47 {
48     private int radius;
49     double Pi=3.14;
50     public Circle(int r)
51     {
52         radius=r;
53     }
54     public double getPerimeter()
55     {
56         double Perimeter=2*Pi*radius;
57         return Perimeter;
58     }
59     public double getArea()
60     {
61         double Area=Pi*radius*radius;
62         return Area;
63     }
64
65 }

实验总结:通过上机实验,更好的将课本知识运用到实践中,有利于更好的掌握了解Java知识。通过这次实验,我了解到了什么是类,对象。但是对于上机实验仍不是很熟悉,缺少练习,学习Java的关键是要坚持练习,慢慢地了解代码的内容,课后多做练习,多上机做实验,熟能生巧。通过多次练习,查漏补缺,加强对代码的编程。

转载于:https://www.cnblogs.com/sisi-713/p/9696457.html

徐思201771010132《面向对象程序设计(java)》第四周学习总结相关推荐

  1. 20155334 2016-2017-2 《Java程序设计》第四周学习总结

    20155334 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第六章:继承与多态 继承:面对对象中,子类继承父类,避免重复的行为定义 extends表示会 ...

  2. 20155303 2016-2017-2 《Java程序设计》第四周学习总结

    20155303 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 继承避免多个类间重复定义共同行为,使用关键字exten ...

  3. 20155305乔磊2016-2017-2《Java程序设计》第四周学习总结

    20155305乔磊2016-2017-2<Java程序设计>第四周学习总结 教材学习内容总结 继承 继承就是避免多个类间重复定义共同行为. 面向对象中,子类继承父类,就是把程序中相同的代 ...

  4. Week04《Java程序设计》第四周学习总结

    Week04<Java程序设计>第四周学习总结 1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:static关键字,final关键字,静态初始块,抽象类,继承, ...

  5. 20165218 2017-2018-1 《Java程序设计》第四周学习总结

    20165218 2017-2018-1 <Java程序设计>第四周学习总结 教材学习内容总结 第五章 子类与继承 子类与父类 通过关键字extands定义子类 class 子类 exta ...

  6. 201771010118马昕璐《面向对象程序设计java》第八周学习总结

    第一部分:理论知识学习部分 1.接口 在Java程序设计语言中,接口不是类,而是对类的一组需求描述,由常量和一组抽象方法组成.Java为了克服单继承的缺点,Java使用了接口,一个类可以实现一个或多个 ...

  7. 【Java】《面向对象程序设计——Java语言》Castle代码修改整理

    前言 最近闲来无事刷刷MOOC,找到以前看的浙大翁凯老师的<面向对象程序设计--Java语言>课程,重新过一遍仍觉受益颇深. 其中有一个Castle的例子,思路很Nice但代码很烂,翁凯老 ...

  8. 20155225 2006-2007-2 《Java程序设计》第四周学习总结

    20155225 2006-2007-2 <Java程序设计>第四周学习总结 教材学习内容总结 对"是一种"语法测试几次之后,总结一句:满足"是一种" ...

  9. 20175126《Java程序设计》第四周学习总结

    # 20175126 2016-2017-2 <Java程序设计>第四周学习总结 ## 教材学习内容总结 - 本周学习方式主要为手动敲打教材代码和观看APP上的视频资源自学. - 学习内容 ...

  10. 计算机JAVA相关说课稿_面向对象程序设计-java说课稿

    面向对象程序设计-java说课稿 面向对象程序设计-JAVA说课稿,计算机系 毕景霞,目录,一.说教材 二.说教学目标 三.说重点难点 四.说教学方法 五.说教学内容 六.教学效果及总结,(一)教材的 ...

最新文章

  1. Linux 入门记录:六、Linux 硬件相关概念(硬盘、磁盘、磁道、柱面、磁头、扇区、分区、MBR、GPT)...
  2. qq空间等闪动的文字怎么做?
  3. 《编写可读代码的艺术》读书笔记
  4. 计算机选修课学什么,计算机专业都学什么 主要课程有什么
  5. 学习笔记6-小项目-走迷宫、推箱子
  6. linux内核压缩制作bzImage
  7. 10个大数据领域的杰出公司
  8. 麒麟芯片或“绝版”,华为多系列手机涨价​;一加回应“刘作虎回归OPPO”;DBeaver 7.2 发布| 极客头条
  9. CSS3 高斯模糊与动画效果
  10. 跳转到系统挑选铃声的页面
  11. Python爬虫教程:包图网免费付费素材爬取【附源码】
  12. 【常用模块】电容触摸按键模块(原理讲解、STM32实例操作)
  13. python wmic_wmic linux python
  14. Android中获取系统所认为的最小滑动距离TouchSlop
  15. 域名注册_申请证书\SSL证书\tls证书
  16. 全球最受欢迎电商平台有哪些?这些平台怎么快速增加销量?
  17. compareto返回1和-1的区别_温故篇:Comparable与Compatator的区别
  18. 二叉树的基本操作的实现
  19. HDMI EDID详细解析——C代码实现
  20. 这3种直觉思维,会让你越来越“穷”!

热门文章

  1. 搜狗高速浏览器收藏夹怎么恢复 搜狗浏览器收藏夹恢复教程
  2. 【Android】Doze模式识别与检测
  3. Excel函数(4)日期、文本函数
  4. ArcGIS之栅格地图配准
  5. 计算机一级改扩展名,怎么改文件扩展名,教您电脑win7改文件扩展名的方法
  6. 俺博士三年的一点体会
  7. mac adb安装和使用
  8. 微信公众号如何运营和管理?
  9. php获取汉字的首字母,php获取汉字拼音首字母的函数(真正可以使用的)
  10. 数据结构(一)、二叉树(BT),二叉查找树(BST),平衡二叉树(AVL树)