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...

  • 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,...

  • 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...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

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
ADVERTISEMENT