Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas"— Presentation transcript:

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

2 Introducing ourselves We 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?

3 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 02/09/2014

4 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 something 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 (Java is a programming language – you write programs in it to make things happen)

5 Why are we starting this way? We want you to get going quickly with programming Seeing how things work and Building things are what computing is all about It is fun (if a bit frustrating at times) It gives you a sense of accomplishment, and It takes PRACTICE!!!

6 Where do we start? (let’s do this now) Logging on Open a browser Accessing the department’s web site Then pick Intranet, Then Teaching The pick Module Information Notes 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

7 PROGRAMMING

8 What is programming? Telling the computer to ‘do’ something You write instructions in a language that you understand The instructions are then translated into something the ‘computer’ understands That is then run The writing is - programming (writing code) The translating is - compiling (sometimes interpreting) The running is - executing (running)

9 Some examples Web page Written in html Every time a browser looks at it, it is translated The equivalent of running would be the displaying The program that UCAS uses to process admissions Written in a programming language Translated into something that runs Run to accept input from AU and produce notifications What we are doing today Written in Java Translated into something that runs in BlueJ Run to make a little Beetle move around the screen

10 BASIC BLUEJ BlueJ is the environment in which we will be writing, translating and running our Java programs

11 02/09/2014 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

12 Here we have used the Java programming language to create 2 Beetles and got them to draw some shapes 02/09/2014

13 Exercise 0 (let’s do this now) Download Beetle.zip from the web page (right click) / 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 project Right 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)

14 Java works by creating Classes and objects The 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 Employee And 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.

15 Ignore this A class called BJBeetle

16 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

17 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

18 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,…

19 So, actually here we have created 2 BJBeetle objects and got them to draw some shapes 02/09/2014 The object bench is a picture of what the memory of the computer looks like

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

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

22 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 method But 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 parameters Some methods have no parameters, some one, some many

23 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

24 Have you completed exercise 1? Created a BJBeetle object Made it do a couple of methods Seen the documentation Can find a method with 4 parameters Please help me! 02/09/2014

25 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 ….

26 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

27 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

28 THE CODE PAD

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

30 We are now writing (short bits of) Java code Notice 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!

31 31

32 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 step When you are done, hit printscreen button and paste into a word document for posterity!

33 Have you completed exercise 2? Drawn one letter Drawn 2 letters Please help me! > What’s the catch? Come back this afternoon it is all gone! 02/09/2014

34 34 What’s the catch? Come back tomorrow it is all gone! A temporary solution: double click on the UseThis class write your code in there (the ‘main code’) click compile then click on the void main of the UseThis class Let’s do it! BJBeetle bob= new BJBeetle(); bob.moveForward(100);

35

36 Here I did this with the code in draw-T.txt 02/09/2014

37 A bit more with coding Not all variables represent objects, some are ‘primitive’: int - means a whole number double 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;

38 Let’s do this now : Create a new class and call it TestOutput Double 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);

39 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)

40 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);

41 LOOPS

42 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

43 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

44 What happened when you tried it?

45 What is happening behind the scenes? for (int counter=0;counter<5;counter++) { System.out.println(“hi”); } This is the loop header Controls the loop with the counter variable This (between the { and the } is the loop body Gets repeated over and over

46 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

47 The loop body Starts with a{ Ends with a} Everything between is the body which gets repeated.

48 How do you design a for loop? Figure out how many times you want to do something – that is your loop control Figure 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 +”: “); } 02/09/2014

49 Exercise 3 (let’s do these now) Download the code Star.txt from the web site Paste it into a class as the ‘main code’ like before and try it Modify 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 )

50 Have you completed exercise 3? Drawn one 5 pointed star Drawn one 7 pointed star Drawn 4 stars Done the times table Please help me! 02/09/2014

51 End of teaching day 1

52 Review Used BlueJ to play with some Java code Created 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++) { } 02/09/2014

53 Have you completed exercise 3? Drawn one 5 pointed star Drawn one 7 pointed star Drawn 4 stars Done the times table Please help me! 02/09/2014

54 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

55 IF s (also input)

56 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

57 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

58 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

59 Look at the documentation 02/09/2014

60 The getInputFromUser() method asks for input 02/09/2014

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

62 The getCurrentValue() method ‘returns’ a value that could be manipulated 02/09/2014

63 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)

64 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

65 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!

66 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

67 This is what happened when I typed in fred Try typing in bill and fred yourself 02/09/2014

68 What happened when you typed in various things? Fred Bill fred &^%*&^ “fred”

69 Now look at Nested Here we have the idea that you can ‘nest’ control structures LOOP{ code IF { …. }

70 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

71 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) >

72 Have you completed exercise 4? Loop through some people Ask if over 18 Print iechyd da if they say yes 02/09/2014

73 Note you are ‘nesting’ an IF in a LOOP You can ‘nest’ all control structures! Note ‘comments’ Already nested loops yesterday when we drew 4 stars BJBeetle 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); }

74 You can nest control structures to any depth for (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 (…) { } } if (…) { //one possibility is break out of the loop } }

75 So you can have an if in a loop or a loop in an if or a loop in an if in a loop The 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 Brown Ms. Mary Smithetc. appropriately

76 First think about the overall structure: Loop through 10 students process 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); } 02/09/2014 Or you could do this in the other order ie first how do one student and then stick a loop around it for lots of students!

77 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!)

78 Have you completed exercise 5? Recognises your initials Drawn the circle if asked for Or draws the star if asked for Please help me! 02/09/2014

79 End of teaching day 2

80 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 - LOOP Have 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?

81 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 object bob.moveForward(100);//making if do something //with an argument There are some things called loops and if statements 02/09/2014

82 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 main You may decide to change module 02/09/2014

83 What next? Instead of just using pre-built classes you will be building your own Instead 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) 02/09/2014

84 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 circle If it gets the word “star” it draws a star If 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.

85 How do I submit it? On Blackboard you should find an assignment for CS12230 called ‘intro assignment’ It is due Monday Oct 7 th at 10am Take screenshots of your code working and paste into a document. Also paste in the code itself Submit that document (don’t forget to press the submit button) 02/09/2014


Download ppt "Starting Java with BlueJ and the Beetle A 2 day course for real beginners – leading to CS12230 Lynda Thomas"

Similar presentations


Ads by Google