Your task is to write a program to sort the sacks of grain that Jack plundered b
ID: 3854098 • Letter: Y
Question
Your task is to write a program to sort the sacks of grain that Jack plundered by the type of grain in each sack, and calculate how much of each type of grain he has. This will be done by adding all the sacks of grain containing one kind of grain to its own list.
Create a new a class called A1Q1. Add the generatePlunder method (given below) to this class. Add the provided Sack.java to your project.
Add a main() method to your A1Q1 class. Inside it, write a program that does the following:
1. Call the following function to generate some data that represents Captain Jack’s plunder for the day:
public static Sack [] g e n e r a t e P l u n d e r ( int howMany ) { Random g ene ra to r = new Random (); Sack
grain [] = new Sack [ howMany ];
for ( int i =0; i < howMany ; i ++) { grain [ i ] = new Sack (
Grain . values ()[ g en er at or . nextInt ( Grain . values (). length )] , ge ne ra to r
. n e x t D o u b l e () * 100 ); }
return grain ; }
This will return array of randomly generated Sack objects with howMany elements. Sack is a simple class that stores the type of grain in a sack (as the enumerated type Grain), and how much it weighs.
The Sack class is provided for you to use. You’ll that see its quite straightforward.
Create an array of linked lists of Sack objects; there should be one list for each type of grain, including OTHER. This should be an array of LinkedList<Sack> objects. Each list in the array will store Sack objects for one and only one type of grain. All of the sacks containing OTHER grain should go on the same list. Remember to create not only the array, but also instantiate a list for each element of the array.
Put each Sack object in the array you created in step 1 onto one of the lists you created in step 2. Use the ordinal of the grain type of the sack object to index the array of linked lists to find the correct list for the type of grain, and add the sack object to that list.
4. Go through each list and compute the total weight of each type of grain that Jack plundered. Remember that since the data is randomly generated, it is possible for a list to be empty!
5. Print a report of Jack’s plundering to the console. Below is a sample of what that should look like.1
Jack plundered 0.0 pounds of WHEAT
Jack plundered 7 5 . 8 4 2 2 9 8 4 9 4 3 7 3 5 pounds of BARLEY
Jackplundered 74.01721574496484 pounds of OATS
Jackplundered 48.82389493369351 pounds of RYE
Jackplundered 44.962951754620065 pounds of OTHER
SACK.java
enum Grain {
WHEAT, BARLEY, OATS, RYE, OTHER
}
public class Sack {
protected Grain type;
protected double weight;
/**
* @param type - the type of grain
* @param weight - how much grain in pounds
*/
public Sack(Grain type, double weight) {
super();
this.type = type;
this.weight = weight;
}
/**
* @return the type of grain
*/
public Grain getType() {
return type;
}
/**
* @param type the type of grain for this plunder
*/
public void setType(Grain type) {
this.type = type;
}
/**
* @return the weight of grain for this plunder
*/
public double getWeight() {
return weight;
}
/**
* @param weight the weight of grain for this plunder
*/
public void setWeight(double weight) {
this.weight = weight;
}
public String toString() {
return "("+ type.toString() + ", " + weight + ")";
}
}
3 Your Tasks Question 1 (8 points): Tractor Jack is a notorious pirate captain who sails the Suskatchawan River plundering farrns for whoat, barley, and all the other grains At the end of each day, Jack enters into his computer a log of each sack of grain he has plundored, what kind of grain it is, and how much it woighs You wil help Jack Write a program to organize this data, and ca culate much of each type of grain he plundered Enumerations In this question we're gcing to use a data type in Java caled an enumeration. Enumerations define a focad set of named constant values. The grains Jack most commonly plunders are wheat, barley, oats and rye so he wants tlo count the amount of those four grains separately. Arny other types of grain he wants to count together. We can use an enumeration to dofine five constants to denate what type of grain is in a sack enum Grain WHEAT,BARLEY, OATS, RYE, OTHER This declaraton defnes a data type called Grain and five values which we can assign to variabes of type Grain. You can find it at the top of Sack.java. Now we can then write in Java: Grain g Grain. WHEATW Assign vaue WHEAT to the variable g Here are some other thing we can do with enumerations that you will need Enumeration types have a static method called values() which returns an array of the values defnes. For example: W This roturns the array: (WHEAT, BARLEY, OATS, RYE, OTHER) Grain values (: Each value defined by the enumeration is associated with an integer value between 0 andN (where N is the number of values in the enumeraton). These ordina's are a convenient wary to map values n an enumeration to offsets of an N-element array Grain g Grain, OATS g. ordinal () returms the integer 2 W becausOATS is the 3rd value in the enum g Grain.WHEAT: g.ordinal 0 the integer O W becaus WHEAT is the 1st value in the enum for tho Yau can find out how mary by the values) method: valuos are in an Grain values ( longth tíss5 bacause there are fve values Page 4Explanation / Answer
A1Q1.java
=================================================================================
Sack.java
===================================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.