Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java Write a loop to keep interacting with the user until they ask to get out (-

ID: 3695585 • Letter: J

Question

java Write a loop to keep interacting with the user until they ask to get out (-1) In that loop: 1. Display the current vertex 2. display the possible neighbors from the current vertex 3. ask the user for their choice of a vertex to go to 4. if their choice is -1 then exit 5. if the user enters a value too large or too small report an out of range error 6. if their choice is a valid neighbor then change the current vertex to that choice 7. if their choice is not a valid neighbor then report the vertex they entered is unreachable 8. assume user will enter an int

Explanation / Answer

NeighbourVertex.java

package org.students1;

import java.util.Scanner;

public class NeighbourVertex {

   public static void main(String[] args) {
       int vertex1 = 1;
       char ch = 'A';
       int select=0;
       Scanner sc = new Scanner(System.in);
       System.out.println("The current Vertex is:" + ch);
       while (vertex1 != -1) {

           switch(vertex1){
           case 1:
               System.out.println("The Possible Neighbours for the Vertex "
                       + ch + " are:");
               System.out.println(" 'B','D','E','F'");
               System.out.println("1 . B");
               System.out.println("2 . D");
               System.out.println("3 . E");
               System.out.println("4 . F");
               System.out.print("Choose ::");
               select=sc.nextInt();
                   if (select == 1) {
                       vertex1 = 2;
                       ch = 'B';
                       continue;
                   } else if (select == 2) {
                       vertex1 = 4;
                       ch = 'D';
                       continue;
                   } else if (select == 3) {
                       vertex1 = 5;
                       ch = 'E';
                       continue;
                   } else if (select == 4) {
                       vertex1 = 6;
                       ch = 'F';
                       continue;
                   }
                   else if (select == -1) {
                       System.out.println("::Program Exit ::");
                   System.exit(1);
                       }
                   else
                   {
                       System.out.println(":: Out of Range Error ::");
                       continue;
                   }

              
           case 2:
                   System.out
                           .println("The Possible Neighbours for the Vertex "
                                   + ch + " are:");
                   System.out.println(" 'A','D','E','F'");
                   System.out.println("1 . A");
                   System.out.println("2 . D");
                   System.out.println("3 . E");
                   System.out.println("4 . F");
                   System.out.print("Choose ::");
                   select=sc.nextInt();
                   if (select == 1) {
                       vertex1 = 1;
                       ch = 'A';
                       continue;
                   } else if (select == 2) {
                       vertex1 = 4;
                       ch = 'D';
                       continue;
                   } else if (select == 3) {
                       vertex1 = 5;
                       ch = 'E';
                       continue;
                   } else if (select == 4) {
                       vertex1 = 6;
                       ch = 'F';
                       continue;
                   }
                   else if (select == -1) {
                       System.out.println("::Program Exit ::");
                   System.exit(1);
                       }
                   else
                   {
                       System.out.println(":: Out of Range Error ::");
                       continue;
                   }
           case 4:
                   System.out
                           .println("The Possible Neighbours for the Vertex "
                                   + ch + " are:");
                   System.out.println(" 'A','B','C','F'");
                   System.out.println("1 . A");
                   System.out.println("2 . B");
                   System.out.println("3 . C");
                   System.out.println("4 . F");
                   System.out.print("Choose ::");
                   select=sc.nextInt();
                   if (select == 1) {
                       vertex1 = 1;
                       ch = 'A';
                       continue;
                   } else if (select == 2) {
                       vertex1 = 2;
                       ch = 'B';
                       continue;
                   } else if (select == 3) {
                       vertex1 = 3;
                       ch = 'C';
                       continue;
                   } else if (select == 4) {
                       vertex1 = 6;
                       ch = 'F';
                       continue;
                   }
                   else if (select == -1) {
                       System.out.println("::Program Exit ::");
                   System.exit(1);
                       }
                   else
                   {
                       System.out.println(":: Out of Range Error ::");
                       continue;
                   }
           case 5:
                   System.out
                           .println("The Possible Neighbours for the Vertex "
                                   + ch + " are:");
                   System.out.println(" 'A','B','F'");
                   System.out.println("1 . A");
                   System.out.println("2 . B");

                   System.out.println("3 . F");
                   System.out.print("Choose ::");
                   select=sc.nextInt();
                   if (select == 1) {
                       vertex1 = 1;
                       ch = 'A';
                       continue;
                   } else if (select == 2) {
                       vertex1 = 2;
                       ch = 'B';
                       continue;
                   }

                   else if (select == 3) {
                       vertex1 = 6;
                       ch = 'F';
                       continue;
                   }
                   else if (select == -1) {
                       System.out.println("::Program Exit ::");
                   System.exit(1);
                       }
                   else
                   {
                       System.out.println(":: Out of Range Error ::");
                       continue;
                   }
           case 6:
                   System.out
                           .println("The Possible Neighbours for the Vertex "
                                   + ch + " are:");
                   System.out.println("'A', 'B','D','E'");
                   System.out.println("1 . A");
                   System.out.println("2 . B");
                   System.out.println("3 . D");
                   System.out.println("4 . E");
                   System.out.print("Choose ::");
                   select=sc.nextInt();
                   if (select == 1) {
                       vertex1 = 1;
                       ch = 'A';
                       continue;
                   } else if (select == 2) {
                       vertex1 = 2;
                       ch = 'B';
                       continue;
                   } else if (select == 3) {
                       vertex1 = 4;
                       ch = 'D';
                       continue;
                   } else if (select == 4) {
                       vertex1 = 5;
                       ch = 'E';
                       continue;
                   }
                   else if (select == -1) {
                       System.out.println("::Program Exit ::");
                   System.exit(1);
                       }
                   else
                   {
                       System.out.println(":: Out of Range Error ::");
                       continue;
                   }

           case 3:
                   System.out
                           .println("The Possible Neighbours for the Vertex "
                                   + ch + " are:");
                  

                   System.out.println("1 . B");
                   System.out.println("2 . D");
                   System.out.print("Choose ::");
                   select=sc.nextInt();

                   if (select == 1) {
                       vertex1 = 2;
                       ch = 'B';
                       continue;
                   } else if (select == 2) {
                       vertex1 = 4;
                       ch = 'D';
                       continue;
                   }
                   else if (select == -1) {
                       System.out.println("::Program Exit ::");
                   System.exit(1);
                       }
                   else
                   {
                       System.out.println(":: Out of Range Error ::");
                       continue;
                   }

               }
          
          

       }

   }

}

______________________________________________________________________________________________

output:

The current Vertex is:A
The Possible Neighbours for the Vertex A are:
'B','D','E','F'
1 . B
2 . D
3 . E
4 . F
Choose ::1
The Possible Neighbours for the Vertex B are:
'A','D','E','F'
1 . A
2 . D
3 . E
4 . F
Choose ::2
The Possible Neighbours for the Vertex D are:
'A','B','C','F'
1 . A
2 . B
3 . C
4 . F
Choose ::3
The Possible Neighbours for the Vertex C are:
1 . B
2 . D
Choose ::7
:: Out of Range Error ::
The Possible Neighbours for the Vertex C are:
1 . B
2 . D
Choose ::2
The Possible Neighbours for the Vertex D are:
'A','B','C','F'
1 . A
2 . B
3 . C
4 . F
Choose ::4
The Possible Neighbours for the Vertex F are:
'A', 'B','D','E'
1 . A
2 . B
3 . D
4 . E
Choose ::7
:: Out of Range Error ::
The Possible Neighbours for the Vertex F are:
'A', 'B','D','E'
1 . A
2 . B
3 . D
4 . E
Choose ::4
The Possible Neighbours for the Vertex E are:
'A','B','F'
1 . A
2 . B
3 . F
Choose ::-1
::Program Exit ::

____________________________________________________________________________________________