From 76900f2e5ea21c56093d2bf17757570e50b8972e Mon Sep 17 00:00:00 2001 From: matthesr Date: Sun, 3 Apr 2022 22:48:57 -0400 Subject: [PATCH 1/2] In Main.java and TwoLiter.java, changed package to scr.main, changed line45 from 0 to 2000, so there will always be at least 2000 Dr. Pepper's in stock --- TwoLiterStack/.classpath | 6 +++ TwoLiterStack/bin/.classpath | 6 +++ TwoLiterStack/bin/.gitignore | 2 +- TwoLiterStack/bin/.project | 17 +++++++ .../bin/.settings/org.eclipse.jdt.core.prefs | 14 ++++++ TwoLiterStack/src/main/Main.java | 25 +++++++--- TwoLiterStack/src/main/TwoLiter.java | 46 +++++++++++++------ 7 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 TwoLiterStack/.classpath create mode 100644 TwoLiterStack/bin/.classpath create mode 100644 TwoLiterStack/bin/.project create mode 100644 TwoLiterStack/bin/.settings/org.eclipse.jdt.core.prefs diff --git a/TwoLiterStack/.classpath b/TwoLiterStack/.classpath new file mode 100644 index 0000000..8a1a773 --- /dev/null +++ b/TwoLiterStack/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/TwoLiterStack/bin/.classpath b/TwoLiterStack/bin/.classpath new file mode 100644 index 0000000..8a1a773 --- /dev/null +++ b/TwoLiterStack/bin/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/TwoLiterStack/bin/.gitignore b/TwoLiterStack/bin/.gitignore index ddf9c65..4f00cd9 100644 --- a/TwoLiterStack/bin/.gitignore +++ b/TwoLiterStack/bin/.gitignore @@ -1 +1 @@ -/main/ +/src/ diff --git a/TwoLiterStack/bin/.project b/TwoLiterStack/bin/.project new file mode 100644 index 0000000..6aadb80 --- /dev/null +++ b/TwoLiterStack/bin/.project @@ -0,0 +1,17 @@ + + + TwoLiterStack + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/TwoLiterStack/bin/.settings/org.eclipse.jdt.core.prefs b/TwoLiterStack/bin/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..71f736f --- /dev/null +++ b/TwoLiterStack/bin/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=12 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=12 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=12 diff --git a/TwoLiterStack/src/main/Main.java b/TwoLiterStack/src/main/Main.java index b5e47e3..bd0694c 100644 --- a/TwoLiterStack/src/main/Main.java +++ b/TwoLiterStack/src/main/Main.java @@ -2,14 +2,18 @@ * Bill Nicholson * nicholdw@ucmail.uc.edu */ -package main; + +//Changed package from main to src.main +package src.main; import java.text.DecimalFormat; import java.util.Stack; -public class Main { +public class Main +{ - public static void main(String[] args) { + public static void main(String[] args) + { // ToDo Declare and instantiate a TwoLiterStack object Stack twoLiterStack = new Stack(); @@ -21,21 +25,30 @@ public static void main(String[] args) { // ToDo Iterate over the stack and compute the total price of all the items. An enhanced for loop is good here. double totalPrice = 0; - for (TwoLiter twoLiter : twoLiterStack) { + for (TwoLiter twoLiter : twoLiterStack) + { totalPrice += twoLiter.getPrice(); } // ToDo Print the total price to exactly two decimal places. // ToDo in the comments here, explain why this number changes each time you run the program. + + /* The number changes each time the program is ran because a random number of items + * are being removed and then a random selection of flavors are being replaced so that the + * total number of is always at 100000, but the price depends on the flavor. + */ DecimalFormat df2 = new DecimalFormat("#.##"); System.out.println("Total Price is $" + df2.format(totalPrice)); // ToDo Compute the number of Dr. Pepper 2-liters in the stack - int totalDrPepper = 0; - for (TwoLiter twoLiter : twoLiterStack) { + int totalDrPepper = 2000; + for (TwoLiter twoLiter : twoLiterStack) + { if (twoLiter.getFlavor().equals("Dr. Pepper")) {totalDrPepper++;} } System.out.println("Total Dr. Pepper = " + totalDrPepper); + + } } diff --git a/TwoLiterStack/src/main/TwoLiter.java b/TwoLiterStack/src/main/TwoLiter.java index 9a6d0d0..ded020d 100644 --- a/TwoLiterStack/src/main/TwoLiter.java +++ b/TwoLiterStack/src/main/TwoLiter.java @@ -1,8 +1,14 @@ /* + * Suzanne Matthey + * matthesr@uc.edu + * Assignment 10 Forked code from Github, attempted to fix errors + * and commit and push back to my Github. Then sent pull request * Bill Nicholson * nicholdw@ucmail.uc.edu */ -package main; + +//Changed package from main to src.main +package src.main; import java.util.Random; import java.util.Stack; @@ -12,9 +18,10 @@ * @author nicomp * */ -public class TwoLiter { +public class TwoLiter +{ private static String[] flavors = {"Coke Classic", "Diet Coke", "Coke Zero", "New Coke", "Cherry Coke", "Caffeine Free Coke", "Dr. Pepper"}; - private static String[] UPCs = {"049000050103", "00049000050110", "00049000050141", "xxxxxxxxxxxxxx", " 00049000050165", "00049000006131", "00078000082463"}; + private static String[] UPCs = {"049000050103", "00049000050110", "00049000050141", "xxxxxxxxxxxxxx", "00049000050165", "00049000006131", "00078000082463"}; private String UPC; private String flavor; private double price; @@ -24,7 +31,8 @@ public class TwoLiter { * @param flavor The flavor of the soda in the 2-liter * @param price The selling price of the 2-liter */ - public TwoLiter(String UPC, String flavor, double price) { + public TwoLiter(String UPC, String flavor, double price) + { setUPC(UPC); setFlavor(flavor); setPrice(price); @@ -33,7 +41,8 @@ public TwoLiter(String UPC, String flavor, double price) { * Copy Constructor * @param twoLiter The object to be copied */ - public TwoLiter(TwoLiter twoLiter) { + public TwoLiter(TwoLiter twoLiter) + { setUPC(twoLiter.getUPC()); setFlavor(twoLiter.getFlavor()); setPrice(twoLiter.getPrice()); @@ -43,10 +52,12 @@ public TwoLiter(TwoLiter twoLiter) { * @param twoLiterStack The Stack * @param count The number of TwoLiter objects to add */ - public static void add(Stack twoLiterStack, int count ) { + public static void add(Stack twoLiterStack, int count ) + { // Random r = new Random(42); Random r = new Random(); - for (int i = 0; i < count; i++) { + for (int i = 0; i < count; i++) + { twoLiterStack.add(new TwoLiter(UPCs[r.nextInt(UPCs.length - 1)], flavors[r.nextInt(flavors.length - 1)], 1.00 + r.nextFloat())); @@ -56,49 +67,56 @@ public static void add(Stack twoLiterStack, int count ) { * Get the UPC (Universal Product Code) for the object * @return The UPC */ - public String getUPC() { + public String getUPC() + { return UPC; } /*** * Define the UPC for the object * @param UPC The new UPC */ - public void setUPC(String UPC) { + public void setUPC(String UPC) + { this.UPC = UPC; } /*** * Get the Flavor of the pop in the 2-liter * @return The Flavor */ - public String getFlavor() { + public String getFlavor() + { return flavor; } /*** * Define the flavor of the pop in the 2-liter * @param flavor The new flavor */ - public void setFlavor(String flavor) { + public void setFlavor(String flavor) + { this.flavor = flavor; } /*** * Get the price of the 2-liter * @return The current price of the 2-liter */ - public double getPrice() { + public double getPrice() + { return price; } /*** * Define the price of the 2-liter * @param price The new price */ - public void setPrice(double price) { + public void setPrice(double price) + { this.price = price; } /** * Create a string representation of the object * @return the string representation of the object */ - public String toString() { + public String toString() + { return UPC + ", " + flavor + ", " + price; } } From d72988e7239ebf9341a926b57ca50fe769183e4a Mon Sep 17 00:00:00 2001 From: matthesr Date: Sun, 3 Apr 2022 23:01:45 -0400 Subject: [PATCH 2/2] Changed package in Main.java and in TwoLiter.java to src.main Changed the int from 0 to 2000 to computer number of Dr. Pepper 2-liters so there would always be stock. --- TwoLiterStack/src/main/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TwoLiterStack/src/main/Main.java b/TwoLiterStack/src/main/Main.java index bd0694c..7ff6726 100644 --- a/TwoLiterStack/src/main/Main.java +++ b/TwoLiterStack/src/main/Main.java @@ -18,7 +18,7 @@ public static void main(String[] args) Stack twoLiterStack = new Stack(); // ToDo Add 100000 items to the stack by calling the add method in the TwoLiter class. Note that the method is static - TwoLiter.add(twoLiterStack, 100000); + TwoLiter.add(twoLiterStack, 100000); // ToDo print the number of items in the TwoLiterStack object System.out.println("The stack has " + twoLiterStack.size() + " items in it.");