Computers and Technology

Grocery FRQ (ArrayLists) public class Grocery
{
private String category;
private int units;
private double price;
/*there may be other instance variables, constructors and methods not shown*/
public boolean equals (Grocery g)
{
/*implementation not shown*/
}
public int getUnits(){
return units;
}
public double getPrice(){
return price;
}
public String getCategory(){
return category;
}
public class Shopping
{
private ArrayList myGroceries;
public Shopping(Grocery[] groc){
/*to be implemented in part A*/
}
public Grocery findBestValue(String c){
/*to be implemented in part B*/
}
}
The Shopping constructor initializes the myGroceries instance variable with elements from the groc array. Only unique Grocery items are added to the myGroceries list (i. e. there are no duplicate Grocery items in myGroceries). A Grocery item is considered unique if at least one of the attributes differs (the category, units, and/or price). For example, if given the code:
Grocery[] theGroceries = {new Grocery("cereal", 1, 4.99), new Grocery("milk", 1, 4.29),
new Grocery("cereal", 2, 7.99), new Grocery("cereal", 1, 4.99),
new Grocery("candy", 48, 10.99), new Grocery("candy", 6, 1.00)};
Shopping myShopping = new Shopping(theGroceries);
The Grocery item "cereal", 1, 4.99 is not unique as there is another Grocery item with the same category, unit, and price.
The Grocery item "cereal", 2, 7.99 is unique because it is different from the other Grocery item by both the number of units and price.
Then the myGroceries arraylist of the Shopping class would be initialized with the following Grocery contents:
"cereal", 1, 4.99
"milk", 1, 4.29
"cereal", 2, 7.99
"candy", 48, 10.99
"candy", 6, 1.00
Complete the Shopping constructor.
Grocery items can be categorized. Grocery items have a number of units (quantity) and price (for the collection of units). The findBestValue method locates all Grocery items of the Shopping class with the same category c and determines the best priced option of the category by determining the lowest price per unit. The findBestValue method returns the best priced Grocery item of category c.
Precondition - there exists at least one such element with category c.
For example, the myGroceries arrayList contains the following Grocery items:
"cereal", 1, 4.99 (price per unit is 4.99)
"milk", 1, 4.29 (price per unit is 4.29)
"cereal", 2, 8.00 (price per unit is 4.00)
"candy", 50, 10.00 (price per unit is 0.20)
"candy", 10, 1.00 (price per unit is 0.10)
Then a call to findBestValue() would return the last Grocery item (with the category "candy", number of units is 10 and price is 1.00);
Write the findBestValue() method below.

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 19:50, rosyposy43
Write a car class having two private member variables called tank and speed. write public methods called pumpgas and gofast. the method pumpgas gets an integer for gas that must be pumped. that value needs to be added to tank (no more than 20 gallons). it must return the amount of gas that is purchased ($4 per gallon). the method gofast should increase the speed by 5 each time it is called. write a constructor for the above class that initialized both variables to zero. write a tostring to display both the tank and speed when the car is printed. modify the car class to implement the interface comparable and an interface called carinter having the public methods in carinter. write the main program to create an array of size 5 of type car. create 5 car objects having each location of the array to refer to one of the cars. test the pumpgas, gofast, equals method on the array items. write an enhanced loop to print all the car values (using a tostring written last time).write a generic method to find the minimum of four items. pass int, double, char, string and car objects to test this method.
Answers: 1
image
Computers and Technology, 22.06.2019 20:00, random286
How is the number 372 written when expanded out to place values in the base 8 (octal) number system? a. 2 x 4 + 3 x 2 + 4 x 1 b. 3 x 64 + 7 x 8 + 2 x 1 c. 3 x 8 + 7 x 7 + 2 x 6 d. 3 x 100 + 7 x 10 + 2 x 1
Answers: 1
image
Computers and Technology, 23.06.2019 12:30, umimgoingtofail
What is the difference between the internet and the world wide web?
Answers: 1
image
Computers and Technology, 23.06.2019 22:20, andrew412603
Learning sign language is an example of a(n) learning sign language is an example of a(n)
Answers: 2
Do you know the correct answer?
Grocery FRQ (ArrayLists) public class Grocery
{
private String category;
private...

Questions in other subjects: