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
  • 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...

  • //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.

  • 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...

  • write a c++ program: User enter in 5 numbers, stores  them in an array then displays them...

    write a c++ program: User enter in 5 numbers, stores  them in an array then displays them from least -> greatest. Then create a char array the user will enter some letters, it gives a count on every 'a' then it gets replaced by an uppercase 'A'. Display the result. Then display only the last letter.

  • 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)

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • 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...

  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • Questions Write a method that returns the union of two array lists of integers using the...

    Questions Write a method that returns the union of two array lists of integers using the following header: public static ArrayList<Integer> union(ArrayList<Integer> list1, ArrayList<Integer> list2) Write a test program that: 1. prompts the user to enter two lists, each with five integers, 2. displays their union numbers separated by exactly one space., and 3. finds the maximum number and the minimum number in the combined list. Here is a sample run of the program. Enter five integers for list1: 5...

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
ADVERTISEMENT