Answered step by step
Verified Expert Solution
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 Im 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 nfformatcost;
public String getType
return type;
public void setTypeString type
this.type type;
public int getNumInPck
return numInPck;
public void setNumInPckint numInPck
this.numInPck numInPck;
public double getCost
return cost;
public void setCostdouble cost
this.cost cost;
DRIVER CLASS TO BE FINISHED:
import java.util.ArrayList;
public class CookiesDriver
public static void mainString args
always make your array lists generic
ArrayList candy new ArrayList;
candy.addnew CookiesOreos;
candy.addnew CookiesMacaroon;
candy.addnew CookiesChocolate Chip", ;
candy.addnew CookiesPeanut Butter", ;
candy.addnew CookiesMacaroon; I know it is here multiple times
candy.addnew CookiesMacaroon;
candy.addnew CookiesMacaroon;
candy.addnew CookiesBiscotti;
candy.addnew CookiesSugar;
you could use the following to print out all of the cookies
Systemout.printlncookies;
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
printcandy;
I want to add an Oatmeal Raisin at the end the default location
candy.addnew CookiesOatmeal Raisin", ;
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.addnew CookiesGingerbread;
print the names again by calling our method
printcandy;
remove the Peanut Butter. But I first have to find where it is
try
Cookies c new Cookies Peanut Butter", ;
int location candy.indexOfc;
System.out.printlnlocation;
but location shows as 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 for the location of unsuccessful searches.
System.out.printlnRemoving the Peanut Butter...";
for int i ; i candy.size; i
c candy.geti;
if cgetTypeequalsPeanut Butter"
candy.removei;
That works!! Check it out
printcandy;
System.out.println;
NOW YOU FINISH THESE!!!! FIND METHODS FOR THESE!!
System.out.println
Here is where I am starting ;
System.out.printThe 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
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 printArrayList c
for int i;i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started