代写CS 1410、代做java程序语言
- 首页 >> OS编程 CS 1410-001 Fall 2023
Assignment 8: Gacha game
Assignment 8: Gacha game
Due Friday by 11:59pm Points 100
Submi!ng an external tool
Available un!l Nov 12 at 11:59pm
This tool needs to be loaded in a new browser
window
The purpose of this assignment is to prac!ce
inheritance concepts by making an abstract
class and extending it with several subclasses,
as well as implemen!ng Java's Comparable
interface to allow for sor!ng of an ArrayList
.
Background
In a gacha game , a player can acquire items
by drawing randomly-selected items of
varying quality. The goal is to draw more and
more items to get the best ones possible. In
our game, there are four types of items: tools,
armor, magic, and upgrades. Your task is to
create the classes represen!ng these four
types, which extend one abstract class for all
items. These classes must implement Java’s
Comparable interface so that items can be
sorted.
Setup
To get started, add a new package
called assign08 and import GachaGame.java
. This provided class handles the
interac!ve mechanics of the game and
contains the main method.
Requirements
Create five new classes in the
assign08 package: one abstract class Item and
four subclasses Tool, Armor, Magic, and
Upgrade, all of which extend Item. Also,
complete the getNewItems
and getRandomItem methods in the provided
GachaGame class.
Item abstract class
The Item class has one private instance
variable:
a string to represent the name of the item
The following constructor must be defined:
public Item(String name)
Also, the following instance methods must be
included:
public String getName()
public abstract String getDescrip!on()
public abstract void useUpgrade()
Finally, the Item class must implement a
specific version of the Comparable interface,
wri"en as “Comparable- ”, which means
it is for comparing objects of the Item class.
The compareTo method required by the
Comparable interface does not appear in the
Item class itself (since Item is an abstract class).
Instead, compareTo must appear in each class
that extends Item (see below.)
Subclasses
The Tool, Armor, Magic, and Upgrade classes
must extend Item and define the inherited
abstract methods.
In addi!on to any inherited instance variables,
each subclass needs its own unique variables:
Tool – an integer for the power of the tool
Armor – an integer for the armor’s defense
value
Magic – an integer for the power of the
magic and an integer for its cost
Upgrade – a boolean for whether the
upgrade has been used or not
The following constructors must be defined:
public Tool(String name, int power)
public Armor(String name, int defense)
public Magic(String name, int power, int
cost)
public Upgrade()
The name parameter of the first three
constructors should be used to set the
inherited name instance variable, by calling the
superclass constructor. The name of Upgrade
objects should always be "Upgrade".
public String getDescrip"on() for each
subclass returns a String describing the item.
Examples for an item named “My item”:
For a Tool object with 400 power → “Tool:
My item – power=400”
For an Armor object with 400 defense →
“Armor: My item – defense=400”
For a Magic object with 400 power and 20
cost → “Magic: My item – power=400,
cost=20"
For an Upgrade object that is not used →
“Upgrade: ready”
For an Upgrade object that is used →
“Upgrade: used”
public void useUpgrade() for Tool, Armor and
Magic increase the stats for power, defense,
and cost by at least one. How exactly it
increases is le$ up to you. However, if the
values get too large it may cause errors, so try
to keep it under 100,000,000 even a$er
several consecu!ve upgrades. Also make sure
that the stats increase by at least one for each
upgrade.
For the Upgrade class, calling useUpgrade
should make the “used” variable true.
public boolean isUsed() should be in the
Upgrade class only and return a boolean for
whether the upgrade object is used or not.
Implemen!ng Comparable
The purpose of implemen!ng Comparable is so
that we can sort an ArrayList of Item objects.
Note that should not do any sor!ng in the
code that you write, since the provided code
already does this. If you are curious, look for
the method call Collec!ons.sort(inventory) and
recall sor!ng an example ArrayList object in
Class Mee!ng 13. By implemen!ng the
Comparable interface for all items in our game,
we unlocked the ability to use the sort
method.
See Java's documenta!on for the method
you must include in each subclass, in order to
implement the Comparable interface. Scroll
down to the sec!ons named "Method
Summary" and "Method Details". Note that
the type of the parameter is wri"en as T. In
our class, T should be replaced with Item.
In wri!ng the compareTo method to compare
two objects (this and the parameter), the
following rules apply:
When comparing Item objects of different
types: Tool > Armor > Magic > Upgrade
For two Tool objects: compare the power
numbers
For two Armor objects: compare the
defense numbers
For two Magic objects: compare the
frac!on power/cost and use cau!on when
dividing integers
For two Upgrade objects: unused > used,
otherwise they are equal
Note that the parameter of the compareTo
method has type Item. Recall that the
instanceof operator determines whether an
object has certain data type. Use this
operator to determine if the Item parameter
has the same type as a subclass; e.g.,
if(other instanceof Tool)
...
else if(other instanceof Armor)
...
Complete the GachaGame
class
getRandomItem() creates a new Item object of
one of the four subclasses at random. Use a
Random object to generate a random number
from 0 to 3 (inclusive) to choose the subclass.
Use the provided makeItemName method to
create a random name. Use the Random
object again to generate the needed stats for
the item. The values you generate for the
stats are le$ up to you, but they must be
randomly chosen and at least 1. Also, be
careful that the stats values are not so big that
your useUpgrade method could cause
problems (value under 1,000,000 are probably
safe).
getNewItems(int howMany) uses the
getRandomItem method to fill an array with
howMany new Item objects. Polymorphism
means that we do not need separate methods
and separate arrays to get and store different
types of items, since they all belong to the
Item superclass. The body of this method is
simple, and we can add new Item subclasses in
the future without having to change this
method.
Tes!ng
Create a JUnit test class called ItemTest. Add
tests to thoroughly check the normal and edge
cases of all instance methods in the four
subclasses, paying par!cular a"en!on to
the compareTo methods. Note that you are
not able to make an instance of Item because
it is an abstract class.
To test your comple!on of the two empty
methods in the GachaGame class, try playing
the game:
1. Run the GachaGame main method by
pressing the green run bu"on in Eclipse.
2. The game repeatedly prompts for one of
the following input commands:
"get n", where n is a number between 1
and 10, gets n new items and prints the
best ones in your inventory.
"upgrade" applies any unused upgrades
to your best items and prints the result.
"show" sorts and displays your en!re
inventory.
"exit" ends the game.
Note that ge&ng new items may or may not
change the printed best ones. Use the "show"
command to see your en!re inventory and
make sure that things are sorted as expected
(grouped by type and ordered from worst to
best). Con!nue un!l you acquire upgrade
items and use "upgrade" to make sure that the
stats of your best items are increasing
according to your useUpgrade method code.
The "show" command should now display the
upgrades as used. For example, entering "get
10" followed by "show" could produce the
following output. Remember that this is
random, so you will not get exactly the same
result.
Next command? (get n (0ow, exit)
get 10
Getting 10 new items.
Best equipment:
- Tool: mighty rock of doom - power=276
- Armor: quick boots of satisfaction - de
fense=883
- Magic: shining rain of time - power=485
22, cost=34
(1 upgrades ready)
Next command? (get n (0ow, exit)
show
Inventory:
- Upgrade: ready
- Magic: sneeze - power=0, cost=1000
- Magic: powerful ice of time - power=792
1, cost=64
- Magic: mighty ice of doom - power=2905
7, cost=76
- Magic: quick flame of satisfaction - po
wer=75576, cost=86
- Magic: shining rain of time - power=485
22, cost=34
- Armor: garbage bag - defense=0
- Armor: quick mask of the ancients - def
ense=306
- Armor: appropriate helmet of satisfacti
on - defense=493
- Armor: shining mask of satisfaction - d
efense=643
- Armor: quick boots of satisfaction - de
fense=883
- Tool: soggy banana - power=0
- Tool: mighty rock of doom - power=276
Preparing to submit
As you finish your program, add and/or check
the following to ensure a complete, correct,
and professional-looking submission:
1. Update the Javadoc comment for the
GachaGame class with your name and the
current date.
2. Add a Javadoc comment for both methods
you completed in GachaGame.
3. Add a Javadoc comment to the Item class,
its four subclasses, and ItemTest.
4. Add a Javadoc comment for each instance
method (abstract and non-abstract) in
the Item class and its four subclasses.
5. Make sure that you have not added any
methods to the Item class and its four
subclasses that are not listed above.
6. Check that the forma&ng of ALL of your
source code is consistent and makes the
code readable for humans. Use
indenta!on and spaces as we have in class.
Be consistent — the forma&ng of all
classes should match.
7. Make sure that any variables you have
created have good and meaningful names
that use the camel-case conven!on for
Java.
8. Finally, run your program again to make
certain that the changes you made did not
alter the source code.
Submission
Your solu!on for this assignment consists of
seven files: GachaGame.java, Item.java,
Tool.java, Armor.java, Magic.java,
Upgrade.java, and ItemTest.java.
See Assignment 1 for instruc!ons on how to
export a Java file from Eclipse, how to submit
to Gradescope, and how to ensure your
program passes the visible Gradescope
automated tests.
Load Assignment 8: Gacha game in a new
window
Previous Next
Assignment 8: Gacha game
Assignment 8: Gacha game
Due Friday by 11:59pm Points 100
Submi!ng an external tool
Available un!l Nov 12 at 11:59pm
This tool needs to be loaded in a new browser
window
The purpose of this assignment is to prac!ce
inheritance concepts by making an abstract
class and extending it with several subclasses,
as well as implemen!ng Java's Comparable
interface to allow for sor!ng of an ArrayList
.
Background
In a gacha game , a player can acquire items
by drawing randomly-selected items of
varying quality. The goal is to draw more and
more items to get the best ones possible. In
our game, there are four types of items: tools,
armor, magic, and upgrades. Your task is to
create the classes represen!ng these four
types, which extend one abstract class for all
items. These classes must implement Java’s
Comparable interface so that items can be
sorted.
Setup
To get started, add a new package
called assign08 and import GachaGame.java
. This provided class handles the
interac!ve mechanics of the game and
contains the main method.
Requirements
Create five new classes in the
assign08 package: one abstract class Item and
four subclasses Tool, Armor, Magic, and
Upgrade, all of which extend Item. Also,
complete the getNewItems
and getRandomItem methods in the provided
GachaGame class.
Item abstract class
The Item class has one private instance
variable:
a string to represent the name of the item
The following constructor must be defined:
public Item(String name)
Also, the following instance methods must be
included:
public String getName()
public abstract String getDescrip!on()
public abstract void useUpgrade()
Finally, the Item class must implement a
specific version of the Comparable interface,
wri"en as “Comparable
it is for comparing objects of the Item class.
The compareTo method required by the
Comparable interface does not appear in the
Item class itself (since Item is an abstract class).
Instead, compareTo must appear in each class
that extends Item (see below.)
Subclasses
The Tool, Armor, Magic, and Upgrade classes
must extend Item and define the inherited
abstract methods.
In addi!on to any inherited instance variables,
each subclass needs its own unique variables:
Tool – an integer for the power of the tool
Armor – an integer for the armor’s defense
value
Magic – an integer for the power of the
magic and an integer for its cost
Upgrade – a boolean for whether the
upgrade has been used or not
The following constructors must be defined:
public Tool(String name, int power)
public Armor(String name, int defense)
public Magic(String name, int power, int
cost)
public Upgrade()
The name parameter of the first three
constructors should be used to set the
inherited name instance variable, by calling the
superclass constructor. The name of Upgrade
objects should always be "Upgrade".
public String getDescrip"on() for each
subclass returns a String describing the item.
Examples for an item named “My item”:
For a Tool object with 400 power → “Tool:
My item – power=400”
For an Armor object with 400 defense →
“Armor: My item – defense=400”
For a Magic object with 400 power and 20
cost → “Magic: My item – power=400,
cost=20"
For an Upgrade object that is not used →
“Upgrade: ready”
For an Upgrade object that is used →
“Upgrade: used”
public void useUpgrade() for Tool, Armor and
Magic increase the stats for power, defense,
and cost by at least one. How exactly it
increases is le$ up to you. However, if the
values get too large it may cause errors, so try
to keep it under 100,000,000 even a$er
several consecu!ve upgrades. Also make sure
that the stats increase by at least one for each
upgrade.
For the Upgrade class, calling useUpgrade
should make the “used” variable true.
public boolean isUsed() should be in the
Upgrade class only and return a boolean for
whether the upgrade object is used or not.
Implemen!ng Comparable
The purpose of implemen!ng Comparable is so
that we can sort an ArrayList of Item objects.
Note that should not do any sor!ng in the
code that you write, since the provided code
already does this. If you are curious, look for
the method call Collec!ons.sort(inventory) and
recall sor!ng an example ArrayList object in
Class Mee!ng 13. By implemen!ng the
Comparable interface for all items in our game,
we unlocked the ability to use the sort
method.
See Java's documenta!on for the method
you must include in each subclass, in order to
implement the Comparable interface. Scroll
down to the sec!ons named "Method
Summary" and "Method Details". Note that
the type of the parameter is wri"en as T. In
our class, T should be replaced with Item.
In wri!ng the compareTo method to compare
two objects (this and the parameter), the
following rules apply:
When comparing Item objects of different
types: Tool > Armor > Magic > Upgrade
For two Tool objects: compare the power
numbers
For two Armor objects: compare the
defense numbers
For two Magic objects: compare the
frac!on power/cost and use cau!on when
dividing integers
For two Upgrade objects: unused > used,
otherwise they are equal
Note that the parameter of the compareTo
method has type Item. Recall that the
instanceof operator determines whether an
object has certain data type. Use this
operator to determine if the Item parameter
has the same type as a subclass; e.g.,
if(other instanceof Tool)
...
else if(other instanceof Armor)
...
Complete the GachaGame
class
getRandomItem() creates a new Item object of
one of the four subclasses at random. Use a
Random object to generate a random number
from 0 to 3 (inclusive) to choose the subclass.
Use the provided makeItemName method to
create a random name. Use the Random
object again to generate the needed stats for
the item. The values you generate for the
stats are le$ up to you, but they must be
randomly chosen and at least 1. Also, be
careful that the stats values are not so big that
your useUpgrade method could cause
problems (value under 1,000,000 are probably
safe).
getNewItems(int howMany) uses the
getRandomItem method to fill an array with
howMany new Item objects. Polymorphism
means that we do not need separate methods
and separate arrays to get and store different
types of items, since they all belong to the
Item superclass. The body of this method is
simple, and we can add new Item subclasses in
the future without having to change this
method.
Tes!ng
Create a JUnit test class called ItemTest. Add
tests to thoroughly check the normal and edge
cases of all instance methods in the four
subclasses, paying par!cular a"en!on to
the compareTo methods. Note that you are
not able to make an instance of Item because
it is an abstract class.
To test your comple!on of the two empty
methods in the GachaGame class, try playing
the game:
1. Run the GachaGame main method by
pressing the green run bu"on in Eclipse.
2. The game repeatedly prompts for one of
the following input commands:
"get n", where n is a number between 1
and 10, gets n new items and prints the
best ones in your inventory.
"upgrade" applies any unused upgrades
to your best items and prints the result.
"show" sorts and displays your en!re
inventory.
"exit" ends the game.
Note that ge&ng new items may or may not
change the printed best ones. Use the "show"
command to see your en!re inventory and
make sure that things are sorted as expected
(grouped by type and ordered from worst to
best). Con!nue un!l you acquire upgrade
items and use "upgrade" to make sure that the
stats of your best items are increasing
according to your useUpgrade method code.
The "show" command should now display the
upgrades as used. For example, entering "get
10" followed by "show" could produce the
following output. Remember that this is
random, so you will not get exactly the same
result.
Next command? (get n (0
get 10
Getting 10 new items.
Best equipment:
- Tool: mighty rock of doom - power=276
- Armor: quick boots of satisfaction - de
fense=883
- Magic: shining rain of time - power=485
22, cost=34
(1 upgrades ready)
Next command? (get n (0
show
Inventory:
- Upgrade: ready
- Magic: sneeze - power=0, cost=1000
- Magic: powerful ice of time - power=792
1, cost=64
- Magic: mighty ice of doom - power=2905
7, cost=76
- Magic: quick flame of satisfaction - po
wer=75576, cost=86
- Magic: shining rain of time - power=485
22, cost=34
- Armor: garbage bag - defense=0
- Armor: quick mask of the ancients - def
ense=306
- Armor: appropriate helmet of satisfacti
on - defense=493
- Armor: shining mask of satisfaction - d
efense=643
- Armor: quick boots of satisfaction - de
fense=883
- Tool: soggy banana - power=0
- Tool: mighty rock of doom - power=276
Preparing to submit
As you finish your program, add and/or check
the following to ensure a complete, correct,
and professional-looking submission:
1. Update the Javadoc comment for the
GachaGame class with your name and the
current date.
2. Add a Javadoc comment for both methods
you completed in GachaGame.
3. Add a Javadoc comment to the Item class,
its four subclasses, and ItemTest.
4. Add a Javadoc comment for each instance
method (abstract and non-abstract) in
the Item class and its four subclasses.
5. Make sure that you have not added any
methods to the Item class and its four
subclasses that are not listed above.
6. Check that the forma&ng of ALL of your
source code is consistent and makes the
code readable for humans. Use
indenta!on and spaces as we have in class.
Be consistent — the forma&ng of all
classes should match.
7. Make sure that any variables you have
created have good and meaningful names
that use the camel-case conven!on for
Java.
8. Finally, run your program again to make
certain that the changes you made did not
alter the source code.
Submission
Your solu!on for this assignment consists of
seven files: GachaGame.java, Item.java,
Tool.java, Armor.java, Magic.java,
Upgrade.java, and ItemTest.java.
See Assignment 1 for instruc!ons on how to
export a Java file from Eclipse, how to submit
to Gradescope, and how to ensure your
program passes the visible Gradescope
automated tests.
Load Assignment 8: Gacha game in a new
window
Previous Next