Answered step by step
Verified Expert Solution
Question
1 Approved Answer
3 Your Tasks Win the Grain enumeration Question 1 (8 points) Because enumerations have toString) methods, we can print enumerated types, and concatenate thom with
3 Your Tasks Win the Grain enumeration Question 1 (8 points) Because enumerations have toString) methods, we can print enumerated types, and concatenate thom with strings. For example: Tractor Jack is a notorious pirate captain who sails the Saskatchewan River plundoring farms for wheat barley, and all the other grains. At the end of each day, Jack enters into his computer a log of each ack af grain he has plundored, whut kind of grain it is, and how much it woighs. You will heip Jack write a program to orgarize this data, and caculate much of each type of grain he plundered Grain g Grain . RYE ; System, out. printin (g) W prints: RYE System. out. printin("The grain is: '+g " prits: The grain is RYE The Problem In this question we're going to use a data type in Java caled an enumeration. Enumeratons define a fixed set of narned constant values. The grains Jack most commonly plunders are wheat, barlcy, oats and rye so he wants to count the amount of those four grains separately. Any other types of grain he wants to count together. We can use an enumeraton to define five constants to dencte what type of grain is in a sack: Your task is to write a program to sort the sacks of grain that Jack plundered by the type of grain in oach sack, and calculate how much of each type of grain he has. This wil be done by adding all the sacks of grain containing one kind of grain to its own Ist Create a new project that references lib280-asn1 as descrbed in the sef-guided tutorial on the dass website Add to a class called A1Q1. Add the generatePlunder method (given below) to this class. Add the provided Sackjava to your projoct enum Grain WHEAT,BARLEY, OATS, RYE. OTHER Add a main) mothod to your A101 class. Inside it, write a program that does the folowing This decanton defines data type called Grain and fve values which wg can assign to variables of . Call the follawing functian to gonerate some data that reprosents Captain Jack's plunder for the type Grain. You can fnd it at the top of Sack.java. Now we can then write in Java Grain g Grain. WHEAT: W Assign value WHEAT to the variable g Here are some cther thing we can do wth enumerations that you will need day public static Sack D g eneratePlunde r (int howMany) Random g ene ra tor new Random 0: Sack grain = new Sack [ howMany I; Enumeration types have a static method ca led values) hich returns an array of the velues it defines. For exampe: for ( int i1 objects. Each list in the aray will store Sack objects for one and only one type of grain. All of the sacks containing OTHER grain should go on the same ist. Remember to croate nct only the array, but also instantiate a list for each element of the array. the nteger O W becaus0 WHEAT is the 1st value in the enum Ycu can find out how marry values are in anonumoration asking for tho longth of tho array roturned by the values) method: 3. Put each Sack object in the array you created in step 1 onto one of the ists you created in slep 2 Use the ardinal of the grain type of the sack abjoct to index the aray of linked lists to find the correct list for the type of grain, and add the sack object to that ist Grain. values ( longth W this is 5 because there are five value Page 4 Page 5
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