Question

kindly Explain, briefly IN PLAIN ENGLISH, how this exercise was completed and the algorithm you used...

kindly Explain, briefly IN PLAIN ENGLISH, how this exercise was completed and the algorithm you used

/*

* 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;

/**

*

* @author omora

*/

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

for(int i=0; i

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, 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 , }

boolean isFirst = true;

for(int i=0; i

if(courses[i].getIsRegisteredFor()){

if(!isFirst)

System.out.print(", ");

System.out.print(courses[i].getCode());

isFirst = false;

}

}

System.out.println(" }");

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

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

Please find the updated code below:

package com.company;

import java.util.Scanner;

/**
 *
 * @author omora
 */
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 , }

        for(Course c: courses){
            if(c.getIsRegisteredFor()){
                System.out.print(c.getCode() + ", ");
            }
        }
        System.out.println("\b\b}" );

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

}

C: \Program Files\Java\jdk1.8.0_151\bin\java Teachers Copy Please type the number inside the register for a [] to course T

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
kindly Explain, briefly IN PLAIN ENGLISH, how this exercise was completed and the algorithm you used...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Debug and fix the Java console application that uses 2 dimensional arrays but the application does...

    Debug and fix the Java console application that uses 2 dimensional arrays but the application does not compile nor execute. 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. 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...

  • KINDLY Describe the approach (in plain English) used to completing the coding below and explain the major decisions made...

    KINDLY Describe the approach (in plain English) used to completing the coding below and explain the major decisions made in designing the program. As part of your explanation, be sure to identify the Java constructs you used that are specific and relevant to the program below. Depending on the program, these may include the mechanisms for output, input, selection statements, loops, methods, and so forth. ***********BEGINNING OF CODE*************** import java.util.*; public class CountOccurances{ public static void main(String[] args) { Scanner...

  • Could you help me pleas , this is my code I want change it to insert...

    Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

  • This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying...

    This program is giving me an error in the main method at "outputFile.flush();" and "outputFile.close();" saying that it is unreachable code. Why is that, and how can I fix this?? import java.util.Scanner; import java.io.*; public class Topic7Hw {    static Scanner sc = new Scanner (System.in);       public static void main(String[] args) throws IOException {               PrintWriter outputFile = new PrintWriter ("output.txt");               outputFile.println("hello");               final int MAX_NUM = 10;...

  • How do I record every user Input and then display all of it in the scream....

    How do I record every user Input and then display all of it in the scream. In my code i ask the user for a series of food item, then i ask for the toppings. I need to display every input of food and toppings the user enters. The output must look like this Here is your order <name> Appetizer: [ Wings, Blue cheese, Ranch ] Main Course: [ Hamburger: mushrooms , Advocado ] Dessert: [ Pie: Powder Sugar, Scoop...

  • Can you help me rearrange my code to make it look cleaner but still give the...

    Can you help me rearrange my code to make it look cleaner but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string, double and int array containing * the command line arguments. * @exception Any exception * @return an arraylsit of...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT