Java第五次作业
(一)学习总结
1.在上周完成的思维导图基础上,补充本周的学习内容,对Java面向对象编程的知识点做一个全面的总结。
2.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路并画出类图。
(1)定义一个抽象类:有编号、名称、租金三个属性。
(2)定义两个接口:载客量接口和载货量接口。
(3)三个类:客车类、货车类、皮卡类。客车类继承抽象类和载客量接口;货车类继承抽象类和载货量接口;皮卡车类继承抽象类、载客量接口和在货量的接口。
3.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果
`interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I'm breathing");
}
void eat(){
System.out.println("I'm eating");
}
}
public class Test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
不能编译通过,因为Dog继承了Animal的接口,Animal接口中有三个方法,Dog继承它,要对这三个方法都进行覆写;Dog中的eat()方法不能降低原来的权限。
改:
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I'm breathing");
}
public void eat(){
System.out.println("I'm eating");
}
public void run(){
System.out.println("I'm running");
}
}
public class Test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
4.运行下面的程序
import java.util.Arrays;
public class Test{
public static void main(String[] args){
String[] fruits = {"peach","banana","orange","apple"};
Arrays.sort(fruits);
for(int i = 0;i < fruits.length;i++)
{
System.out.println(fruits[i]);
}
}
}
程序输出的结果是升序排序的:
查看String 类的源码,说明是如何实现的:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
/**
* Class String is special cased within the Serialization Stream Protocol.
*
* A String instance is written into an ObjectOutputStream according to
* <a href="{@docRoot}/../platform/serialization/spec/output.html">
* Object Serialization Specification, Section 6.2, "Stream Elements"</a>
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
/**
* Initializes a newly created {@code String} object so that it represents
* an empty character sequence. Note that use of this constructor is
* unnecessary since Strings are immutable.
*/
public String() {
this.value = new char[0];
}
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
/**
* Allocates a new {@code String} so that it represents the sequence of
* characters currently contained in the character array argument. The
* contents of the character array are copied; subsequent modification of
* the character array does not affect the newly created string.
*
* @param value
* The initial value of the string
*/
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
/**
* Allocates a new {@code String} that contains characters from a subarray
* of the character array argument. The {@code offset} argument is the
* index of the first character of the subarray and the {@code count}
* argument specifies the length of the subarray. The contents of the
* subarray are copied; subsequent modification of the character array does
* not affect the newly created string.
*
* @param value
* Array that is the source of characters
*
* @param offset
* The initial offset
*
* @param count
* The length
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and {@code count} arguments index
* characters outside the bounds of the {@code value} array
*/
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
/**
* Allocates a new {@code String} that contains characters from a subarray
* of the <a href="Character.html#unicode">Unicode code point</a> array
* argument. The {@code offset} argument is the index of the first code
* point of the subarray and the {@code count} argument specifies the
* length of the subarray. The contents of the subarray are converted to
* {@code char}s; subsequent modification of the {@code int} array does not
* affect the newly created string.
*
* @param codePoints
* Array that is the source of Unicode code points
*
* @param offset
* The initial offset
*
* @param count
* The length
*
* @throws IllegalArgumentException
* If any invalid Unicode code point is found in {@code
* codePoints}
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and {@code count} arguments index
* characters outside the bounds of the {@code codePoints} array
*
* @since 1.5
*/
public String(int[] codePoints, int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
// Note: offset or count might be near -1>>>1.
if (offset > codePoints.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
final int end = offset + count;
// Pass 1: Compute precise size of char[]
int n = count;
for (int i = offset; i < end; i++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
continue;
else if (Character.isValidCodePoint(c))
n++;
else throw new IllegalArgumentException(Integer.toString(c));
}
// Pass 2: Allocate and fill in char[]
final char[] v = new char[n];
for (int i = offset, j = 0; i < end; i++, j++) {
int c = codePoints[i];
if (Character.isBmpCodePoint(c))
v[j] = (char)c;
else
Character.toSurrogates(c, v, j++);
}
this.value = v;
}
如果现在希望对输出的结果进行降序排序,该如何处理?修改上述代码,实现按照字母顺序逆序排序。
import java.util.Arrays;
public class Test{
public static void main(String[] args){
String[] fruits = {"peach","banana","orange","apple"};
Arrays.sort(fruits);
for(int i = fruits.length-1;i>=0;i++)
{
System.out.println(fruits[i]);
}
}
}
5其他总结:
实验中使用工厂的设计方法,工厂设计方法灵活性比较高子类扩充可以直接修改工厂类。
Data类的应用。
(二)实验总结
实验内容:
1.某工厂生产各种音乐盒,客户无需知道音乐盒的制作过程,只需知道如何播放音乐盒即可。用简单工厂设计模式实现该过程:接口MusicBox具有方法play(),两个音乐盒类PianoBox,ViolinBox,MusicBoxFactory 产生MusicBox的实例。
- 程序设计思路:程序中有一个MusicBox的接口,PianoBox,ViolinBox中实现接口,重写MusicBox的play()方法;在MusicBoxFactory的类中实现对象实例和子类的扩充。
- 使用工厂的设计方法灵活性比较高,如果有子类扩充的话,直接修改工厂类就可以了;灵活性比较高。
2.修改第三次作业的第一题,使用java.util.Date类表示职工的生日和参加工作时间,并将职工信息按照生日大小排序后输出。(分别用comparable和comparator实现)
程序设计思路:在员工类中定义生日和参加工作时间直接使用java.until.Data类中的构造方法就可以了。定义一个员工类,里面有员工的属性,使用头String的方法里面使用匿名对象输出生日和参加工作时间,在测试类中赋值是给一个实例化模板;使用comparator的比较方法是需要一个comparaTo()方法。接收两个对象比较,通过返回值判断大小。使用comparable比较,需要使员工类实现Comparable接口。 - 问题1: 先把字符串类的生日信息转化成Data类的生日输出,在比较排序时,还需要在转化回来。
- 解决方案:直接在toString方法中直接使用匿名对象输出,在测试类中赋值是给一个实例化模板。
3.在案例宠物商店的基础上,实现以下功能:
(1)展示所有宠物
(2)购买宠物 - 程序设计思路:定义一个Pet类的接口,里面有编号、颜色、种类等属性,Cat()、Dog()类继承Pet()接口,但是Dog还有一个大小型的属性,需要在Dog类里面添加一个类型的属性。还需要一个宠物商店的工厂,在里面对宠物对象实例。在工厂里需要判断是否满足宠物信息,如果满足存入数组。在测试类中定义数组长度,如果超过长度添加不进去。
- 问题2:输出的信息在测试类里写的输出方法,但是宠物猫和宠物狗的属性不一样,输出的信息不全
- 解决方案:可以在宠物猫和宠物狗中分别写头toString()方法,在测试类中调用。
- 问题3:输出宠物的时候,宠物的数组是空
解决方案:在满足在条件后没有存进新的数组里。
代码托管(https://git.oschina.net/hebau_cs15/Java-CS01yxr.git)
(四)学习进度条
代码行数(新增/累积) 学习时间(新增/累积) 本周学习内容 目标 5000行 300小时 第2-4周 150/300 30/30 学习了.... 第5周 220/300 30/50 学习了关于double类型存在精度问题,取指定位置和截取字符串;数组的应用 第6周 550/600 60/80 第8周 700/800 60/80 面向对象的继承和多态 第9周 800/800 80/100 工厂设计和Java常用的类