Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Copy the Cookies and CookiesDriver classes that are found in the JAVA code I ' m providing. Finish the code in CookiesDriver. Start at the

Copy the Cookies and CookiesDriver classes that are found in the JAVA code I'm providing. Finish the code in CookiesDriver. Start at the /////// NOW YOU FINISH THESE!!!! FIND METHODS FOR THESE!!
Cookies class:
import java.text.NumberFormat;
public class Cookies {
private String type;
private int numInPck;
private double cost;
public Cookies(){
}
public Cookies( String t, int n, double c){
type = t;
numInPck = n;
cost = c;
}
public String toString(){
NumberFormat nf = NumberFormat.getCurrencyInstance();
return type +" has "+ numInPck +" cookies and a cost of "+ nf.format(cost);
}
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
public int getNumInPck(){
return numInPck;
}
public void setNumInPck(int numInPck){
this.numInPck = numInPck;
}
public double getCost(){
return cost;
}
public void setCost(double cost){
this.cost = cost;
}
}
DRIVER CLASS (TO BE FINISHED):
import java.util.ArrayList;
public class CookiesDriver {
public static void main(String[] args){
// always make your array lists generic
ArrayList candy = new ArrayList();
candy.add(new Cookies("Oreos",12,2.50));
candy.add(new Cookies("Macaroon",5,1.35));
candy.add(new Cookies("Chocolate Chip", 12,.80));
candy.add(new Cookies("Peanut Butter", 4,1.90));
candy.add(new Cookies("Macaroon",5,1.35)); // I know it is here multiple times
candy.add(new Cookies("Macaroon",5,1.35));
candy.add(new Cookies("Macaroon",5,1.35));
candy.add(new Cookies("Biscotti",12,1.90));
candy.add(new Cookies("Sugar",16,1.90));
//you could use the following to print out all of the cookies
//System.out.println(cookies);
// but these puts the entire collection in [] and we do not want that
// It is better to do it explicitly yourself!!
//I have written a method at the bottom to print just the names so we do not have to keep
//repeating the code
print(candy);
// I want to add an Oatmeal Raisin at the end (the default location)
candy.add(new Cookies("Oatmeal Raisin", 12,2.55));
//now add a Gingerbread cookie between the Oreos and first Macaroon
// I use the overloaded method to tell it the location of where I want it
candy.add(1,new Cookies("Gingerbread",10,2.30));
//print the names again by calling our method
print(candy);
// remove the Peanut Butter. But I first have to find where it is
//try
Cookies c = new Cookies ("Peanut Butter", 4,1.90);
int location = candy.indexOf(c);
System.out.println(location);
//but location shows as -1 which means it does not exist
// It is looking for the object - not an object with the same values
// for the fields. Since they are not the same object, it is not
// found. Java returns a -1 for the location of unsuccessful searches.
System.out.println("Removing the Peanut Butter...");
for (int i =0; i < candy.size(); i++){
c = candy.get(i);
if (c.getType().equals("Peanut Butter"))
candy.remove(i);
}
//That works!! Check it out
print(candy);
System.out.println();
/////// NOW YOU FINISH THESE!!!! FIND METHODS FOR THESE!!
System.out.println("
**** Here is where I am starting ****");
System.out.print("The last item in the list is ");
// print out the toString() for the last item in the list
// remove all of the locations of Macaroon
//print out all of the names of the cookies
//print out how many items there are currently in the collection
// Print the name of all cookies that come by the dozen on one line - have numInPck as 12
//you need to find it first - you cannot just count in the ArrayList to find its location
//print out all of the name of cookies that start with the letter O. Print them out all in capitals
// HINT!!! There is no single method for this.
// You will need to loop through the ArrayList getting
// each entry's name and checking to see if the string starts with an O.
// clear out all the members of the ArrayList
// print out the ArrayList now and the size now.*/
}
public static void print(ArrayList c){
for (int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions