《面向对象程序设计(java)》 第四周学习总结

第一部分:理论知识

1.类与对象

a.类(class)是构造对象的模板或蓝图。由类构造对象的过程称为创建类的实例;

java中类声明的格式如下:

[类修饰符] class 类名 [extends 父类名称] [implements 接口名称列表]

{

变量定义及初始化;

方法定义及方法体;

}

public:类的访问控制符。Java类具有两种访问访问修饰符:public和default。public允许类具有完全开放的可见性,所有其他类都可以访问它,省略public,则为default,即只有位于同一个包(本质上就是文件夹)中的类可以访问该类。

abstract指明该类为一个抽象类,说明该类是一个定义不完全的类,需要被继承,才能实例化创建对象。

b.对象:即数据,对象有三个特性——1.行为 2.状态 3.标识。

c、方法,变量.

d.类是对象,事物的描述和抽象,是具有相同属性和行为的对象集合。对象则是该类事物的实例。    

2.预定义类 、API 

Math类、String类、math类、Scanner类、LocalDate类

3.自定义类

4.静态域和静态方法    绝大多数面向对象程序设计语言中,静态域被称为类域。如果将域定义为static,每个类中只有一个这样的域。而每个对象对于所有的实例域却都有自己的一份拷贝。

构造器、更改器方法、访问器方法,静态方法

5.方法参数

6.包

7.文档注释技术

第二部分:实验部分

1、实验目的与要求

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

(2) 掌握对象的声明;

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

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

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

2、实验内容和步骤

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

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

测试结果截图如下:

实验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 运行结果截图:

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

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

 import java.time.LocalDate;
import java.util.Scanner;
/**
* This program tests the Employee class.
* @version 1.12 2018-09-25
* @author yangxiaoxiao
*/
public class StudentTest
{
String name;
String sex;
double score;
public static void main(String[] args)
//public static void ScannerTest(){
{ int i = 0; System.out.print("numer:");
Scanner sc= new Scanner(System.in);
int number = sc.nextInt();
StudentTest Stu[] = new StudentTest[number];
for (i = 0; i < Stu.length; i++) {
Stu[i] = new StudentTest();
System.out.print( (i + 1)+" "+"姓名:" );
Stu[i].name = sc.next();
System.out.print( (i + 1)+" " +"性别:");
Stu[i].sex = sc.next();
System.out.print( (i + 1)+" " +"java成绩:");
Stu[i].score = sc.nextDouble();
} System.out.println("\t姓名\t性别\t成绩");
for (StudentTest StudentTest : Stu) {
System.out.println("\t" + StudentTest.name+ "\t" + StudentTest.sex + "\t" + StudentTest.score);
} }
}

测试程序2:

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

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

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

 /**
* This program demonstrates static methods.
* @version 1.01 2004-02-19
* @author Cay Horstmann
*/
public class StaticTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3]; 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;
} 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
{
Employee e = new Employee("Harry", 50000);
System.out.println(e.getName() + " " + e.getSalary());
}
}

测试程序

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

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

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

测试程序

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

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

 import java.util.*;

 /**
* This program demonstrates object construction.
* @version 1.01 2004-02-19
* @author Cay Horstmann
*/
public class ConstructorTest
{
public static void main(String[] args)
{
// fill the staff array with three Employee objects
Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry", 40000);
staff[1] = new Employee(60000);
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
{
Random 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;
}
}

测试程序5:

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

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

4-6

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

4-7

 package com.horstmann.corejava;

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

实验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) 将两个对象的周长加总输出,将两个对象的面积加总输出。

 import java.util.*;

 public class Com {

     public static void main(String[] args) {

         Scanner in = new Scanner(System.in);
System.out.println("输入长:");
double length = in.nextDouble();
System.out.println("输入宽:");
double width = in.nextDouble();
System.out.println("输入半径:");
double radius = in.nextDouble();
Rectangle a=new Rectangle(length,width);
Circle b=new Circle(radius);
System.out.println("矩形周长:"+a.getPerimeter()+"矩形面积:"+a.getArea());
System.out.println("圆周长"+b.getPerimeter()+"圆面积:"+b.getArea());
double x = a.getPerimeter()+b.getPerimeter();
double y = a.getArea()+b.getArea();
System.out.println("周长和:"+x+"面积和:"+y);
} } class Rectangle {
private double width;
private double length;
public Rectangle(double w,double l)
{
width=w;
length=l;
}
public double getPerimeter()
{
double Perimeter = (width+length)*2;
return Perimeter;
}
public double getArea()
{
double Area = width*length;
return Area;
}
} class Circle { private double radius;
double PI = 3.14;
public Circle(double r)
{
radius=r;
}
public double getPerimeter()
{
double Perimeter = 2*PI*radius;
return Perimeter;
}
public double getArea()
{
double Area = PI*radius*radius;
return Area;
}
}

第三部分:实验总结

通过这个周的学习,学习了面向对象程序设计的一些基础知识,类、对象、对象的三个特征(行为、状态和标识),明确了创建类、构造和初始化对象的格式、以及熟悉预定义类API的使用。学学习了一些常用的类:Math类、String类、Scanner类、LocalDate类等。在课上课下的阅读程序中,以及编写程序时,有些疑惑,不清楚代码语句的作用和和如何去编写代码,看懂不等于会!

杨其菊201771010134《面向对象程序设计(java)》第四周学习总结的更多相关文章

  1. 201771010134杨其菊《面向对象程序设计java》第十周学习总结

    第8章泛型程序设计学习总结 第一部分:理论知识 主要内容:   什么是泛型程序设计                   泛型类的声明及实例化的方法               泛型方法的定义      ...

  2. 201771010134杨其菊《面向对象程序设计java》第九周学习总结

                                                                      第九周学习总结 第一部分:理论知识 异常.断言和调试.日志 1.捕获 ...

  3. 201771010134杨其菊《面向对象程序设计java》第八周学习总结

    第八周学习总结 第一部分:理论知识 一.接口.lambda和内部类:  Comparator与comparable接口: 1.comparable接口的方法是compareTo,只有一个参数:comp ...

  4. 201771010134杨其菊《面向对象程序设计java》第十二周学习总结

    第十二周学习总结 第一部分:理论知识 内容概要: AWT与Swing简介:框架的创建:图形程序设计: 显示图像: 1.AWT组件: 2.Swing 组件层次关系 3 .AWT与Swing的关系:大部分 ...

  5. 201771010134杨其菊《面向对象程序设计java》第七周学习总结

    第七周学习总结 第一部分:理论知识 1.继承是面向对象程序设计(Object Oriented Programming-OOP)中软件重用的关键技术.继承机制使用已经定义的类作为基础建立新的类定义,新 ...

  6. 201771010134杨其菊《面向对象程序设计(java)》第十六周学习总结

    第十六周学习总结 第一部分:理论知识 1. 程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程.操作系统为每个进程分配一段独立的内 ...

  7. 201771010134杨其菊《面向对象程序设计(java)》第十七周学习总结

    第十七周学习总结 1. 程序是一段静态的代码,它是应用程序执行的蓝本.进程是程序的一次动态执行,它对应了从代码加载.执行至执行完毕的一个完整过程.操作系统为每个进程分配一段独立的内存空间和系统资源,包 ...

  8. 201771010134杨其菊《面向对象程序设计(java)》第十五周学习

    第十五周学习总结 第一部分:理论知识 JAR文件: 应用程序首选项存储: Java Web Start JAR文件: 1.Java程序的打包:程序编译完成后,程序员将.class文件压缩打包为.jar ...

  9. 201771010134杨其菊《面向对象程序设计(java)》第十三周学习总结

    第十三周学习总结 第一部分:理论知识 第11章 事件处理(事件处理基础; 动作; 鼠标事件;AWT事件继承层次) 1. 事件源(event source):能够产生事件的对象都可 以成为事件源,如文本 ...

  10. 杨其菊201771010134《面向对象程序设计(java)》第六周学习总结

    <面向对象程序设计(java)>第六周学习总结 第一部分:理论知识 1)类.超类和子类2)Object:所有类的超类 3)泛型数组列表4)对象包装器和自动打包 5)参数数量可变的方法 6) ...

随机推荐

  1. 京东返利渠道,自己拿返利,无需A推B操作

    京东返利渠道,自己拿返利,无需A推B操作,简单快捷方便 1.在微信小程序中搜索 “京东饭粒” 2.进入京东饭粒,进购物车下单(只能在购物车内下单返利) 3.收货后26天返京豆到你的京东账号中,”佛系返 ...

  2. nginx的启动与停止

    参考 :http://www.cnblogs.com/codingcloud/p/5095066.html 启动: /usr/local/nginx/sbin/nginx -c /usr/local/ ...

  3. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

  4. .net mvc 分页

    1.分页实体类 public class PageDto { public int PageIndex { get; set; } public int PageSize { get; set; } ...

  5. python学习笔记之四-多进程&多线程&异步非阻塞

    ProcessPoolExecutor对multiprocessing进行了高级抽象,暴露出简单的统一接口. 异步非阻塞 爬虫 对于异步IO请求的本质则是[非阻塞Socket]+[IO多路复用]: & ...

  6. windows:plsql配置oracle连接

    1.plsql安装 此处省略,后续添加 2.plsql连接oracle: (1) 下载Instant client:http://www.oracle.com/technetwork/cn/topic ...

  7. leetcode每日刷题计划-简单篇day2

    今天数模比赛爆肝&操作系统大作业 脖子疼orz先把题过了保证flag不倒..个别细节回头看吧 Num 13 罗马数字转整数 Roman to Integer 一遍提交过,开始编译出了点问题 具 ...

  8. Centos nginx安装

    1.下载nginx http://nginx.org/en/download.html 2.上传到服务器上,并解压: rz 后选择上传的文件 tar -zxvf /fish/download/ngin ...

  9. 同步对象(同步条件Event)

    event = threading.Event()   #创建同步对象 event.wait()     #同步对象等待状态 event.set() #同步对象设置Trueevent.clear()  ...

  10. threading join用法

    join():在子线程完成运行之前,这个子线程的父线程将一直被阻塞 import threading #线程import time def Beijing(n): print('Beijing tim ...