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

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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