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

I need it in two hours plzzzz. Java Programming (8th Edition) [ test 3 problem ]

ID: 3836645 • Letter: I

Question

I need it in two hours plzzzz.

Java Programming (8th Edition)

[ test 3 problem ]

Write an application that asks a user to enter the course numbers (e.g. 210) and course titles (e.g. Introduction to Computer Programming) of up to 7 courses. Continue to prompt the user for course numbers and course titles until the user enters the sentinel value 666 for a course number or has entered 7 course numbers, whichever comes first. When the user is finished entering course numbers and titles, produce a count of how many course numbers were entered, and then display the course numbers in one line with 2 spaces inbetween. In a loop, continuously ask the user to type one of the course numbers, and display the corresponding course title or an error message if the course number has not been previously entered. The loop continues until the user enters 666 for a course number. Save the application as Nightmares.java.

Explanation / Answer

/*
* The java program that prompts user to enter
* course number and course names until user
* enters 666 to stop. Then print number of
* courses entered. Then prompt user
* to enter course number to find course
* title .
* */
//Nightmares.java
import java.util.Scanner;
public class Nightmares
{
   public static void main(String[] args)
   {
       //Create a Scanner object to read input
       Scanner scanner=new Scanner(System.in);
       //Set size to create an array of course numbers
       //course names
       final int SIZE=7;
       //set counter =0
       int counter=0;
       //set SENTINAL
       final int SENTINAL=666;
      
       //create two arrays
       int[] courseNumbers=new int[SIZE];
       String[] courseTitles=new String[SIZE];
      
       int courseNum;
       String courseName;
      
       System.out.println("Enter course number or 666 to stop");
       //prompt for course number
       courseNum=scanner.nextInt();
       //read enter key and ignore
       scanner.nextLine();
       while(courseNum!=SENTINAL && counter<SIZE)
       {
          
           courseNumbers[counter]=courseNum;
           System.out.println("Enter course title");
           courseName=scanner.nextLine();
          
           courseTitles[counter]=courseName;          
           System.out.println("Enter course number or 666 to stop");
           courseNum=scanner.nextInt();
           //read enter key and ignore
           scanner.nextLine();
          
           counter++;
       }
      
       //print counter
       System.out.println("Number of courses entered are "+counter);
      
       //print course numbers and course titles
       for (int i = 0; i < counter; i++)
       {
           System.out.printf("%-10d%s ",
                   courseNumbers[i],courseTitles[i]);
       }
      
       System.out.println("Enter course number to find :");
       //prompt for course number to search
       int searchCourse=scanner.nextInt();
       boolean found=false;
       int pos=-1;
       //search the course number array to find the pos
       //of corresponding course title
       for (int i = 0; i < counter && !found; i++)
       {
           //check for matching
           if(courseNumbers[i]==searchCourse)
           {
               found=true;
               pos=i;
           }
       }
      
       //check if pos is not -1
       if(pos!=-1)
           System.out.printf("The courset name of %d is %s ",searchCourse,courseTitles[pos]);
       else
           System.out.println("No course title is found for "+searchCourse);
      
      
   }
}


----------------------------------------------------------------------------------------------------------------

Sample Output:

Enter course number or 666 to stop
111
Enter course title
cs111
Enter course number or 666 to stop
222
Enter course title
cs222
Enter course number or 666 to stop
333
Enter course title
cs333
Enter course number or 666 to stop
444
Enter course title
cs444
Enter course number or 666 to stop
666
Number of courses entered are 4
111       cs111
222       cs222
333       cs333
444       cs444
Enter course number to find :
444
The courset name of 444 is cs444