Lab 1: Using BlueJ at UofW

advertisement
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
Using BlueJ at UofW
The purpose of this lab is to ensure you have your Workstation and PASS user ids, and
that you have some experience with BlueJ. Over the course of the term, in classes and in
labs, you will receive more instruction. If you have any questions during this lab do not
hesitate to ask your lab demonstrator.
In this lab you will :
 Obtain your Workstation user id
 Start BlueJ
 Create a new BlueJ project
 Import, compile and run a Java program
 Interact with an object system by sending messages
 Obtain you PASS user id
 Print assignment 1
This lab is to be completed during the lab period; when you are finished this lab, hand in
this sheet and keep the others.
Student Identification
Student number
____________________________________________
Last name
____________________________________________
First name
____________________________________________
Workstation user id
____________________________________________
PASS user id
____________________________________________
Email address
____________________________________________
1/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
1. Logon to your workstation
Log on to a workstation using the Windows user id and password that are given. This
user id is for use in 3D03 and 3C13. The system will ask you to change your password
when you log on. You must remember your user id and password.
Your workstation user id: ___________________
2. Obtain the Java files
Create a folder on your workstation called MyShapesSource. This folder could be
located in MyDocuments. Dowload the four java files that are found at
http://io.uwinnipeg.ca/~rmcfadye/1903/shapes/ to your MyShapesSource folder.
3. Start BlueJ
Click Start, then click on Programs, then click on BlueJ, and then click the BlueJ program
Start >> Programs >> BlueJ >> BlueJ
4. Create a BlueJ Project
Create a project called MyShapes by selecting New Project from the Project Menu. It
can be useful to store such a project on the H drive. H is available from both labs.
Project >> New Project
2/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
5. Importing java files
Import the four java files to your shapes project by using the Import option found in the
Project menu: Project >> Import, and select MyShapesSource
After the files are imported you will see a window such as:
The files must be compiled; use the Compile option: Tools >> Compile
When the compile is done
you will see the message
You may get warnings from the compiler which you should be able to ignore.
3/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
6. Reviewing the Java Program
You can view a Java class by double-clicking on the appropriate rectangle in the above
display.
Double-click on the orange rectangle named Triangle. This action opens the class editor.
Double-click
Reviewing the Triangle class
Examine the code on your workstation, and note there is coloured text in use: some red,
some blue, etc. BlueJ uses colour to denote special aspects of the code.
The first line is a Java import statement.
import java.awt.*;
In this case the program is stating that it will use a Java library known as awt. The editor
shows the word import is in red because it is keyword.
The blue-coloured text is used for comments.
Following the comments, you will see the beginning of the Triangle class.
public class Triangle
{
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
4/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
A class always begins with the keywords public class followed by its name, Triangle.
The above shows the variables in Triangle. These hold values that describe a particular
triangle (its height, width, position, etc).
Following the lines giving the name of the class and its variables, we see a method that is
executed whenever we ask for an instance (object) of Triangle to be created. This type of
method is called a constructor method.
public Triangle()
{
height = 30;
width = 40;
xPosition = 50;
yPosition = 15;
color = "green";
isVisible = false;
}
Questions
Where do new triangles position themselves when they are instantiated?
Answer: __________________________________________________
What colour is given to a newly instantiated triangle?
Answer: __________________________________________________
How high is a newly instantiated triangle?
Answer: __________________________________________________
How wide is a newly instantiated triangle?
Answer: __________________________________________________
Demonstrator’s initials
5/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
Following the “constructor” method, we have several methods which begin with the
words public void followed by the name of the method. Each method defines a
responsibility an object has, or a capability it has. In this example you will see that a
triangle can
make itself visible
make itself invisible
move to the right
move to the left
etc.
The programmer that designed shapes decided that triangles should be responsible for
making themselves visible and invisible, moving themselves right, left, up, or down on
the screen. As you will see, we can ask a triangle to do these things.
Question:
What can we ask a triangle to do? (The answer is the names of its methods)
Answer:
Demonstrator’s initials
6/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
7. Interacting with the shapes program
Let’s create a triangle …. Remember the constructor method – the triangle will initially
be invisible and so we will ask it to make itself visible.
Begin by right-clicking the Triangle class and choose newTriangle(). You will be asked
to name the triangle.
When you click OK for naming the triangle you will see a red object appear on your
screen indicating that you have instantiated an object.
7/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
Right-click on an object and the messages it can respond to are shown.
Choose makeVisible().
After selecting makeVisible() look at the bottom of your screen. You should see a new
button,
8/9
Sept 12, 2008
ACS-1903
Lab 1
Name: _______________________________
If you don’t see the following image, then click that button and then you will
Now make the triangle move to the right by sending it a moveRight() message.
Show the result to your lab demonstrator.
Demonstrator’s initials
8. Get your PASS user id and Assignment 1
a) Open a web browser and go to venus.acs.uwinnipeg.ca
Log on to PASS using the id and password you have been given. This user id is
used to log on to PASS from any browser and submit assignments. The system
will ask you to change your password when you log on. You must remember your
user id and password.
Your PASS user id: ____________________
b) Navigate to 1903 and view the first assignment. Print your assignment in the lab.
Notice the due date… next Friday. During the lab on Friday Sept 19 you will be
asked to submit your assignment.
Demonstrator’s initials
9/9
Download