Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
BlueJ Lab 
Page 1 of 6 
 
Introduction to BlueJ 
The purpose of this lab is to allow you to become more familiar with the BlueJ Integrated 
Development Environment, (IDE). You should have already briefly used BlueJ in your first lectoral 
class. 
Preliminaries 
Once you are logged on to the system, go to the start menu, then Computing, then Java and then 
BlueJ. Click on the BlueJ option. This should start the BlueJ. You will see the following window: 
 
 
Figure 1: BlueJ IDE 
Starting to use BlueJ 
In the Java world a program under development is known as a project. A Java program will typically 
consist of several files. The first thing you need to do is to set up your own project. 
Task 1 
In the BlueJ window select “Project”-> “New Project”. (see figure 2) 
BlueJ Lab 
Page 2 of 6 
 
                                   
Figure 2:  
You will be presented with a dialog window: 
 
Figure 3:  
Navigate to the location you want to use to for the project. This should be on your U: drive, probably 
in a folder called CM0047, that you will have created. Once there, enter the name of the project in 
the file name field. For this exercise we want you to call the project, Shapes. 
Enter Shapes and press return. You have now created your first project. You have created a folder 
which contains two files: 
                                      
README.TXT package.bluej
 
The first is a text file that can be used to record notes about the project. 
The next one belongs to BlueJ. It contain administrative information that is used by BlueJ. You 
cannot read it. 
Task 2 
The next stage is to create some files. Normally you would do this but for this part of the lab we are 
going to provide you will some files. You will find these on Blackboard. The files are called: 
 Canvas.java 
 Circle.java 
 Square.java 
 Triangle.java 
 
Copy these files into you folder Shapes. 
 
Task 3 
Select  Edit and  then select  “Add Class from File”. You will be presented with a file selection dialog. 
Move to the folder Shapes and select the 4 java files. 
BlueJ Lab 
Page 3 of 6 
 
 
Figure 4:  
You will need to click the Compile button in the BlueJ tool bar. You should now see something 
similar to this: 
 
Figure 5:  
Task 4 
You can now explore this project. 
1. Right click on the Square class and choose new Square() 
2. Left click on the Square object in the Object Bench 
                                      
        You will see a list of the methods available for your use. Select makeVisible(). A small 
window will appear showing a square in its default position and default colour.  
3. Experiment with moveLeft(), moverRight(), moverUp(0, moveDown() ,makeInvisible() 
and makeVisible(). 
 
Task 5 
If you invoke moveHorizontal you will see the following dialog: 
BlueJ Lab 
Page 4 of 6 
 
 
Figure 6:  
You are being asked to enter some data. (You are being asked to provide a value for a parameter.) In 
this case this will be an integer which equals the number of pixels you want to move the square – a 
positive number moves it to the right and a negative number to the left. Enter a value in the range 
50 to 100. 
Experiment with the other methods that require an integer parameter. There are four others. 
Task 6 
There is just one method left to use – changeColor( String newColor). Invoke this method and you 
should see 
 
Figure 7:  
 
The parameter this time is of type String. We will look at type String  in one of the lectorals. For the 
moment take it to be a sequence of characters inside “ “. The acceptable strings are shown in the 
dialog box. Type one in. 
 
Task 7 
Now create some circle and triangle objects and perform similar experiments with them. 
Task 8 
You are going to start a new exercise using the classes in the shapes project and we need to reset 
the canvas. Go to Tools in the menu bar and select  “Reset Java Virtual Machine”. This has the effect 
of restarting the project. 
BlueJ Lab 
Page 5 of 6 
 
 
Figure 8:  
 
Look at the following picture 
 
      
Figure 9:  
 
By creating various objects and using their methods build this picture. As you do write down the 
steps you are taking. These steps are an “algorithm” for producing the picture. 
Task 9 
You are going to carry out some simple editing on a Java class that will, hopefully, draw the picture 
shown in figure 9. Close your current project and create a new one called Picture2. Add the four java 
files you used in the Shapes to this new project. (If you have forgotten how, look at Task 3 above). 
From Blackboard add the file Picture2.java to the project. ( you will need to copy it into the Picture2 
folder and then add it to the project).  Once you have added it to the project double click on the 
Picture2 class and select “OpenEditor”. 
  
Figure 10:  
 
Find the draw method. It contains four sections of code – two for squares, one triangle and a circle. 
Where ever you see the phrase “ENTER AN INTERGER VALUE” replace it with a suitable  integer 
value and when you see the phrase “ENTER A COLOR STRING” enter a suitable color string, e.g. 
“red”. 
BlueJ Lab 
Page 6 of 6 
 
When you have entered all the necessary integer values and strings click Compile. You will get a 
message at the bottom of the edit window. Hopefully it will say  “Class compiled – no syntax errors”. 
If it doesn’t say this see if you can understand what the problem is and then ask the lab tutor for 
help. 
 
Once the file has compiled create a picture object and select the draw method for that object. Did 
you draw the correct picture? 
 
Task 10 
The final task in this lab is for you to create your own picture using Circle, Square and Triangle 
objects.  In order to do this you must first create a new project. Then add the Square, Circle, Triangle 
and Canvas classes to the project. On Blackboard you will find a file MyPicture.java copy it and add it 
to the project. 
 
The following is some of the contents of that file: 
 
 
 
 
 
 
 
 
  
Figure 11:  
 
You need to: 
1. Plan your picture 
2. Open the editor for MyPicture 
3. Declare each of the shapes you want to use, (look back at the code in Picture2.java to 
see the kind of thing  you should be entering) 
4. Compile 
a. If you have a syntax error correct it 
b. If there is no error continue 
5. Add the instructions for your first shape 
6. Compile 
a. If you have a syntax error correct it 
b. If there is no error continue 
7. Repeat for all your shapes. 
When you have finished create a MyPicture object and execute its draw method. 
 
public class MyPicture 
{ 
   // declare the shapes you want to use here 
 
....... 
 
    /** 
     * Draw this picture. 
     */ 
    public void draw() 
    { 
       // Add the instructions for each of your shapes 
here 
    } 
CM0718 : Week 01 Exercises 
These exercises are taken from the BlueJ book.  
 
Exercise 1.13 In the source code of class Picture, find the part that actually 
draws the picture. Change it so that the sun will be blue rather than yellow. 
 
Exercise 1.14 Add a second sun to the picture. To do this, pay attention to the 
field definitions close to the top of the class. You will find this code: 
private Square wall; 
private Square window; 
private Triangle roof; 
private Circle sun; 
You need to add a line here for the second sun. For example: 
private Circle sun2; 
Then write the appropriate code for creating the second sun. 
 
Exercise 1.15 Challenge exercise (This means that this exercise might not be 
solved quickly. We do not expect everyone to be able to solve this at the 
moment. If you do – great. If you don’t, then don’t worry. Things will become 
clearer as you read on. Come back to this exercise later.) Add a sunset to the 
single-sun version of Picture. That is: make the sun go down slowly. Remember: 
The circle has a method slowMoveVertical that you can use to do this. 
 
Exercise 1.16 Challenge exercise If you added your sunset to the end of the 
draw method (so that the sun goes down automatically when the picture is 
drawn), change this now. We now want the sunset in a separate method, so that 
we can call draw and see the picture with the sun up, and then call sunset (a 
separate method!) to make the sun go down. 
 
Exercise 1.20 Call the numberOfStudents method of that class. What does it do? 
 
Exercise 1.21 Look at the signature of the enrollStudent method. You will notice 
that the type of the expected parameter is Student. Make sure you have two or 
three students and a LabClass object on the object bench, then call the 
enrollStudent method of the LabClass object. With the input cursor in the dialog 
entry field, click on one of the student objects – this enters the name of the 
student object into the parameter field of the enrollStudent method (Figure 1.8). 
Click Ok, and you have added the student to the LabClass. Add one or more 
other students as well. 
 
Exercise 1.22 Call the printList method of the LabClass object. You will see a 
list of all the students in that class printed to the BlueJ terminal window (Figure 
1.9). 
 
Exercise 1.23 Create three students with the following details: 
Snow White, student ID: 100234, credits: 24 
Lisa Simpson, student ID: 122044, credits: 56 
Charlie Brown, student ID: 12003P, credits: 6 
Then enter all three into a lab and print a list to the screen. 
 
Exercise 1.24 Use the inspector on a LabClass object to discover what fields it 
has. 
 
Exercise 1.25 Set the instructor, room, and time for a lab, and print the list to the 
terminal window to check that these new details appear. 
 
Exercise 1.26 In this chapter we have mentioned the data types int and String. 
Java has more predefined data types. Find out what they are and what they are 
used for. To do this, you can check Appendix B, or look it up in another Java 
book or in an online Java language manual. One such manual is at 
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html 
 
Exercise 1.27 What are the types of the following values? 
0 
"hello" 
101 
–1 
true 
"33" 
3.1415 
 
Exercise 1.28 What would you have to do to add a new field, for example one 
called name, to a circle object? 
 
Exercise 1.29 Write the signature for a method named send that has one 
parameter 
of type String, and does not return a value. 
 
Exercise 1.30 Write the signature for a method named average that has two 
parameters, both of type int, and returns an int value. 
Exercise 1.31 Look at the book you are reading right now. Is it an object or a 
class? If it is a class, name some objects. If it is an object, name its class. 
 
Exercise 1.32 Can an object have several different classes? Discuss.