COMP2396B-Java代写-Assignment 1
时间:2023-03-03
COMP2396B Object-oriented Programming and Java
Assignment 1
Due date: 4th March 2023 (Saturday), 23:59
Introduction
This assignment tests your understanding of basic inheritance in Java.
The storyline of a certain role-playing game is that a student wanted to break into our general
office to steal the final examination paper. A security guard discovered and a combat took place
in our general office.
You are required to implement three classes to model some characters, which will enter a combat
with each other.
There are two kinds of characters – SecurityGuard and Student. Both of them are holding laser
guns and they will lose energy upon being attacked by laser guns. However, they have special
features as follows:
• The SecurityGuard can call the department head to boost his/her laser gun for a stronger
attack.
• The Student can hide himself/herself to dodge an attack.
You are required to implement a Character class, which represents a game character in general,
and two classes, SecurityGuard and Student, which are the subclasses of the Character class.
For this assignment, you are required to write JavaDoc comments for all classes and all methods.
A OfficeCombat class will be provided to simulate the fighting event.
Implementation
The Character class:
Properties
String characterName
Name of the character
int power
Power level of the character
int energyLevel
Energy level, which indicates the remaining energy of the character. The character
will be considered as lose when this value drops to zero or below.
Methods (all method must be public)
Character(String name, int power, int energyLevel)
Constructor, which setup the three properties accordingly
String getName()
Returns the name of the character
void hurt(int amount)
Reduces the energy level of the character by the specific amount.
It must print out “characterName takes a hurt of amount!
Remaining energy becomes energyLevel.”, with characterName,
amount and energyLevel replaced by the appropriate values.
int attack()
Returns the power of the next attack. It always returns the value of the level
property. It must print out “characterName launches an attack!”,
with characterName replaced by an appropriate value.
boolean isLose()
Return true if the character’s energy level drops to zero or below.
The SecurityGuard class, a subclass of the Character class:
Methods (all methods must be public)
SecurityGuard(String name, int power, int energyLevel)
Constructor, which setup the three properties accordingly
void boost()
Boost laser gun for the next attack. It must print “characterName boosts
its laser gun!”, with characterName replaced by an appropriate value.
int attack(), an overridden method
If boosted, return twice of the power level if it is boosted. Otherwise return the
power level as usual. Reset boosting effect afterwards.
If boosted, it must print “characterName makes a heavy strike!”.
Otherwise it must print “characterName strikes hard!”
(characterName must be replaced by an appropriate value).
The Student class, a subclass of the Character class:
Methods (all methods must be public)
Student(String name, int power, int energyLevel)
Constructor, which setup the three properties accordingly
void hide()
Hides himself/herself from the next attack. It must print “characterName
hides itself from attacks!”, with characterName replaced by the
appropriate values.
void hurt(int amount), an overridden method
If hidden, he / she takes no hurt at all. Otherwise reduce energy level with the
amount specified as usual. Reset the hiding status afterwards.
If hidden, it must print “characterName hides from the attack!”.
Otherwise it must print “characterName takes a hurt of amount!
Remaining energy becomes energyLevel” as usual.
(characterName, amount and energyLevel must be replaced by an
appropriate value.)
You may add additional properties and methods to these three classes.
The main program
A sample main class, OfficeCombat is given to test your program. Here is the code in the main
method of the class:
import java.io.*;
public class OfficeCombat {
public static void main(String[] args) throws IOException {
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
String sg_name = inData.readLine();
int sg_power = Integer.parseInt(inData.readLine());
int sg_engeryLevel = Integer.parseInt(inData.readLine());
String s_name = inData.readLine();
int s_power = Integer.parseInt(inData.readLine());
int s_engeryLevel = Integer.parseInt(inData.readLine());
SecurityGuard sGuard = new SecurityGuard(sg_name, sg_power, sg_engeryLevel);
Student student = new Student(s_name, s_power, s_engeryLevel);
System.out.println("Now fighting: " + sGuard.getName() + " vs. " + student.getName());
int round = 0;
while (!sGuard.isLose() && !student.isLose()) {
if (round % 2 == 0) {
int attackAmount = sGuard.attack();
student.hurt(attackAmount);
if (round % 3 == 0) {
student.hide();
}
} else {
int attackAmount = student.attack();
sGuard.hurt(attackAmount);
if (round % 3 == 1) {
sGuard.boost();
}
}
round++;
}
if (sGuard.isLose())
System.out.println(student.getName() + " wins! Exam paper stolen.");
else
System.out.println(sGuard.getName() + " wins! Exam paper is safe.");
}
}
Sample Input
Secure
10
15
Naughty
10
15
Sample Output
(once you’ve finished programming, running the given OfficeCombat.java with above input lines will give you
the following output)
Now fighting: Secure vs. Naughty
Secure strikes hard!
Naughty takes a hurt of 10! Remaining energy becomes 5.
Naughty hides itself from attacks!
Naughty launches an attack!
Secure takes a hurt of 10! Remaining energy becomes 5.
Secure boosts its laser gun!
Secure makes a heavy strike!
Naughty hides from the attack!
Naughty launches an attack!
Secure takes a hurt of 10! Remaining energy becomes -5.
Naughty wins! Exam paper stolen.
A Piece of Intelligence:
If doc comments for JavaDoc are not complete, you will have at most 15% of your marks deducted.
You will get 0 mark if:
you submit .class files instead of .java source files, or
you submit java source files that cannot be compiled, or
you submit java source files that are downloaded from the Internet, or
you submit java source files from your classmates, or
you submit java source files from friends taken this course last year.
Your program must be compliable and executable with the given OfficeCombat class.
Submission:
Please submit the following files to Moodle and evaluate.
• Character.java
• SecurityGuard.java
• Student.java
You must evaluate your program before the assignment due date in order to get marks. The auto-
grade system gives you a maximum mark of 85% for the correctness of program output. You
should make sure the program output is an exact match with the expected output. Any additional
or missing space character will lead to 0 mark.
TAs will manually assign the remaining 15% of marks for JavaDoc after the due date. Make sure
the doc comments in your submitted source files can generate proper JavaDoc on your computer.
You don’t need to submit the generated JavaDoc.