Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

coding with multiple classes - writing multiple types of statements (choice/switch) - write overloaded methods and utilize them. - turn the problem to be the

coding with multiple classes

- writing multiple types of statements (choice/switch)

- write overloaded methods and utilize them.

- turn the problem to be the logic of a machine.

- following the class diagram in section 2.

- use the code they gave on page 2 and make it correct.

- use test class on section 3.

- make the output like in the sample they gave.

- coding the coinchanger on the last photo by following the class diagram.

- The result must have three java codes.

lic static final String FAKE_COIN = "fake"; private String type; private int value; public coin(String atype) { // **********ADD YOUR CODE HERE // using switch stmt to help to perform the task 1 public String getType() { return type; ) public int getValue() { return value; } public boolean isValid) { if (type == null || type == FAKE_COIN) return false; return true; > public String tostring() { return String.format("Coin %s bahts (value Xd)", type, value); 1 public static void main(String [] args) { Coin d .new Coin (Coin.COIN10); Coin c2 = new Coin("10 Bahts"); System.out.println(1 + ": "+ch isvalid()); System.out.println(c2 + ": "c2. isvalid()); } After adding the correct code, when you run the Coin class, it should report as follows: Coin 10

true Coin fake bahts (value e): false 2. Create a class named CoinChanger (as the above given class diagram). The CoinChanger has 4 coins namely, c1 for Coini, c2 for Coin2, c5 for Coins, and c10 for Coint0. The instance variables, c1, c2, c5, c10 are initialized in the constructor with the correct corresponding Coin. This class also makes coin change using either the minimum coins or the specified coins mixed with 1 Baht coin for the remained amount to which the specified coin could not be applied. Thus, the change methods have been overloaded to:

For the minimum number of coins, the change strategy uses a greedy algorithm in which it takes coins with greater value first. This can reduce the total number of coins needed. Start from the largest possible denomination and keep adding denominations while the remaining value is greater than 0. This method will print the number of each coins used for the change and the total number of coins requied to make the change. Note:

any coin that is not used to make the change will not be printed 2.2. change(int amount, coin coin): the change tries to match most of the amount using the specified coin and then the rest remainder using 1 Baht coin. In case that the specified coin is invalid, it would use the 1 Baht coin only. It also prints the total number of coins required to make the change. Both methods also check that the amount has value greater than 0. In case the given amount is zero or negative number, it informs error, "Cannot make any change for [- amount]" Write the CoinChanger class that would work with the above Coin class. Section

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions

Question

When are credits increases? When are credits decreases?

Answered: 1 week ago