In: Computer Science
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)
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.