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
6 changes: 6 additions & 0 deletions TwoLiterStack/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
6 changes: 6 additions & 0 deletions TwoLiterStack/bin/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion TwoLiterStack/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/main/
/src/
17 changes: 17 additions & 0 deletions TwoLiterStack/bin/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TwoLiterStack</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
14 changes: 14 additions & 0 deletions TwoLiterStack/bin/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
27 changes: 20 additions & 7 deletions TwoLiterStack/src/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,53 @@
* 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<TwoLiter> twoLiterStack = new Stack<TwoLiter>();

// 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.");

// 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);


}
}
46 changes: 32 additions & 14 deletions TwoLiterStack/src/main/TwoLiter.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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<TwoLiter> twoLiterStack, int count ) {
public static void add(Stack<TwoLiter> 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()));
Expand All @@ -56,49 +67,56 @@ public static void add(Stack<TwoLiter> 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;
}
}