Subject Computer Science Java Programming

Question

1. Write a Java program using the following methods:
A method to fill the array with random numbers: the elements of the array should be random integers between 0 and 100 (declared as class constants).
A method to print the elements of the array in a table like format (6 elements/line - declared as a class constant)
A method to find the maximum element in the array
A method to find the minimum element in the array
Have the size of the array declared as a class constant (30).
The methods should take as input parameters the array and its size.
The method to print should add one more parameter for the number of elements on each line.
Write a driver program to call these methods and display the difference between the maximum and the minimum element in the array.

Sample output:
Do you want to start(Y/N): y
The array elements are:
56 90 14 85 39 34
83 81 79 7 63 72
65 56 52 30 91 34
8 23 91 81 63 100
90 22 26 37 13 90
The difference between the max value 100 and the min value 7 = 93
Do you want to continue(Y/N): y
The array elements are:
29 37 46 94 2 13
7 79 91 41 46 23
61 17 55 75 22 30
28 71 71 2 63 52
8 7 83 64 27 24
The difference between the max value 94 and the min value 2 = 92
Do you want to continue(Y/N): y
The array elements are:
55 48 60 23 43 35
25 54 74 18 78 16
44 30 70 25 6 24
93 42 39 91 1 84
76 40 35 1 17 79
The difference between the max value 93 and the min value 1 = 92
Do you want to continue(Y/N): n

2. Write a Java program to read n positive integers from an array and graph the information in the form of a bar chart or a histogram - each number is printed, and then a number consisting of that many asterisks is printed beside the number.
Have a method to fill the array with random numbers: the elements of the array should be random integers between 1 and 15. Also, have a method to print the array and another method to print the histogram, as in the sample output below.
Work with dynamic arrays (ask the user to input the size of the array) and make sure you validate the input.

Sample output:
Do you want to start(Y/N): y
Enter array size: 12
The array elements are:
8 8 10 6 9 2 8 7 10 12 10 8
Index Value Histogram
---------------------------------
0 8 ********
1 8 ********
2 10 **********
3 6 ******
4 9 *********
5 2 **
6 8 ********
7 7 *******
8 10 **********
9 12 ************
10 10 **********
11 8 ********
Do you want to continue(Y/N): y
Enter array size: 15
The array elements are:
9 6 14 1 5 5 3 14 3 6 12 3 11 9 3
Index Value Histogram
---------------------------------
0 9 *********
1 6 ******
2 14 **************
3 1 *
4 5 *****
5 5 *****
6 3 ***
7 14 **************
8 3 ***
9 6 ******
10 12 ************
11 3 ***
12 11 ***********
13 9 *********
14 3 ***
Do you want to continue(Y/N): y
Enter array size: -7ERROR! Should be positive. REENTER: 7
The array elements are:
10 10 8 13 13 15 10
Index Value Histogram
---------------------------------
0 10 **********
1 10 **********
2 8 ********
3 13 *************
4 13 *************
5 15 ***************
6 10 **********
Do you want to continue(Y/N): n

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public class Problem1 {

private static final int ARRAY_SIZE = 30;

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String choice;
System.out.print("Do you want to start(Y/N): ");
choice = input.next();
if(choice.compareTo("Y") == 0|| choice.compareTo("y") == 0)
while(true)
{
int[] array = fillArray(new int[ARRAY_SIZE], ARRAY_SIZE);
printArray(array, ARRAY_SIZE, 6);
System.out.println("The difference between the max value " + findMax(array, ARRAY_SIZE) + " and the min value " + findMin(array, ARRAY_SIZE) + " = " + (findMax(array, ARRAY_SIZE) - findMin(array, ARRAY_SIZE)));
System.out.print("Do you want to continue(Y/N): ");
choice = input.next();
if(choice.compareTo("N") == 0 || choice.compareTo("n") == 0)
break;
}
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Java Programming: Strings, Odd & Even Numbers, Absolute Values
Homework Solution
$25.00
Java
Programming
Computer Science
Odd Numbers
Even Numbers
Integers
Absolute Values
While Loop
IF-Statements
Data Sets
Input
Output
Sum
Functions
Methods
Strings
Lexicography Order
Java Programming Questions
Homework Solution
$50.00
Java
Programming
Computer Science
Algorithms
Classes
Shapes
Iterations
Information
Sample Runs
Console Application
Mutator Methods
Colors
User Input
Inheritance
Java & UML Programming Problems
Homework Solution
$20.00
Java
UML
Programming
Coding
Computer Science
Fraction
Denominator
Numerator
Big Integers
Printing Outputs
Decimal Values
Arithmetic Operations
Java Programming: Stack & Queue
Homework Solution
$40.00
Programming
Java
Coding
Computer Science
Data Sets
Stack
Queue
Matrix
Circular Array
Algorithm
Duplicates
Sequences
Test Files
Random Functions
Mathematicians
Josephus Problem
Statements
Variables
Get help from a qualified tutor
Live Chats