Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSF-Questions HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


badrul hisham
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
1 Answer(s)      3 years and a month ago
Posted in : Java Server Faces Questions


import java.util.LinkedList; import java.util.*; import java.io.*; import java.lang.*;

public class LinkedListv4 {

char question;
Scanner console = new Scanner(System.in);
LinkedList listA = new LinkedList();

public void Switchers(){

            System.out.println("| a. Append a specified elements to the list");
            System.out.println("| b. Add an element at specified index");
            System.out.println("| c. Add a new elements in front of the list");
            System.out.println("| d. Add new elements with a specified value into the list alternately");
            System.out.println("| e. Remove the first element in the list");
            System.out.println("| f. Find an element at a specified position in the list");
            System.out.println("| g. Replace the elements at a specified index");
            System.out.println("| h. Print the list");
            System.out.println("| i. Print the list from a specified index");
            System.out.println("| j. Print the list in reverse order");
            System.out.println("| k. Remove all elements in the list");
            System.out.println("| l. Check for empty list");
            System.out.println("| q. Quit");



            question = console.next().charAt(0);
            System.out.println();



            switch (question) {
              case 'a': AppendElement();

              case 'b': AddElementSpecific();

              case 'c': AddElementFront();

              case 'd': EditExistingList();

              case 'e': RemoveFirstElement();

              case 'f': ViewElements();

              case 'g': ReplaceElement();

              case 'h': PrintList();

              case 'i': PrintListSpecific();

              case 'j': PrintReverseOrder();

              case 'k': ClearAll();

              case 'l': CheckEmptyList();

              case 'q': Quit(); 

              case 'Q': Quit(); break;

              default: System.out.println("Invalid Input"); 
                        System.out.println();
                        Switchers(); break;
}

}

            //1 --- DONE
            public void AppendElement() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Create/Store objects in an LinkedList container.                    |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    System.out.println("Please set maximum elements required");
                    System.out.println();
                    int Max = console.nextInt();

                    for (int i = 0; i < Max; i++) {

                        System.out.println("Please insert number for element "+ i);
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(new Double(element));
                    }

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());
                            }

                            Switchers();

            }

            //2 --- DONE FIXED - ???
            public void AddElementSpecific() {



                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Create/Store objects in specified  LinkedList container.            |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    System.out.println("Please insert specified index :");
                    int index = console.nextInt();

                        System.out.println("Please insert number for element  :");
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(index,new Double(element));

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());


                            }

                            Switchers();

                }

            //3 --- DONE
            public void AddElementFront() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Create/Store objects in front of LinkedList container.              |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                        int i= 0;
                        System.out.println("System will store element to the first index, Insert new element please  :");
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(0,new Double(element));


                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());


                            }

                            Switchers();
                }

            //4 --- DONE - ???
            public void EditExistingList() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Add new elements with a specified value into the list alternately   |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    Iterator IteInitial = listA.iterator();
                            while (IteInitial.hasNext()) {
                                System.out.println(IteInitial.next());
                            }

                    System.out.println("Please choose desirable index to modify elements");
                    System.out.println();
                    int SpecIndex = console.nextInt();
                    listA.remove(SpecIndex);

                        System.out.println("Please insert new number for element "+ SpecIndex);
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(SpecIndex,new Double(element));

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());
                            }

                            Switchers();                        

                }

            //5 --- DONE
            public void RemoveFirstElement() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("|      Remove fisrt element in the LinkedList container.              |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    Object object = listA.removeFirst();
                    System.out.println(object + " has been removed from the first index of LinkedList");

                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    Iterator Ite = listA.iterator();
                    while (Ite.hasNext()) {
                    System.out.println(Ite.next());


                            }

                            Switchers();

                }

            //6 --- DONE
            public void ViewElements() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Find an element at a specified position in the list                 |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    Iterator Ite = listA.iterator();
                    while (Ite.hasNext()) {
                    System.out.println(Ite.next());
                    }

                    System.out.println("Select any element in the list to review index position :");
                    double element = console.nextDouble();
                    System.out.println();

                    int NotNegOne = listA.indexOf(element);

                    if (NotNegOne != -1) {
                      System.out.println("element "+element+" is at index "+listA.indexOf(element));
                    } 

                    else {
                      System.out.println("LinkedList does not contain "+element);
                    }

                    Switchers();

                }


            //7 --- DONE
            public void ReplaceElement() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Replace the elements at a specified index                           |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();
                    Iterator IteInitial = listA.iterator();
                            while (IteInitial.hasNext()) {
                                System.out.println(IteInitial.next());
                            }

                    System.out.println();
                    System.out.println("Please choose desirable index to modify elements");
                    System.out.println();
                    int SpecIndex = console.nextInt();
                    listA.remove(SpecIndex);

                        System.out.println("Please insert new number for index "+ SpecIndex);
                        double element = console.nextDouble();
                        System.out.println("  - Storing Number(" + element + ")");
                        listA.add(SpecIndex,new Double(element));

                        System.out.println();
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println("| Retrieve objects in an LinkedList container using an Iterator.      |");
                        System.out.println("+---------------------------------------------------------------------+");
                        System.out.println();

                        Iterator Ite = listA.iterator();
                            while (Ite.hasNext()) {
                                System.out.println(Ite.next());
                            }

                    Switchers();

                }

            //8 --- DONE
            public void PrintList() {

                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("|      Print List in the LinkedList container.                    |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    if (listA.isEmpty()){
                    System.out.println("Linked list is empty");
                    }
                    else{
                     System.out.println (listA);
                    }

                    Switchers();

                }

            //9 --- DONE
            public void PrintListSpecific() {

                                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("|      Print List from specified index in the LinkedList container.   |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    try {

                    int indexSize = listA.size();
                    int realIndexSize = indexSize - 1;
                    System.out.println("Total Index in LinkedList (counting from O) "+realIndexSize);
                    System.out.println();
                    System.out.println();
                    System.out.println("Please choose desirable index to view its element");
                    System.out.println();
                    int SpecIndex = console.nextInt();
                    System.out.println();
                    System.out.println("The element is "+listA.get(SpecIndex)+" at index "+SpecIndex);

                    }

                    catch ( Exception e )  {
                        System.out.println();
                        System.out.println("LinkedList does not contain index requested");  
                    }


                    Switchers();

                }

            //10 --- DONE
            public void PrintReverseOrder() {

                    System.out.println();
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println("| Reverse the list in the LinkedList container.                       |");
                    System.out.println("+---------------------------------------------------------------------+");
                    System.out.println();

                    ListIterator itr = listA.listIterator();

                    System.out.println("Iterating through elements of Java LinkedList using ListIterator in forward direction...");
                    while(itr.hasNext())
                    {
                    System.out.println(itr.next());
                    }

                    System.out.println("Iterating through elements of Java LinkedList using ListIterator in reverse direction...");
                    while(itr.hasPrevious())
                    {
                    System.out.println(itr.previous());
                    }


            Switchers();

                }

            //11 --- DONE
            public void ClearAll() {

                System.out.println("+---------------------------------------------------------------------+");
                System.out.println("|      Clear entire LinkedList                                        |");
                System.out.println("+---------------------------------------------------------------------+");
                System.out.println();

                listA.clear();
                System.out.println("All element has been removed");
                System.out.println();
                System.out.println();

                if (listA.isEmpty()){
                    System.out.println("Linked list is empty");
                    }
                    else{
                     System.out.println (listA);
                    }

                Switchers();

                }

            //12 --- DONE
            public void CheckEmptyList() {

                if (listA.isEmpty()){
                    System.out.println();
                    System.out.println("Linked list is empty");
                    System.out.println();
                    }
                    else{
                     System.out.println (listA);
                    }

                Switchers();

                }

            //13 ---DONE
            public void Quit() {

                System.exit(0);
                }


public static void main (String args[]) {

    LinkedListv4 MYlinkList = new LinkedListv4();
    MYlinkList.Switchers();
}

}


Advertisement
View Answers

September 19, 2011 at 1:04 PM


Do modifications:

public void Switchers(){
              boolean exit=false;
              do{
            System.out.println("| a. Append a specified elements to the list");
            System.out.println("| b. Add an element at specified index");
            System.out.println("| c. Add a new elements in front of the list");
            System.out.println("| d. Add new elements with a specified value into the list alternately");
            System.out.println("| e. Remove the first element in the list");
            System.out.println("| f. Find an element at a specified position in the list");
            System.out.println("| g. Replace the elements at a specified index");
            System.out.println("| h. Print the list");
            System.out.println("| i. Print the list from a specified index");
            System.out.println("| j. Print the list in reverse order");
            System.out.println("| k. Remove all elements in the list");
            System.out.println("| l. Check for empty list");
            System.out.println("| q. Quit");


            System.out.print("Enter your choice: ");
            question = console.next().charAt(0);
            System.out.println();

            if(question=='a'){
                 AppendElement();
            }

            else if(question=='b'){
                 AddElementSpecific();
            }

            else if(question=='c'){
                 AddElementFront();
             }
            else if(question=='d'){
                 EditExistingList();
             }
            else if(question=='e'){
                 RemoveFirstElement();
             }
            else if(question=='f'){
                 ViewElements();
            }
            else if(question=='g'){
                 ReplaceElement();
            }
            else if(question=='h'){
                 PrintList();
            }
            else if(question=='i'){
                 PrintListSpecific();
            }  
            else if(question=='j'){
                 PrintReverseOrder();
            }
            else if(question=='k'){
                 ClearAll();
            }
            else if(question=='l'){
                 CheckEmptyList();
            }
            else if(question=='q'){
                 Quit();
                 exit=true;
            }
            else if(question=='Q'){
                 Quit();
                 exit=true;
            }

          }
               while(!exit);

}


Related Tutorials/Questions & Answers:
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
Advertisements
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO FOR THIS CODING
HOW TO I CHANGE THE SWITCH TO IF ELSE OR DO WHILE OR WHILE DO.... Print the list"); System.out.println("| i. Print the list from..."); System.out.println("| q. Quit"); question = console.next().charAt(0
Use if, if else,for,while,do wile ad switch in same Java Class
Use if, if else,for,while,do wile ad switch in same Java Class  hi how to use if, if else,for,while,do wile ad switch in same class? very urgent... "); System.out.println("5. Exit"); boolean quit = false; do{ System.out.print("Please
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?
How do I change the while loop in this code to the range with range list style display page for a resultSet() in jsp?  this is count record code...); while (rs.next()) { rows =rs.getInt(1); %> <
The while and do
While and do-while      ...-while statement now. Here is the syntax: do { statement(s) } while (expression); Lets tweak an example of do-while loop
while and do while
while and do while  hello, What is the difference between a while statement and a do statement?   hello, A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do
While and do-while
While and do-while      ... of nested classes i)  Static classes ii) Inner classes ( Non-static... { ... } class InnerClass { ... } } i) Static Nested Classes
php do while syntax
php do while syntax  How to create a do while loop in php. What is the syntax
do-while loop
do-while loop  how many times will the following loop get executed and what will be the final value of the variable I after execution the loop is over. int I = 5; do{ I + = 3; System.out.println(""+I); I=I+1; }while(I>=9
php do while loop
php do while loop  Difference between do while loop and simple loop in PHP
php do while example
php do while example  Simple example of do while loop in PHP
php do while break
php do while break   receiving a Fatal saying cannot break/continue.. please suggest. Thank U
php do while false
php do while false   Is there any difference between FALSE and false
while & do..while Loop
while & do..while Loop while loop The while loop iterate until provided... : Value of j 1Value of j 2Value of j 3 The do...while statement The do-while... : do   {   code to be executed;   } while (condition
programes on do... while
programes on do... while   write a program to print reverse...; for (int i = 0; i <= num; i++) { int r = num % 10...; i = 0; } return reversedNumber
How do i do the coding for 'leaving a comment' in java
How do i do the coding for 'leaving a comment' in java  i am designing a webpage.In my webpage i want to add the option of adding a comment by the readers of the page.how do i do
Do-while loop in Java
Do-while loop in Java In this section you will learn about do-while loop in Java. do-while loop is similar to while loop but the difference is do-while loop... condition repeatedly and run at least once. For that we need a do-while loop so
do-while Loop in Java
; while(condition); In this example you will see how to use the do-while loop... do-while Loop in Java      ... and processes once at least. For that you have to use the do while loop in  after
Do While Loop in Java
); In this example you will see how to use the do-while loop statement. The values...Do while loop in Java There are three loops most commonly used in Java e.g..... Do while loop is java is slightly different with While loop as in While loop
Class.forName will do while loading drivers
Class.forName will do while loading drivers  What Class.forName will do while loading drivers
PHP Do While Loop
Do-While Control Structure: Do-While loop is another type of loop which runs... within. We generally use Do-While loop  when the iteration must run at least once despite of the validity of the variable. In Do-While loop we need
PHP Do While Loop
of the variable within the loop. PHP do while loop Example: <?php $i=34; do... of $i is not valid but do-while loop runs at least one time despite...Do-While Loop Function PHP: PHP Do-While loop method is another type of loop
While and do-while
While and do-while      ... at do-while statement now. Here is the syntax: do { statement(s) } while (expression); Lets tweak an example of do-while loop
do while loop
do while loop       In java a do while loop is a control flow statement that allows a certain code... with a condition associated with the while keyword. The code inside the do construct
Do..While Loops
3.10.2. Do…While Loops The do...while statement always execute... the condition is true. Syntax do { code to be executed; } while (condition); e.g.... reaches to either 5 or less than 5. <?php $i=1; do { $i++; echo &ldquo
I want to change my user name on your website ,how can i do this
I want to change my user name on your website ,how can i do this  I want to change my user name on your website ,how can i do this   You can create another account very easily with different username
How do I upgrade mysql?
How do I upgrade mysql?  How do I upgrade mysql
how do i solve this problem?
how do i solve this problem?  Define a class named Circle... the Circle object is data fields. A public method named changeColour(String) to change... objects. The first object must be created using the default constructor, while
how do i solve this question?
how do i solve this question?  1.Define a class named Circle... changeColour(String) to change the value of the Circle objectââ?¬â?¢s colour... be created using the default constructor, while the second object must be created
 

 

 

DMCA.com