Question

In: Computer Science

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 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

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CODE TO COPY

ScoreBoard.java

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() {
       ScoreIterator si = new ScoreIterator() {

           private int row = 0;
           private int col = 0;

           @Override
           public int next() {
               if (hasNext()) {
                   while (row < scores.length) {
                       while (col < scores[0].length) {

                           int x = scores[row][col];
                           col++;
                           if (col == scores[0].length) {
                               col = 0;
                               row++;
                           }
                           return x;
                       }
                       row++;
                   }
               }
               return 0;
           }

           @Override
           public boolean hasNext() {
               return row < scores.length && col < scores[0].length;
           }
       };

       return si;
   }

   public static void main(String[] args) {
       ScoreBoard sb = new ScoreBoard(3, 2);

       System.out.println("using for loop");
       for (int i = 0; i < sb.scores.length; i++) {
           for (int j = 0; j < sb.scores[0].length; j++) {
               System.out.println(sb.scores[i][j]);
           }
       }

       System.out.println("----------------");
       System.out.println("using iterator");

       ScoreIterator iterator = sb.scoreIterator();
       while (iterator.hasNext()) {
           System.out.println(iterator.next());
       }
   }

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ScoreIterator.java

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

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

EXPLANATION

import java.util.*; // for using random class

public class ScoreBoard { // creating a class names ScoreBoard
   int[][] scores; // to store numbers in 2d array

   public ScoreBoard(int numOfPlayers, int numOfRounds) { //constructor to intialize scores
       scores = new int[numOfPlayers][numOfRounds];
       Random rand = new Random(); // creating a random object rand
       for (int i = 0; i < numOfPlayers; i++) { // for loop for rows
           for (int j = 0; j < numOfRounds; j++) { // for loop for each element in row
               scores[i][j] = 50 + rand.nextInt(51); // generating a random number
           }
       }
   }

   public ScoreIterator scoreIterator() { // creating a scoreiterator method which returns ScoreIterator object anonymously
       ScoreIterator si = new ScoreIterator() { //creating a ScoreIterator object si

           private int row = 0; // set row as 0
           private int col = 0; // set col as 0

           @Override
           public int next() { // to return next element
               if (hasNext()) { // if the scores array has elements
                   while (row < scores.length) { // then check if the row is not at the end
                       while (col < scores[0].length) { // check if the column is not at the end

                           int x = scores[row][col]; // get the element at the position
                           col++; // increase column by 1
                           if (col == scores[0].length) { // if element is at the end of the row
                               col = 0; // set col as 0
                               row++; // increase row by 1
                           }
                           return x; // return the number
                       }
                       row++; // increase the row number after each element is returned, in order to return the number
                   }
               }
               return 0; // return 0 if no number is found
           }

           @Override
           public boolean hasNext() {
               return row < scores.length && col < scores[0].length; // check if the row and col are inside the range
           }
       };

       return si; //return the object
   }

   public static void main(String[] args) {
       ScoreBoard sb = new ScoreBoard(3, 2);

//remove these lines and for loop if needed, this was only to check the elements and show you

       System.out.println("using for loop"); // printing statement
       for (int i = 0; i < sb.scores.length; i++) { // for loop for row
           for (int j = 0; j < sb.scores[0].length; j++) { // for loop for each element in row
               System.out.println(sb.scores[i][j]); // print the element
           }
       }

       System.out.println("----------------"); // printing line
       System.out.println("using iterator"); // printing statement

       ScoreIterator iterator = sb.scoreIterator(); // creating a iterator using the method which return the object anonymously
       while (iterator.hasNext()) { // while loop to check if element exsists
           System.out.println(iterator.next()); // printing the element
       }
   }

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

OUTPUT

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

IF YOU HAVE ANY DOUBTS REGARDING THE SOLUTION PLEASE COMMENT BELOW I WILL HELP YOU

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


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...
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...
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 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:...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to...
JAVA CODE BEGINNERS, I already have the DEMO CLASS(NEED YOU TO USE), I need you to use all methods, also switch statements. Write a Temperature class. The class will have three conversion methods: toCelsius(), toKelvin() and toFahrenheit(). These methods will return a Temperature in those three scales equal to the this temperature. Note that the value of this is not changed in these conversions. In addition to these three conversion methods the class will have methods add(Temperature), subtract(Temperature), multiply(Temperature), and...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
How would I get this java code to work and with a main class that would...
How would I get this java code to work and with a main class that would demo the rat class? class rat { private String name; private String specialAbility; private int TotalHealth; private int shieldedHealth; private int cooldown; public rat() { } public rat(String n,String SA,int TH,int SH,int cd) { name=n; specialAbility=SA; TotalHealth=TH; shieldedHealth=SH; cooldown=cd; } public void setname(String n) { name=n; } public String getname() { return name; } public void setability(String SA) { specialAbility=SA; } public String getability()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT