Learning Unit 5 Check Points

advertisement
Learning Unit 5
EXERCISE 2
1.
Output 
I am going to the
shopping mall.
There is no bus
may I have a lift?
CHECK POINT 1
1.
Two forward slashes (//).
2.
Comments are ignored by the compiler. They are helpful to you as a programmer so that you can
enter text for your own understanding. Other programmers who may refer to your code will also be
guide by the comments you make.
3.
Refers to the fact that upper and lowercase letters are not treated the same in Java, for
example, the text “PROGRAM” is not treated the same as "program”.
4.
4.1. To gain access to the methods of the Gogga class by making all the classes in the “it” folder,
including Gogga, available for use in the current program.
4.2. There are several classes that make up the it package. By using the *, you are
instructing this program to import ALL the classes that make up the it package.
5.
All statements in Java end in a semi–colon (;).
6.
Gogga slug = new Gogga();
7.
Fields.
8.
Methods.
9.
Use an open curly bracket ({) to open a class or main method, and use a close curly (}) bracket
to close a class or main method.
10.
turnLeft(); turnRight();
11.
All methods (behaviours) of any object must have round brackets. This identifies them to Java
as methods.
The following may differ according to the IDE
you are using.
12.1. Select Save As... from the File menu.
12.2. Type in the new file name.
12.3. Press the Enter key.
12.4. Press the keyboard combination CTRL
+ F (Using the JCreator or JGrasp IDE).
12.
Page 1
12.5. Find all the instances of the previous file name, and replace them with the new file name as
seen below:
13.
13.1. End User: Anyone who uses a computer system or its output.
Programmer: A person who writes sets of instructions (programs) telling the computer
how to perform a specific task.
13.2. Code (Editor) Window: The area where you as programmer type your Java code.
Output Window: The area where Java displays what the end–user will see or displays the
result of the executed code.
Code (Editor) Window
Output Window
CHECK POINT 2
1.
2.
3.
4.
5.
6.
7.
8.
The name of the class is ColouredLines.
2.1. The outer set – Indicates the code that is bound within the class
2.2. The inner set – Indicates the code that is bound within the main method.
Gogga();
The constructor method has the same name as the class and starts with a capital letter. The
constructor method is preceded with the Java keyword new.
louse.
The main method is the starting point from which a Java application is run. The main() method
contains the program instructions that are carried out when the program is run. These instructions or
commands must be inside the main method’s opening and closing curly brackets.
The awt part of this instruction refers to a Java package or folder. A package groups a set of related
classes (or program files). The java.awt package contains all of the classes for creating user
interfaces and for painting graphics and images. Conceptually you can think of packages as being
similar to folder. Because software written in the Java programming language can be composed of
hundreds or thousands of individual classes, it makes sense to keep things organised by placing
related classes and interfaces into packages.
The classes contained in this package are made available to the entire program/class, thus giving
access to the methods and fields needed for the use of colour in your program.
Page 2
9.
10.
setPosition(2, 3);
setColor(Color.BLUE);
setDirection(Gogga.RIGHT);
move();
10.1. True, the louse object can only use methods from the Gogga class.
11.
louse.setDirection(Gogga.LEFT);
12.
Not to be confused with louse.turnLeft();This would turn the louse left based on its previous
position.
Yes. Only the code that follows this method will have the new colour applied to it.
13.
14.
14.1.
14.2.
14.3.
14.4.
14.5.
False
False
False
True
True
15.
15.1. louse.setPosition(2,5);
louse.setColor(Color.YELLOW);
louse.move();
louse.move();
16.
17.
//Your Name: xxxxxxxxxx Date: xx/xx/xxxx
import it.*;
import java.awt.Color;
public class ColouredLines
{
public static void main (String[]args)
{
Gogga louse = new Gogga();
louse.setPosition(2,1);
louse.setDirection(louse.RIGHT);
louse.setColor(Color.BLACK);
louse.move();
louse.move();
louse.move();
louse.move();
Page 3
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.setPosition(2,3);
louse.setDirection(Gogga.RIGHT);
louse.setColor(Color.BLUE);
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.setPosition(2,5);
louse.setColor(Color.YELLOW);
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
louse.move();
}//end ColouredLines class
}//end main method
18.
18.1. Gogga jo = new Gogga();
18.2. jo.setPosition(14,0);
18.3. jo.setDirection(Gogga.DOWN);
18.4. jo.move();
18.5. jo.move();
18.6. jo.move();
19. setPosition(int x, int y);
setDirection(int direction)
setTrailWidth(int t);
setLabel(String label);
setColor(Color col);
20. setPosition(int x, int y);
This method changes the position of the Gogga object to the new x and y co–ordinates.
setDirection(int direction)
Changes the direction of the Gogga object.
The directions are Gogga.UP, Gogga.DOWN, Gogga.LEFT, Gogga.RIGHT.
setTrailWidth(int t);
Changes the width of the Gogga objects trail. If it is set to zero, it will leave no trail.
setLabel(String label);
Page 4
Shows a text label next to the Gogga object.
setColor(Color col);
Changes the colour of the Gogga object and its trail.
21.
parktownPrawn.setLabel(“Parky”);
22.
23.
24.
Include double quotes (“Label”);
import java.awt.Color; OR import java.awt.*;
25.
Error
Example
Fixed
Missing semi–colon (;)
bug.move()
bug.move();
Missing round brackets (())
bug.move(
bug.move();
Missing one of the double quotes (“”)
bug.setLabel(“Thabo);
bug.setLabel(“Thabo”);
Incorrect Spelling
bug.moove();
bug.move();
Another type of error is a logical error. This type of error does not prevent the program from
compiling, but can produce incorrect output. These are errors made by the programmer.
CHECK POINT 3
1.
IDE stands for Integrated Development Environment, which is software that you can use to
type in code, compile it and run it.
2.
3.
4.

The Java code is scrutinised for syntax errors. If errors are found, error message are
displayed.

Once all the syntax errors have been corrected, the Java source code is converted into
byte code format. The byte code format of the original java file has a .class extension.

Once this has been done, a confirmation message is displayed, such as “Process
Complete”.
setGridSize(int column, int row);
Gogga.setGridSize(11, 6);
The Grid has 11 columns numbered from 0 to 10 and six rows numbered from 0 to 5.
Page 5
5.
This is because the grid is independent of any Gogga objects that will be on it, and it must be
set up before any Gogga objects are created, so that it exists for objects to be placed on.
6.

The maximum size of the grid is 31 columns and 19 rows for screen resolutions of 800 x
600. (Gogga.setGridSize(31,19);)

The maximum size of the grid is 40 columns and 26 rows for screen resolutions of
1024 x 768. (Gogga.setGridSize(40,26);)
This may not be valid for all IDE’s.
Page 6
Download