问题描述
- 关于Java的一个小程序
-
刚从Python转到Java,感觉不适应。老师让我们编一个简单的小程序不需要用的GUI。但是里面有很多东西没学过。比如在java里随机数怎么生成,以及Point,toString怎么使用。最重要的是java里的类要怎么写。是在一个Package里面写还是要在一个java类里面写。下面是有关的说明,希望有人可以指点一下,谢谢We can see the following classes in the diagram:
● Ship - handles the ship position and associated movement behaviour;
● Asteroid - for a single asteroid, handles the position and associated movement;
● Field - a class that represents a field within which the ship and many asteroids can move around; and
● HighScores - a class that records player scores.We can also see a number of “manual unit testing” classes:
● TestShip - used to check that the ship functionality works;
● TestAsteroid - used to check that the asteroid functionality is okay;
● TestField - used to verify that the field functionality is working; and
● TestHighScores - used to check the score handling functionality works.The features you are expected to implement:
Field Class
● Field.ship is a single reference to a Ship object
● Field.asteroids is a primitive array containing 10 asteroids
● Both the ships and the asteroids must be initialised inside the field’s default constructor
● Field.width and Field.height define the boundary inside which the ship and the asteroids are allowed to move. Use the field’s default constructor to initialise Field.width to 100 and Field.height to 50
● Field.setup() does the following:
○ Creates a random number generator and sets its random seed to 0
○ The ship is placed in the centre of the field
○ The asteroids are placed at random positions using the random object within the field
● Field.update() causes the ship and then the asteroids to move and bounce within the field boundary
● Field.toString() returns the string representation of the current positions of the ship and then the asteroids*
Ship Class
● Ship.position is the current centre-point where the ship is located. The ship’s default constructor should initialise this to (0,0)
● Ship.width and Ship.height represent the boundary of the ship - since this is a model we don’t need to record the exact shape of the ship
● The ship’s default constructor should initialise the width to 1 and height to 1
● Ship.xOffset represents the current horizontal motion of the ship
○ +1 - motion is rightwards across the screen
○ -1 - motion is leftwards across the screen
○ 0 - no motion in horizontal direction
● Ship.yOffset represents the current vertical motion of the ship
○ +1 - motion is downwards
○ -1 - motion is upwards
○ 0 - no motion in vertical direction
● The ship’s default constructor should initialise the xOffset to 1 and yOffset to 1
● Ship.toString() returns a string representation* of the ship’s position and motion - you are expected to use the String.format() method to generate this string
● Ship.move() is used to change the current position based on the current motion
● Ship.bounce(boundaryWidth, boundaryHeight) is used to change the current motion of the ship, based on its current position:
○ If the current motion is leftwards and the current x position is less than or equal to 0 then the current motion becomes rightwards
○ If the current motion is rightwards and the current x position is greater than or equal to the boundary width then the current motion becomes leftwards
○ If the current motion is upwards and the current y position less than or equal to 0 then the current motion becomes downwards
○ If the current motion is downwards and the current y position greater than or equal to the boundary height then the current motion becomes upwards
Asteroid Class
● The Asteroid class is identical to the Ship class in all respects
HighScores Class
● HighScores.scores is an ArrayList that holds a new class called Score
○ A Score is represented by a player’s name and an integer value, and Score needs an initial-value constructor for setting the name and value for new Score objects
○ Score’s toString() returns a string with the player name and score value separated by a colon
● Use the HighScores default constructor to initialise an empty ArrayList
● HighScores.addScore(String, int) is used to add a new score into the ArrayList
○ Scores are added into the ArrayList such that the ArrayList remains sorted from highest score value to lowest score value
○ You may assume that no two score values will be the same in this assignment
● Given a filename, HighScores.load(String) and HighScores.save(String) are used to load and save the score information to disk
● Both the load and save methods must return a boolean, indicating success or failure
● HighScores.getTopScores(int) retrieves the top scores, it has a single integer parameter (the number of top scores to retrieve) and returns a new ArrayList containing those high scores - if there are not enough scores to make up the requested number of top scores then return the entire scores ArrayListHere are the required “manual unit tests” for your code:
● TestShip.main() is used to:
○ create a single ship using Ship’s default constructor
○ adjust the ship’s position manually to be (1, 1)
○ demonstrate that the ship can be moved and bounced within a boundary width of 10 and a boundary height of 6 (see the end of this document for examples of the necessary sample output)
● TestAsteroid.main() does exactly the same as TestShip but for a single Asteroid
● TestField.main() is used to:
○ create a Field object using the field’s default constructor
○ demonstrate the ship and asteroids moving and bouncing around a boundary of size (100, 50) for 100 iterations (see the end of this document for examples of the necessary sample output)
● TestHighScores.main() is used to:
○ create a new HighScores object
○ add the following score information: (in this order!)
■ Harry:100, Ron:10, Han Solo:1000, Yoda:0
○ save the scores to disk using a filename “scores.txt”
○ create a new HighScores object and load the scores back from disk
○ check that the top two scores are Han Solo:1000 and then Harry:100
○ You also need to include two (2) additional tests of the HighScores class that have not already been tested above