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...
(Java) Building a Doubly Linked List off of an interface I am having trouble with these...
(Java) Building a Doubly Linked List off of an interface I am having trouble with these two methods @Override public void add(T newEntry) { DoubleLinkedNode newNode = new DoubleLinkedNode(newEntry); if (!isEmpty()) { } if (isEmpty()) { first = newNode; last = newNode; } else { last.setNextNode(newNode); newNode.setPreviousNode(last); last = newNode; } // length++; numElements++; } @Override public void add(int newPosition, T newEntry) { DoubleLinkedNode newAdder = new DoubleLinkedNode(newEntry); if ((newPosition >= 0) && (newPosition <= numElements)) { numElements++; if (newPosition...
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):               ...
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)...
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:   ...
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 just having a hard time understanding this question. The question is below. I...
Hello, I am just having a hard time understanding this question. The question is below. I am not supposed to write a program but instead in regular english sentence form just name the methods, fields, and variables I would use for this loan class. Im not really sure what a loan class means as well, thank you for your help. USING JAVA Given a Loan class, name the necessary fields and methods that we would need in this class. Be...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT