java程序設計mooc攻略
單元測驗 | 互評作業 | 考試 |
---|---|---|
week1測驗 | 2019冬期中測驗 | |
week2測驗 | week2互評作業 | 2019冬期末測驗 |
week3測驗 | week3互評作業 | |
week4測驗 | week4互評作業 | |
week5測驗 | week5互評作業 | |
week6測驗 | week6互評作業 | |
week7測驗 | week7互評作業 | |
week8測驗 | week8互評作業 | |
week9測驗 | week9互評作業 | |
week10測驗 | week10互評作業 | |
week11測驗 | 吃不消,我自己是github上找了個項目上去 |
編寫一個小的程序,其中定義一些接口、類、抽象類,定義它們的成員(字段及方法), 要求使用使用setter/getter, static, final, abstract,@Override等語法要素,並寫一個main函數來使用它們。這些類、接口可以是圍繞以下選題之一
飛翔世界:來一次飛翔接力(即多個可飛翔的對象依次調用);
動物世界:來一次吃西瓜大賽;
圖書館:模擬一天的借閱過程;
學校:模擬選課過程;
等等
要求寫個簡要說明。
文件結構
.
├── Game.java
├── animals
│ ├── Animal.java
│ ├── Elephant.java
│ ├── Lion.java
│ ├── Monkey.java
│ ├── Pig.java
│ └── Rabbit.java
└── prop
├── Plate.java
├── Record.java
└── Watermelon.java
每個文件的代碼
Game.java
import animals.*;
import prop.Plate;
import prop.Record;
import prop.Watermelon;
import java.util.Arrays;
import java.util.Random;
import java.util.Vector;
public class Game {
public static void main(String[] args) {
System.out.println("觀衆朋友們,大家好,一年一度的吃西瓜大賽又開始了。");
System.out.println("讓我們歡迎我們今年的選手:");
System.out.println("");
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
Vector<Animal> players = new Vector<Animal>();
players.add(new Rabbit(10));
players.add(new Pig(30));
players.add(new Elephant(28));
players.add(new Monkey(18));
players.add(new Lion(24));
for (Animal each : players) {
System.out.println(each.getName() + ": " + each.say());
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
Plate<Watermelon> p;
Record[] record = new Record[5];
int index = 0;
System.out.println("");
for (Animal each : players) {
System.out.println("好的,接下來有請" + each.getName() + "開始吃西瓜,計時開始");
p = new Plate<>(10);
Random seed = new Random(1);
for (int i = 0; i < 10; i++) {
p.add(new Watermelon(seed.nextInt(20)));
}
for (int i = 0; i < 10; i++) {
System.out.print(".");
System.out.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
}
record[index] = new Record(each.getName(), each.eatWatermelon(p));
System.out.println("");
System.out.printf("總共花費了%.2fs的時間。非常不錯的成績\n", record[index].getTime());
System.out.println("");
index++;
}
Arrays.sort(record);
System.out.println("接下來公佈成績:");
try {
Thread.sleep(3000);
} catch (InterruptedException e) { }
for (int i = 0; i < 5; i++) {
System.out.print("第" + (i + 1) + "名是");
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
System.out.println(record[i].getName());
}
}
}
prop/Plate.java
package prop;
import java.util.concurrent.ArrayBlockingQueue;
public class Plate<T> {
private ArrayBlockingQueue<T> container;
public Plate(int limits) {
this.container = new ArrayBlockingQueue<T>(limits);
}
public boolean add(T one) {
return container.add(one);
}
public T reduce() {
return container.poll();
}
public boolean isEmpty() {
return container.isEmpty();
}
public T getOne() {
return container.peek();
}
}
prop/Record.java
package prop;
public class Record implements Comparable<Record>{
public Record(String name, double time) {
this.name = name;
this.time = time;
}
public String getName() {
return name;
}
public double getTime() {
return time;
}
@Override
public int compareTo(Record o) {
return Double.compare(this.time,o.time);
}
private String name;
private Double time;
}
prop/Watermelon.java
package prop;
public class Watermelon {
private final int size;
public int getSize() {
return size;
}
public Watermelon(int size) {
this.size = size;
}
}
aninals/Animals.java
package animals;
import prop.Plate;
import prop.Watermelon;
public abstract class Animal {
abstract public String say();
abstract public int getEatSpeed();
public double eatWatermelon(Plate<Watermelon> plate) {
int sum = 0;
while (!plate.isEmpty()) {
Watermelon w = plate.reduce();
sum += w.getSize();
}
return sum*1.0/getEatSpeed();
}
abstract public String getName();
}
animals/Elephant.java
package animals;
public class Elephant extends Animal {
public Elephant(int eatSpeed) {
name = "大象";
this.eatSpeed = eatSpeed;
}
@Override
public String getName() {
return name;
}
@Override
public int getEatSpeed() {
return eatSpeed;
}
@Override
public String say() {
return "我的鼻子可以捲起許多西瓜。";
}
final private String name;
private int eatSpeed;
}
aninals/Lion.java
package animals;
public class Lion extends Animal {
public Lion(int eatSpeed) {
name = "獅子";
this.eatSpeed = eatSpeed;
}
@Override
public String getName() {
return name;
}
@Override
public int getEatSpeed() {
return eatSpeed;
}
@Override
public String say() {
return "我是森林之王。";
}
final private String name;
private int eatSpeed;
}
aninals/Monkey.java
package animals;
public class Monkey extends Animal {
public Monkey(int eatSpeed) {
name = "猴子";
this.eatSpeed = eatSpeed;
}
@Override
public String getName() {
return name;
}
@Override
public int getEatSpeed() {
return eatSpeed;
}
@Override
public String say() {
return "我要讓你看看我吃的有多快。";
}
final private String name;
private int eatSpeed;
}
aninals/Pig.java
package animals;
public class Pig extends Animal {
public Pig(int eatSpeed) {
name = "豬";
this.eatSpeed = eatSpeed;
}
@Override
public String getName() {
return name;
}
@Override
public int getEatSpeed() {
return eatSpeed;
}
@Override
public String say() {
return "我有一個超級大的胃。";
}
final private String name;
private int eatSpeed;
}
aninals/Rabbit.java
package animals;
public class Rabbit extends Animal {
public Rabbit(int eatSpeed) {
name = "兔子";
this.eatSpeed = eatSpeed;
}
public String getName() {
return name;
}
@Override
public int getEatSpeed() {
return eatSpeed;
}
@Override
public String say() {
return "我喜歡胡蘿蔔。";
}
final private String name;
private int eatSpeed;
}
程序啓動
運行Game.java的main函數。