Question 1: Basic Programming Problem (Class and Static Method)
public class Q1 {
public static String diagonals(int height, int gap, char c1, char c2) throws IllegalArgumentException
{
if(c1 == c2)
{
throw new IllegalArgumentException();
}
if(height > 31 || height < 7)
{
throw new IllegalArgumentException();
}
if(gap < 1)
{
throw new IllegalArgumentException();
}
StringBuilder s = new StringBuilder(new String());
for (int i = 0; i < height; i++) {
s.append(c1);
for (int j = 0; j < gap; j++) {
s.append(c2);
}
i = i + gap;
}
StringBuilder ss = new StringBuilder(new String());
for(int i = 0; i < height; i++)
{
ss.append( s.subSequence(i, s.length()-1) );
ss.append( s.subSequence(0, i));
ss.append("\n");
}
return ss.toString();
}
}
截图:
Question 2: Modelling and Processing Files and Objects
FootballGame.java
public class FootballGame implements Game{
private int scoreA;
private int scoreB;
Team teamA;
Team teamB;
public FootballGame(Team teamA, int scoreA, Team teamB, int scoreB)
{
this.scoreA = scoreA;
this.scoreB = scoreB;
this.teamA = teamA;
this.teamB = teamB;
}
@Override
public Result getResult(Team a) throws IllegalArgumentException{
if(a.getName() == teamA.getName())
{
if(scoreA > scoreB){
return Result.WIN;
} else if (scoreA < scoreB) {
return Result.LOSS;
} else {
return Result.DRAW;
}
}else if(a.getName() == teamB.getName()){
if(scoreA > scoreB){
return Result.LOSS;
} else if (scoreA < scoreB) {
return Result.WIN;
} else {
return Result.DRAW;
}
}else{
throw new IllegalArgumentException();
}
}
@Override
public int getGoalsFor(Team a) throws IllegalArgumentException{
if(a.getName() == teamA.getName()) {
return scoreA;
} else if (a.getName() == teamB.getName()) {
return scoreB;
}else {
throw new IllegalArgumentException();
}
}
@Override
public int getGoalsAgainst(Team a) throws IllegalArgumentException{
if(a.getName() == teamA.getName()) {
return scoreB;
} else if (a.getName() == teamB.getName()) {
return scoreA;
}else {
throw new IllegalArgumentException();
}
}
}
FootballTeam.java
public class FootballTeam implements Team{
private String name;
private int wins;
private int losses;
private int draws;
private int goals;
private int goalsAgainst;
FootballTeam(String name) {
this.name = name;
this.goalsAgainst = 0;
this.goals = 0;
}
@Override
public String getName() {
return this.name;
}
@Override
public void addGame(Game game) {
this.goalsAgainst += game.getGoalsAgainst(this);
this.goals += game.getGoalsFor(this);
Result r = game.getResult(this);
if(r == Result.WIN){
wins += 1;
}else if(r == Result.LOSS){
losses += 1;
}else{
draws +=1;
}
}
@Override
public int getWins() {
return wins;
}
@Override
public int getLosses() {
return losses;
}
@Override
public int getDraws() {
return draws;
}
@Override
public int getPoints() {
return wins * 3 + draws;
}
@Override
public int getGoalsFor() {
return goals;
}
@Override
public int getGoalsAgainst() {
return goalsAgainst;
}
}
WorldCupGroup.java
import java.util.ArrayList;
import java.util.Collections;
public class WorldCupGroup implements Group{
String groupName;
FootballTeam a;
FootballTeam b;
FootballTeam c;
FootballTeam d;
ArrayList<FootballTeam> s;
public WorldCupGroup(String groupName, Team a, Team b, Team c, Team d)
{
this.groupName = groupName;
this.a = (FootballTeam) a;
this.b = (FootballTeam) b;
this.c = (FootballTeam) c;
this.d = (FootballTeam) d;
}
@Override
public String getName() {
return groupName;
}
@Override
public String getTable() {
s = new ArrayList<>();
s.add(a);
s.add(b);
s.add(c);
s.add(d);
for (int i = 0; i < s.size(); i++) {
for (int j = i+1; j < s.size(); j++) {
if(s.get(i).getPoints() < s.get(j).getPoints())
{
Collections.swap(s, i, j);
}
if(s.get(i).getPoints() == s.get(j).getPoints())
{
if((s.get(i).getGoalsFor()-s.get(i).getGoalsAgainst()) < (s.get(j).getGoalsFor()-s.get(j).getGoalsAgainst()))
{
Collections.swap(s, i, j);
}
}
}
}
StringBuffer str = new StringBuffer();
str.append("Group "+ this.groupName +":\n");
str.append("Team W D L F A P \n");
for (int i = 0; i < s.size(); i++) {
String a = s.get(i).getName();
int b = a.length();
str.append(s.get(i).getName());
for (int j = 0; j < 23-b; j++) {
str.append(" ");
}
str.append(s.get(i).getWins() +" ");
str.append(s.get(i).getDraws() +" ");
str.append(s.get(i).getLosses() +" ");
str.append(s.get(i).getGoalsFor() +" ");
str.append(s.get(i).getGoalsAgainst() +" ");
str.append(s.get(i).getPoints() +"\n");
}
this.s = s;
return str.toString();
}
@Override
public Team getWinner() {
this.getTable();
return this.s.get(0);
}
@Override
public Team getRunnerUp() {
this.getTable();
return this.s.get(1);
}
}
测试全通过