I have a multi-level feedback class which I am not sure what its doing - it only ever seems to print the output I want when I run a pre-emptive multi-level class before it, when I don't it prints a different output. I am not sure how these are connected and making this happen.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MultiLevelFeedback {
List<Process> processList;
private int timeQuantum1;
private int timeQuantum2;
private int count;
int j=0;
private int ganntP[];
private int ganntT[];
private int totalWaitingTime = 0;
private int totalTurnAroundTime = 0;
private float avgWatingTime = 0;
private float avgTurnaroundTime = 0;
MultiLevelFeedback(List<Process> processList, int timeQuantum1,int timeQuantum2) {
count = processList.size();
ganntT=new int[200];
ganntP=new int[200];
this.timeQuantum1 = timeQuantum1;
this.timeQuantum2 = timeQuantum2;
this.processList=new ArrayList<Process>();
for(Process p : processList)
{
this.processList.add(new Process(p.getProcessId(), p.getArrivalTime(), p.getBurstTime(),p.getPriority()));
}
Collections.sort(this.processList, Process.BY_PRIORITY);
}
public void simulate() {
int currentTime =0;
int remainingProcess = count;
while (remainingProcess > processList.size()/2)
{
int clockTime=currentTime;
for (int i = 0; i < count; i++)
{
Process current = processList.get(i);
if(currentTime<current.getArrivalTime())
break;
if (current.getStartTime() == -1)
current.setStartTime(currentTime);
ganntP[j]=current.getProcessId();
ganntT[j]=currentTime;
j++;
if (current.getRemainingTime() <= timeQuantum1 && current.getEndTime()==-1)
{
current.setEndTime(currentTime + current.getRemainingTime());
currentTime += current.getRemainingTime();
current.setRemainingTime(0);
remainingProcess--;
}
else if (current.getRemainingTime()>timeQuantum1)
{
currentTime += timeQuantum1;
current.setRemainingTime(current.getRemainingTime()-timeQuantum1);
}
}
if(clockTime==currentTime)
{
currentTime++;
}
}
while (remainingProcess > processList.size()/2)
{
int clockTime=currentTime;
for (int i = 0; i < count; i++)
{
Process current = processList.get(i);
if(currentTime<current.getArrivalTime())
break;
if (current.getStartTime() == -1)
current.setStartTime(currentTime);
ganntP[j]=current.getProcessId();
ganntT[j]=currentTime;
j++;
if (current.getRemainingTime() <= timeQuantum2 && current.getEndTime()==-1)
{
current.setEndTime(currentTime + current.getRemainingTime());
currentTime += current.getRemainingTime();
current.setRemainingTime(0);
remainingProcess--;
}
else if (current.getRemainingTime()>timeQuantum2)
{
currentTime += timeQuantum2;
current.setRemainingTime(current.getRemainingTime()-timeQuantum2);
}
}
if(clockTime==currentTime)
{
currentTime++;
}
}
for(int i=0;i<count;i++)
{
Process current=processList.get(i);
if(current.getRemainingTime()>0 )
{
if(currentTime<current.getArrivalTime())
{
currentTime=current.getArrivalTime();
current.setStartTime(currentTime);
}
current.setEndTime(currentTime+current.getRemainingTime());
currentTime+=current.getRemainingTime();
}
}
for (int i = 0; i < count; i++)
{
Process current = processList.get(i);
current.setWaitingTime(current.getEndTime()-current.getBurstTime()-current.getArrivalTime());
current.setTurnaroundTime(current.getEndTime() - current.getArrivalTime());
totalWaitingTime += current.getWaitingTime();
totalTurnAroundTime += current.getTurnaroundTime();
}
avgWatingTime = (float) totalWaitingTime / count;
avgTurnaroundTime = (float) totalTurnAroundTime / count;
}
public void printResult()
{
System.out.println("Simulation result of MultiLevelFeedback ");
System.out.println("PId ArrivalT BurstT Priority StartT EndT WaitingT TurnAroundT");
for (Process p : processList)
{
System.out.println(p);
}
System.out.println("Average Waiting Time of MultiLevelFeedback "
+ avgWatingTime);
System.out.println("Average TurnAround Time of MultiLevelFeedback "
+ avgTurnaroundTime);
for(int i=0;i<j;i++)
{
System.out.println("time "+ganntT[i]+" process "+ganntP[i]);
}
System.out.println();
}
}
I am confused as there's nothing in the mlfq class which has anything to do with the pre-emptive multi-level class - they are both separate algorithms - except it is differing the output when I run both.
I'm trying to get the code to search for a songs that were made in a specific year
I've tried implementing my own binary search code but it's not working and instead asks for an input when I never asked for an input
Class:
public class MusicV3
{
// instance variables
private int year;
private String title;
private String artist;
// Constructor for objects of class Music
public MusicV3(String t, int y, String a)
{
// initialize instance variables
title = t;
year = y;
artist = a;
}
public String getTitle()
{
return title;
}
public void setTitle(String t)
{
title = t;
}
public String getArtist()
{
return artist;
}
public void setArtist(String a)
{
artist = a;
}
public int getYear()
{
return year;
}
public void setTitle(int y)
{
year = y;
}
public String toString()
{
String str = String.format( "%-25s %4d %-20s ", title, year , artist);
return str;
}
}
Tester class:
public class MusicV3Tester
{
public static void main(String[]args)
{
int find = 0;
MusicV3[] songs = new MusicV3[10];
MusicV3[] sortedSongs = new MusicV3[songs.length];
songs[0] = new MusicV3("Sugar", 2014, "Maroon 5");
songs[1] = new MusicV3("Mercy", 2016, "Shawn Mendes");
songs[2] = new MusicV3("Shape of You", 2017, "Ed Sheeran");
songs[3] = new MusicV3("Photograph", 2014, "Ed Sheeran");
songs[4] = new MusicV3("Closer", 2016, "The Chainsmokers");
songs[5] = new MusicV3("Galway Girl", 2017, "Ed Sheeran");
songs[6] = new MusicV3("Counting Stars", 2013, "OneRepublic");
songs[7] = new MusicV3("7 Years", 2015, "Lukas Graham");
songs[8] = new MusicV3("Night Changes", 2014, "One Direction");
songs[9] = new MusicV3("What Makes You Beautiful", 2011, "One Direction");
printSongs(songs);
System.out.println();
sortedSongs = sortBySong(songs);
System.out.println("Song list sorted by songs:");
//printSongs(sortedSongs);
System.out.println();
System.out.println("Searching for the song: Paris");
find = findSong(songs, "Paris");
if(find != -1){
System.out.println("We found Paris in the song list: ");
System.out.println(sortedSongs[find]);
}
else
System.out.println("Paris is not in the song list");
System.out.println();
System.out.println("Searching for the song: Sugar");
find = findSong(songs, "Sugar");
if(find != -1){
System.out.println("We found Sugar in the song list: ");
System.out.println(sortedSongs[find]);
}
else
System.out.println("Sugar is not in the song list");
System.out.println();
sortedSongs = sortByYear(songs);
System.out.println("Song list sorted by year:");
//printSongs(sortedSongs);
System.out.println();
System.out.println("Searching for the year: 2000");
find = findYear(songs, 2000);
if(find != -1){
System.out.println("We found songs made in the year 2000 in the song list: ");
System.out.println(sortedSongs[find]);
}
else
System.out.println("Songs made in the year 2000 are not in the song list");
System.out.println();
System.out.println("Searching for the year: 2014");
findYear(songs, 2014);
if(find != -1){
System.out.println("We found songs made in the year 2014 in the song list: ");
System.out.println(sortedSongs[find]);
}
else
System.out.println("Songs made in the year 2014 are not in the song list");
System.out.println();
sortedSongs = sortByArtist(songs);
System.out.println("Song list sorted by artist:");
//printSongs(sortedSongs);
System.out.println();
System.out.println("Searching for the artist: Sia");
findArtist(songs, "Sia");
System.out.println();
System.out.println("Searching for the artist: Ed Sheeran");
findArtist(songs, "Ed Sheeran");
System.out.println();
}
public static void printSongs(MusicV3[] s)
{
System.out.println("Song Year Artist");
System.out.println("-------------------------------------------------------");
for(int i = 0; i < s.length; i++)
System.out.println(s[i]);
}
public static MusicV3[] sortBySong(MusicV3 [] songs){
MusicV3[] sortedList = songs;
int i;
int k;
int posmax;
MusicV3 temp;
for ( i = songs.length - 1 ; i >= 0 ; i-- )
{
posmax = 0;
for ( k = 0 ; k <= i ; k++ )
{
if (songs[k].getTitle().compareTo(songs[posmax].getTitle()) > 0)
posmax = k;
}
temp = songs[i];
songs[i] = songs[posmax];
songs[posmax] = temp;
}
return sortedList;
}
public static MusicV3[] sortByYear(MusicV3 [] movies){
MusicV3[] sortedList = movies;
int i;
int k;
int posmax;
MusicV3 temp;
for ( i = movies.length - 1 ; i >= 0 ; i-- )
{
posmax = 0;
for ( k = 0 ; k <= i ; k++ )
{
if (movies[k].getYear()> movies[posmax].getYear())
posmax = k;
}
temp = movies[i];
movies[i] = movies[posmax];
movies[posmax] = temp;
}
return sortedList;
}
public static MusicV3[] sortByArtist(MusicV3 [] songs){
MusicV3[] sortedList = songs;
int i;
int k;
int posmax;
MusicV3 temp;
for ( i = songs.length - 1 ; i >= 0 ; i-- )
{
posmax = 0;
for ( k = 0 ; k <= i ; k++ )
{
if (songs[k].getArtist().compareTo(songs[posmax].getArtist()) > 0)
posmax = k;
}
temp = songs[i];
songs[i] = songs[posmax];
songs[posmax] = temp;
}
return sortedList;
}
public static int findSong( MusicV3[] songs, String song){
int high = songs.length;
int low = -1;
int probe;
while ( high - low > 1 )
{
probe = ( high + low ) / 2;
if (songs[probe].getTitle().compareTo(song) > 0)
high = probe;
else
low = probe;
}
if ( low >= 0 && songs[low].getTitle().compareTo(song) == 0){
return low;
}
else{
return -1;
}
}
public static int findYear(MusicV3[] songs, int year){
int high = songs.length - 1;
int low = 0;
int probe;
while (low <= high)
{
probe = ( high + low ) / 2;
if (songs[probe].getYear() == year)
return probe;
if(songs[probe].getYear() > year)
low = probe;
else
low = probe + 1;
}
return -1;
}
public static void findArtist(MusicV3[] s, String artist)
{
int found = 0;
for(int i = 0; i < s.length; i++){
if (s[i].getArtist().compareTo(artist) == 0)
{
System.out.println(s[i]);
found++;
}
}
if (found == 0)
{ // we have not found the location
System.out.println("There are no songs on the songs list made in the year " + artist);
System.out.println();
}
else
{
System.out.print("There were " + found + " listings for " + artist);
System.out.println();
}
}
}
Ignore the song and artist search, those I can do and have no problems with but with the year search, it's not working correctly since it is supposed to correctly print all of the songs that are in the specific year giving (2000 and 2014) but it isn't and instead is not working at all and asking for an input for some reason. This is my first time doing a binary search so i'm pretty new to this.
How can you possibly show all of them when you are only returning the index of one of them (and not even the first one at that) ?
Try returning a list of results. or a Pair<int, int> of indexes begin and end
I'm new with arrays.
This program i have created is throwing an out of bounds error with every method inside of the BlueJays class.
To be honest, im not even sure if im searching through the array right. Any help would be appreciated.
Thanks
This is my main
public class Lab1 {
static final int NBPLAYERS = 11;
static final int NBMONTHS = 6;
public static void main(String[] args) {
String[] month = {"April", "May","June", "July", "August","September"};
String[] players = {"Colabello","Donaldson","Smoak","Martin","Goins","Encarnacion","Carrera","Tulowitzki","Pillar","Bautista","Travis"};
double[][] battingAvg = {
{0,.368,.300,.224,.386,.268},
{.319,.306,.269,.287,.324,.296},
{.229,.310,.213,.191,.203,.262},
{.197,.327,.239,.256,.138,.213},
{.276,.236,.172,.240,.314,.279},
{.205,.225,.303,.241,.407,.279},
{0,.302,.282,.244,.333,.231},
{0,0,0,.357,.214,.237},
{.273,.181,365,.283,.240,.323},
{.164,.295,.226,.219,.286,.293},
{.325,.189,.313,.368,0,0}};
double [][] onBase = {
{.417,.330,.286,.413,.362,.429},
{.370,.373,.322,.370,.408,.403},
{.372,.333,.275,.283,.243,.324},
{.367,.362,.329,.322,.263,.300},
{.323,.278,.221,.286,.442,.347},
{.258,.333,.382,.384,.460,.411},
{0,.357,.333,.277,.333,.313},
{0,0,0,.400,.325,.250},
{.297,.237,.380,.323,.283,.363},
{.325,.418,.388,.300,.370,.436},
{.393,.246,.313,.421,0,0}};
PlayerStats Player;
BlueJays team = new BlueJays(NBPLAYERS, NBMONTHS);
team.getHighestSingleMonthBattingAvg();
team.getHighestOnBase(5);
team.getLowestBattingAvg(6);
team.getBestMonth("Bautista");
team.getBestOverallRecord();
team.getLowestOnBase();
}
}
This is my PlayerStats which has accessor/mutator methods.
class PlayerStats {
private String name;
private int month;
private double battAvg, onBase;
public PlayerStats(String name, int month, double battingAvg, double onBase2) {
this.name = name;
this.month = month;
this.battAvg = battingAvg;
this.onBase = onBase2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public double getAvg() {
return battAvg;
}
public double getOnBase() {
return onBase;
}
public double getBoth() {
return battAvg + onBase;
}
}
And this is the class where the 2d array is created.
class BlueJays {
int nbPlayers;
int nbMonths;
int j = 0;
int highestBattingAvg;
int highestBattingMonth;
String highestBattingPlayer;
int highestOnBase;
int lowestAvg;
String lowestAvgPlayer;
int highestOverall;
String highestOverallPlayer;
int lowestOnBase;
int lowestOnBaseMonth;
String highestOnBasePlayer;
double bestOverAllMonth;
public BlueJays(int nbplayers2, int nbmonths2) {
this.nbPlayers = nbplayers2;
this.nbMonths = nbmonths2;
}
PlayerStats[][] stats = new PlayerStats[nbPlayers][nbMonths];
PlayerStats setPlayerStatsCell(PlayerStats player, int iPlayer, int iMonth) {
return player;
}
PlayerStats getHighestSingleMonthBattingAvg() {
while(j < nbMonths) {
for(int i = 0; i < nbPlayers; i++) {
stats[i][j].getAvg();
if(highestBattingAvg < stats[i][j].getAvg()) {
highestBattingMonth = stats[i][j].getMonth();
highestBattingPlayer = stats[i][j].getName();
}
if (i == nbMonths) {
j++;
i = 0;
}
System.out.println("Highest average batting player for the month " + highestBattingMonth + " is " + highestBattingPlayer);
}
}
return null;
}
PlayerStats getHighestOnBase(int month) {
while(j < nbMonths) {
for(int i = 0; i < nbPlayers; i++) {
stats[i][month].getOnBase();
if(highestOnBase < stats[i][month].getOnBase()) {
highestOnBasePlayer = stats[i][month].getName();
}
if (i == nbMonths) {
j++;
i = 0;
}
System.out.println("Highest average onBase player for the month " + month + highestOnBasePlayer);
}
}
return null;
}
public PlayerStats getLowestBattingAvg(int month) {
while(j < nbMonths) {
for(int i = 0; i < nbPlayers; i++) {
stats[i][month].getOnBase();
if(lowestAvg > stats[i][month].getAvg()) {
lowestAvgPlayer = stats[i][month].getName();
}
if (i == nbMonths) {
j++;
i = 0;
}
System.out.println("Lowest average batting player for the month " + month + " is " + lowestAvgPlayer);
}
}
return null;
}
PlayerStats getBestMonth(String player) {
while(j < nbMonths) {
for(int i = 0; i < nbPlayers; i++) {
stats[i][j].getBoth();
if(bestOverAllMonth > stats[i][j].getAvg() && stats[i][j].getName().contains(player)) {
bestOverAllMonth = stats[i][j].getBoth();
}
if (i == nbMonths) {
j++;
i = 0;
}
System.out.println("Best month for the player " + player + " is " + bestOverAllMonth);
}
}
return null;
}
public String getBestOverallRecord() {
while(j < nbMonths) {
for(int i = 0; i < nbPlayers; i++) {
stats[i][j].getBoth();
if(highestOverall < stats[i][j].getBoth()) {
highestOverallPlayer = stats[i][j].getName();
}
if (i == nbMonths) {
j++;
i = 0;
}
System.out.println("Highest overall record is " + highestOverallPlayer);
}
}
return null;
}
public PlayerStats getLowestOnBase() {
while(j < nbMonths) {
for(int i = 0; i < nbPlayers; i++) {
stats[i][j].getOnBase();
if(lowestOnBase > stats[i][j].getOnBase()) {
double lowestOnBase = stats[i][j].getOnBase();
if(lowestOnBase > 0) {
lowestAvgPlayer = stats[i][j].getName();
} else {
i++;
}
if (i == nbMonths) {
j++;
i = 0;
}
}
System.out.println("Lowest On Base is " + lowestOnBase);
}
}
return null;
}
}
Here is the exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at BlueJays.getHighestSingleMonthBattingAvg(BlueJays.java:42)
at Lab1.main(JonathanRoy_Lab1.java:40)
Your problem may lay here:
// ... lots of other stuff
public BlueJays(int nbplayers2, int nbmonths2) {
this.nbPlayers = nbplayers2;
this.nbMonths = nbmonths2;
}
PlayerStats[][] stats = new PlayerStats[nbPlayers][nbMonths];
If you initialize an instance of your BlueJay-class it will create the array before you set the values nbPlayers and nbMonths. So you'll have an array of the size 0 and 0.
To avoid this you have to do following:
// ... lots of other stuff
public BlueJays(int nbPlayers, int nbMonths) {
this.nbPlayers = nbPlayers;
this.nbMonths = nbMonths;
// doesn't matter if you take the parameters or fields
this.stats = new PlayerStats[nbPlayers][nbMonths];
}
Hope I helped you out :)
I don't know what the exception is but here's what I got by scanning through your code.
You never reset j so most of your function calls won't make it past the first comparison.
The function call team.getLowestBattingAvg(6); uses the index 6, but since nbMonths=6, the highest index you can use is 5 (the indices go from 0 to 5 inclusive).
I'm currently trying to convert my code to ArrayList and I can't seem to make it work. I'm running the whole program and it tells me Index out bounds. I'm sure I forgot to add the size of the array for the cards, but I don't know how to add it. Thanks for the help!
Edit: The error I get is at the bottom. Also, it tells me to go to the removeTop method. It looks fine there.
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
public class CardPile {
private ArrayList<Card> cards = new ArrayList<Card>();
private static Random r = new Random(1);
public void addToBottom(Card c) {
if (this.cards.size() == 52) {
System.out.println("The CardPile is full. You cannot add any more Card objects.");
}
this.cards.add(c);
}
public Card removeCard(Card c) {
if (this.cards.contains(c)) {
this.cards.remove(c);
}
return null;
}
public Card removeTop() {
return this.cards.remove(0);
}
public int searchValue(int value) {
int count = 0,
for (int i = 0;i < this.cards.size();i++) {
if (this.cards.get(i).getValue() == value) {
count++;
}
}
//System.out.println("Count = "+count);
return count;
}
public Card[] removeAll(int value)
//System.out.println("(removeAll) cards ="+ cards);
int count = searchValue(value);
Card[] removed = new Card[count];
int deletedCount = 0;
int i = 0;
while (deletedCount < count) {
if (this.cards.get(i).getValue() == value) {
removed[deletedCount] = this.cards.remove(i);
deletedCount++;
} else {
i++;
}
}
return removed;
}
public int getNumberCards() {
return this.cards.size();
}
public String toString() {
if (this.cards.isEmpty()) {
return "";
}
String builder = "";
for (int i = 0;i < this.cards.size() - 1;i++) {
builder = builder + this.cards.get(i) + ", ";
}
builder = builder + this.cards.get(this.cards.size() - 1);
return builder;
}
public void shuffle() {
if (this.cards.isEmpty()) {
return;
}
for (int count = 0; count < 100000;count++) {
int i = r.nextInt(this.cards.size());
int j = r.nextInt(this.cards.size());
Card temp = this.cards.get(i);
this.cards.set(i, this.cards.get(j));
this.cards.set(j, temp);
}
}
public static CardPile makeFullDeck() {
CardPile deck = new CardPile();
for (int suit = 0;suit < 4;suit++) {
for (int value = 1; value <= 13;value++) {
deck.addToBottom(new Card(suit, value));
}
}
deck.shuffle();
return deck;
}
}
**Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.remove(ArrayList.java:492)
at CardPile.removeTop(CardPile.java:40)
at GoFish.dealCards(GoFish.java:112)
at GoFish.main(GoFish.java:13)**
EDIT:
This is the Player class:
public class Player {
private boolean[] books;
private CardPile pile;
private static int MAXIMUM_VALUE_CARD = 13;
public Player()
{
this.pile = new CardPile();
this.books = new boolean[13]; //by default all are false
}
public boolean hasCard(int value)
{
return this.pile.searchValue(value) > 0;
}
public Card[] removeAll(int value)
{
return this.pile.removeAll(value);
}
public void addAll(Card[] cards)
{
for (int i = 0; i < cards.length; i++)
{
this.pile.addToBottom(cards[i]);
}
}
//optional additional method
public void addCard(Card card)
{
this.pile.addToBottom(card);
}
public int getNumberCards()
{
return this.pile.getNumberCards();
}
public int countBooks()
{
int count = 0;
for (int i = 0; i < MAXIMUM_VALUE_CARD; i++)
{
if (books[i])
{
count++;
}
}
return count;
}
public void addBook(int value)
{
this.books[value - 1] = true;
}
public void printHand()
{
System.out.println("Player's hand is " + this.pile);
}
}
And this is the GoFish class:
import java.util.Scanner;
public class GoFish {
private static Scanner reader;
public static void main(String[] args)
{
System.out.println("How many players?");
reader = new Scanner(System.in);
int numberPlayers = reader.nextInt();
Player[] players = createPlayersArray(numberPlayers);
int currentTurn = 0;
CardPile deck = CardPile.makeFullDeck();
dealCards(deck, players);
int maximumRetries = 2;
int numRetries = 0;
while(deck.getNumberCards() > 0 && players[currentTurn].getNumberCards() > 0)
{
updateBooks(players[currentTurn]);
if (numRetries == maximumRetries)
{
numRetries = 0;
currentTurn++;
if (currentTurn == numberPlayers)
{
currentTurn = 0;
}
}
System.out.println("Player " + currentTurn + ", here is your hand. What card would you like to ask for?");
players[currentTurn].printHand();
int queryCard = reader.nextInt();
System.out.println("And from whom would you like to get it from?");
int queryPlayer = reader.nextInt();
if (queryCard < 1 || queryCard > 13 || queryPlayer < 0 || queryPlayer >= numberPlayers || queryPlayer == currentTurn)
{
System.out.println("Invalid entries. Please retry");
numRetries++;
}
else
{
numRetries = 0;
boolean hasCard = players[queryPlayer].hasCard(queryCard);
if (hasCard)
{
System.out.println("Cards found!");
Card[] removed = players[queryPlayer].removeAll(queryCard);
players[currentTurn].addAll(removed);
}
else
{
System.out.println("Go fish!");
Card top = deck.removeTop();
System.out.println("You drew " + top);
players[currentTurn].addCard(top);
//check to make sure this extra card didn't form a book
//Note this could happen even if it doesn't match the card they were asking about
updateBooks(players[currentTurn]);
if (top.getValue() == queryCard)
{
System.out.println("You successfully went fishing!");
}
else
{
currentTurn++;
if (currentTurn == numberPlayers)
{
currentTurn = 0;
}
}
}
}
}
//calculate the winner now
int maxPlayer = 0;
int maxPlayerBooks = players[0].countBooks();
for (int i = 1; i < numberPlayers; i++)
{
int currentBooks = players[i].countBooks();
if (currentBooks > maxPlayerBooks)
{
maxPlayer = i;
maxPlayerBooks = currentBooks;
}
}
System.out.println("Congratulations! Player " + maxPlayer + " you have won the game by accumulating " + maxPlayerBooks + " books!");
}
private static Player[] createPlayersArray(int numPlayers)
{
Player[] players = new Player[numPlayers];
for (int i = 0; i < numPlayers; i++)
{
players[i] = new Player();
}
return players;
}
private static void dealCards(CardPile deck, Player[] players)
{
final int NUMBER_CARDS_PER_PLAYER = 7;
for (int i = 0; i < NUMBER_CARDS_PER_PLAYER * players.length; i++)
{
Card next = deck.removeTop();
players[i % players.length].addCard(next);
}
}
private static void updateBooks(Player player)
{
for (int i = 1; i <= 13; i++)
{
//alternative option would be to modify the hasCard method to return an int instead of boolean. Then we could just count (this is probably better design)
Card[] valued = player.removeAll(i);
if (valued.length == 4)
{
player.addBook(i);
}
else
{
player.addAll(valued);
}
}
}
}
The method "aboveAverage" in the following code is not displaying correctly and I've tried everything I can. Could someone please explain what's going wrong?
My code:
import java.util.*;
public class DailyCatch
{
private int fishermanID, fisherID;
private String dateOfSample, date;
private double[] fishCaught = new double[10];
private int currWeight = 0;
private String summary;
private double average;
private int aboveAvg;
public DailyCatch() { }
public DailyCatch (int fishermanID, String dateOfSample)
{
fisherID = fishermanID;
date = dateOfSample;
}
public DailyCatch (int fishermanID, String dateOfSample, String weight)
{
this(fishermanID, dateOfSample);
readWeights(weight);
}
public void addFish(double weight)
{
if (currWeight > 10)
{
// array full
}
else
{
fishCaught[currWeight] = weight;
currWeight += 1; // update current index of array
}
}
private void readWeights(String weightsAsString)
{
String[] weightsRead = weightsAsString.split("\\s+");
for (int i = 0; i < weightsRead.length; i++)
{
this.addFish(Double.parseDouble(weightsRead[i]));
}
}
public String toString()
{
return "Fisherman ID: " + fisherID + "\nDate:" + date + "\nFish Caught with Weights: " + Arrays.toString(fishCaught);
}
public void printWeights()
{
for (int i = 0; i < fishCaught.length; i++)
{
System.out.println(fishCaught[i]);
}
}
public double averageWeight()
{
double sum = 0;
double count = 0;
for (int i = 0; i < fishCaught.length; i++)
{
if (fishCaught[i] != 0)
{
sum += fishCaught[i];
count += 1;
average = sum/count;
}
}
return average;
}
public String getSummary()
{ int storyTellerCount = 0;
int keeperCount = 0;
int throwBackCount = 0;
for (int i = 0; i < fishCaught.length; i++)
{
if (fishCaught[i] > 5)
{
storyTellerCount++;
}
else if (fishCaught[i] >=1 && fishCaught[i] <= 5)
{
keeperCount++;
}
else if (fishCaught[i] < 1 && fishCaught[i] > 0)
{
throwBackCount++;
}
} String summary = ("\nStoryteller - " + storyTellerCount+ "\nKeeper - " + keeperCount + "\nThrowback - " + throwBackCount);
return summary;
}
public int aboveAverage()
{
int greatAvgCount = 0;
for (int i = 0; i < fishCaught.length; i++)
{
if (fishCaught[i] > average)
{
aboveAvg = greatAvgCount++;
}
}
return aboveAvg;
}
}
Test Code:
public class BigBass
{
public static void main (String[]args)
{
//Part 1
DailyCatch monday1 = new DailyCatch(32, "4/1/2013", "4.1 5.5 2.3 0.5 4.8 1.5");
System.out.println(monday1);
//Part 2
DailyCatch monday2 = new DailyCatch(44, "4/1/2013");
System.out.println(monday2);
monday2.addFish(2.1);
monday2.addFish(4.2);
System.out.println(monday2);
//Part 3
System.out.println("\n\nSUMMARY OF FISHERMAN 32");
System.out.println(monday1.getSummary());
//Part 4
double avg = monday1.averageWeight();
System.out.printf("\nThere are %d fish above the average weight of %.1f.", monday1.aboveAverage(), avg);
}
}
I just need to get Part 4 to work here. What it does is return that there have been 2 fish caught that are above average when I know it should be 3. The average is 3.1.
A simple mistake.
public int aboveAverage() {
int greatAvgCount = 0;
for (int i = 0; i < fishCaught.length; i++) {
if (fishCaught[i] > 3.1) {
greatAvgCount++; // no 'return'
}
}
return greatAvgCount;
}
if (fishCaught[i] > 3.1)
{
return greatAvgCount++;
}
First try : 4.1 > 3.1
true
returns 0 ++ which is 0 basically
You can increment the counter inside the loop and keep the return statement for the end only.
try
public int aboveAverage() {
int greatAvgCount = 0;
for (int i = 0; i < fishCaught.length; i++) {
if (fishCaught[i] > 3.1) {
greatAvgCount++;
}
}
return greatAvgCount;
}
This line is your problem,
return greatAvgCount++;
you are incrimenting greatAvgCount then returning its initial value, there should be no "return" on this line
The aboveAverage method should be
public int aboveAverage()
{
int greatAvgCount = 0;
for (int i = 0; i < fishCaught.length; i++)
{
if (fishCaught[i] > 3.1)
{
greatAvgCount++;
}
}
return greatAvgCount;
}
Also, you may just be doing it for debug, in which case fair enough, but hardcoding the "average" as 3.1 is generally considered bad practice. If you want average to be always 3.1 (i.e. its a global average that you've looked up from a book then its more usual to declare a static variable called double AVERAGE=3.1 and then use that where ever average is required, that way if the "book value" changes you only need to change average in one place in your code. If average is calculated from your data obviously you should use the calculated value.
Also not directly related to your problem but why are you using an array for your caught fish with a predefined maximum of 10. If you used an ArrayList you could add to it as you saw fit and it would auto expand to accommodate
private double[] fishCaught = new double[10];
becomes
private ArrayList<Double> fishCaught = new ArrayList<Double>();