summaryrefslogtreecommitdiffstats
path: root/Java/Cup
diff options
context:
space:
mode:
Diffstat (limited to 'Java/Cup')
-rw-r--r--Java/Cup/Cup.java55
-rw-r--r--Java/Cup/CupDemo.java33
2 files changed, 88 insertions, 0 deletions
diff --git a/Java/Cup/Cup.java b/Java/Cup/Cup.java
new file mode 100644
index 0000000..c313c07
--- /dev/null
+++ b/Java/Cup/Cup.java
@@ -0,0 +1,55 @@
+public class Cup {
+
+ // data member
+ private double capacity;
+ private double holding;
+ private String color;
+
+
+
+ // Constructors
+ public Cup(double c, String co, double h) {
+ capacity = c;
+ color = co;
+ holding = h;
+ }
+ public Cup() {
+ color = "red";
+ capacity = 16;
+ holding = 0;
+ }
+ // set
+ public void fillCup(double h) {
+ if (h > capacity){
+ capacity = holding;
+ } else {
+ holding = holding + h;
+ }
+ }
+ public void sip() {
+ if(holding - 1 < 1){
+ holding = holding-1;
+ } else {
+ //System.out.println("CUPPER UNDERFLOW DETECTED");
+ }
+ }
+
+ public void gulp() {
+ if(holding-3 > 1){
+ holding = holding-3;
+ } else {
+ //System.out.println("CUPPER UNDERFLOW DETECTED");
+ }
+ }
+ public void emptyCup() {
+ holding = 0;
+ }
+
+ // modify and return
+ public String status() {
+ return capacity + " ounce " + color + " cup that contains " + holding + " ounce[s].";
+ }
+
+}
+
+
diff --git a/Java/Cup/CupDemo.java b/Java/Cup/CupDemo.java
new file mode 100644
index 0000000..9c00d94
--- /dev/null
+++ b/Java/Cup/CupDemo.java
@@ -0,0 +1,33 @@
+public class CupDemo {
+
+ public static void main (String args []) {
+
+ //cup init
+ Cup FirstCup = new Cup();
+ Cup SecondCup = new Cup(20.0, "Blue", 0.0);
+
+//fill both with 20 oz, this breaks the first cup and will default to the maximum possible capacity
+ FirstCup.fillCup(20);
+ SecondCup.fillCup(20);
+ //print red
+ System.out.println(FirstCup.status());
+ //sip and gulp from blue
+ SecondCup.sip();
+ SecondCup.sip();
+ SecondCup.gulp();
+ //Fill by 6
+ SecondCup.fillCup(6);
+ //print blue
+ System.out.println(SecondCup.status());
+ //empty red
+ FirstCup.emptyCup();
+ //fill red with five
+ FirstCup.fillCup(5);
+ //2x gulp first
+ FirstCup.gulp();
+ FirstCup.gulp();
+ //final print
+ System.out.println(FirstCup.status());
+ }
+}
+