java基础-关于Java的一个小程序

问题描述

关于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 ArrayList

Here 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

时间: 2024-10-26 00:01:44

java基础-关于Java的一个小程序的相关文章

Java初学一枚 一个小程序 求有人帮我看看

问题描述 Java初学一枚 一个小程序 求有人帮我看看 package malnAV; public class Work3_3 { public static void main(String[] args) { //??? //方法 main 不能声明为"静态":只能在静态类型或顶级类型中才能声明静态方法 Emp e1=new Emp(001,"张三"); Emp e2=new Emp(002,"李四"); Emp e3=new Emp(00

关于java配置环境的搭建和一个小程序

问题描述 1这样是不是说明配置环境已经ok了?2我运行一个小程序始终是这样子的? 放在D盘a文件夹代码是这个public class HelloJava{ public static void main(String[] args){ System.out.println("Hello Java"); }} 解决方案 后缀应该还是个txt的解决方案二:HelloJava 为什么你在书写文件名时是Hellojava,文件名与类名需要保持一致.解决方案三:你是在什么系统上搭建的java环境

对象-java中有关TreeSet的一个小疑问

问题描述 java中有关TreeSet的一个小疑问 看书上说java所有集合框架的具体类都至少有两种构造方式,一种是无参构造方法,一种是用另一个已有的容器来构造. 比如先前已经有了一个HashSet类的对象hh,但是他内部的元素是无序的,而我们这时再创建一个TreeSet(hh),他就会根据hh中存有元素自带的compareTo方法自动排好序,并且这个排序过程只进行一次.而如果我们将这些带排序的元素逐个加入到TreeSet中去,则就要加一次排一次序,会更耗时间. 请问,是不是有所遇到这种用到Tr

eclipse开发Hbase的一个小程序,但是出现下面一些问题,请求大神赐教

问题描述 eclipse开发Hbase的一个小程序,但是出现下面一些问题,请求大神赐教 代码: import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import

设计一个小程序,将已有的asp程序转换为jsp程序

问题描述 设计一个小程序,将已有的asp程序转换为jsp程序 设计一个小程序,将已有的asp程序转换为jsp程序.运用java与web应用 解决方案 说得轻巧,"设计一个小程序",还"运用java与web应用".这是两种不同的技术,不但语言不同,而且类库也不同,就算用到"编译原理"也无济于事. 解决方案二: 这个跨度可有点大了. 解决方案三: 可以参考: <设计一个小程序,将windows转换为国有操作系统> 解决方案四: 这个比较麻

想用c#做一个小程序。要把oracle数据库里的某个表的昨日更新内容导出到xml中

问题描述 想用c#做一个小程序.要把oracle数据库里的某个表的昨日更新内容导出到xml中,现在还没点头绪,求大神指点 解决方案 解决方案二:数据库中的表增加两个字段"最后更新时间""最后更新人",然后就行了啊解决方案三:引用1楼xxoo2007的回复: 数据库中的表增加两个字段"最后更新时间""最后更新人",然后就行了啊 重要的不是数据库里面的操作.我是想用c#做个小程序,吧数据库里的表自动导成xml文件解决方案四:其实就

一个小程序引发的思考

   既然是一个小程序引发的思考,那么我们就先看看这个小程序,看看他有何神奇之处: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { MyClass s = new MyClass(); s.val = 10; int i = 20; Console.WriteLine("s.val={0},i={1}",s.val,i); MyMethod(s, i); Console.

结果是什么啊-一个小程序,求解答啊

问题描述 一个小程序,求解答啊 在别人文章里看到的程序 程序的输出结果是什么,why? #include int main() { int a[5] = {1,2,3,4,5}; int p = (int)(&a + 1); printf("%d, %dn", *(a + 1), *(p -1)); } 解决方案 int a[5] = {1,2,3,4,5}; int* p = (int*)(&a + 1); printf("%d, %dn", *(

用VB.net制作一个小程序(2)

程序 用VB.net制作一个小程序(2)----制作"Lucky7"程序作者:高山 它是一个模拟幸运数字投币机的游戏程序.此程序的用户界面中包括两个按钮,3个幸运数字框,表示获胜的数字图象以及Lucky 7标签. Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows 窗体设计器生成的代码 " Public Sub New() MyBase.New() '该调用是 Windows