Download Creating a Java class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ACS-1903
Feb 14
Creating a Java class with a main method
The purpose of this exercise is to ensure you can code your own Java application class.
This type of class used here is special in the sense that it has a main method; the main
method is the place where application programs begin their execution. Many of the
programs in our Java text are exactly that: a Java class with one method which begins
with the line:
public static void main(String[] args) {
Previously you interacted with a set of Java classes using the BlueJ workbench. You may
want to take a moment and examine those classes … especially Triangle, Circle, and
Square. If you examine Triangle:
 You will see, in lines 12-16, fields/variables ( height, width, xPosition, yPosition,
color) that have meaning for a triangle. For instance, xPosition and yPosition
determine where an object will exist on a canvas.
 You should also note there are chunks of code called methods (e.g. makeVisible,
changeColor) that are responsible for doing things.
In this exercise you create a new Java class with a main method that does the following:
 Create an object (e.g. a triangle)
 Request the object to make itself visible
 Request the object to do other things (e.g. change its colour)
 Repeat the above as necessary to achieve the ‘picture’ you want.
You may do this work on your own laptop if you have that with you.
1
ACS-1903
Feb 14
1. Create a BlueJ project and import/copy the Shapes example classes into the
project.
2. Create a new class: press the New Class button and give the class a name (e.g.
MakePicture)
3. Modify the class. Your new class will have some code in it already. This skeleton
code is generated automatically by BlueJ and is useful when you are creating
classes such as Canvas, Triangle, and so on …, but the generated code is not so
useful for our purpose here. Right-click on the class and select Open Editor and
you will be able to edit the code so it becomes the following:
/**
* This program creates a picture and
* displays the picture on the canvas.
*
* @author ( ... )
*/
public class MakePicture
{
public static void main(String[] args) {
}
}
Some points about the above
 The lines delimited by /** and **/ are called comments that are useful for
humans, but are ignored by Java. Modify the @author comment to include
your name.
 The name of the class is MakePicture
 The characters ‘{‘ and ‘}’ always appear in pairs.
 The outermost pair delimits the code that comprises the makePicture
class.
 The innermost pair delimits the code that comprises the method named
main.
 We need to add code to the main method, but first, click the Compile
button to ensure your program is executable.
o It is good practice to frequently compile code to ensure your
changes do not have syntax errors.
o If your code does not compile then, at this point in the course, you
should probably ask the instructor to examine your code to inform
you of your error and its correction.
2
ACS-1903
Feb 14
4. Create a triangle, make it do things and have it make itself visible on the canvas.
To create a triangle and make it visible you need to add code (see the highlighted
code below) such as:
/**
* This program creates a picture and
* displays the picture on the canvas.
*
* @author ( ... )
*/
public class MakePicture
{
public static void main(String[] args) {
Triangle t1 = new Triangle();
t1.moveVertical(25);
t1.changeColor("red");
t1.makeVisible();
}
}
Some points about the above code:

The line
Triangle t1 = new Triangle();
Results in the creation of triangle that is known by the name t1 in the program.

We say the line
t1.moveVertical(25);
is asking the triangle t1 to execute its moveVertical method with the
argument 25. The result of this is that t1 will move on the screen to a new
position 25 pixels downward.

We say the line
t1.changeColor("red");
is asking the triangle t1 to execute its changeColor method with the
argument “red”. The result of this is that t1 changes its colour.

We say the line
t1.makeVisible();
is asking the triangle t1 to execute its makeVisible method. The result of this
is that t1 now appears on the canvas.
3
ACS-1903
Feb 14
5. Compile the code. Every time you modify code you will see the diagonal lines in
the rectangular box that represents the class in the project. This indicates you must
compile the code. Click the Compile button.
6. Running the code. Once compiled, and with no compile-time errors, you can run
the main method by selecting it: Right-click the class and choose as shown below.
You must click on the OK button for the code to run:
4
ACS-1903
Feb 14
7. The expected outcome is the canvas:
8. Now, more objects … add more code to create more objects. Give each one a
name such as t2, c3, s1, p1, p2, etc. Can you arrange the objects to form a picture?
If requested by the instructor submit your .java files via email.
5