Ask Computer Engineering Expert

Application:

HammurabiVideo games are a great example of complex programming. Even games as simple as Asteroidsor Pac-Mancontain many different elements that must work seamlessly together.

As a result, videogame programmers spend countless hours coding, testing, and debugging beforethe public can enjoytheir games.

In this Application,yourecreate a very old, simple computer game called Hammurabi.

Before Java existed, Hammurabiwas written in BASIC.

Your task is to write Hammurabias a well-structured Java program using a Scannerobject to handle input.

Here are the standard instructions for the game:

Congratulations, you are the newest ruler of ancient Samaria, elected for a 10year term of office. Your duties are to dispense food, direct farming, and buy and sell land as needed to support your people.

Watch out for rat infestations and the plague! Grain is the general currency, measured in bushels. The following will help you in your decisions:

- Each person needs at least 20bushels of grain per year to survive
- Each person can farm at most 10acres of land
- It takes 2bushels of grain to farm an acre of land
- The market price for land fluctuates yearlyRule wisely andyou will be showered with appreciation at the end of your term. Rule poorly and you will be kicked out of office!

Define a single class named Hammurabi. Use the following skeleton:

As you know, comments do not affect the program. Therefore, you may either leave the comments in your code or delete them.
/--
- The Hammurabi game.
- @author
Your name goes here
-/
public class Hammurabi { //save in a file named
Hammurabi.java
Random rand = new Random();
Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
new Hammurabi().playGame();
}
voidplayGame() {
//define local variables here: grain, population, etc.
// statements go after the declarations
}
//other methods go here
}

Start the game with the following:

100people

2800bushels of grain in storage

1000acres of land

Land value is 19bushels/acre

Each year, print out a summary following this format:

O great Hammurabi!

You are in year 1 of your ten year rule.

In the previous year 0people starved to death.

In the previous year 5people entered the kingdom.

The population is now 100.

We harvested 3000bushels at 3 bushels per acre.

Rats destroyed 200bushels, leaving 2800 bushels in storage.

The city owns 1000acres of land.

Land is currently worth 19 bushels per acre.

This summary represents the initial state, at the beginningof the first year-that is, when the playerfirst takesofficeand before the programdoesany of the computations below.

For example, the previous year (under a different ruler) started with 95 people; none starved, and fiveentered the kingdom.So as the playerentersoffice,he/sherules100people.

Write a playGamemethod that callsthe following methods each year, for up to 10 years. Each method asks the player one question. Call the methods in the order given, and do not allow the player to "back up" and change previous answers.

Your playGamemethod must read in all input from the player as strings. You must parse through the strings and determine if the input is valid.

Return a value from the method based on the player's input, as specified below:

intaskHowManyAcresToBuy(int price, int bushels)
Ask the player how many acres of land to buy, and return that number. The playermust have enough grain to pay for the purchase.

intaskHowManyAcresToSell(intacresOwned)
Ask the player how many acres of land to sell, and return that number. The playercannot sell morelandthan he/shehas. Do not ask this question if the player is buying land; it doesnotmake sense to do both in one turn.

intaskHowMuchGrainToFeedPeople(int bushels)
Ask the player how much grain to feedthepeople, and return that number. The playercannot feed peoplemore grain than he/shehas. The playercanfeed peoplemore than they need to survive, but it does not benefit the player.

intaskHowManyAcresToPlant(intacresOwned, int population, int bushels)
Ask the player how many acres to plant with grain, and return that number. The playermust have enough acres, enough grain, and enough people to do the planting. Any grain left over goes into storage for next year.
For each question, write code that checks the player's input. This is known as "sanity checking." For example, the program should make sure the playerhasenough grain, enough people to do the planting,etc. If the player enters an invalid value, have the programprint a message such as, O Great Hammurabi,surely you jest! We have only 3415 bushels left!Have the program keep asking until the player enters an allowable value.

After the above methods are called,the playGamemethod determinesthe following, in this order:

1.Is there a plague, and if so, how many people die from it.
2.How many people starve?
3.How many people come to the city?
4.How good the harvest is.
5.Is there a problem with rats, and if so, how much grain they eat.
6.The price of land for the following year.

Keep the above information in local variables in your playGamemethod. Then use that information to call the following methods, in the specified order, to make the necessary calculations. Use TDD-Test-Driven Development-to test as you go.

intplagueDeaths(int population)
Each year, there is a 15% chance of a horrible plague. When this happens, half of the player'speople die. Return the number of plague deaths (possibly zero). If a plague happens, print a separate line in the summary to notify the player of the number of plague deaths.

intstarvationDeaths(int population, intbushelsFedToPeople)
Each person needs 20 bushels of grain tosurvive. If the playerfeedsthem more than this, the peopleare happy, but the grain is still gone. The playerdoes not get any benefit from having happy subjects. Return the number of deaths from starvation (possibly zero).

boolean uprising(int population, inthowManyPeopleStarved)
Return trueif more than 45% of the people starve. This causesthe playerto be immediately thrown out of office, ending the game.

int immigrants(int population, intacresOwned, int
grainInStorage)
Nobody comesto the city if people are starving (so donot call this method if starvationDeaths is greater than 0). If everyone is well fed, compute how many people come to the city as: (20 - number of acres you have+ amount of grain you have in storage) / (100 - population) + 1.

int harvest(int acres)
Choose a random integer between oneand six, inclusive. Each acre planted with seed yieldsthis many bushels of grain. For example,if the playerplants50 acres, and the random integeris three, the playerharvests150 bushels of grain.Return the number of bushels harvested.

intgrainEatenByRats(int bushels)
Each year, there is a 40% chance ofa rat infestation. When this happens, rats eat somewhere between 10% and 30% of the player'sgrain. Return the amount of grain eaten by rats (possibly zero).

intnewCostOfLand()
The price of land is random, and ranges from 17 to 23 bushels per acre. Return the new price for the next set of decisions the player has to make. The player needsthis information in order to buy or sell land.

Do these computations, in this order,for each of the years that the player holds office, for up to ten years. Perform each computation in a separate method;none of these methods read or printanythingunless the user enters an invalid value.

When the computationsfor the current yearare finished, call a method
printSummaryto print the summaryof the current status of the kingdomat the beginning of the next year. This method requiresseveral parameters and its output willbe similar to the sample output
summary listed at the beginning of this assignment.

When the game ends, use a method finalSummaryto print out a summaryof the final year of the player's rule. This summaryprints the same information as the printSummarymethod, but also includes an overall evaluation of the player's rulebased onseveral of the variables in the program.You must decide which variables to include in the evaluation and how to compute the evaluation.

Note that your playGamemethod isquite long, but very straightforward; it does very little but call other methods.

Students often wonder how to test a method that returns a random result. The answer is-the same way you test any method-call it and examine the results. For a method that uses random numbers, call it many times and determine if the average result of the method is consistent with your expectations.

For example: If you call plagueDeathswith 1,000 people, 15% of the time it will return 500, and the other 85% of the time it will return zero. The expected result is0.15-500 + 0.85-0 = 75, so if you call the method 1000 times and add the results, you should get a total close to 75,000. You could then say:

assertTrue(65000

This is not a perfect test, but it is certainly better than no test at all.

Include screenshots of your program running as part of your submission. Show each action being performed and the summary for each year. Also show attempts to enter invalid values. Perform one play-through of the game that results in a successful ten-year rule, and one that results in the people uprising and overthrowing the player. Show screenshots of your program being tested with JUnit.
Save your NetBeans Project and screen shots of the working program as a ".zip" file.

Attachment:- Hammurabi-game.rar

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M91897369

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As