85
Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas [email protected]

Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas [email protected]

Embed Size (px)

Citation preview

Page 1: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Starting Java with BlueJ and the BeetleA 2 day course for real beginners – leading to CS12230

Lynda [email protected]

Page 2: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Introducing ourselvesWe are …Introduce yourself to your neighbours

Where are you from?Why you chose the degree scheme you’re

enrolled on?How much (if any) programming have you

done before?What interests you about computing?

Page 3: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Which of the following have you done before? (Choose all that apply)

Used Word or … Made a web site Built a database application Written program in a programming

language None of the above

11/04/23

Page 4: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What should you be able to do at the end of next week (with some help)?

Use the BlueJ IDE (Integrated Development Environment). This is a way beginners can write Java programs

Have a basic understanding of Iteration: lots of times do somethingSelection: if something then do one thing

if not do something else

Mainly by using a ‘Beetle’ that responds to commands

Write a simple Java program (Java is a programming language – you write programs in it to make things happen)

Page 5: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Why are we starting this way?We want you to get going quickly with

programmingSeeing how things work and Building things

are what computing is all aboutIt is fun (if a bit frustrating at times)It gives you a sense of accomplishment, and

It takes PRACTICE!!!

Page 6: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Where do we start? (let’s do this now)Logging onOpen a browserAccessing the department’s web sitehttp://www.aber.ac.uk/compsciThen pick Intranet, Then TeachingThe pick Module InformationNotes are at either:

Blackboard, or Dept’s Web site

Materials for just these 2 days are on the department’s web site under CS12230, under zz-introductory-2Days

Page 7: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

PROGRAMMING

Page 8: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What is programming?

Telling the computer to ‘do’ somethingYou write instructions in a language that you

understandThe instructions are then translated into

something the ‘computer’ understandsThat is then run

The writing is - programming (writing code)The translating is - compiling (sometimes interpreting)The running is - executing (running)

Page 9: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Some examplesWeb page

Written in htmlEvery time a browser looks at it, it is translatedThe equivalent of running would be the

displayingThe program that UCAS uses to process

admissionsWritten in a programming languageTranslated into something that runsRun to accept input from AU and produce

notificationsWhat we are doing today

Written in JavaTranslated into something that runs in BlueJRun to make a little Beetle move around the

screen

Page 10: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

BASIC BLUEJBlueJ is the environment in which we will be

writing, translating and running our Java programs

Page 11: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

11/04/23

This is what BlueJ looks like.

We are going to be creating BJBeetle-y kinds of things – you can think of them as little robots you can control that draw as they move

Ignore this

Page 12: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Here we have used the Java programming language to create 2 Beetles and got them to draw some shapes

11/04/23

Page 13: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Exercise 0 (let’s do this now)Download Beetle.zip from the web page (right click)http://www.aber.ac.uk/~dcswww/Dept/Teaching/CourseNotes/2011-2012/CS12230/zz-introductory-

2Days/

Unzip it (right click and pick extract) and put it in a new directory on your m: drive

Now run BlueJ you will find it under the start button All Programs/ Courseware/ Computer Science

Open the Beetle projectRight click on BJBeetle and pick new BJBeetle()

that will give you a new BJBeetle object(by right clicking on that object you can get it to do

things)

Page 14: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Java works by creating Classes and objectsThe classes describe things in the ‘world’They are like ‘patterns’For instance every employee has a an employee record

with their information in it.

We would have a class called EmployeeAnd then build objects that were instances of that class

each with the same kind of information but different values

In this course we will start with a class called BJBeetle, we can use that class as a ‘pattern’ to make objects with names like bJBeetle1 which you can think of as little robots you can control that draw as they move.

Page 15: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Ignore this

A class called BJBeetle

Page 16: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

BJBeetle is a class and we can make objects of that class by right click, selecting new BJBeetle()

The objects then show up on the ‘object bench’You can have more than one of them – do that

Select this

Page 17: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

It also brings up the actual Beetle on the pink canvas –

but that is because we have programmed it

to do so

and nothing to do with BlueJ as such

Page 18: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

One thing some people get wrong is The class is a pattern for making objects

It is not a collection of objects

So all objects that you make are examples (instances) of the class.

They have the same structure but different values – for instance the colour, the position,…

Page 19: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

So, actually here we have created 2 BJBeetle objects and got them to draw some shapes

11/04/23

The object bench is a picture of what the memory of the computer looks like

Page 20: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

We can make objects do things by selecting their ‘methods’ again with a right click

Page 21: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Find out more about methods by double clicking BJBeetle and selecting ‘documentation’ on top right

Page 22: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Arguments / Parameters

The values that you type in are called arguments. When you moveForward(100) the 100 is the argument.

So we ‘pass in’ the argument to the methodBut you need a way to talk about the value that is moved

forward in a general way. The Beetle moves forward by a ‘distance’. In the context of the method moveBackward, distance is called a parameter

The arguments then, get copied into the parametersSome methods have no parameters, some one, some many

Page 23: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Exercise 1 (let’s do this now)Make a BJBeetle object by right clicking on the

BJBeetle class and selecting new BJBeetle() - you can call it anything but the default will be bJBeetle1

Right click on that object and see the list of things it can do - the methods

Pick several of the methods in turn and see what happens as you move the Beetle around

Most of the methods will ask you to enter arguments(colours are typed in as java.awt.Color.red etc.)

Make sure you make more than one object – just so you can see that one class can describe many similar objects

Page 24: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Have you completed exercise 1?

Created a BJBeetle objectMade it do a couple of

methodsSeen the documentationCan find a method with 4

parametersPlease help me!

11/04/23

Page 25: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

We have to talk about variable names

Variables are names we give to pieces of computer storage/memory so we can manipulate them.

We have seen variables already:

On the object bench we created object variables bJBeetle1 was a variable name

AND ….

Page 26: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Parameters are also variable names

parameters - these are ones that accept the arguments when we call a method bJBeetle1.turn(45); public void turn(int degrees) {

} degree is a parameter variable name - needed so

that in the code for the turn method you can use that instead of ‘45’ or ‘90’.

You need something generic to talk about

Page 27: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The idea that we use names to stand in for values is essential to programming

You can guess that when we tell bJBeetle1 to turn(45) what happens is that the little Beetle ‘s picture is turned by that number of degrees.

When we tell it to moveForward(100) it moves forward by that distance

So we can express what happens generically as an ‘algorithm’ or set of instructions using ‘degrees’ and ‘distance’ to describe the general instructions

Page 28: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

THE CODE PAD

Page 29: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

As a first step towards programming let’s use the code pad instead of selecting methods

Page 30: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

We are now writing (short bits of) Java codeNotice how you write method calls:

bJBeetle1.turn(45);objectName.methodName(argument);

You can define the variables on the code pad instead of the object bench BJBeetle fred=new BJBeetle();

This is how you say fred is a ‘BJBeetle-y thing’fred.turn(45);

Notice how fussy it is!

Page 31: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

31

Page 32: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Exercise 2 (let’s do this now)Use the code pad to define a new BJBeetle to draw

the letters A and U (nice blocky ones – like the T). If that is too boring do your own initials, Don’t do

‘T’ and you probably should pick letters that are ‘straight’! We’ll do curves later.

You’ll need to use position() in order to move from initial to initial

To start again completely RIGHT CLICK on ‘reset virtual machine’

oops() allows you to back up one stepWhen you are done, hit printscreen button and

paste into a word document for posterity!

Page 33: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Have you completed exercise 2?

Drawn one letterDrawn 2 lettersPlease help me!

<<end of first session?>>

What’s the catch?Come back this afternoon it is all gone!

11/04/23

Page 34: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

34

What’s the catch?

Come back tomorrow it is all gone!

A temporary solution:double click on the UseThis classwrite your code in there (the ‘main code’)click compilethen click on the void main of the UseThis class

Let’s do it! BJBeetle bob= new BJBeetle();bob.moveForward(100);

Page 35: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk
Page 36: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Here I did this with the code in draw-T.txt

11/04/23

Page 37: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

A bit more with codingNot all variables represent objects, some are ‘primitive’:int - means a whole numberdouble is a number with a decimal point boolean is true or false

String is not primitive but it is very simple

These kinds of variables are created without saying ‘new’. You just say String name="fred"; int age=19;

Page 38: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Let’s do this now :Create a new class and call it TestOutputDouble click to see its code and replace all its code with the code from UseThis – except replace the word ‘UseThis’ with ‘TestOutput’. Compile.Then replace the ‘main code’ with (or copy output.txt)compile and run (right click the class and select void main)int age=19;

double hourlyWage=4.5;

boolean penUp=false;

String name="fred";

age++;

hourlyWage=hourlyWage*2;

System.out.println(name+" is "+age+” and makes “+ hourlyWage);

Page 39: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

System.out.println() prints stuff in the terminal window

So, up pops the Terminal window and the values of the variables are printed (along with explaining text that you determine)

The + comes between things you want to print, to link them

Notice how ++ adds 1 to an int, and *2 multiplies by 2

Notice also how the quotes and ()s need to match

(to clear the window pick Options-clear)

Page 40: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What is happening?

Declares these variables, says what kinds of things they are

allowed to hold, and puts some initial values in

Changes values

Prints values

int age=19;

double hourlyWage=4.5;

boolean penUp=false;

String name="fred";

age++;

hourlyWage=hourlyWage*2;

System.out.println(name+" is "+age+” and makes “+ hourlyWage);

Page 41: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

LOOPS

Page 42: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

OK, now the first of our ‘control structures’ At the moment our code just runs one

line, then the next etc. Often we want to do the same thing

over and over (for instance printing an invoice of items, a list of students etc)

Called ‘iteration’ There are several different ways of

doing iteration – the loops. We will look at one

Page 43: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The ‘counted for loop’ Does something a certain number of times Looks like this example:

for (int counter=0;counter<5;counter++) {System.out.println(“hi ”+counter);

}Try it now in the UseThis class or a copy

Page 44: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What happened when you tried it?

Page 45: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What is happening behind the scenes?

for (int counter=0;counter<5;counter++) {

System.out.println(“hi”);

}

This is the loop headerControls the loop with the

counter variable

This (between the { and the }is the loop body

Gets repeated over and over

Page 46: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The loop header

for (int counter=0; counter<5; counter++)

Says what kind of loop

This starts up a variable called counter with value 0(the variable can have any

name)

Do the loop while the counter is less than 5

Every time you go through the loop add

one to counter – after doing body

Page 47: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The loop body

Starts with a {

Ends with a }

Everything between is the body which gets repeated.

Page 48: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

How do you design a for loop?Figure out how many times you want to do

something – that is your loop controlFigure out what you want to do over and over – that

is your loop body.Sometimes the control is used in the body:

for (int count=0;count<30; count++) {System.out.println(“Student “+ count +”: “);

}

11/04/23

Page 49: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Exercise 3 (let’s do these now)Download the code Star.txt from the web sitePaste it into a class as the ‘main code’ like before and try itModify it to draw a 7 pointed star (the angle you need is

720/number of sides)

Now can you draw 4 stars? What about 10 stars?(hint: What do you need to do between drawing each star?)

Write a loop that prints out the 5 times table up to 5*10(hint: easier if you add 1 to counter each time and then print counter*5 )

Page 50: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Have you completed exercise 3?

Drawn one 5 pointed starDrawn one 7 pointed starDrawn 4 starsDone the times tablePlease help me!

11/04/23

Page 51: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

End of teaching day 1

Page 52: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

ReviewUsed BlueJ to play with some Java codeCreated several objects of a class like BJbeetle

BJbeetle bob=new BJBeetle(); Called the methods for those objects this involved

passing arguments into the methods – like bob.turn(90);

Done some output like System.out.println(“hi”+name);Looked at loops like

for (int counter=0;counter<10;counter++) {

}

11/04/23

Page 53: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Have you completed exercise 3?

Drawn one 5 pointed starDrawn one 7 pointed starDrawn 4 starsDone the times tablePlease help me!

11/04/23

Page 54: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What should you be able to do at the end of next week (with some help)?Use the BlueJ IDE Have a basic understanding of Iteration

Have a basic understanding of Selection: if something then do one thing

if not do something else

Mainly by using a ‘Beetle’ that responds to commands

Write a simple Java program

Page 55: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

IF s(also input)

Page 56: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

OK, now the second of our ‘control structures’

At first, our code just ran one line, then the next etc.

But now we have seen iteration (doing something over and over)

Sometimes we want to do one thing in one case and something different in another

This is called ‘selection’ We will look at the ‘if’ statement

Page 57: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The ‘if’ statement Does different things in different situations For instance, BJBeetle objects draw if the pen

is down and don’t otherwise

But that means our code usually needs some input to get a different situation

So, first let’s get some input into the program to create different situations

Page 58: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Input (let’s do this now) Download BeetleWithInput.zip from the web

site Unzip it There is a new class called InputGetter that

grabs a String from the user It has 3 methods

Page 59: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Look at the documentation

11/04/23

Page 60: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The getInputFromUser() method asks for input

11/04/23

Page 61: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The getInputFromUser(String message) method asks for input but with a message – note these methods have the same name but different parameters needed

11/04/23

Page 62: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The getCurrentValue() method ‘returns’ a value that could be manipulated

11/04/23

Page 63: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Returning values from a method This is sort of the opposite of passing a value

into a method through argument/parameter this method produces a value that can then be

used – in this case a String Look at the documentation for these methods

(double click on InputGetter and select documentation)

Page 64: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

So, when you create an InputGetter object It sort of sits there and gets a String and returns it to you as

requested Create one, inputGetter1, give it a String NOW double click it This is how you can ‘inspect’ an object in BlueJ

Most of this is not of interest but look at ‘str’ which holds what I typed

Page 65: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

The ‘if’ statement Does different things in different situations Looks like this example:InputGetter reader=new InputGetter();reader.getInputFromUser();String name=reader.getCurrentValue();if (name.equals("fred")) {

System.out.println("found fred");}else { System.out.println("not Fred");}

This code is in UseThis Try it!

Page 66: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What is actually happening?

if (name.equals("fred")) {System.out.println("found fred");

}else { System.out.println("not Fred");}

This is where we test the condition – must be true or false

If true do this

If false do this

Page 67: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

This is what happened when I typed in fredTry typing in bill and fred yourself

11/04/23

Page 68: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What happened when you typed in various things?

FredBillfred389458&^%*&^“fred”

Page 69: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Now look at Nested

Here we have the idea that you can ‘nest’ control structures

LOOP{codecodeIF {

….}

}

Page 70: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

public class Nested{

public static void main(String args[]) {

InputGetter reader=new InputGetter();

reader.getInputFromUser("how many students");

int number=Integer.parseInt(reader.getCurrentValue());

for (int i=0;i<number;i++) {

System.out.println("student "+i);

//process a student

//read name

InputGetter nameReader=new InputGetter();

nameReader.getInputFromUser("enter name");

String name=nameReader.getCurrentValue();

//read gender

InputGetter genderReader=new InputGetter();

genderReader.getInputFromUser("what is your gender (male or female)");

String answer=genderReader.getCurrentValue();

//process and print

if (answer.equals("male")) {

System.out.println("Mr. "+name);

}

else {

System.out.println("Ms. "+name);

}

}

}

11/04/23

Try it!!

Page 71: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Exercise 4 (let’s do this now)Write come code with a loop and an if statement

that Asks someone whether they are over 18. If they say “yes” print “iechyd da”, if not print

“have a coke”

(you can use the Nested as a pattern)

<<end of session 3>>

Page 72: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Have you completed exercise 4?

Loop through some peopleAsk if over 18Print iechyd da if they say

yes

11/04/23

Page 73: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Note you are ‘nesting’ an IF in a LOOPYou can ‘nest’ all control structures! Note ‘comments’

Already nested loops yesterday when we drew 4 starsBJBeetle bob=new BJBeetle();

//four times

for (int counter=0;counter<4;counter++) {

//draw a star this also involves a loop

bob.setPenUp(true); bob.moveForward(100); bob.setPenUp(true);

for (int i=0;i<5;i++) {

bob.turn (720/5);

bob.moveForward(70);

}

}

Page 74: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

You can nest control structures to any depthfor (int i=0;i<4;i++) { for (int j=0;j<3;j++){ System.out.println

(i+" "+j);}

}

if (age<3) { System.out.println("play at home");}else if (age<6) { System.out.println("at nursery");}else if (age<13) { System.out.println("play outside!");}else if (age<18) { System.out.println("no - I won't give you

any money");}else { System.out.println("you are on your own

love");}

if (something) {for (…) {}

}

for (…) {if (…) {

//one possibility is break out of the loop}

}

Page 75: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

So you can have an if in a loop or a loop in an ifor a loop in an if in a loopThe trick is to figure out ‘in words’ what you want to do and only actually code just a bit at a time:

Suppose you want to ask 10 students their names and genders and print something like:

Mr. John BrownMs. Mary Smith etc.

appropriately

Page 76: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

First think about the overall structure:

Loop through 10 studentsprocess a student

for (int i=0; i<10; i++){

//process a student}Now how process just one student? (ie do the body of the loop)

String name=reader1.getCurrentValue();

String gender=reader2.getCurrentValue();

if (gender.equals("male")) {

System.out.println("Mr. "+name);

}

else {

System.out.println("Ms. "+name);

}11/04/23

Or you could do this in the other orderie first how do one student and then stick a loop around it for lots of students!

Page 77: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Exercise 5 (let’s start these now)Modify the code for testif.txt so it recognises

your User ID when you input it and outputs a suitable message.

Modify the code for testif.txt so if you input “circle” a BJBeetle draws a circle.

If not it prints System.out.println(“ok”);you can find the code for a circle in zzCircle.txt this is a loop (the circle) inside an if!

Modify your last code so that also if the user input “star” a BJBeetle draws a star (two loops inside the if!)

Page 78: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Have you completed exercise 5?

Recognises your initialsDrawn the circle if asked forOr draws the star if asked

forPlease help me!

11/04/23

Page 79: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

End of teaching day 2

Page 80: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What should you be able to do at the end of next week (with some help)?Use the BlueJ IDE Have a basic understanding of Iteration - LOOPHave a basic understanding of Selection - IF

Mainly by using a ‘Beetle’ that responds to commands

Write a simple Java program – for that we need to be breaking up our code! Do you notice how complicated it is getting?

Page 81: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Aaaaargh! I am feeling a bit lost!!!!

If this describes you We will be going over all this again in CS122, so don’t panic!I do hope that this brief introduction will have given you some experience that you can bring into the course.If only this much:

BJBeetle bob=new BJBeetle(); //creating an objectbob.moveForward(100); //making if do something

//with an argument

There are some things called loops and if statements

11/04/23

Page 82: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

Aaaaargh! This is really easy!!!!

If this describes you Try

The CS12130 workshop at 11 on Monday in B23 and lecture at 3pm Tuesday in Physics A (I think)

The CS12230 lecture at 5pm Monday in Physics mainYou may decide to change module

11/04/23

Page 83: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

What next?Instead of just using pre-built classes you will be

building your ownInstead of always using the UseThis class you will

create your own proper classes to run proper applications

These classes will be able to be run independent of BlueJ altogether in other ways (actually we already have this – type java UseThis at the command line prompt)

11/04/23

Page 84: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

How do I get marked on this? By Monday Oct 7th:Modify the code you produced in Exercise 5 so that

your ‘program’ asks for input 10 times.If it gets the word “circle” it draws a circleIf it gets the word “star” it draws a starIf it recognises your user id it draws it in block letters

(ie my program would draw LTT)Otherwise it just says “cannot recognise”

That means you will have to have designed code for your UID. (Start by just recognising it.)

Completing this counts as 2 worksheets in CS122 (there are 10 worth 10%)

If you don’t get this done then:Completing Exercise 5 - counts as 1 worksheet in

CS122.

Page 85: Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas ltt@aber.ac.uk

How do I submit it?On Blackboard you should find an assignment for

CS12230 called ‘intro assignment’It is due Monday Oct 7th at 10amTake screenshots of your code working and paste

into a document.Also paste in the code itselfSubmit that document (don’t forget to press the

submit button)

11/04/23