programming task: task 1 controlled assessment practice

12
Programming Task: Task 1 Controlled Assessment Practice

Upload: vanessa-fox

Post on 24-Dec-2015

231 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Programming Task: Task 1 Controlled Assessment Practice

Programming Task: Task 1

Controlled Assessment Practice

Page 2: Programming Task: Task 1 Controlled Assessment Practice

1. In NetBeans, create a new project by going to "File > New Project." In the "Choose Project" window, click "Java Application" and click "Next." Name the project “ControlledAssessment" and click "Finish.“

2. Under "Source Packages" double-click the “controlledassessment" package (with the brown box icon)

3. Click "New > Java Class." Name the class “FirstApplet" and click "Finish”

Creating An Applet

Page 3: Programming Task: Task 1 Controlled Assessment Practice

4. Under the line "package controlledassessment;" type the following two lines of Java code:

import java.applet.*;import java.awt.*;

This imports the Applet and Graphics Java libraries, which you will use in your code.

5. 4 Between "public class FirstApplet" and the "{" type "extends Applet" to tell Java that the FirstApplet class is going to be an Applet.

Creating An Applet

* Le me wondering why my applet needs to go to the library

* Le me finding out that library files are pre-written files of code that I can call inside my code!

Page 4: Programming Task: Task 1 Controlled Assessment Practice

1. Create the following methods to have your Java code initialize the Applet "paint" a line of text on the screen: public void paint (Graphics g) {g.drawstring (“Hello World”, 25, 50);}

2. Click "Run" from the top menu of NetBeans, then click "Run File."

Your Java Applet's window will pop up and you will see your text on the screen.

Creating An Applet

Page 5: Programming Task: Task 1 Controlled Assessment Practice

We know that we need a number between 0 and 255 for our binary game, but it needs to be random…..

1. Under the line "package controlledassessment;" type another line to import the Random library:

import java.util.Random;

2. Inside the Paint() class, we create a new instance of a Random:

Random myRand = new Random();

Generating Random Numbers

Page 6: Programming Task: Task 1 Controlled Assessment Practice

1. Next, we create an integer variable and give use myRand to generate a random number up to 255:

int randomInt = myRand.nextInt(255);

2. Finally, we print this out to the applet screen:

Generating Random Numbers

g.drawString ("Random Number was:" + randomInt, 25, 60);

* Le me realising that to draw a new line, I need to add 10 to the y axis of the draw() co-ordinates!

Page 7: Programming Task: Task 1 Controlled Assessment Practice

1. Firstly, we create two TextField variables

private TextField input; private TextField output;

2. Then, we create a new method inside the applet:

public void init() { }

3. Then we create an instance of a TextField on the applet screen:

this.input = new TextField(10); this.output = new TextField(50);

Entering Data We are going to create a new function within the applet where we will begin to create text boxes to allow the user to type in data.

Page 8: Programming Task: Task 1 Controlled Assessment Practice

4. Finally, we want to add these textboxes to the screen!

this.add(input); this.add(output);

5. Click "Run" from the top menu of NetBeans, then click "Run File."

Your Java Applet's window will pop up and you will see your text and the new text boxes on the screen.

Entering Data

Page 9: Programming Task: Task 1 Controlled Assessment Practice

1. We first import the extra library classes needed for ‘listening’ for events.

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

2. Then we implement the ActionListener in our main class so that we can run our button. And create an instance of a button.

User InteractionBefore your user can interact with the screen at all, the form will need a button to call the function. This is called an ‘on-click event’

Page 10: Programming Task: Task 1 Controlled Assessment Practice

1. Next we want to add the button to the applet in the same way that we did for the text boxes

2. Then link the button to the ActionListener.

3. Finally, we need to create a method called ‘actionPerformed’ which hears the button click and performs an action (like putting some text into a text box).

4. Now, run your file!

User Interaction

Page 11: Programming Task: Task 1 Controlled Assessment Practice

Your finished code should look like this:

Import library classes

Instantiate (create) variables

Initial function

actionPerformed function listening for button click

Function creating random numbers

Page 12: Programming Task: Task 1 Controlled Assessment Practice

1. Can you put the text from one box into another?

2. Can you put the random number into a text box?

3. Can you save a random number and add it to the next one?

Extra Tasks