forked from Paraoia/java-2017f-homework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
624 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class BubbleSort implements Sort { | ||
|
||
@Override | ||
public void sort(Queue queue) { | ||
Creature creature; | ||
Position[] positions = queue.getPositions(); | ||
|
||
for (int i = 0; i < positions.length - 1; i++) { | ||
for (int j = 0; j < positions.length - 1 - i; j++) { | ||
if (((Comparable) (positions[j].getHolder())).biggerThan((Comparable) (positions[j + 1].getHolder()))) { | ||
creature = positions[j].getHolder(); | ||
positions[j + 1].getHolder().setPosition(positions[j]); | ||
creature.setPosition(positions[j + 1]); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
public interface Comparable { | ||
|
||
public boolean biggerThan(Comparable another); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public interface Creature { | ||
|
||
public void report(); | ||
|
||
public void reportName(); | ||
|
||
|
||
public void setPosition(Position position); | ||
|
||
|
||
public Position getPosition(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import java.util.ArrayList; | ||
|
||
public interface Formation { | ||
public void excuete(ArrayList<? extends Creature> creatures, Creature c); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
public class Grandpa implements Creature{ | ||
private String name; | ||
private Position position; | ||
|
||
Grandpa(){ | ||
this.name = "爷爷"; | ||
this.position = null; | ||
} | ||
|
||
|
||
Grandpa(int x, int y){ | ||
this.name = "爷爷"; | ||
this.position = new Position(x, y); | ||
} | ||
|
||
@Override | ||
public Position getPosition() { | ||
return position; | ||
} | ||
|
||
|
||
@Override | ||
public void setPosition(Position position) { | ||
this.position = position; | ||
position.setHolder(this); | ||
} | ||
|
||
|
||
@Override | ||
public void report() { | ||
System.out.println(this.toString()); | ||
} | ||
|
||
|
||
@Override | ||
public void reportName(){ | ||
System.out.print(name); | ||
} | ||
|
||
|
||
@Override | ||
public String toString(){ | ||
return "爷爷" + "at[" + this.position.getX() + "][" + this.position.getY() + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import javafx.geometry.Pos; | ||
|
||
import javax.management.ListenerNotFoundException; | ||
import java.util.ArrayList; | ||
|
||
public class HEYI implements Formation { | ||
private ArrayList<? extends Creature> creatures; | ||
|
||
@Override | ||
public void excuete(ArrayList<? extends Creature> creatures, Creature c){ | ||
int num = 7; | ||
try{ | ||
this.creatures = creatures; | ||
}catch(NullPointerException e) { | ||
System.out.println("ArrayList 空指针异常"); | ||
e.printStackTrace(); | ||
} | ||
Position p = new Position(num/2 + 5, 15); | ||
this.creatures.get(0).setPosition(p); | ||
int j = 1; | ||
for(int i = 1; i < num; i++){ | ||
if(i % 2 == 1){ | ||
p = new Position(num/2 - j + 5, j + 15); | ||
this.creatures.get(i).setPosition(p); | ||
} | ||
else{ | ||
p = new Position(num/2 + j + 5, j + 15); | ||
this.creatures.get(i).setPosition(p); | ||
j++; | ||
} | ||
} | ||
try { | ||
p = new Position(num / 2 + 5, 18); | ||
c.setPosition(p); | ||
}catch(NullPointerException e) { | ||
System.out.println("Creature 空指针异常"); | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
enum Color { | ||
红, 橙, 黄, 绿, 青, 蓝, 紫 | ||
} | ||
|
||
enum Rank { | ||
老大, 老二, 老三, 老四, 老五, 老六, 老七 | ||
} | ||
|
||
public class Hulu implements Creature, Comparable { | ||
private Rank name; | ||
private Color color; | ||
private Position position; | ||
|
||
|
||
public Hulu(Color color, Rank name) { | ||
this.name = name; | ||
this.color = color; | ||
} | ||
|
||
public Rank getName(){ | ||
return name; | ||
} | ||
|
||
public Color getColor() { | ||
return color; | ||
} | ||
|
||
public void print_name() { | ||
System.out.println(name); | ||
} | ||
|
||
public void print_color() { | ||
System.out.println(color); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public Position getPosition() { | ||
return position; | ||
} | ||
|
||
@Override | ||
public void setPosition(Position position) { | ||
this.position = position; | ||
position.setHolder(this); | ||
} | ||
|
||
@Override | ||
public boolean biggerThan(Comparable brother){ | ||
|
||
if (brother instanceof Hulu) | ||
return this.getName().ordinal()> ((Hulu) brother).getName().ordinal(); | ||
else | ||
return false; | ||
} | ||
|
||
@Override | ||
public void report() { | ||
System.out.println(this.toString()); | ||
} | ||
|
||
|
||
@Override | ||
public void reportName(){ | ||
System.out.print(name); | ||
} | ||
|
||
|
||
@Override | ||
public String toString(){ | ||
return this.name.toString() + "(" + this.color.toString() + ")at[" + this.position.getX() + "][" + this.position.getY() + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class InsertionSort implements Sort { | ||
|
||
@Override | ||
public void sort(Queue queue) { | ||
Position[] positions = queue.getPositions(); | ||
Creature creature = null; | ||
int j; | ||
for (int i = 1; i < positions.length; i++) { | ||
for (j = i; j > 0; j--) { | ||
if (!((Comparable) (positions[j].getHolder())).biggerThan((Comparable) (positions[j - 1].getHolder()))) { | ||
creature = positions[j].getHolder(); | ||
positions[j - 1].getHolder().setPosition(positions[j]); | ||
creature.setPosition(positions[j - 1]); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
public class Minion implements Creature { | ||
private String name; | ||
private Position position; | ||
|
||
Minion(int index){ | ||
String s = String.valueOf(index); | ||
this.name = "喽啰" + s; | ||
} | ||
|
||
@Override | ||
public Position getPosition() { | ||
return position; | ||
} | ||
|
||
@Override | ||
public void setPosition(Position position) { | ||
this.position = position; | ||
position.setHolder(this); | ||
} | ||
|
||
|
||
@Override | ||
public void report() { | ||
System.out.println(this.toString()); | ||
} | ||
|
||
@Override | ||
public void reportName(){ | ||
System.out.print(name); | ||
} | ||
|
||
|
||
@Override | ||
public String toString(){ | ||
return this.name + "at[" + this.position.getX() + "][" + this.position.getY() + "]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
public class Position { | ||
|
||
private int x; | ||
private int y; | ||
|
||
private Creature holder; | ||
|
||
public Creature getHolder() { | ||
return holder; | ||
} | ||
|
||
public void setHolder(Creature holder) { | ||
this.holder = holder; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public int getY(){ | ||
return y; | ||
} | ||
|
||
public void setX(int x) { | ||
this.x = x; | ||
} | ||
|
||
public void setY(int y){ | ||
this.y = y; | ||
} | ||
|
||
public Position(int x, int y){ | ||
super(); | ||
this.x = x; | ||
this.y = y; | ||
this.holder = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import java.util.Random; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Queue { | ||
final int N = 20; | ||
|
||
private Position[] positions; | ||
private ArrayList<? extends Creature> creatures; | ||
|
||
public Position[] getPositions() { | ||
return positions; | ||
} | ||
|
||
public ArrayList<? extends Creature> getCreatures() { | ||
return creatures; | ||
} | ||
|
||
public Queue(ArrayList<? extends Creature> list){ | ||
this.creatures = list; | ||
this.positions = new Position[creatures.size()]; | ||
for(int i =0; i < creatures.size(); i++){ | ||
this.positions[i] = new Position(i + 5, 5); | ||
this.creatures.get(i).setPosition(this.positions[i]); | ||
} | ||
new BubbleSort().sort(this); | ||
} | ||
|
||
public void rollCall(){ | ||
System.out.println("葫芦娃报告位置:"); | ||
for(Creature creature: this.creatures){ | ||
creature.report(); | ||
} | ||
System.out.println("\n"); | ||
System.out.flush(); | ||
|
||
} | ||
|
||
public void shuffle() { | ||
Random rnd = ThreadLocalRandom.current(); | ||
for (int i = creatures.size() - 1; i > 0; i--) { | ||
int index = rnd.nextInt(i + 1); | ||
Position position = creatures.get(index).getPosition(); | ||
creatures.get(index).setPosition(creatures.get(i).getPosition()); | ||
creatures.get(i).setPosition(position); | ||
} | ||
} | ||
|
||
/* | ||
public static void main(String[] args){ | ||
Hulu[] brothers = new Hulu[7]; | ||
for(int i = 0; i< brothers.length; i++){ | ||
brothers[i] = new Hulu(Color.values()[i], Rank.values()[i]); | ||
} | ||
Queue queue = new Queue(brothers); | ||
queue.rollCall(); | ||
queue.shuffle(); | ||
queue.rollCall(); | ||
System.out.println("InsertionSort"); | ||
new InsertionSort().sort(queue); | ||
queue.rollCall(); | ||
queue.shuffle(); | ||
queue.rollCall(); | ||
System.out.println("BubbleSort"); | ||
new BubbleSort().sort(queue); | ||
queue.rollCall(); | ||
} | ||
*/ | ||
} |
Oops, something went wrong.