Question

In: Computer Science

The application uses Java Object-Oriented features for its implementation. Students select from a menu of courses...

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. Your program interaction should look like the sample interaction video, “OO Console Register for Course”

PLEASE COMMENT THROUGHOUT CODE IN BOLD WHAT YOU USED/DID AND EXPLAIN WHY

REGISTER FOR COURSE CODE:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package u10a1_ooconsoleregisterforcourse;

import java.util.Scanner;


public class U10A1_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("\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 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);
}
  
}

COURSE CLASS CODE:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package u10a1_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;
}
  
}

Solutions

Expert Solution

// Course.java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package u10a1_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;

             }

}

//end of Course.java

// U10A1_OOConsoleRegisterForCourse .java

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package u10a1_ooconsoleregisterforcourse;

import java.util.Scanner;

public class U10A1_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("\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 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

// loop over the courses array

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

             {

// display the selection number using (i+1) since array index starts from 0, get the code and credit hours from the Course's objects methods getCode() and getCreditHour() and display them as required

                    System.out.println("["+(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, already 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 , }

// define a String to hold the courses code registered

             String str = "";

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

             {

                    if(courses[i].getIsRegisteredFor()) // if ith course is registered

                    {

                           if(str.length() > 0) // check if string non-empty string, then append this code by separating it with the previous code using , (comma)

                                 str += ", "+courses[i].getCode();

                           else // if string is empty string then this is the first code to be added

                                 str = courses[i].getCode();

                    }

             }

            

             System.out.print(str); // print the String of course codes separated by ,(comma)

             System.out.println(" }" );  

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

       }

      

}

//end of U10A1_OOConsoleRegisterForCourse .java

Output:


Related Solutions

Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Describe how you would develop object-oriented features of Java for the Quiz program developed in the...
Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments. In particular, describe how the program could use each of the following: class variables, instance variables, inheritance, polymorphism, abstract classes, "this", "super", interfaces, and event listeners.
Describe how you would develop object-oriented features of Java for the Quiz program developed in the...
Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments. In particular, describe how the program could use each of the following: class variables, instance variables, inheritance, polymorphism, abstract classes, "this", "super", interfaces, and event listeners. Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
Create a menu in Java. A menu is a presentation of options for you to select....
Create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Buy Stock. B. Sell Stock X. Exit Enter your Selection: 3. Prompt for the value menuChoice....
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a...
-What is object-oriented programming? -What is a class? -What is an object? -A contractor uses a blueprint to build a set of identical houses. Are classes analogous to the blueprint or the houses? Explain. -What is a class diagram? How is it used in object-oriented programming? -What is an attribute in OOP? What is a data member? -What is a method in OOP? What is a member function? -What is the difference between private members and public members of a...
PHP You will be using the object oriented features of PHP to design a music album...
PHP You will be using the object oriented features of PHP to design a music album processing page. First you will build a form page called addAlbum.php. This form will contain text fields for album title, artist, publisher (Sony, BMI, etc.) and genre. Add two more fields of your choice. You will post this form to the file process.php. If all the fields have values, we will create a new Album object and print the details of the object. You...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create...
*OBJECT ORIENTED PROGRAMMING* JAVA PROGRAMMING GOAL: will be able to throw and catch exceptions and create multi-threaded programs. Part I Create a class called Animal that implements the Runnable interface. In the main method create 2 instances of the Animal class, one called rabbit and one called turtle. Make them "user" threads, as opposed to daemon threads. Some detail about the Animal class. It has instance variables, name, position, speed, and restMax. It has a static boolean winner. It starts...
Object-Oriented Design and Patterns in Java (the 3rd Edition Chapter 5) - Create a directory named...
Object-Oriented Design and Patterns in Java (the 3rd Edition Chapter 5) - Create a directory named as the question number and save the required solutions in the directory. - Each problem comes with an expected tester name. In the directory for a given problem, including the tester and all java classes successfully run this tester. Exercise 5.2 ObserverTester.java - Improve Exercise 1 by making the graph view editable. Attach a mouse listener to the panel that paints the graph. When...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Question #11. Write a script that uses the menu function to prompt the user to select...
Question #11. Write a script that uses the menu function to prompt the user to select the current day of the week. Then use the menu function to prompt the user to select their favorite day of the week. Print a message telling the user how many days it will be until their favorite day. The output must include the current day, the favorite day, and the number of days until the favorite day. Debug your programming by running it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT