Beginning the FakeCoin practice solutions.

This commit is contained in:
Jonathan Turner 2024-02-13 21:18:13 -05:00
parent d007cc2212
commit abe66c0862
2 changed files with 104 additions and 1 deletions

View File

@ -96,7 +96,7 @@ public abstract class Application {
* @postcondition the menu is displayed.
*/
private void displayMenu() {
System.out.println("-----------------MAIN MENU--------------");
System.out.println("-----------------MAIN MENU-----------------");
for (int index = 0; index < this.numOfOptions; index++) {
System.out.println((index+1) + ". " + this.menuOptions[index]);
}

View File

@ -0,0 +1,103 @@
package Practice.Decrease_Conquer;
import Practice.Application;
public class FakeCoin extends Application {
private Coin[] pile;
private boolean setLocation;
/**
* Starts the application.
* @param args the application arguments.
*/
public static void main(String[] args) {
Application program = new FakeCoin();
program.start();
}
/**
* Creates a new FakeCoin application.
*
* @precondition none
* @postcondition a new FakeCoin application is created.
*/
public FakeCoin() {
super(new String[]{"The number of coins","Set FakeCoin Weight and Location","Find Fake Coin"});
this.setLocation = false;
}
@Override
public void executeOption(int option) {
switch (option) {
case 1:
System.out.println("Please enter a number of coins in the pile (including fake coin).");
System.out.print("Coin Count: ");
int numOfCoins = this.getIntegerInput();
while (numOfCoins < 1) {
System.out.println();
System.out.println("Please enter a positive value.");
System.out.print("Coin Count: ");
numOfCoins = this.getIntegerInput();
}
this.pile = new Coin[numOfCoins];
break;
case 2:
if (this.pile == null) {
System.out.println("Please specify a number of coins before setting the fake location.");
break;
}
System.out.println("Please specify a location for the fake coin. Between 0 and " + (this.pile.length-1));
System.out.print("Location: ");
int location = this.getIntegerInput();
while (location < 0 || location >= this.pile.length) {
System.out.println();
System.out.println("Please enter a valid location: Between 0 and " + (this.pile.length-1));
System.out.print("Location: ");
location = this.getIntegerInput();
}
/* Implement the functionality to do both lighter and heavier. */
this.pile[location] = new Fake(location,true);
for (int i = 0; i < this.pile.length; i++) {
if (this.pile[i] == null) {
this.pile[i] = new Coin(i);
}
}
this.setLocation = true;
break;
case 3:
if (!this.setLocation) {
System.out.println("Please specify the fake coins location.");
break;
}
this.performAlgorithm();
break;
}
System.out.println();
}
private void performAlgorithm() {
}
private static class Coin {
public int weight;
public int location;
public Coin(int location) {
this.weight = 3;
this.location = location;
}
}
private static class Fake extends Coin {
public Fake(int location, boolean isLighter) {
super(location);
if (isLighter)
super.weight = 2;
else
super.weight = 4;
}
}
}