Question

In: Computer Science

In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...

In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow

Example:

Score 1 score 2
Player1 20 21
Player2 15 32
Player3 6 7
Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext()

public interface ScoreIterator {
    int next();
    boolean hasNext();

Class ScoreBoard :

import java.util.*;

public class ScoreBoard {
    int[][] scores ;

    public ScoreBoard (int numOfPlayers, int numOfRounds) {
        scores = new int[numOfPlayers][numOfRounds];
        Random rand = new Random();
        for (int i = 0; i < numOfPlayers; i++) {
            for (int j = 0; j < numOfRounds; j++) {
                scores [i][j] = 50 + rand.nextInt(51);
            }
        }
    }

    public ScoreIterator scoreIterator () {
        // return anonymous object of type ScoreIterator 

        
    }

    public static void main(String[] args) {
        ScoreBoard sb = new ScoreBoard (3, 2);
        ScoreIterator iterator = sb.ScoreIterator ();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
}

Solutions

Expert Solution

The solution to the above problem in java is as follows:-

Code-


import java.util.*;

interface ScoreIterator {
    int next();
    boolean hasNext();
}
public class ScoreBoard {
    int[][] scores ;

    public ScoreBoard (int numOfPlayers, int numOfRounds) {
        scores = new int[numOfPlayers][numOfRounds];
        Random rand = new Random();
        for (int i = 0; i < numOfPlayers; i++) {
            for (int j = 0; j < numOfRounds; j++) {
                scores [i][j] = 50 + rand.nextInt(51);
            }
        }
    }

    ScoreIterator scoreIterator () {
        // return anonymous object of type ScoreIterator
        ScoreIterator x= new ScoreIterator(){ //Create new object
            int i=0; //devclare player index
            int j=0; //Score index
            @Override
            public int next(){ //Calculate scores for the index
                int score=scores[i][j];
                j++;
                if(j==scores[i].length){
                    i++; ///increase iterator
                    j=0;
                }
                return score; //return total score
            }
            @Override
            public boolean hasNext(){ //Check if new player exists
                return i<scores.length;
            }
        };
        return x;
      
    }

    public static void main(String[] args) {
        ScoreBoard sb = new ScoreBoard (3, 2);
        ScoreIterator iterator = sb.scoreIterator ();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}


Code Screenshots-

Outputs-

Feel free to comment for any issues, do rate the answer positively


Related Solutions

In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard (int...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
java 1.) a method to append a 2d double array at the right of another 2d...
java 1.) a method to append a 2d double array at the right of another 2d double array, return a new array. E.g. numss1: {{1, 2}, {3, 4, 5}, {6}}, numss2: {{7}, {8, 9}}, return {{1, 2, 7}, {3, 4, 5, 8, 9}, {6}}
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle...
JAVA CODE BEGINNERS, I already have the demo code included Write a Bottle class. The Bottle will have one private int that represents the countable value in the Bottle. Please use one of these names: cookies, marbles, M&Ms, pennies, nickels, dimes or pebbles. The class has these 14 methods: read()(please use a while loop to prompt for an acceptable value), set(int), set(Bottle), get(), (returns the value stored in Bottle), add(Bottle), subtract(Bottle), multiply(Bottle), divide(Bottle), add(int), subtract(int), multiply(int), divide(int), equals(Bottle), and toString()(toString()...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT