Question

In: Computer Science

It's java file 1. declare an vector without specifying the size 2. use push_back to add...

It's java file

1. declare an vector without specifying the size

2. use push_back to add random integers between 100 and 999 to the vector

3. write a function that returns the smallest, largest, and average of the numbers in the vector

3. display the smallest, largest, and average of the numbers in the vector

Solutions

Expert Solution

Hi, please fallow the important steps for solution of your code.

1. In this we used Math.random() function is used to generate the random numbers based on given minimum and max value.

this function needs to convert to int because by default it was double. So that's reason we used to cast int

2. In vector we can get the element based on the index. Index will start from 0 (Zero)

getIndex(0) will return first value

3. size() nethod returns the total number of elements or size of the vactor.

4. function will return only one value

Please find the code and screen shot of the code along with comments

Java file

-----------------------------


import java.util.Vector;

public class Vector_Random {

    public static void main(String[] args) {

        // vector with out size
        Vector<Integer> userVector = new Vector<>();

        // this line will generate random number between 1 to 5. you may chnage
        int randomNumber = (int) (1 + (Math.random() * 5) );

        // used push_back it will add a random number between 100 and 999 in vector object
        for (int i = 0; i < randomNumber; i++) {

            userVector.add((int) ( 100 + (Math.random() * 999) ));
        }

        //printing vector on the console
        System.out.println(userVector);

        // this function will return average of vector
        System.out.println("Average is : " + getAverageFromVector(userVector));
    }

    private static double getAverageFromVector(Vector<Integer> userVector) {

        // initially we declared avg and total is 0
        double avg =0,total=0;

        // assign first value from the vector to smallest and greatest
        int smallest=userVector.get(0), greatest =userVector.get(0);

        for (int i = 0; i < userVector.size(); i++) {

            // every value will be added to the total
            total+=userVector.get(i);

            // this is the logic and check each value with smallest and highest and store the value based on condition
            if(userVector.get(i) < smallest) {
                smallest = userVector.get(i);
            }

            if(userVector.get(i) > greatest) {
                greatest = userVector.get(i);
            }
        }

        // printing details
        System.out.println("Smallest is : " +smallest);
        System.out.println("greatest is : " +greatest);

        // calculate the avg
        avg = total / userVector.size();

        // return the avg
        return  avg;
    }
}

Snippet code:



import java.util.Vector;

public class Vector_Random {

    public static void main(String[] args) {

        // vector with out size
        Vector<Integer> userVector = new Vector<>();

        // this line will generate random number between 1 to 5. you may chnage
        int randomNumber = (int) (1 + (Math.random() * 5) );

        // used push_back it will add a random number between 100 and 999 in vector object
        for (int i = 0; i < randomNumber; i++) {

            userVector.add((int) ( 100 + (Math.random() * 999) ));
        }

        //printing vector on the console
        System.out.println(userVector);

        // this function will return average of vector
        System.out.println("Average is : " + getAverageFromVector(userVector));
    }

    private static double getAverageFromVector(Vector<Integer> userVector) {

        // initially we declared avg and total is 0
        double avg =0,total=0;

        // assign first value from the vector to smallest and greatest
        int smallest=userVector.get(0), greatest =userVector.get(0);

        for (int i = 0; i < userVector.size(); i++) {

            // every value will be added to the total
            total+=userVector.get(i);

            // this is the logic and check each value with smallest and highest and store the value based on condition
            if(userVector.get(i) < smallest) {
                smallest = userVector.get(i);
            }

            if(userVector.get(i) > greatest) {
                greatest = userVector.get(i);
            }
        }

        // printing details
        System.out.println("Smallest is : " +smallest);
        System.out.println("greatest is : " +greatest);

        // calculate the avg
        avg = total / userVector.size();

        // return the avg
        return  avg;
    }
}

Sample Output:

All the best..


Related Solutions

C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Add a generic Node class to the Java project. In the class Declare the fields using...
Add a generic Node class to the Java project. In the class Declare the fields using the generic type parameter, follow the book specification Define the accessor method getLink( ) Define the constructor, follow the book implementation Note: at the definition the name of the constructor is Node, however every time you use it as a type must postfix the generic type like Node<T> Define the addNodeAfter( ) method as explained in the PP presentation, use the generic type as...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
im trying to write a java code that take a matrix of vector and fine it's...
im trying to write a java code that take a matrix of vector and fine it's inverse (the the inverse in linear algebra) then multiple this matrix with a vector to fine other vector (matrix)-1 × ( vector ) = (vector)
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without using methods from the Rectangle class, return true if the point is inside the rectangle and false otherwise. 2. Write a second method with the same functionality as Exercise 1. However, use a method from the Rectangle class this time. 3. Normally, the == operator cannot be used to compare two strings for equality. There are 2 main exceptions we talked about. The first...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
1. create a .txt file and call it fruits.txt, add the following items to the file...
1. create a .txt file and call it fruits.txt, add the following items to the file Apple Banana Peach Strawberry Watermelon Kiwi Grape _______ 2. Write a program that does the following: - Reads the fruit.txt - Converts the contents into all uppercase - Writes the uppercased values into a text file called "UpperFruit.txt" OUTPUT SHOULD BE: APPLE BANANA PEACH STRAWBERRY WATERMELON KIWI GRAPE
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
A random sample of size n = 2 is chosen without replacement from the set{ 1...
A random sample of size n = 2 is chosen without replacement from the set{ 1 , 2 , 3 } . X = 0 if the first number is even, and X = 1 if the first number is odd. Y = 0 if the second number is even, and Y = 1 if the second number is odd. a) List all of the samples. (b) Find the joint distribution of X and Y. (c) Are X and Y...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the...
IN JAVA!!! In this project, you will use radix.txt as the input file, and output the integers SORTED USING RADIX SORT. You may assume all your input consists of integers <=9999. Your main program will input the integers and put them into a QUEUE. It will then pass this queue to a method called radixSort which will sort the numbers in the queue, passing the sorted queue back to main. The main program will then call another method to print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT