Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Programming II Lab 6-Threading and Inheritance

Abstract: This lab is part 3 and the final part of a series of Labs. After this, you will have built a very crude but effective game simulation. It will involve warriors, weapons, armies, and a battlefield.

1. Create a new project, do not use the one from Lab 4
2. Copy all of the code and packages from Lab 4 into this new project.
3. Create one more package called Battle

a. Inside Battle you'll need three classes

i. Battlefield

1. This class should hold both ArmyOne and ArmyTwo
2. Create methods to:

a. Pass in and set each army (an army is an array list of Warriors)
b. Calculate the total remaining health of each army
c. Print out the attack strength of each warrior of each army

3. Is also responsible for having two methods that each specifically have one army attacking the other:

a. They should request the Lock and Unlock so only one can run at a time
b. Does the army attacking have any warriors left whose health is greater than 0?
c. To attack, get a warrior from the army and use its attack strength.
d. Check to see if the army they are attacking has any health left
e. If they do, pick a warrior from that army and attack them:

i. If they have more health than the attack, set the health to the difference. (Hint: you'll need to update the super class warrior and all sub class warriors)
ii. If the attack is greater, set the health to that warrior to zero and continue attacking until that attack is used up.
ii. ArmyOneAttacks thread class, takes a battlefield, count of times to attack, and a sleep delay in the constructor
iii. ArmyTwoAttacks thread class, takes a battlefield, count of times to attack, and a sleep delay in the constructor

4. In your Main class, do the following:

a. Create a battlefield object
b. Using the random generation of warriors create an army of 10 warriors and set that to Army One in the battlefield
c. Using the random generation of warriors create a different army of 5 warriors and set that to Army Two in the battlefield.
d. Create the two threads. Pass in the battlefield object and for count of attacks set each to 20. For ArmyOneAttacks make the sleep delay 3000. For ArmyTwoAttacks make the sleep delay 1000.

5. You will have to put print line statements in various places to produce the output below. It doesn't have to match exactly as we may have different warrior classes but it should be pretty close.

Output should read....
Army One's warriors:

classWarriors.InfantryWarrior who attacks -> 4
classWarriors.CavalryWarrior who attacks -> 8
classWarriors.ArtilleryWarrior who attacks -> 2
classWarriors.InfantryWarrior who attacks -> 4
classWarriors.CavalryWarrior who attacks -> 8
classWarriors.ArtilleryWarrior who attacks -> 2
classWarriors.InfantryWarrior who attacks -> 4
classWarriors.InfantryWarrior who attacks -> 4
classWarriors.CavalryWarrior who attacks -> 8
classWarriors.CavalryWarrior who attacks -> 8

Army One's total health is: 52
Army Two's warriors:

classWarriors.InfantryWarrior who attacks -> 4
classWarriors.CavalryWarrior who attacks -> 8
classWarriors.InfantryWarrior who attacks -> 4
classWarriors.CavalryWarrior who attacks -> 8
classWarriors.ArtilleryWarrior who attacks -> 2

Army Two's total health is: 26
Army One attacks and Army Two has 22 remaining.
Army Two attacks and Army One has 48 remaining.
Army Two attacks and Army One has 44 remaining.
Army Two attacks and Army One has 40 remaining.
Army Two attacks and Army One has 38 remaining.
Army Two attacks and Army One has 36 remaining.
Army Two attacks and Army One has 34 remaining.
Army Two attacks and Army One has 28 remaining.
Army One attacks and Army Two has 14 remaining.
Army Two attacks and Army One has 26 remaining.
Army Two attacks and Army One has 24 remaining.
Army Two attacks and Army One has 20 remaining.
Army Two attacks and Army One has 16 remaining.
Army One attacks and Army Two has 10 remaining.
Army One attacks and Army Two has 6 remaining.
Army Two attacks and Army One has 8 remaining.
Army Two attacks and Army One has 0 remaining.
Army One is defeated!!!!
Army One is dead and has no one left to strike.
Army One is defeated!!!!
Army One is defeated!!!!
Army One is defeated!!!!
Army One is dead and has no one left to strike.
Army One is defeated!!!!
Army One is defeated!!!!
Army One is defeated!!!!

Attachment:- Lab_4.zip

Java, Programming

  • Category:- Java
  • Reference No.:- M91888482

Have any Question?


Related Questions in Java

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Question slideshows or carousels are very popular in

Question : Slideshows (or carousels) are very popular in websites. They allow web developers to display news or images on the website in limited space. In this code challenge, you are required to complete the JavaScript ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

  • 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