Question

In: Computer Science

In this assessment, you will debug and fix a given Java console application that uses 2...

In this assessment, you will debug and fix a given Java console application that uses 2 dimensional arrays but the application does not compile nor execute. You can use either the Toolwire environment or your local Java development environment to complete this assignment.The application has four bugs. Your assignment is to find these bugs and fix them so that the application meets its stated requirements.The requirements of this application are as follows: The application is register students for courses in a term of study.

The assumptions used by the application are:

Student enters only integers to select courses for registration from a menu. No data type validation of the user menu selection is checked or required.

The program terminates only when the student closes it

The program must follow these registration business rules:

No registration of other courses not displayed by the program

  No registration more than once for the same course

  No registration for more than 9 credit hours (e.g. no more than 3 courses)

Use these course codes, in this order, to test your application:

IT4079

IT1006

IT2230

Please comment throughout code what you used different and explain why you used it or in a paragraph below the code.


package u8a1_2dconsoleregisterforcourse;

import java.util.Scanner;


public class U8A1_2DConsoleRegisterForCourse {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
  
System.out.println("Teacher's Copy");
  
Scanner input = new Scanner(System.in);
  
// courses 2d array hold course code and their credit hours
String[][] courses = {
{"IT1006", "IT4782", "IT4789", "IT4079", "IT2230", "IT3345", "IT2249"},
{"6", "3", "3", "6", "3", "3", "6"}
};
  
// validChoices 2d array holds valid number choices (as strings) selected by user
// and their corresponding courses
//e.g String[][] choices = { {"5", "IT2230"}, {"1", "IT1006"}, {"6", "IT3345"} };
String[][] validChoices = {
{"", ""},
{"", ""},
{"", ""}
};
  
int choice;
int totalCredit = 0;
String yesOrNo = "";

  
do {

choice = getChoice(courses, input);
  
switch (ValidateChoice(choice, validChoices, totalCredit, courses)) {
case -1:
System.out.println("**Invalid** - Your selection of " +
choice + " is not a recognized course.");
break;
case -2:
System.out.println("**Invalid** - You have already registerd for this " +
courses[0][choice-1] + " course.");
break;
case -3:
System.out.println("**Invalid** - You can not register for more than 9 credit hours.");
break;
case 0:
System.out.println("Registration Confirmed for course " +
courses[0][choice-1] );
totalCredit += Integer.parseInt(courses[1][choice-1]);
if (validChoices[0][0].equals("")) {
validChoices[0][0] = Integer.toString(choice);
validChoices[0] = courses[0][choice-1];
} else if (validChoices[1][0].equals("")) {
validChoices[1][0] = Integer.toString(choice);
validChoices[1][1] = courses[0][choice-1];
} else if (validChoices[2][0].equals("")) {
validChoices[2][0] = Integer.toString(choice);
validChoices[2][1] = courses[0][choice-1];
}
break;
}

WriteCurrentRegistration(validChoices, totalCredit);
  
System.out.print("\nDo you want to try again? (Y|N)? : ");
  
yesOrNo = input.next().toUpperCase();
  
} while (yesOrNo.equals("Y"));

System.out.println("Thank you for registering with us");
  
}

//This method prints out the selection menu to the user in the form of
//[selection number]Course Code (Course Credit Hours)
//from the courses array one per line
//and then prompts the use to make a number selection
public static int getChoice(String[] courses, Scanner input) {
System.out.println("Please type the number inside the [] to register for a course");
System.out.println("The number inside the () is the credit hours for the course");
for(int i = 0; i < courses[0].length; i++ )
System.out.println("[" + (i+1) + "]" + courses[0][i] + "(" + courses[1][i] + ")");
System.out.print("Enter your choice : ");
return (input.nextInt());
}

  

//This method validates the user menu selection
//against the given registration business rules
//it returns the following code based on the validation result
// -1 = invalid, unrecognized menu selection
// -2 = invalid, alredy registered for the course
// -3 = invalid, No more than 9 credit hours allowed
// 0 = menu selection is valid
public static int ValidateChoice(int choice, String[][] validChoices, int totalCredit, String[][] courses) {
String choiceAsString = Integer.toString(choice);
if (choice < 1 || choice > 7)
return -1;
else if (choiceAsString.equals(validChoices[0][0])
|| choiceAsString.equals(validChoices[])
|| choiceAsString.equals(validChoices[2][0]))
return -2;
else if ( totalCredit + Integer.parseInt(courses[1][choice-1]) > 9)
return -3;
return 0;
}
  
  
//This method prints the current list of registered courses thus far
//from the courses array separated by , and enclosed inside { }
//It also prints the total credit registered for thus far
public static void WriteCurrentRegistration(String[][] validChoices, int totalCredit) {

if (validChoices[0][0].equals(""))
System.out.println("Current course registration: { none } " );   
else if (validChoices[1][0].equals(""))
System.out.println("Current course registration: { " + validChoices[0][i] + " }" );
else if (validChoices[2][0].equals(""))
System.out.println("Current course registration: { " + validChoices[0][1] +
", " + validChoices[1][1] + " }");
else
System.out.println("Current course registration: { " + validChoices[0][1] +
", " + validChoices[1][1] + ", " + validChoices[2][1] + " }");
System.out.println("Current registration total credit = " + totalCredit);
}
  
}

Solutions

Expert Solution

solution

//corrected code

import java.util.Scanner;

public class U8A1_2DConsoleRegisterForCourse {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

System.out.println("Teacher's Copy");

Scanner input = new Scanner(System.in);

// courses 2d array hold course code and their credit hours

String[][] courses = {

{"IT1006", "IT4782", "IT4789", "IT4079", "IT2230", "IT3345", "IT2249"},

{"6", "3", "3", "6", "3", "3", "6"}

};

// validChoices 2d array holds valid number choices (as strings) selected by user

// and their corresponding courses

//e.g String[][] choices = { {"5", "IT2230"}, {"1", "IT1006"}, {"6", "IT3345"} };

String[][] validChoices = {

{"", ""},

{"", ""},

{"", ""}

};

int choice;

int totalCredit = 0;

String yesOrNo = "";

do {

choice = getChoice(courses, input);

switch (ValidateChoice(choice, validChoices, totalCredit, courses)) {

case -1:

System.out.println("**Invalid** - Your selection of " +

choice + " is not a recognized course.");

break;

case -2:

System.out.println("**Invalid** - You have already registerd for this " +

courses[0][choice-1] + " course.");

break;

case -3:

System.out.println("**Invalid** - You can not register for more than 9 credit hours.");

break;

case 0:

System.out.println("Registration Confirmed for course " +

courses[0][choice-1] );

totalCredit += Integer.parseInt(courses[1][choice-1]);

if (validChoices[0][0].equals("")) {

validChoices[0][0] = Integer.toString(choice);

validChoices[0][1] = courses[0][choice-1];

} else if (validChoices[1][0].equals("")) {

validChoices[1][0] = Integer.toString(choice);

validChoices[1][1] = courses[0][choice-1];

} else if (validChoices[2][0].equals("")) {

validChoices[2][0] = Integer.toString(choice);

validChoices[2][1] = courses[0][choice-1];

}

break;

}

WriteCurrentRegistration(validChoices, totalCredit);

System.out.print("\nDo you want to try again? (Y|N)? : ");

yesOrNo = input.next().toUpperCase();

} while (yesOrNo.equals("Y"));

System.out.println("Thank you for registering with us");

}

//This method prints out the selection menu to the user in the form of

//[selection number]Course Code (Course Credit Hours)

//from the courses array one per line

//and then prompts the use to make a number selection

public static int getChoice(String[][] courses, Scanner input)

{

System.out.println("Please type the number inside the [] to register for a course");

System.out.println("The number inside the () is the credit hours for the course");

for(int i = 0; i < courses[0].length; i++ )

System.out.println("[" + (i+1) + "]" + courses[0][i] + "(" + courses[1][i] + ")");

System.out.print("Enter your choice : ");

return (input.nextInt());

}

//This method validates the user menu selection

//against the given registration business rules

//it returns the following code based on the validation result

// -1 = invalid, unrecognized menu selection

// -2 = invalid, alredy registered for the course

// -3 = invalid, No more than 9 credit hours allowed

// 0 = menu selection is valid

public static int ValidateChoice(int choice, String[][] validChoices, int totalCredit, String[][] courses) {

String choiceAsString = Integer.toString(choice);

if (choice < 1 || choice > 7)

return -1;

else if (choiceAsString.equals(validChoices[0][0])

|| choiceAsString.equals(validChoices[1][0]) //error

|| choiceAsString.equals(validChoices[2][0]))

return -2;

else if ( totalCredit + Integer.parseInt(courses[1][choice-1]) > 9)

return -3;

return 0;

}

//This method prints the current list of registered courses thus far

//from the courses array separated by , and enclosed inside { }

//It also prints the total credit registered for thus far

public static void WriteCurrentRegistration(String[][] validChoices, int totalCredit) {

if (validChoices[0][0].equals(""))

System.out.println("Current course registration: { none } " );  

else if (validChoices[1][0].equals(""))

System.out.println("Current course registration: { " + validChoices[0][1] + " }" );

else if (validChoices[2][0].equals(""))

System.out.println("Current course registration: { " + validChoices[0][1] +

", " + validChoices[1][1] + " }");

else

System.out.println("Current course registration: { " + validChoices[0][1] +

", " + validChoices[1][1] + ", " + validChoices[2][1] + " }");

System.out.println("Current registration total credit = " + totalCredit);

}

}

//output


Related Solutions

In this assessment, you will debug and fix a given Java console application that uses 2...
In this assessment, you will debug and fix a given Java console application that uses 2 dimensional arrays but the application does not compile nor execute. You can use either the Toolwire environment or your local Java development environment to complete this assignment.The application has four bugs. Your assignment is to find these bugs and fix them so that the application meets its stated requirements.The requirements of this application are as follows: The application is register students for courses in...
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
Write a java console application,. It simulates the vending machine and ask two questions. When you...
Write a java console application,. It simulates the vending machine and ask two questions. When you run your code, it will do following: Computer output: What item you want? User input: Soda If user input is Soda Computer output: How many cans do you want? User input:            3 Computer output: Please pay $3.00. END The vending machine has 3 items for sale: Soda the price is $1.50/can. Computer should ask “How many cans do you want?” Chips, the price is $1.20/bag....
Can you fix please? this is adding the given location of doubly linked list in java...
Can you fix please? this is adding the given location of doubly linked list in java but if I make reverse is not adding the new node that I added but is printing forward correctly. just fix to me that part public void addAtLocation(E newNode, int location) {        Node node = new Node(newNode);               node.data = newNode;               Node previous = head;                      int counter = 1;...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this...
ONLY USE VISUAL STUDIO (NO JAVA CODING) VISUAL STUDIO -> C# -> CONSOLE APPLICATION In this part of the assignment, you are required to create a C# Console Application project. The project name should be A3<FirstName><LastName>P2. For example, a student with first name John and Last name Smith would name the project A1JohnSmithP2. Write a C# (console) program to calculate the number of shipping labels that can be printed on a sheet of paper. This program will use a menu...
C# Create a console application named that creates a list of shapes, uses serialization to save...
C# Create a console application named that creates a list of shapes, uses serialization to save it to the filesystem using XML, and then deserializes it back: // create a list of Shapes to serialize var listOfShapes = new List<Shape> { new Circle { Colour = "Red", Radius = 2.5 }, new Rectangle { Colour = "Blue", Height = 20.0, Width = 10.0 }, new Circle { Colour = "Green", Radius = 8 }, new Circle { Colour = "Purple",...
What factors should you consider when choosing between a console application and a GUI application?
What factors should you consider when choosing between a console application and a GUI application?
Create an application that uses a constructor and two different methods. JAVA
Create an application that uses a constructor and two different methods. JAVA
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed … what you did to fix it. import java.util.Scanner; public static void main(String[] args) { // This program Converts grade Points into a Letter grade. int gradePoints == 00; // Declare variable and assign initial value System.out.printl("Enter Grade Points: "; //Input gradePoints; if ( gradePoints >= -42 ) { System.out.println("Grade = A"); } // if true, then ends here, if false drop to next...
Describe options for debugging applications and specifically how you can debug an application with Visual Studio...
Describe options for debugging applications and specifically how you can debug an application with Visual Studio Code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT