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

Explain the approach you took to complete this assignment and the major decision

ID: 3728814 • Letter: E

Question

Explain the approach you took to complete this assignment and the major decisions you made. As part of your explanation, be sure to identify the fundamental Java constructs you used that were specific and relevant to your submitted program.

Complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using the Object-Oriented features of the Java programming language. The application does compile and does run, but it does not produce the expected result as stated in its requirements.

The application uses Java Object-Oriented features for its implementation. Students select from a menu of courses for which they wish to register. The program then validates the user selection against the registration business rules. If the selection is valid, the program prints out a confirmation message. Otherwise, the program prints out the current list of registered classes along with total registered credit hours. The program terminates when the user does not want to register for classes any more.

There are two Java classes of this application:

1. Course.java. This Java class is complete and does not need any modification

2. U10A1_OOConsoleRegisterForCourse.java. This is the class with the two methods that need to be completed

The two methods that need to be completed of the U10A1_OOConsoleRegisterForCourse.java are:

1. getChoice() method. This method loops over an array of course objects and prints out their attributes, one per line according to this format: [selection number]Course Code (Course Credit Hours)

2. WriteCurrentRegistration() method. This method also loops over the array of course objects and prints out a list of registered courses thus far. The list current registered courses are enclosed inside a { } and separated by a ,. The methods also prints out the total credit hours thus far.

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

IT2230

IT2249

IT2230

IT3345

Successful completion of this assignment will display a menu of courses from which to select to register in the format of

[selection number]Course Code (Course Credit Hours)

In addition, the application should display and update current list of registered courses and their total credit hours.

Explain the approach you took to complete this assignment and the major decisions you made. As part of your explanation, be sure to identify the fundamental Java constructs you used that were specific and relevant to your submitted program.

Course.Java

package ooconsoleregisterforcourse;

/**
*
*
*/
public class Course {

private String code = "";
private int creditHour = 0;
private boolean isRegisterdFor = false;
  
public Course(String code, int creditHour){
this.code = code;
this.creditHour = creditHour;
}
  
public void setCode(String code){
this.code = code;
}
  
public String getCode() {
return this.code;
}
  
public void setCrditHour(int creditHour) {
this.creditHour = creditHour;
}
  
public int getCreditHour() {
return this.creditHour;
}
  
public void setIsRegisteredFor(boolean trueOrFalse){
this.isRegisterdFor = trueOrFalse;
}
  
public boolean getIsRegisteredFor() {
return this.isRegisterdFor;
}
  
}

U10A1_OOConsoleRegisterForCourse.java

package ooconsoleregisterforcourse;

import java.util.Scanner;

public class OOConsoleRegisterForCourse {


/**
* @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 is an array of course objects
//see the Course.java source code for members of Course
Course[] courses = {
new Course("IT1006", 6),
new Course("IT4782", 3),
new Course("IT4789", 3),
new Course("IT4079", 6),
new Course("IT2230", 3),
new Course("IT3345", 3),
new Course("IT2249", 6)
};
  

//choice is the number selected by the user
int choice;
int totalCredit = 0;
String yesOrNo = "";


do {

choice = getChoice(courses, input);

switch (ValidateChoice(choice, 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[choice-1].getCode() + " 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[choice-1].getCode() );
totalCredit += courses[choice-1].getCreditHour();
courses[choice-1].setIsRegisteredFor(true);
break;
}
  
WriteCurrentRegistration(courses, totalCredit);

System.out.print(" Do 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 user to make a number selection
public static int getChoice(Course[] 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");
  
// TO DO
// loop over the courses array and print out the attributes of its
//objects in the format of
//[selection number]Course Code (Course Credit Hours)
//one per line

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, int totalCredit, Course[] courses) {
if (choice < 1 || choice > 7)
return -1;
else if (IsRegisteredBefore(choice, courses) )
return -2;
else if ( (totalCredit + courses[choice-1].getCreditHour()) > 9)
return -3;
return 0;
}
  
//This method checks the courses array of course object to
//see if the course has already been registered for or not
public static boolean IsRegisteredBefore(int choice, Course[] courses) {
for(int i = 0; i < courses.length; i++)
if(courses[choice-1].getIsRegisteredFor() == true)
return true;
return false;
}
  
//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(Course[] courses, int totalCredit) {

System.out.print("Current course registration: { " );
  
// TO DO
// loop over the courses array, determine which courses are registered
//for thus and print them out in the format of
//{ list of courses separated by , }
  
System.out.println(" }" );   

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

Explain the approach you took to complete this assignment and the major decisions you made. As part of your explanation, be sure to identify the fundamental Java constructs you used that were specific and relevant to your submitted program.

Output should look like:

Explain the approach you took to complete this assignment and the major decisions you made. As part of your explanation, be sure to identify the fundamental Java constructs you used that were specific and relevant to your submitted program.

Teacher's Copy Please type the number inside the [ to register for a course The number inside the is the credit hours for the course 11IT1006 (6) 21 IT4782 (3) 31 IT4789 (3) 41IT4079 (6) 51 IT2230 (3) 6] IT3345 (3) 7 IT2249 () Enter your choice1 Registration Confirmed for course IT1006 Current course registration: IT100E Current registration total credit 6 Do you want to try again? (YIN)? Y Please type the number inside the [ to register for a course The number inside the is the credit hours for the course 11IT1006 (6) 21 IT4782 (3) 31 IT4789 (3) 41IT4079 (6) 51 IT2230 (3) 6] IT3345 (3) 7 IT2249 () Enter your choice2 Registration Confirmed for course IT4782 Current course registration: IT100 IT4782 Current registration total credit 9 Do you want to try again? (YIN)? n Thank you for registering with us BUILD SUCCESSFUL (total time: 21 seconds)

Explanation / Answer

Hello, I have completed the two required methods getChoice() and WriteCurrentRegistration() exactly as needed, and verified the output.

In the getChoice method, looped through each courses, displayed each of them using a properly formatted way using printf() method

In the WriteCurrentRegistration method, looped through each courses, checked if the course is registered, if yes printed the course on the same line, or else not printed. Used appropriate variables to properly separate each course codes with a comma.

Comments are included wherever code has been modified. If you have any queries, feel free to drop a comment. Thanks.

// OOConsoleRegisterForCourse.java (modified)

package ooconsoleregisterforcourse;

import java.util.Scanner;

public class OOConsoleRegisterForCourse {

                /**

                * @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 is an array of course objects

                                // see the Course.java source code for members of Course

                                Course[] courses = { new Course("IT1006", 6), new Course("IT4782", 3),

                                                                new Course("IT4789", 3), new Course("IT4079", 6),

                                                                new Course("IT2230", 3), new Course("IT3345", 3),

                                                                new Course("IT2249", 6) };

                                // choice is the number selected by the user

                                int choice;

                                int totalCredit = 0;

                                String yesOrNo = "";

                                do {

                                                choice = getChoice(courses, input);

                                                switch (ValidateChoice(choice, 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[choice - 1].getCode() + " 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[choice - 1].getCode());

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

                                                                courses[choice - 1].setIsRegisteredFor(true);

                                                                break;

                                                }

                                                WriteCurrentRegistration(courses, totalCredit);

                                                System.out.print(" Do 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 user to make a number selection

                public static int getChoice(Course[] 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");

                                /**

                                * looping through each courses, and displaying them in proper format

                                */

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

                                                /**

                                                * using printf() method to display the course details in required

                                                * format, here %d means an integer, %s means a String

                                                */

                                                System.out.printf("[%d] %s (%d) ", (i + 1), courses[i].getCode(),

                                                                                courses[i].getCreditHour());

                                }

                                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, int totalCredit,

                                                Course[] courses) {

                                if (choice < 1 || choice > 7)

                                                return -1;

                                else if (IsRegisteredBefore(choice, courses))

                                                return -2;

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

                                                return -3;

                                return 0;

                }

                // This method checks the courses array of course object to

                // see if the course has already been registered for or not

                public static boolean IsRegisteredBefore(int choice, Course[] courses) {

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

                                                if (courses[choice - 1].getIsRegisteredFor() == true)

                                                                return true;

                                return false;

                }

                // 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(Course[] courses,

                                                int totalCredit) {

                                System.out.print("Current course registration: { ");

                                /**

                                * A variable to denote the first entry (so that no comma should be

                                * printed before it)

                                */

                                boolean first = true;

                                /**

                                * Looping through each courses

                                */

                                for (Course c : courses) {

                                                /**

                                                * Checking if the course is registered

                                                */

                                                if (c.getIsRegisteredFor()) {

                                                                /**

                                                                * checking if this is first entry

                                                                */

                                                                if (first) {

                                                                                /**

                                                                                * displaying the course code

                                                                                */

                                                                                System.out.print(c.getCode());

                                                                                // making first variable to false

                                                                                first = false;

                                                                } else {

                                                                                /**

                                                                                * displaying a comma, a space and then followed by the

                                                                                * course code

                                                                                */

                                                                                System.out.print(", " + c.getCode());

                                                                }

                                                }

                                }

                                System.out.println(" }");

                                System.out

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

                }

}

//Course.java (unmodified)

package ooconsoleregisterforcourse;

public class Course {

                private String code = "";

                private int creditHour = 0;

                private boolean isRegisterdFor = false;

                public Course(String code, int creditHour) {

                                this.code = code;

                                this.creditHour = creditHour;

                }

                public void setCode(String code) {

                                this.code = code;

                }

                public String getCode() {

                                return this.code;

                }

                public void setCrditHour(int creditHour) {

                                this.creditHour = creditHour;

                }

                public int getCreditHour() {

                                return this.creditHour;

                }

                public void setIsRegisteredFor(boolean trueOrFalse) {

                                this.isRegisterdFor = trueOrFalse;

                }

                public boolean getIsRegisteredFor() {

                                return this.isRegisterdFor;

                }

}

/*OUTPUT*/

Teacher's Copy

Please type the number inside the [] to register for a course

The number inside the () is the credit hours for the course

[1] IT1006 (6)

[2] IT4782 (3)

[3] IT4789 (3)

[4] IT4079 (6)

[5] IT2230 (3)

[6] IT3345 (3)

[7] IT2249 (6)

Enter your choice : 2

Registration Confirmed for course IT4782

Current course registration: { IT4782 }

Current registration total credit = 3

Do you want to try again? (Y|N)? : Y

Please type the number inside the [] to register for a course

The number inside the () is the credit hours for the course

[1] IT1006 (6)

[2] IT4782 (3)

[3] IT4789 (3)

[4] IT4079 (6)

[5] IT2230 (3)

[6] IT3345 (3)

[7] IT2249 (6)

Enter your choice : 3

Registration Confirmed for course IT4789

Current course registration: { IT4782, IT4789 }

Current registration total credit = 6

Do you want to try again? (Y|N)? : Y

Please type the number inside the [] to register for a course

The number inside the () is the credit hours for the course

[1] IT1006 (6)

[2] IT4782 (3)

[3] IT4789 (3)

[4] IT4079 (6)

[5] IT2230 (3)

[6] IT3345 (3)

[7] IT2249 (6)

Enter your choice : 5

Registration Confirmed for course IT2230

Current course registration: { IT4782, IT4789, IT2230 }

Current registration total credit = 9

Do you want to try again? (Y|N)? : N

Thank you for registering with us

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote