Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 54 additions & 29 deletions src/main/java/ICSatKCC/Coin.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,107 @@
package ICSatKCC;

import java.util.Random;
/**
* Basic Coin super class.
* Flippable Coin super class.
* @author Lisa Miller
* @since 1/24/22
* @since 9/9/2017
*/

public class Coin {
public class Coin implements Flippable {
//instance variables
/** the Coin value. */
private double value;
/** the name of the Coin type. */
/** The Coin name. */
private String name;
/** the image on the front of the Coin. */
private String front;
/** the image on the back. */
private String back;
/** The coin color. */
private String color;
//added for flippable
/** The Coin side facing up. */
private int upSide;
/** The Coin side facing down. */
private int downSide;

/**
* Two parameter constructor.
* @param newValue the coin value
* @param newName the coin type name
*/
public Coin(double newValue, String newName, String newFront, String newBack) {
public Coin(double newValue, String newName) {
this.value = newValue;
this.name = newName;
this.front = newFront;
this.back = newBack;
this.toss(); //randomly set upside/downSide
}

//get methods
/**
* Gets the value of this Coin.
* @return the value of the Coin.
* @return the value
*/
public double getValue() {
return this.value;
}

/**
* Gets the name of this Coin.
* @return the name of the Coin.
* @return the name
*/
public String getName() {
return this.name;
}

/**
* Gets the color of this Coin.
* Always returns Silver as the default color.
* @return the color of the Coin, Silver.
* @return "Silver by default
*/
public String getColor() {
return "Silver";
}


/**
* Switches value of upSide and downSide.
*/
@Override
public void flip() {
if (upSide == 0) {
upSide = 1;
downSide = 0;
}
else {
upSide = 0;
downSide = 1;
}
}

/**
* Gets the description of the front image.
* @return the front of the Coin.
* Randomly sets upSide and corresponding downSide.
*/
public String getFront() {
return this.front;
@Override
public void toss() {
Random r = new Random();
upSide = r.nextInt(2);
if (upSide == 0) {
downSide = 1;
}
else {
downSide = 0;
}
}

/**
* Gets the description of the back image.
* @return the back of the Coin.
* Returns the upSide.
* @return the up facing side
*/
public String getBack() {
return this.back;
@Override
public int getUpSide() {
return upSide;
}

/**
* Sets the description of the back image.
* @param the back of the Coin.
* Sets the upSide.
* @param i the new up facing side
*/
public void setBack(String newBack) {
this.back = newBack;
@Override
public void setUpSide(int i) {
upSide = i;
}

}
59 changes: 0 additions & 59 deletions src/main/java/ICSatKCC/CoinDriver.java

This file was deleted.

75 changes: 75 additions & 0 deletions src/main/java/ICSatKCC/CoinFlipper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ICSatKCC;

/**
* Driver class for Flippable Coins
* @author Lisa Miller
* @since 9/9/2017
*/

public class CoinFlipper{
public static void main( String [] args){

//array of coins can hold all subclasses
Coin[] coinArr = new Coin[2];

coinArr[0] = new Dime();
coinArr[1] = new Penny();

for (int i = 0; i < coinArr.length; i++){
System.out.print("Coin type: " + coinArr[i].getName() + "\tValue: " + coinArr[i].getValue());
System.out.println("\tColor: " + coinArr[i].getColor() + "\tUp Side: " + coinArr[i].getUpSide());
}

//do ten coin tosses:
for (int i = 0; i < 10; i++) {
System.out.print(printUpSide(coinArr[0].getUpSide()) + " -> ");
coinArr[0].toss();
System.out.println(printUpSide(coinArr[0].getUpSide()));
}

Flippable f = doubleFlip(coinArr[0]);
System.out.println("Got back : " + f);


}//close main

/**
* private static method for getting upside as a string
* @param i the upSide int value
* @return the upSide String value
*/
private static String printUpSide(int i) {
if (i == 0){
return("Heads");
}else {
return("Tails");
}
}

/**
* mathod that takes in a flippable object, makes a new coin based on the side
* and returns it
* @param an object to get side
* @return a new COin object
*/
public static Flippable doubleFlip(Flippable f1){
f1.flip();
f1.flip();
int side = f1.getUpSide();
Coin c;

switch(side){
case 0:
c = new Penny();
break;
case 1:
c = new Nickel();
break;
default:
c = new Coin(0.0, "some other coin");
break;
}
return c;
}//end doubleFlip

}//close class
14 changes: 5 additions & 9 deletions src/main/java/ICSatKCC/Dime.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package ICSatKCC;

/**
* Dime .10 value Coin Subclass.
* Dime Coin Subclass
* @author Lisa Miller
* @since 1/24/22
* @since 9/9/2017
*/
public class Dime extends Coin {

/**
* Constructor, needs no parameters.
* Sends default Dime values
*/
public Dime() {
super(.10, "Dime", "Franklin D. Roosevelt", "torch");
}
public Dime(){
super(.10,"dime");
}
}
28 changes: 10 additions & 18 deletions src/main/java/ICSatKCC/DollarCoin.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package ICSatKCC;

/**
* DollarCoin 1.00 value Coin Subclass.
* DollarCoin Coin Subclass
* @author Lisa Miller
* @since 1/24/22
* @since 9/9/2017
*/
public class DollarCoin extends Coin {
/**
* Constructor, needs no parameters.
* Sends default Dollar values
*/
public DollarCoin() {
super(1.0, "Dollar", "Sacagawea", "Bald Eagle");
}

/**
* Gets the color of a Dollar Coin.
* Overrides superclass getColor method
* @return the color of a DollarCoin Coin.
*/
public String getColor() {
return "Gold";
}

public DollarCoin(){
super(1.0,"dollar");
}

public String getColor () {
return "gold";
}
}
32 changes: 32 additions & 0 deletions src/main/java/ICSatKCC/Flippable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ICSatKCC;

/**
* Interface for objects that have flippable property.
* @author Lisa Miller
* @since 9/7/2017
*/

public interface Flippable {

/**
* Swaps caller's up-side
*/
public void flip();

/**
* Randomly set caller's up-side
*/
public void toss();

/**
* Sets up-side value
* @param i the new up-side value
*/
public void setUpSide(int i);

/**
* Returns the value of the up-side
* @return the up-side
*/
public int getUpSide();
}
15 changes: 6 additions & 9 deletions src/main/java/ICSatKCC/HalfDollar.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package ICSatKCC;

/**
* HalfDollar .50 value Coin Subclass.
* HalfDollar Coin Subclass
* @author Lisa Miller
* @since 1/24/22
* @since 9/9/2017
*/
public class HalfDollar extends Coin {

/**
* Constructor, needs no parameters.
* Sends default HalfDollar values
*/
public HalfDollar() {
super(.50, "Half Dollar", "John F. Kennedy", "Presidential Coat of Arms");
}
public HalfDollar(){
super(.50,"halfdollar");
}
}
Empty file.
Loading