Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

-Most of the program has been written for you. A big part of this assignment is to understand what the program is doing. -DO NOT

-Most of the program has been written for you. A big part of this assignment is to understand what the program is doing.

-DO NOT make any changes to the main() method. It must stay exactly as it is.

-The program compiles as-is but does not do anything because the methods are almost empty.

-You are being asked to complete only 5 methods:

1. getTopTen

Expects an array of integers as a parameter.

Returns a new array with only the first 10 elements in the given array.

If the given array has less than 10 elements, the new array will have the same number of elements as the given array.

2. checkLuckyNumbers

Expects 3 parameters: an array of integers, and 2 numbers.

Searches the given array for the occurrence of the 2 given numbers side-by-side.

Returns true if it finds the 2 numbers next to each other in the array, and false otherwise.

3. replaceNumbers

Expects 3 parameters: an array of integers, and 2 numbers (a minNumber and a maxNumber).

Returns a version of the given array where all numbers that are out of range (less than minNumber or greater than maxNumber) have been replaced with 0.

The original array (passed as a parameter) does not change.

4. countDigits

Expects an array of integers as a parameter.

Counts the number of times each digit (0 to 9) appears in the given array.

Returns an array, returnArray (with length = 10), with the counts for each digit, where:

returnArray[0] has the number of occurrences of the digit 0 in the given array,

returnArray[1] has the number of occurrences of the digit 1 in the given array,

and so on...

5. printGraph

Expects an array of integers as a parameter.

For each element in the given array, print a line of asterisks.

The number of asterisks in each line, is the value of that array element.

E.g. if the name of the given array is frequencies, and frequencies[0] is 5, and frequencies[1] is 3, print:

0 * * * * * 1 * * *

CODE:

import java.util.Scanner;

// Please do not use any other library

public class A9 {

// Start of main method

public static void main (String [] args) {

Scanner input = new Scanner(System.in);

// Ask the user for the size of the array

System.out.println ("This program will create a list of random

numbers.");

System.out.println ("How many random numbers would you like in your

list?");

int size = input.nextInt();

// Create an array with the size entered by the user.

int[] randomNumbers = new int[size];

// Populate the array with random numbers.

populateArray(randomNumbers);

// Print the contents of the array numbers.

printArray(randomNumbers);

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

// Get a new array topTen, returned by the method getTopTen()

int[] topTen = getTopTen(randomNumbers);

// Display the contents of the array topTen ;

printArray(topTen);

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

// Ask the user for 2 lucky numbers.

System.out.println("Enter 2 lucky numbers between 1 and 10: ");

byte num1 = input.nextByte();

byte num2 = input.nextByte();

// Call checkLuckyNumbers() to check if the user's numbers appear

// next to each other on the array topTen, or on the array numbers.

if (checkLuckyNumbers(topTen, num1, num2))

System.out.println("Top Ten Winner!");

else if (checkLuckyNumbers(randomNumbers, num1, num2))

System.out.println("Winner!");

else

System.out.println("Sorry, not a winner.");

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

// Let's now get an array with single-digit numbers only.

// Call replaceNumbers(), passing the min and max values.

// Any numbers out of range will be replaced with 0.

System.out.println("Removing double-digit numbers from the

list...");

int[] singleDigits = replaceNumbers(randomNumbers, 0, 9);

// Print the contents of this new array.

printArray(singleDigits);

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

// Print the original array of random numbers

System.out.println("The original list again...");

printArray(randomNumbers);

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

// Populate an array frequencies with the total number of times

// each digit (from 0 to 9) appears in the array singleDigits.

int[] frequencies = countDigits(singleDigits);

// Print the array with the counts (frequencies)

System.out.println("The number of times each digit appears is: ");

printArray(frequencies);

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

// Prints a graph using asterisks (to display the frequency

distribution)

printGraph(frequencies);

} // End of main method

// ---- Other Methods ----

// populateArray takes an array of integers as a parameter, and

// populates this array with random numbers (between 1 and 15).

public static void populateArray(int[] numbers) {

for (int i = 0; i

numbers[i] = (int)(Math.random() * 15) + 1;

}

} // end populateArray

// printArray takes an array of integers as a parameter and

// displays each element in this array, separated by spaces.

public static void printArray(int[] numbers) {

for (int i = 0; i

System.out.print(numbers[i] + " ");

}

} // end printArray

// getTopTen takes an array and returns a new array with only

// the first 10 elements in the given array.

public static int[] getTopTen(int[] numbers) {

/* Do not return the array "numbers"...

this is just a placeholder (so that the program compiles).

Return your new array that has only 10 numbers. */

return numbers; //

} // end getTopTen

/* checkLuckyNumbers searches the given array

for the occurrence of the 2 given numbers.

Returns true if it finds the 2 numbers next to each other in the

array. */

public static boolean checkLuckyNumbers(int[] numbers, byte num1, byte

num2) {

boolean found = false;

return found;

} // end checkLuckyNumbers

/* replaceNumbers takes an array of integers,

and 2 other integers as parameters.

It returns a new array where all numbers maxNumber

have been replaced with 0. */

public static int[] replaceNumbers(int[] numbers, int minNumber, int

maxNumber) {

int[] singleDigits = new int[numbers.length];

return singleDigits;

} // end replaceNumbers

/* countNumbers counts the number of times each digit (0 to 9)

appears in the given array.

Returns a new array with the counts for each digit

in the corresponding array position. */

public static int[] countDigits(int[] numbers) {

int[] frequencies = new int[10];

return frequencies;

} // end countDigits

/* printGraph displays a line of asterisks for each element

in the given array.

The number of asterisks in each line,

is the value stored in that array element. */

public static void printGraph(int[] frequencies) {

} // end printGraph

}

SAMPLE OUTPUTS:

image text in transcribed

jGRASP exec: java A9 This program wi11 create a list of random numbers How many random numbers would you like in your list? 45 2 11 15 13 3 15 4 11 8 7 8 5 14 9 11 3 4 13 8 4 7 15 15 9 11 638751814314 6 15 15 12 14 15 15 5 11 1 2 11 15 1331541187 Enter 2 3 4 winner! lucky numbers between 1 and 10: Removing double-digit numbers from the list 2 0 0 0 3 0 4 0 8 7 8 5 0 9 0 3 4 0 8 4 7 0 0 9 0 6 3 8 7518030600000 0 501 The original list again 2 11 15 13 3 15 4 11 8 7 8 5 14 9 11 3 4 13 8 4 7 15 15 9 11 638751814314 6 15 15 12 14 15 15 5 11 1 The number of times each digit appears is: 0 4 3 3 2 32 jGRASP operation complete. 2 115 1 2 2 0123456789 | |

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions