Question

In: Computer Science

Hello, I need to convert this java array into an array list as I am having...

Hello, I need to convert this java array into an array list as I am having trouble please.

import java.util.Random;
import java.util.Scanner;

public class TestCode {
    public static void main(String[] args) {
        String choice = "Yes";
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int[] data = new int[1000];
        int count = 0;
        while (!choice.equals("No")) {
            int randomInt = 2 * (random.nextInt(5) + 1);
            System.out.println(randomInt);
            data[count++] = randomInt;
            System.out.print("Want another random number (Yes / No)? ");
            choice = scanner.next();
        }
        int min = data[0], max = data[0];
        for (int i = 0; i < count; i++) {
            if (data[i] > max) max = data[i];
            if (data[i] < min) min = data[i];
        }
        System.out.println("Percentage of Yes values is " + (((count-1)*100.0)/count));
        System.out.println("Maximum value is " + max);
        System.out.println("Minimum value is " + min);
    }
}

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

import java.util.*;

public class TestCode {
public static void main(String[] args) {
String choice = "Yes";
Random random = new Random();
Scanner scanner = new Scanner(System.in);
  
//declare an arraylist here
ArrayList<Integer> data = new ArrayList<Integer>();
int count = 0;
while (!choice.equals("No")) {
int randomInt = 2 * (random.nextInt(5) + 1);
System.out.println(randomInt);
//change here add() method
data.add(randomInt);
count++;
System.out.print("Want another random number (Yes / No)? ");
choice = scanner.next();
}
//NOT NEEDED
// int min = data[0], max = data[0];
// for (int i = 0; i < count; i++) {
// if (data[i] > max) max = data[i];
// if (data[i] < min) min = data[i];
// }
  
//USE THIS
int min=Collections.min(data);
int max=Collections.max(data);
  
System.out.println("Percentage of Yes values is " + (((count-1)*100.0)/count));
System.out.println("Maximum value is " + max);
System.out.println("Minimum value is " + min);
}
}

==========================

SCREENSHOT:

OUTPUT:


Related Solutions

JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
Convert arrays.py to Java. ‘’’’’’ File: arrays.py An Array is like a list, but the client...
Convert arrays.py to Java. ‘’’’’’ File: arrays.py An Array is like a list, but the client can use Only [], len, iter, and str. To instantiate, use <variable> = Array(<capacity>, <optional fill value>) The fill value is None by default. ‘’’’’’ class Array(object): ‘’’’’’Represents an array.’’’’’’ def__init__(self, capacity, fillValue = None):                ‘’’’’’Capacity is the static size of the array.                fillValue is place at each position.’’’’’’                self.items = list()                for count in range(capacity):                self.items.append(fillValue) def__len__(self):               ...
C++ Hello .I need to convert this code into template and then test the template with...
C++ Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance #include using namespace std; class DynamicStringArray {    private:        string *dynamicArray;        int size;    public:   ...
Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I...
Hello, I am having difficulty with this assignment. I chose Amazon as my stock, but I am confused on what they're asking :             Choose a stock that interests you. Utilizing Bloomberg (or other financial websites) as a source of data, collect the following      information:                         a. The stock’s Beta                         b. The rate of return on the market (S&P 500 Index)                         c. The risk-free rate (rRF)                         d. The last dividend paid (D0)...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked...
Java ArrayList Parking Ticket Simulator, Hello I am stuck on this problem where I am asked to calculate the sum of all fines in the policeOfficer class from the arraylist i created. I modified the issueParking ticket method which i bolded at the very end to add each issued Parking ticket to the arrayList, i think thats the right way? if not please let me know. What I dont understand how to do is access the fineAmountInCAD from the arrayList...
Hello i am working on an assignment for my programming course in JAVA. The following is...
Hello i am working on an assignment for my programming course in JAVA. The following is the assignment: In main, first ask the user for their name, and read the name into a String variable. Then, using their name, ask for a temperature in farenheit, and read that value in. Calculate and print the equivalent celsius, with output something like Bob, your 32 degrees farenheit would be 0 degrees celsius Look up the celsius to farenheit conversion if you do...
i need a pseudocode for a program in java that will convert dollars into euros and...
i need a pseudocode for a program in java that will convert dollars into euros and japanese yen using the print and prinln methods an if, if -else, statement a switch statement a while statement utilizes the file class uses the random class and random number generator and uses at least three methods other than main
hello, I am having an issue with a question in my highway engineering course. the question...
hello, I am having an issue with a question in my highway engineering course. the question is: An equal tangent sag vertical curve has an initial grade of –2.5%. It is known that the final grade is positive and that the low point is at elevation 82 m and station 1 + 410.000. The PVT of the curve is at elevation 83.5 m and the design speed of the curve is 60 km/h. Determine the station and elevation of the...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize...
I am working on an accounting assignment and am having problems. Firstly, 1.I need to journalize these entries and post the closing entries 2. i need to prepare Dalhanis multi-step income statement and statement of owners equity for August 2010 3. i need to prepare the blance sheet at august 31,2010 4. i need to prepare a post-closing trial balance at august 31,2010 DALHANI makes all credit sales on terms 2/10 n/30 and uses the Perpetual Inventory System Aug 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT