Lab 5

Description:
Create a program to analyze data that first will display a menu asking the user to enter: (1) the data type of the elements (string, double or integer) and how many elements to process. The program should have the following function.Display all the elements in the order they were entered.Display all the elements in the reverse order they were entered. Display all the elements in ascending order.Display the elements only once.

Code



import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Test {
	private static List date=new ArrayList();
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean flag=true;
		Scanner sc=new Scanner(System.in);//scan whatever the user entered
		while(flag){
			System.out.println("1-Enter data-" +
					"\n-2. show data by order-" +
					"\n-3. show data in reverse order-" +
					"\n-4. show data in ascending order-" +
					"\n-5. show only once");
			switch (sc.nextInt()) {//determine whatever value entered
			case 1:
				date.add(sc.next());//add whatever value entered in keyboard
				System.out.println("Data added");
				break;
			case 2:
				showDateByorder();
				break;
			case 3:
				showDateByReverseorder();
				break;
			case 4:
				paixu();//sorting
				break;
			case 5:
				elements_only_once();
				break;
			default:
				System.out.println("Error,please enter again");
				break;
			}
		}
	}

	//show data by order
	public static void showDateByorder(){
		if(date.size()!=0){
			for(int i=0;i=0;i--){
				System.out.print(" ");
				System.out.print(date.get(i));
			}
		}
	}

	/**
	 * Output after sorting
	 */
	public static void paixu(){
		String[] sum =new String[date.size()];//create a string 
		if(date.size()!=0){//
			for(int i=0;i<date.size();i++){
				System.out.print(" ");
				sum[i]=date.get(i);//put value i into array
			}
		}
		Arrays.sort(sum);//sorting
		for(int j=0;j<sum.length;j++){
			System.out.print(" ");
			System.out.println(sum[j]);
		}
	} 

	//show element at once
	public static void elements_only_once (){
		List tempList= new ArrayList(); //create new  array
		for(String i:date){  //
			if(!tempList.contains(i)){  
				tempList.add(i);//add value of i into the new array  
			}  
		}
		for(String i:tempList){ 
			System.out.print(" ");
			System.out.println(i);
			//output i
		}  
	}
}

Screen shot:
2

Leave a Reply

Your email address will not be published. Required fields are marked *