JAVA An Application of a Stack For this assignment you are to write a Java progr
ID: 3843005 • Letter: J
Question
JAVA
An Application of a Stack
For this assignment you are to write a Java program that reads a data file consisting of a player’s name followed by their batting average. The program will then print the highest batting average and the names of the people who have the highest batting average. It will also print the lowest batting average and the names of the people who have the lowest average. We will also assume that the file will not contain more than 100 players.
Input:
The program reads an input file consisting of each player’s name followed by their average. Here is a small sample:
John .425
Terry .696
Glenn .704
Gary .400
Joe .704
You Must use a FileChooser to select the file to open.
Output
The program outputs the highest and lowest batting averages and the names associated with these averages. For example, in the above sample data Glenn and Joe have the highest batting average and Gary has the lowest.
In order to keep track of all the people with the highest and lowest you will need to use a stack. Actually you will need to use two instances of the stack. One to hold the names of the highest and one to hold the names of the lowest averages. You should implement the Stack found in the lecture material.
Here is how the program might flow
When you read the first name and average at that time it is both the highest and lowest. You will store the average in variables that keep track of the highest and lowest average. You will then push the name on to both of the stacks. After reading the second name and average you are faced with some choices.
The new average is greater than the highest stored
Update the value of the highest average so far
Initialize the stack that holds the highest – that is remove the names of the players from the stack
Save the name of the player having the highest average so far on the Stack
The new average is equal to the highest average so far. In this case simply add the name of the player to the top of the stack
The new average is smaller than the highest average so far. In this case you check to see if it is less than the lowest average found so far. If this is the case you need to do the following
Update the value of the lowest average so far
Initialize the stack that holds the lowest – that is remove the names of the players from the stack
Save the names of the players having the lowest average so far on the stack
The average is equal to the lowest average so far. In this case add the name of the player to the top of the stack.
The average is not smaller than the smallest average so far and it is not larger than the largest so far. In this case simply discard the name and the average.
When the file has been completely read you should have a stack that contains the names of the players with the highest average and a stack with the names of the lowest average. You should also have a variable that contains the highest average and a variable that contains the lowest average. Print the highest average and the players with this average to a JavaFX GUI component. I will leave it up to you to determine which and how the GUI is designed. Do the same with the lowest.
Note:
Do not place all of your code in one file. At the very least you should have a file called Stack.java and a file that contains the main class. Also, you must supply a GUI for this. I will leave it up to you to decide how it looks but it should be functional and easy to use.
Explanation / Answer
class CricketPlayers { public static void main(String arg[]) { Player players[] = new Player[11]; players[0] = new Batsmen("John", 425, 11324, 100, 125); players[1] = new WicketKeeper("Terry", 696, 6021, 120, 67); players[2] = new Batsmen("Glenn", 704, 4341, 22, 40); players[3] = new Batsmen("Gary", 400, 6533, 15, 46); players[4] = new Batsmen("Joe", 704, 4003, 25, 60); for(Player player : players) { player.print(); System.out.println(); System.out.println("---------------------"); } } } abstract class Player { String name; int matchesPlayed; int runsScored; Player(String name, int matchesPlayed, int runsScored) { this.name = name; this.matchesPlayed = matchesPlayed; this.runsScored = runsScored; } void bat() { } void makeSomeRuns() { } void print() { System.out.print(name + " played " + matchesPlayed + " matches and scored " + runsScored + " runs."); } } class Batsmen extends Player { int numberOfCenturies; int numberOfHalfCenturies; Batsmen(String name, int matchesPlayed, int runsScored, int numberOfCenturies, int numberOfHalfCenturies) { super(name, matchesPlayed, runsScored); this.numberOfCenturies = numberOfCenturies; this.numberOfHalfCenturies = numberOfHalfCenturies; } void openInnings() { } void makeCentury() { } void makeHalfCentury() { } void print() { super.print(); System.out.print(" He is a strong batsmen and made " + numberOfCenturies + " centuries and " + numberOfHalfCenturies + " half centuries."); } } class Bowler extends Player { int numberOfWickets; int numberOf5WicketInnings; Bowler(String name, int matchesPlayed, int runsScored, int numberOfWickets, int numberOf5WicketInnings) { super(name, matchesPlayed, runsScored); this.numberOfWickets= numberOfWickets; this.numberOf5WicketInnings = numberOf5WicketInnings; } void openInnings() { } void bowlYorkers() { } void takeWickets() { } void print() { super.print(); System.out.print(" He is also a good bowler and has taken " + numberOfWickets + " wickets. He has " + numberOf5WicketInnings + " 5WI(5-Wicket Innings) in his account."); } } class WicketKeeper extends Player { int numberOfCatches; int numberOfStumpings; WicketKeeper(String name, int matchesPlayed, int runsScored, int numberOfCatches, int numberOfStumpings) { super(name, matchesPlayed, runsScored); this.numberOfCatches = numberOfCatches; this.numberOfStumpings = numberOfStumpings; } void keepWickets() { } void stumpBatsmen() { } void makeAppeals() { } void print() { super.print(); System.out.print(" He also keeps the wickets and has " + numberOfCatches + " catches and " + numberOfStumpings + " stumpings in his account."); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.