徐思201771010132《面向对象程序设计(java)》第四周学习总结
一、理论知识部分
类是构建造对象的模板和蓝图。类是对一组具有相同属性、行为、关系及语义的对象的描述,是具有相同类型对象的抽象。类中使用变量来表示对象的抽象状态,用方法抽象出对象的行为特征。
封装是将数据和行为组合在一个包内,并对对象的使用者隐藏了数据的实现方法。对象中的数据称为实例域,操作数据的过程称为方法,对于每个特定的类对象都有一组特定的实例域值,这些值的集合就是这个对象地当前状态。
对象的三个主要特性:对象的行为、对象的状态、对象标识。
标准类(预定义类):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的关键是要坚持练习,慢慢地了解代码的内容,课后多做练习,多上机做实验,熟能生巧。通过多次练习,查漏补缺,加强对代码的编程。