Question

In: Computer Science

Using the provided Java program below, complete the code to do the following. You may need...

Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment.

  1. The changes to the methods are listed in the comments above the method names.
  2. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete.
  3. Modify the method findMyCurrency() to do the following:

   a. Use a do-while loop

b. Prompt for a currency to find.

c. Inside this loop, call the lookUpCurrency() method can pass the inputted currency code value.

d. The lookUpCurrency() method needs to loop through the list of currencies and compare the passed currency code with the currency code in the array. This method should return a boolean result.

Look at the showCurrencies() method for a example of how to loop for each element.

e. In the findMyCurrency()  method

   If found - Display the currency code and state it was found - i.e. "FJD found !".

   If not found - Display the currency code and not found - i.e " ZZZ not found!".

f. Prompt with "Retry Y/N ?" to try again. If I select N, then exit the loop. Allow any other char to restart the loop and prompt for a currency to find.

Because you are comparing strings, remember to use the .equals method - not ==.

Below is a partial example of the expected output:

Enter a Currency Code to Find: USD

USD found!

Retry Y/N ? Y

Enter a Currency Code to Find: ZZZ

ZZZ not found!

Retry Y/N ? N

Code you are working with:

/*
 * Student Name - 
 * Student ID   -
 * Semester     - 
 * Campus       - EL Centro
 * Class        - COSC 1436
 * 
 */

import java.util.Currency;
import java.util.Iterator;
import java.util.Set;



public class CheckCurrencyCode {

        
        // Sets the MAX_CURRENCIES value from the Currency Object
        static int MAX_CURRENCIES = Currency.getAvailableCurrencies().size();
        static String[] CURRENCY_CODES = new String[MAX_CURRENCIES];
                        
                        
        /**
         * This method will load the currency_Codes array with 3 char codes.
         * It does not need to change
         */
        
        private static void loadCurrencyCodes() {
                 Set<Currency> currencies = Currency.getAvailableCurrencies();
                 Iterator<Currency> i = currencies.iterator();
                 int idx = 0;
                 while(i.hasNext()) {
                         CURRENCY_CODES[idx++] = i.next().getCurrencyCode();
                 }
        }
        
        /**
         * Raw display of the Currency_codes;
         * Could also use System.out.println(Arrays.toString(CURRENCY_CODES));
         * It does not need to change
         */
        private static void showCurrencies() {
                for (int idx = 0; idx < MAX_CURRENCIES;++idx) {
                        System.out.print(CURRENCY_CODES[idx]+",");
                }
                
                System.out.println();
                //System.out.println(Arrays.toString(CURRENCY_CODES));
        }
        
        
        /**
         * Look up the given currency here and set the return value
         * to true if found or false if not found
         * 
         * Hint: Use a FOR loop to traverse the loop
         * 
         * @param myCurrencyToFind
         * @return
         */
        private static boolean lookUpCurrency(String myCurrencyToFind) {
        
                //TODO remove line below. It was added only for compile purposes
                return true;
        }
        
        /**
         * Prompt for a currency to find
         * Call the lookUpCurrency with the inputted currency code 
         * Display the found result
         * Prompt to try again and exit this method only if the choice is to not continue
         */
        private static void findMyCurrency() {
        
        
        }
        public static void main(String[] args) {
                loadCurrencyCodes();
                showCurrencies();
                findMyCurrency();
                //TODO Display an indication that the program is completed 
                
                                

        }

}

Solutions

Expert Solution

Please up vote ,comment if any query . Thanks for question . Be safe .

Note : check attached image for output ,code tested in java netbeans ide.

Program : ************************CheckCurrencyCode.java**********************************

/*
* Student Name -
* Student ID   -
* Semester     -
* Campus       - EL Centro
* Class        - COSC 1436
*
*/

import java.util.Currency;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class CheckCurrencyCode {

      
        // Sets the MAX_CURRENCIES value from the Currency Object
        static int MAX_CURRENCIES = Currency.getAvailableCurrencies().size();
        static String[] CURRENCY_CODES = new String[MAX_CURRENCIES];
                      
                      
        /**
         * This method will load the currency_Codes array with 3 char codes.
         * It does not need to change
         */
      
        private static void loadCurrencyCodes() {
                 Set<Currency> currencies = Currency.getAvailableCurrencies();
                 Iterator<Currency> i = currencies.iterator();
                 int idx = 0;
                 while(i.hasNext()) {
                         CURRENCY_CODES[idx++] = i.next().getCurrencyCode();
                 }
        }
      
        /**
         * Raw display of the Currency_codes;
         * Could also use System.out.println(Arrays.toString(CURRENCY_CODES));
         * It does not need to change
         */
        private static void showCurrencies() {
                for (int idx = 0; idx < MAX_CURRENCIES;++idx) {
                        System.out.print(CURRENCY_CODES[idx]+",");
                }
              
                System.out.println();
                //System.out.println(Arrays.toString(CURRENCY_CODES));
        }
      
      
        /**
         * Look up the given currency here and set the return value
         * to true if found or false if not found
         *
         * Hint: Use a FOR loop to traverse the loop
         *
         * @param myCurrencyToFind
         * @return
         */
        private static boolean lookUpCurrency(String myCurrencyToFind) {
          
                for(int i=0;i<MAX_CURRENCIES;i++) //runa a loop from 0 to MAX_CURRENCIES
                {
                    //check for string match it will ignore upper case or lower case
                    //shallow comparision
                    if(CURRENCY_CODES[i].equalsIgnoreCase(myCurrencyToFind))
                    {
                        return true; //return true
                    }
                }
                return false; //else return false not found
        }


      
        /**
         * Prompt for a currency to find
         * Call the lookUpCurrency with the inputted currency code
         * Display the found result
         * Prompt to try again and exit this method only if the choice is to not continue
         */
        private static void findMyCurrency() {
            Scanner sc=new Scanner(System.in); //Create a scanner object
            while(true) //infinite loop
            {
                //prompt for currency code
                System.out.print("Enter a Currency Code to Find: ");
                String currencyToFind=sc.nextLine(); //read string
                if(lookUpCurrency(currencyToFind)==true) //if function return true
                {
                    System.out.println(currencyToFind+" found!"); //print code found
                }
                else //else not found
                {
                    System.out.println(currencyToFind+" not found!");
                }//get choice y or n
                System.out.println("Retry Y/N ? ");
                char choice=sc.nextLine().charAt(0); //match with first char
                if(choice=='N') //if N break from loop or exit from function
                    return;
            }
          
        }
        public static void main(String[] args) {
                loadCurrencyCodes(); //get currency in array
                showCurrencies(); //print currency
                findMyCurrency(); //find currency

System.out.println("Program completed. ");
                //TODO Display an indication that the program is completed
              
                              

        }

}

Output :

Please up vote ,comment if any query .


Related Solutions

Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may write this in jGrasp Create a Stack of String called, myStacks Read input from keyboard, 10 names and then add to myStacks As you remove each name out, you will print the name in uppercase along with a number of characters the name has in parenthesis. (one name per line).   e.g.     Kennedy (7) Using existing Stack Java Collection Framework, write Java Code segment to...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text...
URGENT!! DO THIS CODE IN JAVA Write a complete Java FX program that displays a text label and a button.When the program begins, the label displays a 0. Then each time the button is clicked, the number increases its value by 1; that is each time the user clicks the button the label displays 1,2,3,4............and so on.
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
Need to make Java code as following: Create a dice playing threading program to do the...
Need to make Java code as following: Create a dice playing threading program to do the following: 1. Start a thread for a process to simulate a computer rolling a die 1000 times. 2. Start another thread for a process to simulate a user rolling a die 1000 times. 3. Keep track of rolls for user and computer in an array(s). 4. Display on screen when the computer thread starts, the rolls, and when the computer thread ends. 5. Display...
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT