Question

In: Computer Science

Q1-      Write a program that takes a list of values as an input from the user....

Q1-      Write a program that takes a list of values as an input from the user. The program should further ask the user about sorting the list in ascending or descending order. It is desirable to use Arraylist/Vector in place of simple arrays. (Object Oriented Programming java)

Solutions

Expert Solution

I have implemented a java program which read the integers from the user untill the user does not enter -999. Then after program will ask the user in which order he/she want to sort the entered numbers that are stored in array list.

We can sort the array list using inbuild class with its method.

Collection.sort(ArrayList<T> list) :- This is static method which sort the array in ascending order

Collection.sort(ArrayList<T> list, Collections.reverseOrder()) :- This is static method which sort the array in descending order by passing the extra parameter which call the reverseOrder() method and list will be sort into the descending order.

Below program also sort the arraylist using the selection sort and inbuild methods. Program will also ask the user in which methods you want to sort the array list.

TestSorting.java file :-

This program contains belo two methods:-

1> public static void sortAscendingOrder(ArrayList<Integer> list):- This method sort the array list in ascending order using selection sort algorithm.When user select this method from the main() method then after this method called.

2> public static void sortDescendingOrder(ArrayList<Integer> list):- This method sort the array list in descending order using selection sort algorithm.When user select this method from the main() method then after this method called.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class TestSorting {
    
    /**
     * This method sorts the list in descending order using selection sort
     * @param list 
     */
    public static void sortAscendingOrder(ArrayList<Integer> list){
        
        // sort the array list using selection sort
        for(int i=0; i<list.size(); i++){
            
            for(int j=i+1; j<list.size(); j++){
                
                // compare each value in the list and swap it
                if(list.get(i) > list.get(j)){
                    
                    // swap two values
                    int temp = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, temp);
                }
            }
        }
    }
    
    /**
     * This method sorts the list in descending order using selection sort
     * @param list 
     */
    public static void sortDescendingOrder(ArrayList<Integer> list){
        
        // sort the array list using selection sort
        for(int i=0; i<list.size(); i++){
            
            for(int j=i+1; j<list.size(); j++){
                
                // compare each value in the list and swap it
                if(list.get(i) < list.get(j)){
                    
                    // swap two values
                    int temp = list.get(i);
                    list.set(i, list.get(j));
                    list.set(j, temp);
                }
            }
        }
    }
    
    
    public static void main(String[] args) {
        
        // create an object of Scanner class
        Scanner sc = new Scanner(System.in);
        
        // create arrayList which stores the integer values
        ArrayList<Integer> list = new ArrayList<>();
        
        // get the numbers from the user
        int i = 1;
        System.out.print("Enter number "+i+" or -999 to stop: ");
        int number = sc.nextInt();
        
        // user enters number till the user does not enter -999
        while(number != -999){
            
            // add the numbers into the arrayList
            list.add(number);
            
            // increment i
            i++;
            
            System.out.print("Enter number "+i+" or -999 to stop: ");
            number = sc.nextInt();
        }
        
        sc.nextLine();
        
        // now ask the user for sorting
        System.out.println("Do you want numbers sort in ascending or descending order(asc/des)? ");
        String sortAction = sc.nextLine();
        
        // if user want to sort array list in ascending order
        if(sortAction.compareToIgnoreCase("asc") == 0){
            
            /*
                sort the array list in ascending order
                using Collections.sort() or without
            */
            System.out.println("1. Without Collections.sort()");
            System.out.println("2. Using Collections.sort()");
            System.out.print("please enter chooice: ");
            int ch = sc.nextInt();
            
            // when user select 1
            if(ch == 1){
                
                // call the static method which use selection sort
                sortAscendingOrder(list);
                
                // now display the array list using toString() method
                System.out.println("List: "+list.toString());
                
            }
            
            // when user select 2
            else if(ch == 2){
                
                // using collections.sort() in ascending order
                Collections.sort(list);
                
                // now display the array list using toString() method
                System.out.println("List: "+list.toString());
            }
            
            else{
                System.out.println("Invalid Choice");
            }
        }
        
        // else if user want to sort array list in descending order
        else if(sortAction.compareToIgnoreCase("des") == 0){
            
            // sort the array list in descending order
            System.out.println("1. Without Collections.sort()");
            System.out.println("2. Using Collections.sort()");
            System.out.print("please enter chooice: ");
            int ch = sc.nextInt();
            
            // when user enters 1
            if(ch == 1){
                
                // call the static method which use selection sort
                sortDescendingOrder(list);
                
                // now display the array list using toString() method
                System.out.println("List: "+list.toString());
            }
            
            // when user enters 2
            else if(ch == 2){
                
                // using collections.sort() in descending order
                Collections.sort(list, Collections.reverseOrder());
                
                // now display the array list using toString() method
                System.out.println("List: "+list.toString());
            }
            
            else{
                System.out.println("Invalid Choice");
            }
            
            
        }else{
            System.out.println("Invalid Choice");
        }
        
        
        
        
    }
}

Output:-

1> When user want to sort ascending order using selection sort.

2> When user want to sort ascending order using Collections.sort(list).

3> When user want to sort descending order using selection sort.

4> When user want to sort descending order using Collections.sort(list).

I hope you will understand the above program.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

write a program that takes two input values from the user and calculate the division if...
write a program that takes two input values from the user and calculate the division if the first number is greater than the second one otherwise calculate the multiplication.
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
write a program that takes the input value from the user and calculate the sum from...
write a program that takes the input value from the user and calculate the sum from that number to zero in MIPS
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the numbers in values (i.e., the numbers raised to the third power). This version of the function may not use recursion.
Python Program 1: Write a program that takes user input in the form of a string...
Python Program 1: Write a program that takes user input in the form of a string Break up the string into a list of characters Store the characters in a dictionary The key of the dictionary is the character The value of the dictionary is the count of times the letter appears Hint: remember to initialize each key before using Output of program is the count of each character/letter Sort the output by key (which is the character) Python Program...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
Write a Java program that uses printf, and takes user input to find the name of...
Write a Java program that uses printf, and takes user input to find the name of the File. FileCompare (20) Write a program that compares to files line by line, and counts the number of lines that are different. You can use file1.txt and file2.txt when testing your program, but you must ask for the file names in the input. main Interactively requests the names of the two files, after creating the Scanner for the keyboard. Creates a Scanner for...
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT