Java

TOPIC 09 – INTRODUCING OBJECTS

 

LESSON WORK

 



QUESTION 1 (GROUP WORK)

We will do this question (1A, B and C) together as a group.

 

QUESTION 1A

Create a BadGuy class with datafields for

  • name – the name of the individual
  • hp – the individual's health points
  • hpMax – the individual's max health points
  • damage – the damage the individual causes when attacking

 

QUESTION 1B

Create a BadGuyTester class that will create two different BadGuy objects.  Make sure you fill in their datafield values.

 

QUESTION 1C

Continuing in the same class as 1B, make each BadGuy attack the other and update their datafields accordingly (hp will be reduced by the other's damage). 

 

Then output the state of each BadGuy with a line similar to:

 

   Godzilla now has 932 of 950 hp.

 

QUESTION 2

Create a class called Student and come up with at least five attributes that can be represented as datafields.

 

Then, in another class called StudentTester, inside the main function, create the student object that represents you.  Simply set each datafield to correspond to you.  Then output the values to screen.


QUESTION 3

Consider the following class:

 

public class BankAccount

{

   public double balance;

   public double atmCharge;

   public double monthlyCharge;

}

 

Create a BankAccount object and write the required statements to update the account so that it matches the actions below.  Output the balance of the account after each action.

 

ACTIONS

1-You open an account with 500 dollars in it.  The account has a 5 dollars per month charge to run the account and you pay 2 dollars for each withdrawal at an ATM.

 

2-You deposit 200.0 into the account.

 

3-It is the end of the month.

 

4-You deposit 150.0 into the account.

 

5-You withdraw 300 from the account.

 

6-You withdraw 100 at an ATM from the account.

 

7-It is the end of the month.

 

8-Output the value of the account’s balance to the screen.  Compare your answer with your neighbour if possible.