Question

Write a test program that prompt the user to enter seven numbers, stores them in an...

Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following:
1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array:
public static void sort(ArrayList<Integer>list)
2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header:
public static double sum(ArrayList<Integer>list)
3. Write a method that removes the duplicate numbers (elements) from the ArrayList and displays the remaining numbers (elements), using the following header:
public static void removeDuplicate(ArrayList<Integer>list)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package com.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class ArrayListOps {

   public static void main(String... ar) {
       ArrayList<Integer> myArrayList = new ArrayList<Integer>();
       Scanner scanner = new Scanner(System.in);
       System.out.println("Please enter 7 numbers of your choice : ");
       for (int i = 0; i < 7; i++) {
           myArrayList.add(scanner.nextInt());
       }
       ArrayListOps.sort(myArrayList);
       ArrayListOps.removeDuplicate(myArrayList);
   }

   public static void sort(ArrayList<Integer> list) {

       //you can use directly list.sort() but if you are looking for custom logic below is the code
      
       for (int i = 0; i < list.size(); ++i) {

           for (int j = i + 1; j < list.size(); ++j) {

               if (list.get(i) > list.get(j)) {

                   int a = list.get(i);
                   list.set(i, list.get(j));
                   list.set(j, a);

               }

           }

       }
       System.out.println("Arranged : "+list);
   }

   public static void removeDuplicate(ArrayList<Integer> list) {
       Set<Integer> tempSetForUniqueValues = new HashSet<Integer>();
       for(Integer temp : list)
       {
           tempSetForUniqueValues.add(temp);
       }
       list.clear();
       list.addAll(tempSetForUniqueValues);
       System.out.println("Removed Duplicates : "+list);
   }

}

Add a comment
Know the answer?
Add Answer to:
Write a test program that prompt the user to enter seven numbers, stores them in an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for...

    Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers...

    (Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList < Integer > list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers in their input order separated by exactly one space. Sample Run 1 Enter ten integers: 34 5 3 5 6 4 33 2 2 4 The distinct integers are 34 5 3...

  • Write a static recursive method that removes all occurrences of the element from an array list....

    Write a static recursive method that removes all occurrences of the element from an array list. public static void removeAllOccurrences(ArrayList<Integer> list, Integer element)

  • Write a C program to prompt the user to enter 5 numbers in an array. The...

    Write a C program to prompt the user to enter 5 numbers in an array. The program will then calculate the average number in the array and displays the numbers higher than the average value.( using stdio.h).

  • Assignment: Write a program with each of the following methods. You can assume this program is...

    Assignment: Write a program with each of the following methods. You can assume this program is saved in a file called multiArrays.java. A sample main method and sample output is provided at the end of this section. All arrays created are of integer-type. Write a method to declare, initialize, and populate a two-dimensional array. Using the following header:                         public static int[ ][ ] declare2D(Scanner scnr) The user will indicate the size of the array. Write a method to declare,...

  • 1. Write a recursive function that computes the sum of all numbers from 1 to n,...

    1. Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. Here is the method header: public static int sum (int n){...} 2. Write a recursive function that finds and returns the minimum value in an array, where the array and its size are given as parameters. Here is the method header: public static int minValue (int [] data, int size){...} 3. Write a recursive function that reverses the...

  • (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list)...

    (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects The language must be written, compiled, and run on TEXTPAD. The Language is in Java.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT