Write hammurabias a well-structured java program

Assignment Help Computer Engineering
Reference no: EM131141868

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 <totalDeaths&&totalDeaths< 85000);

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

Reference no: EM131141868

Questions Cloud

Write paper on the stages and elements of the audit process : Write a 4-5 page paper on the stages and elements of the audit process. For each stage, list the elements of the audit process and the pivotal parts of each stage.
Determine the influence line ordinates : The two-span continuous beam shown in Figure S5.1has a second moment of area that varies linearly from a maximum value of I at the center to zero at the ends. Determine the influence line ordinates, at intervals of 0.2 l , for the central vertical..
Describe the primary issues presented in the case study : Describe the primary issues presented in the case study. What are some of the main tenets of Hasidic Judaism? Discuss how cultural and religious beliefs may influence Hasidic Jewish patients' desire to hide or conceal medical information
List the auditing procedures you should consider performing : List the auditing procedures you should consider performing to gather evidence concerning subsequent events. What is your responsibility to report errors and fraud as detected to management, the board of directors, and parties outside the entity?
Write hammurabias a well-structured java program : Your task is to write Hammurabias a well-structured Java program using a Scannerobject to handle input - 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 t..
How would you respond to your partner : In this class, you have reflected on multiple aspects of critical thinking. What did you get out of the class? How would you describe this class to others? If you were marketing this class, how would you sell this class to other individuals or for..
Explain the importance of examining socio-cultural factors : How would a psychologist explain the importance of examining socio-cultural factors in developmental research
Explain the importance of examining cross-cultural research : How would a health care professional explain the importance of examining cross-cultural research when searching for developmental trends in health and wellness
What are your responsibilities to detect fraud : What are your responsibilities to detect fraud while performing a financial statement audit? (Do not discuss specific audit procedures, only the responsibilities.)

Reviews

Write a Review

Computer Engineering Questions & Answers

  Questionproduce an average class with a public data member

questionproduce an average class with a public data member to collect the sum of integer entries and a public data

  Write down a program that ask the user to enter a word

Write down a program that ask the user for starting value and an ending value and then writes all the integers (inclusive) between those two values.

  Assess the significance of measuring storage efficiency in

question 1. evaluate the importance of measuring storage efficiency in an organizations storage system. suggest the

  Explain what fields may be used as keys and indexes

select an information system in a health care organization that uses a database. The application can be simple or complex, but it must utilize a database that is part of an overall system to collect, store, process, and disseminate information.

  Make a list of files that are world-writable

make a crontab to perform the tasks listed below at the frequencies specified. Note that you do NOT need to write the actual scripts.

  Writing c code to determine the balance

Write down a program in C++ which determines the balance because of each month on a non-interest loan. Ask user for the loan amount and how much s/he will pay each month.

  Explain the sum of the inverses of all positive integers

though , when we compute the sum in Java's floating point arithmetic the largest value we get is 15.403683, regardless of how many terms we add. Explain the possible reasons for this anomaly.

  How to convert 2d array to 1d array

how to convert 2d array to 1d array for instance

  Why should assembly language be avoided

Why should assembly language be avoided for general application development? Under what ciscumstances is assembly language preferred ir required?

  Why array names are not assignable variables in c

Why  array names are not assignable variables in c

  Where is the reason of most of the attacks

Where is the reason of most of the attacks.explain the differences between the two main classes of intrusions: misuse and anomaly.

  Consider the ways in which you can optimize a file

consider the ways in which you can optimize a file in order to reduce file size and maintain quality. What factors would you consider.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd