CompSci 251: Assignment 6- JAVA Due 3/27, 2017 10:00am Topics Covered: Classes w
ID: 3862017 • Letter: C
Question
CompSci 251: Assignment 6- JAVA
Due 3/27, 2017 10:00am
Topics Covered: Classes with arrays, multiple classes
1 Introduction
So, it’s already 2018 and time for the Winter Olympics. You will write a program to keep track of standings in a new kind of speed skating competition. Skaters will race twice and they will be compared by the lower of the two times they get. Skaters may get disqualified during the race process.
2 Program tasks
You will write a program that keeps track of the race results and can print reports of the standings at any time(you can use the toString method in SkateRace to return a string that represents the current stand- ing). UML diagrams for your classes appear below. Notice that the SkateRace class holds an Array of Skaters.
Your program should ask the user to enter how many skators he wants to enter. The program then will have three phases:
The first phase corresponds to the first half of the race. Your program will read in the name, State, and first race time for a series of skaters.
After each entry, your program will print the current leader(the skater who has the lowest time in the array so far).
The phase will end once the user enters all the skaters in to the array. The complete standings will be printed along with the leader who has the lowest race time(assume each racer has a unique race time)
The second phase corresponds to the second half of the race. The program will read in the name and second race time of the skaters and print the leader after each update.
• The phase will end once the user done updating and the complete standings will be printed.
In the third phase, your program will read in the names of any skaters that have been disqualified and
remove them from the race.
Your program will also print the overall standings at the end of the race.
1
Here are the UML diagrams for the classes we expect you to use. This program structure is pretty common and standard. You have a simple data element class (Skater), a larger class that holds the data elements (SkateRace), and a driver class (RaceDriver) that makes the classes do things.
Skater
-name: String //only one word, no spaces -state: String //only one word, no spaces -time: double
+ public Skater(name: String, state: String ,time: double ) + getName(): String
+ getTime(): double
+ getState(): String
+ setTime(time: double): void + toString(): String
SkateRace
skaters: Skater[ ]
size: int// companion variable to keep track of current size of skaters array
+ SkateRace(numberOfSkaters: int)//constructor
+ getSize():int//accessor for size
+ getLength():int//returns the length of the array of Skaters + addSkater(skater: Skater) : void
+ getSkater(name: String): Skater
+ getSkater(position: int) : Skater
+ updateSkater(name: String, secondTime: double): boolean + removeSkater(name: String) : void
+ toString(): String
RaceDriver
+main(String[] args) : static void
-phase1(stdIn: Scanner, sRace: SkateRace): void -phase2(stdIn: Scanner, sRace: SkateRace): void -phase3(stdIn: Scanner, sRace: SkateRace): void #findLeader(sRace: SkateRace ): Skater
You can assume that the two Strings in the Skater class will only hold single words. This simplifies reading the input from the user, because you can use the Scanner.next() method to read the Strings (instead of Scanner.nextLine())
The addSkater method adds the skater to the array and increments the size, you should check if there is a free space in the array before adding, if not do nothing.
The SkateRace class has two overloaded getSkater methods, getSkater(String name) and getSkater(int position). getSkater(String name) returns the skater whose name matches the argument(assuming unique name for each skater). If the name does not exist, the getSkater(String name) method should return null.
For getSkater(int position), the method should check first if the position is a valid index within the array(position>=0 and position<size). If the position is valid, returns the skater at that position otherwise returns null.
Your driver code that uses getSkater, needs to check for null return values or your program could crash.
2
3
•
•
•
• •
The updateSkater method returns a boolean. It should find the Skater with the given name and, if the new race time is lower, it should change that Skater’s time. It should return true when the Skater is found and updated. It should return false either when the name given doesn’t match any Skaters or the new race time is larger than the current skater’s race time .
The removeSkater method works differently from updateSkater and doesn’t return anything. If the name isn’t found, it does nothing. The driver will have to use the getSkater method to check whether a Skater is already in the race, when checking names to be disqualified.
Your driver class should have four helper methods: one for each phase and findLeader. This design should simplify your main method(we asked you to make the access modifier for findLeader to be protected just for testing purposes).
The findLeader method returns the skater who has the lowest time among the other skaters in the array argument (assume each racer has a unique race time).
The toString method in SkateRace class should return a string for the content of the array(you can rely on the toString for each object inside the array), make sure to handle partially filled array.
Sample Output
The comments below are not part of the output, I just put them for your understanding
3
Goodbye!
Skater
-name: String //only one word, no spaces -state: String //only one word, no spaces -time: double
+ public Skater(name: String, state: String ,time: double ) + getName(): String
+ getTime(): double
+ getState(): String
+ setTime(time: double): void + toString(): String
Explanation / Answer
public class Skater {
private String name;
private String state;
private double time;
public Skater(String name, String state, double time) {
super();
this.name = name;
this.state = state;
this.time = time;
}
public String getName() {
return name;
}
public String getState() {
return state;
}
public double getTime() {
return time;
}
public void setTime(double time) {
this.time = time;
}
@Override
public String toString() {
return name + " " + state + " " + time;
}
}
public class SkateRace {
private Skater skaters[];
private int size = 0;
public SkateRace(int numberOfSkaters) {
skaters = new Skater[numberOfSkaters];
}
public int getSize() {
return this.size;
}
public int getLength() {
return skaters.length;
}
public void addSkater(Skater skater) {
skaters[size++] = skater;
sortSkaters();
}
public Skater getSkater(String name) {
for (int i=0; i<size; i++) {
if (skaters[i].getName().equals(name)) {
return skaters[i];
}
}
return null;
}
/**
* Since the array is sorted at any point of time,
* we are just simply at the given index
*
* @param position
* @return
*/
public Skater getSkater(int position) {
if (position>=0 && position < size) {
return skaters[position];
}
return null;
}
public boolean updateSkater(String name, double secondTime) {
for (int i=0; i<size; i++) {
if (skaters[i].getName().equals(name) && skaters[i].getTime() > secondTime) {
skaters[i].setTime(secondTime);
sortSkaters();
return true;
}
}
return false;
}
public void removeSkater(String name) {
for (int i=0; i<size; i++) {
if (skaters[i].getName().equals(name)) {
leftShift(i);
break;
}
}
}
/**
* This method left shift the array at the given index
* by one step
*
* @param index
*/
private void leftShift(int index) {
for (int i=index; i<=size-2; i++) {
skaters[i] = skaters[i+1];
}
skaters[size-1] = null;
size--;
}
/**
* Sorting method to the sort the array of skaters
* based on their race time
*/
private void sortSkaters() {
for (int i=0; i<size-1; i++) {
int minIndex = i;
for (int j = i+1; j < size; j++) {
if (skaters[j].getTime() < skaters[minIndex].getTime()) {
minIndex = j;
}
}
Skater temp = skaters[minIndex];
skaters[minIndex] = skaters[i];
skaters[i] = temp;
}
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer();
for (int i=0; i<size; i++) {
buffer.append((i+1) + ". "+skaters[i].getName()+ " " + skaters[i].getState() + " " + skaters[i].getTime() + " ");
}
return buffer.toString();
}
}
import java.util.Scanner;
public class RaceDriver {
public void phaseOne(Scanner stdIn, SkateRace sRace) {
System.out.println(" Starting First Phase ");
System.out.println("Enter Skater’s data as : Name State Time ");
for (int i=0; i<sRace.getLength(); i++) {
System.out.print("Enter Skater data : ");
String inputLine = stdIn.nextLine();
String[] inputData = inputLine.trim().split(" ");
Skater skater = new Skater(inputData[0], inputData[1], Double.parseDouble(inputData[2]));
sRace.addSkater(skater);
System.out.println("Current Leader : "+findLeader(sRace)+" ");
}
System.out.println(" Current standings : ");
System.out.println(sRace.toString());
}
public void phaseTwo(Scanner stdIn, SkateRace sRace) {
System.out.println(" Starting Second Phase ");
while (true) {
System.out.print("Enter the name and updated time of the skater you want to update or 'quit' : ");
String inputLine = stdIn.nextLine().trim();
String[] inputData = inputLine.split(" ");
if (inputLine.equalsIgnoreCase("quit")) {
break;
}
if (! sRace.updateSkater(inputData[0], Double.parseDouble(inputData[1]))) {
System.out.println("Skater "+inputData[0] + " not update ");
continue;
}
System.out.println("Skater "+inputData[0] + " is update and the new data is ["+sRace.getSkater(inputData[0]).toString()+"] ");
System.out.println("Current Leader : "+findLeader(sRace));
}
System.out.println(" Current standings : ");
System.out.println(sRace.toString());
}
public void phaseThree(Scanner stdIn, SkateRace sRace) {
System.out.println(" Starting Third Phase ");
while (true) {
System.out.print("Enter the name of the skater you want to disqualify or 'quit' :");
String inputLine = stdIn.nextLine();
if (inputLine.equalsIgnoreCase("quit")) {
break;
}
sRace.removeSkater(inputLine);
System.out.println("Current Leader : "+findLeader(sRace));
}
System.out.println(" Final standings : ");
System.out.println(sRace.toString());
}
public Skater findLeader(SkateRace srace) {
return srace.getSkater(0);
}
public static void main(String args[]) {
System.out.print("Please enter how many Skater you want to enter : ");
Scanner scanner = new Scanner(System.in);
int numOfPlayers = scanner.nextInt();
SkateRace sRace = new SkateRace(numOfPlayers);
RaceDriver raceDriver = new RaceDriver();
System.out.println(" Welcome to Skate Race Program ");
raceDriver.phaseOne(new Scanner(System.in), sRace);
raceDriver.phaseTwo(new Scanner(System.in), sRace);
raceDriver.phaseThree(new Scanner(System.in), sRace);
scanner.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.