Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS1002 Computer Science Week 2 Practical: Objects and Turtles 2011/12 
For Week 2  1 
Week 2 Practical Objects and Turtles 
Aims and Objectives 
Your aim in this practical is: 
• to practise the creation and use of objects in Java 
By the end of this practical you should be able to: 
• create objects 
• decide which methods to use, based on documentation provided 
• call methods on objects 
• do simple debugging 
Introduction 
This practical involves using Turtle graphics. You will write a program that creates a (virtual) turtle and 
issues it with a series of commands to make it draw a particular pattern. Your program will create ob-
jects and call methods on them. 
The main, compulsory, part of the practical is described in Parts 1-4. Part 6 describes some additional 
extension activities, which you can choose to do or not. Without doing any extension activities the high-
est grade that you can achieve is 17. 
Practical Instructions 
Part 1: Setting Up 
The first steps are similar to those you carried out last week: 
• log in to a machine 
• check your email in case of any late announcements regarding the practical (but don’t spend 
hours emailing your friends!) 
• create a new Word document for your report, add the appropriate header at the top, and save it 
as W02-Practical-Report in the Coursework folder in your own file space 
• start Together; if prompted by Together select your workspace in the Coursework folder in your 
own file space in your network home folder as the workspace for Together; close any projects 
currently open; now create a new Java Modeling Project called W02-Practical to be stored in 
your workspace; add the library studres/Library/Wu,2004/Galapagos/galapagos.jar in the same way 
that you added javabook.jar in Practical W01 
If you can’t remember how to do any of these steps, refer back to the instructions for Practical W01 at  
https://studres.cs.st-andrews.ac.uk/CS1002-CS/Practicals/W01/W01-Practical-Introduction.pdf 
Part 2: The Task 
Practical specification 
In this practical you will write and test a Java program that uses Turtle graphics to display the postal 
code of your home address on the screen. It is difficult to draw curved lines with Turtle graphics, so 
you can use stylised characters made up of straight lines. The final output of your program should look 
something like the following example, but with your postal code in place of CS IS COOL. You’re free to 
CS1002 Computer Science Week 2 Practical: Objects and Turtles 2011/12 
For Week 2  2 
experiment with colours and styles so long as the result is recognisable. Use your term address postal 
code if your home address doesn’t have one. 
 
Creating a class 
To start, create a new class on the class diagram called W02Practical. Double click on the class on the di-
agram to launch the editor pane and enter the following code for the main method inside the class defi-
nition 
public static void main(String[] args){ 
} 
Alternatively you can add the class by using the menu and navigating to File->New->Other ->Class. 
Give the class the name W02Practical and check the box to include a main method. If you can’t remem-
ber how to do this, see Practical W01 for more detailed instructions. 
Creating objects 
The first thing your program needs to do is to create a new turtle object, and a drawing area for it to 
walk about in. In this practical you’ll be given fairly detailed instructions; later on in this module you’ll 
have to work out more for yourself. 
Inside the main method, write Java code that declares a variable of type Turtle and assigns a new object 
of class Turtle to it. Remember that the general form of such code is 
ClassName variable_name = new ClassName(creation parameters); 
You’ll need to choose a suitable name for your turtle; it doesn’t matter what it is as long as you use it 
consistently later on. The creation parameter should be 0 (the number zero). This tells the turtle to use a 
special drawing area rather than the default one, which is too small for this practical. 
Now save the file. Together will check what you’ve written so far to see whether it’s valid Java. One or 
more problems will probably be flagged up to the left of the lines containing the errors in the Editor 
pane. If you get the error “Turtle cannot be resolved to a type” when moving the mouse over the error 
symbol on the offending line then it’s because Together doesn’t know where the class Turtle is defined. 
To tell it, enter: 
import galapagos.*; 
import java.awt.Color; 
at the head of the file. This also tells it about the Color class, which you will want to use later on. Save 
the file again. You will probably get a warning (with a yellow marker) about java.awt.Color not being 
used—you can ignore this. If any more problems are shown then you have probably made some other 
typing error. Try to find and correct the error yourself; if you can’t, ask a demonstrator for help. 
CS1002 Computer Science Week 2 Practical: Objects and Turtles 2011/12 
For Week 2  3 
Now, assuming that your current program is valid, you need to create a new drawing area object for the 
turtle to use. After the code that creates the turtle object, add some new code that creates an instance of 
class TurtleDrawingWindow. It will have the same form as before, except that this time no creation pa-
rameters are needed (you still have to put in the brackets though). 
Again, save and fix any problems that are shown. 
Finally, for this section, your program needs to tell the drawing area object about the turtle object. This 
is done by calling the add method on the drawing area object, passing it the turtle object as the parame-
ter. The general form of code calling a method on an object is 
object_name.methodName(method parameters); 
In this case the object name is whatever you named the TurtleDrawingWindow variable, the method 
name is add, and there is a single parameter: whatever you named the Turtle variable. 
Again, save and fix any problems. It’s a good idea to do this frequently as you write your program, for 
two reasons. First, saving the file often means that if you are unlucky and the Together application or 
the whole computer crashes, or there is a power cut in the lab, your work won’t be lost—you can re-
sume later with the most recently saved version. Second, it’s easier to fix problems as you go along, ra-
ther than being confronted with a big batch of errors at the end. 
Setting up ready for drawing 
Before you get the turtle to start drawing the text, there are a few more things to be set up: 
• you need to set the units for the drawing area to be 0.8: this will make the drawing area large 
enough to fit the diagram 
• you need to set the drawing area to be visible, so you can see it 
Both of these will be achieved by making method calls on the drawing area object. To decide which 
methods should be called, and what parameters need to be passed to them, you should consult the 
online documentation for the galapagos package at: 
https://studres.cs.st-andrews.ac.uk/Library/Wu,2004/galapagos/javadoc/ 
When the web page comes up, click on the TurtleDrawingWindow link in the list of classes on the left of 
the window. You’ll then see lots of information about the class. Scroll down to the section Method 
Summary and decide which of the methods look appropriate. 
The documentation web page should give you enough information to be able to add Java code to per-
form the two tasks described above. Each will be a line containing a method call on the drawing area 
object. The web page tells you what the method is called and what type of parameter(s) it takes. 
As usual, save and fix any problems. If you get stuck here or if you can’t work out what the code should 
be, ask a demonstrator. Now try running the program that you’ve written so far. It won’t do anything 
very exciting but it should at least display the drawing area window. Recall from last week that to run 
the program you select Run As > Java Application from the Run menu. Close the drawing area win-
dow after you are satisfied your program is running correctly, otherwise you will end up with many 
open windows during testing, which will result in everything slowing down. 
Drawing the letters 
You are now ready to start writing the code that will control the turtle’s movement and draw out the 
text. There are various methods that can be called on the turtle object, including methods to: 
• put the pen down to the paper 
• lift the pen up from the paper 
• change the pen to a given colour 
• move to a given position in the drawing area 
Although there are others, these are enough to complete this practical. 
Now you need to work out how to get the turtle to trace out the first character. It may help to look at the 
example screenshot shown earlier. The grid lines are spaced at intervals of 50, and the origin in the 
middle of the window is at the point (0, 0). The turtle starts at the origin. Your program should tell the 
CS1002 Computer Science Week 2 Practical: Objects and Turtles 2011/12 
For Week 2  4 
turtle to move to the position where the first line in the character will start, put the pen down to the pa-
per, move to the end of the first line, move to the end of the second line, and so on. When it gets to the 
end of the character, the turtle should raise the pen, move to the start of the second character, and start 
again. 
The online documentation lists the details of the methods available in class Turtle. Use this to write a se-
quence of method calls on the turtle object to make it draw the first character. Save the program, fix any 
problems, and then try running it. If you’re lucky, the turtle will draw a recognisable character and you 
can continue adding more code to draw the rest of the characters. However, it’s more likely that the re-
sults are not as you expected. Now comes the ‘debugging’ phase of program development, in which 
you try to work out what has gone wrong, and how to correct it! 
Likely problems include forgetting to raise the pen before moving the turtle to the start of the first char-
acter, and miscalculating the positions of the various points within the character. Make any necessary 
changes to your program, then save and run again. Repeat this process until the first character looks 
correct, then move on to programming the other characters. Again, after you’ve added code for each 
character, save, run and debug if necessary. 
When you’re programming the last few characters you may get a bit bored waiting for the turtle to 
draw out all the characters every time you run the program. If so, you can make it speed up by putting 
in a call to its speed method. Setting the speed to 100 makes it go fairly fast. 
You need to brighten up the output by drawing each character in a different colour. Look at the online 
documentation to find out which method to call to set the pen colour. To pass the required colour to the 
method you write Color.red, Color.yellow, etc as the parameter. Note the US spelling. 
Finally, incorporate a second (third or fourth⎯you can have up to four turtles at once) turtle to simul-
taneously decorate the drawing window with stars, balloons, fancy scroll-work⎯whatever you like, but 
using different methods to move this turtle. Note that each turtle will run independently of the other(s), 
so each turtle’s movements can be written as a block of code, rather than moving each turtle in turn. For 
example: 
 
Once the result is to your satisfaction, copy the display area window into your report, as described in 
Practical W01. 
Program presentation 
Now that you’ve (we hope) got your program doing what you want it to, it’s time to consider its ap-
pearance. It’s important that program code is neatly presented, with suitable explanatory comments, to 
help someone reading it understand what it does. In the short term the main reader to consider is your 
tutor, since program presentation is one factor taken into account in assessment of the practical. In the 
longer term it’s also helpful if you can understand the code yourself if you need to refer back to it in 
several months’ time. 
CS1002 Computer Science Week 2 Practical: Objects and Turtles 2011/12 
For Week 2  5 
Using Together there’s no excuse for messy program layout, since it can do it all for you! You just need 
to select the code in the Editor pane and select Source > Correct Indentation. Tutors will reduce grades 
if you submit untidy code that could have been fixed easily in this way. 
You should also add comments, where appropriate, to help the reader see what’s going on in various 
parts of the program. For example, in this practical you should at least include comments to say which 
character is being drawn by the turtle at a given point. 
Part 3: Report 
Your report should include a screenshot of the final display area window as mentioned earlier, and an-
swers to the following questions: 
1. What would happen if you left out all the penUp and penDown method calls from your pro-
gram? 
2. Why does the penUp method take no parameters, while moveTo takes two? 
3. In your own words, explain what the Turtle methods bodyColor, jumpTo and penSize do—you are 
expected to do more than simply reproduce the description from documentation. 
Part 4: Finishing Up 
Save your report and create a PDF copy as in the previous practicals.  Close Word and Together. Now 
package up your Together project and PDF report into a form suitable for submitting. Follow the same 
sequence as in Practical W01. You should end up with a single file called W02-Practical.zip, which you 
should now submit to your tutor using MMS, in the slot for Practical W02. Once you have submitted it 
you can delete the zip file. 
Don’t, however, delete your W02-Practical folder itself. You might want to refer back to it in a future 
practical, or you might need to produce it if there is some problem with MMS. 
That’s it—see you next week! 
Part 5: Assessment 
Your tutor will assess your submitted work in two ways: 
• Formative assessment gives you feedback on your performance and advice as to how to im-
prove next time. This will be returned in the form of comments, either in writing on a printed 
copy of your work, or electronically via MMS. 
• Summative assessment gives you a grade from 0-17 (or from 0-20 if you’ve tried one or more of 
the extension activities). This contributes towards your final grade for CS1002. The grade will 
be returned via MMS. 
General practical grading guidelines are available in the School General Handbook: 
https://studres.cs.st-andrews.ac.uk/Library/Handbooks 
Part 6: Extension Activities 
The activities in this section aren’t compulsory, though you need to do at least one of them to achieve a 
grade above 17. Try them if you’re interested and have spare time. 
Further programming 
Refine your program: 
• Make the characters drawn by the turtle more realistic by using more small lines, and not re-
stricting the turtle to right-angle turns. 
CS1002 Computer Science Week 2 Practical: Objects and Turtles 2011/12 
For Week 2  6 
If you do try this, make a copy of your initial W02-Practical Together project, so that if it goes horribly 
wrong you haven’t lost anything. You can duplicate a project by selecting the Navigator pane next to 
the Model Navigator pane, right clicking on the project, selecting Copy and then Paste. 
Web 
Find out more about turtle graphics and the Logo language, and add a brief summary to your report. 
Possible starting points: 
http://el.media.mit.edu/logo-foundation/ (The Logo Foundation) 
http://en.wikipedia.org/wiki/Logo_(programming_language) (Wikipedia)