Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Treasure Hunt Java 2d array exercises, Quizzes of Software Development

You will create an application that simulates a game where the player will use canon to destroy enemies’ ships

Typology: Quizzes

2023/2024

Uploaded on 12/13/2024

thanh-tam-ngoc
thanh-tam-ngoc 🇨🇦

1 document

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Please note if your codes don’t compile, they will be given 0 mark.
You will create an application that simulates a game where the player will
use canon to destroy enemies’ ships.
1. We will still use a two-dimension array to represent the map, where
the size can be set by the input from user, details will be given later
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
2. (3 marks) Please create a class Treasure, which has the following
attributes and method:
position: the position on the graph (two-dimension matrix), int x, y;
value: the value of the treasure;
Collect(): it is empty method for now. (details will be given below).
3. (5 marks) Please create an Investor class, which has (at least) those
attributes:
name: the name of the investor;
balance: money of the investor.
You need to decide the type and modifier of those attributes.
ObtainDrillInfo() : collect the user input as drilling position (x,y) and
the depth. If the input is invalid (smaller than 0 or bigger than the
size of the map) or the depth is deeper than his balance (we assume
drill of depth 1 will cost $1), please ask user to input again until a
valid number is input.
4. (15 marks) Please create a Diamond class, which extends the Treasure
class. It will also add another attribute
depth: the depth of the Diamond beneath the ground.
status: the diamond has been found or not.
pf3
pf4

Partial preview of the text

Download Treasure Hunt Java 2d array exercises and more Quizzes Software Development in PDF only on Docsity!

Please note if your codes don’t compile, they will be given 0 mark.

You will create an application that simulates a game where the player will

use canon to destroy enemies’ ships.

1. We will still use a two-dimension array to represent the map, where

the size can be set by the input from user, details will be given later

2. ( 3 marks ) Please create a class Treasure , which has the following

attributes and method:

position : the position on the graph (two-dimension matrix), int x, y;

value : the value of the treasure;

Collect() : it is empty method for now. (details will be given below).

3. (5 marks) Please create an Investor class, which has (at least) those

attributes:

name : the name of the investor;

balance : money of the investor.

You need to decide the type and modifier of those attributes.

ObtainDrillInfo() : collect the user input as drilling position (x,y) and

the depth. If the input is invalid (smaller than 0 or bigger than the

size of the map) or the depth is deeper than his balance (we assume

drill of depth 1 will cost $1), please ask user to input again until a

valid number is input.

4. (15 marks) Please create a Diamond class, which extends the Treasure

class. It will also add another attribute

depth: the depth of the Diamond beneath the ground.

status: the diamond has been found or not.

You need to implement

CalDistance(): method that gives the distance of the Diamond from

the drilling position. Assume the drilling position is ( xd, yd ) the

coordinate of the Treasure is ( x,y ), thus the distance is calculated as:

Distance = Math.abs(x-xd)+ Math.abs(y-yd);

As you can see the distance is not Euclidean distance, we can think it

is because the distance estimation in field is not accurate 😊

You need to override:

Collect() : if the drilling is successful, i.e., at the right position (x, y)

and the drilling depth is deeper than the Depth. Then the value of

the diamond will be collected by the investor.

toString(): override the toString() method to return a string including

the position, the depth and the value of the diamond.

5. Please create a Game class which contains the main() method. You

might want to create some methods to modularize your codes.

a. Ask user’s input for the size of the map, min is 5. Use this size to

generate a two-dimensional array, int, char or any other type you

feel easier.

Create an array of the Diamond , the size of which is 1/4of the

map size (n*n/4, you might need to cast it to int). Then please

randomly set the position (x, y, from 0 to length of the map minus

one, n-1), depth (random number from 1 to 10) and value

(random number from 1 to 100). Please note that some of the

diamonds might be co-located, it is OK.

Create an Investor object named as “Player” with balance

initialized as 20. ( 10 marks )

b. Display the map with the drilling information, if the player has

attempted at certain position, please display the depth drilled ;

“C” to indicate this position has found diamond; “*” means hasn’t

been attempted yet. Then ask player which (x, y) and depth to

Here is the snapshot of two rounds of the game: Hi, Player, please let me know the size of the map: 7 Here is the map








Hi, Player, please input the position you want to drill: 2, Hi, Player, please input the depth you want to drill: 2 Whoops, we missed the diamond and the shortest distance is 0. Hi, Player, you new balance is 18.


Do you want to continue (Y) or quit (any other input) Y Here is the map



        • 2 * *




Hi, Player, please input the position you want to drill: 2, Hi, Player, please input the depth you want to drill: 2 Congrats! You found diamond with value 9. Hi, Player, you new balance is 25.


Do you want to continue (Y) or quit(999) 999



        • T * *

    • T * * * *