Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Here is the text: This program will use built-in Java classes in the game of Fox, Chicken, and Grain. In this game there is farmer

image text in transcribed

Here is the text:

This program will use built-in Java classes in the game of Fox, Chicken, and Grain. In this game there is farmer on the south bank of a river who needs to transport a fox, chicken, and grain to the north shore. The boat is only large enough for one item or the farmer can travel alone. If the fox is left alone with the chicken, the fox will eat the chicken. If the chicken is left alone with the grain, the chicken will eat the grain. The game is won if the farmer and all three items end up on the north bank.

The FoxGame specification is presented below. Complete the methods of this class without changing the PRE/POST comments. Decide which type of collection is appropriate for this problem. Write an InvalidExceptionCollection class which reports message "Invalid Transport Value" when caught.

public class FoxGame {

String farmerLocation; // location of farmer: south or north

____________________ // items on south bank

____________________ // items on north bank

public FoxGame ( ) // POST: Add three items to south bank

{ } // Place farmer on south bank

// North bank is empty

public boolean won ( ) // POST: return true if game won

{ }

boolean lost ( ) // POST: return true if the game lost

{ }

public boolean found (String item) // POST: return true if item is on current bank

{ } or item is "none"

public void transport (String item) throws InvalidCollectionException

{ } // PRE: item is on current bank or string "none"

// POST: item removed from current bank

// added to opposite bank

// farmer is now on opposite bank

public String getFarmerBank ( ) // POST: return bank holding farmer: north/south

{ }

public String getOtherBank ( ) // POST: return bank not holding farmer

{ }

public String displaySouthBank ( ) // POST: return contents of south bank

{ }

public String displayNorthBank ( ) // POST: return contents of north bank

{ }

} }

Write a class A5FoxGame which creates a FoxGame object and plays the game. Use the try/catch mechanism to assist in error checking when the PRE condition of the transport method is violated.

This program will use built-in Java classes in the game of Fox, Chicken, and Grain. In this game there is farmer on the south bank of a river who needs to transport a fox, chicken, and grain to the north shore The boat is only large enough for one item or the farmer can travel alone. If the fox is left alone with the chicken, the fox will eat the chicken. If the chicken is left alone with the grain, the chicken will eat the grain. The game is won if the farmer and all three items end up on the north bank. The FoxGame specification is presented below. Complete the methods of this class without changing the PRE/POST comments. Decide which type of collection is appropriate for this problem. Write an InvalidExceptionCollection class which reports message "Invalid Transport Value" when caught. public class FoxGame i String farmerLocation: // location of farmer: south or north // items on south bank // items on north bank public FoxGame ( // POST: Add three items to south bank Place farmer on south bank North bank is empty public boolean won () /POST: return true if game won booleau lost ) I/ POST: return true if the game lost public boolean found (String item) I/ POST: return true ifitem is on current bank or item is "none" public void transport (String item) throws InvalidCollestionException PRE: tem is on current bank or string none // POST: item removed from current bank added to opposite bank farmer is now on opposite bank l/ public String getFarmerBank () / POST: return bank holding farmer: north/south public String getOtherBank() II POST: return bank not holding farmer public String displaySouthBank () I/ POST: return contents of south bank public String displayNorthBank () I/ POST: return contents of north bank Write a class A5FoxGame which creates a EoxGame object and plays the game. Use the try/catch mechanism to assist in error checking when the PRE condition of the transport method is violated

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions