Question: After you review the Java programs below . Add code so that the main ( ) method in the ( CookiesAndBookDriver ) below sorts the

After you review the Java programs below . Add code so that the main() method in the ( CookiesAndBookDriver ) below sorts the collections and then prints them again. Use the Comparable interface to do so.(COMPLETE THE SORTING IN THIS JAVA PROGRAM/DRIVER)-
import java.util.ArrayList;
import java.util.Collections;
public class CookiesAndBookDriver {
public static void main(String[] args){
ArrayList books = new ArrayList();
loadBooks(books);
ArrayList yum = new ArrayList();
loadCookies(yum);
printCookies(yum);
printBooks(books);
//sort the Cookies by their cost() method from cheapest to most expensive. Implement Comparable for the sort
//sort the Books by their title. Use a bubble sort.
printCookies(yum);
printBooks(books);
}
public static void loadCookies(ArrayList c){
c.add(new Cookies("Chocolate Chip",0.75,12));
c.add(new Cookies("Peanut butter",0.90,6));
c.add(new Cookies("Oreos",1.10,12));
c.add(new Cookies("Chocolate M&Ms",0.70,6));
c.add(new Cookies("Double Chocolate",0.85,12));
c.add(new Cookies("Key Lime",0.65,12));
c.add(new Cookies("Monster",1.05,6));
}
public static void loadBooks(ArrayList books){
books.add(new Book("Java is Fun!", 100,54.50));
books.add(new Book("Databases for the Common Man", 200,67.00));
books.add(new Book("Networking is Nifty!", 130,45.00));
books.add(new Book("Web Design Fundamentals", 40,120.00));
books.add(new Book("Python Beginnings", 85,34.00));
books.add(new Book("Server Side Java for Money", 100,24.50));
}
public static void printCookies(ArrayList cook){
System.out.println("
The cookies are:");
for (int i =0; i < cook.size(); i++)
System.out.println(cook.get(i).toString());
}
public static void printBooks(ArrayList b){
System.out.println("
The books are:");
for (int i =0; i < b.size(); i++)
System.out.println(b.get(i).toString());
}
}
(COOKIES CLASS)-
import java.text.NumberFormat;
import java.util.Scanner;
public class Cookies {
private String name;
private double costEach;
private int number;
public Cookies(){
}
public Cookies( String n, double c, int num){
name = n;
costEach = c;
number = num;
}
public String toString(){
NumberFormat nf = NumberFormat.getCurrencyInstance();
return name +" cost "+ nf.format(costEach)+" each and you ordered "+ number +
" Total cost: "+ nf.format(cost());
}
public double cost(){
double amount = costEach * number;
return amount;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public double getCostEach(){
return costEach;
}
public void setCostEach(double costEach){
this.costEach = costEach;
}
public int getNumber(){
return number;
}
public void setNumber(int number){
this.number = number;
}
}
(BOOK CLASS)-
public class Book {
private String title;
private int numInStock;
private double cost;
private int stockNum;
private static int nextNum=100;
public Book()
{
stockNum = nextNum;
nextNum = nextNum +100;
}
public Book(String title, int numInStock, double cost){
this.title = title;
this.numInStock = numInStock;
this.cost = cost;
stockNum = nextNum;
nextNum = nextNum +100;
}
public String toString()
{
return title +" with id number "+ stockNum +" has "+ numInStock +" books in stock costing "+ cost + "each.";
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public int getNumInStock(){
return numInStock;
}
public void setNumInStock(int numInStock){
this.numInStock = numInStock;
}
public double getCost(){
return cost;
}
public void setCost(double cost){
this.cost = cost;
}
public int getStockNum(){
return stockNum;
}
public void setStockNum(int stockNum){
this.stockNum = stockNum;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!