Graphical Programming in Java Robert Zimmer 25 St
Graphical Programming in Java Robert Zimmer 25 St James r. zimmer@gold. ac. uk x 7596 CIS 219/CIS 220 1
This Course is About • Object-Orientation – Principles of Structuring Systems • Java Programming – As an implementation language for object-orientation • Graphical Programming (2 D) – As an application area for object-orientation principles • Media Player – As another application area for object-orientation principles CIS 219/CIS 220 2
Programming Paradigms • Imperative This means giving commands Imperative programming works by give a sequence of commands to the computer The flow of control is fixed at the top level CIS 219/CIS 220 3
factorial Read n Set fact to 1 For i= 1 to n make fact be fact * n Return fact ---Note you are only doing one thing. If you are doing more then this would just be embedded as part of a longer thing CIS 219/CIS 220 4
• Nim, for example, Might look like a sequence of steps, all of which are explicitly laid out • Most of what you did in CIS 109 was really imperative in spirit CIS 219/CIS 220 5
Structured Programming • Imperative Programming still, but blocks of code structured as functions and procedures that are called elsewhere • This is on the way to object orientation CIS 219/CIS 220 6
Functional Programming Here you program by writing mathematical functions: fun fact (n) = if n <2 then 1 else n* (fact(n-1)); e. g. LISP, ML, HASKEL CIS 219/CIS 220 7
Logic Programming • Here you program by telling the computer facts about the World • Computation is logical inference CIS 219/CIS 220 8
man (Socrates) mortal (x) : - man (x) mortal (? ) Answer is Socrates CIS 219/CIS 220 9
Object-Orientation • Computing Systems work by having several objects semi-autonomously performing actions and sending each other messages – All systems are a bit like simulation games – Example in Subject Guide: a Cop and Robbers game CIS 219/CIS 220 10
• Each Cop and each robber is an object • But you do not define them individually as objects; you define a general class of each and make the individuals as instances of the classes • The classes have much in common which we take advantage of using inheritance, more next week on that. CIS 219/CIS 220 11
• Encapsulation – Bundle together related behaviour in an object • Hiding – Objects only make public what you need to to be usable – You do not need to know how a cop’s intestine works – This means they present themselves abstractly CIS 219/CIS 220 12
Objects have • Data – A Cop has a position, a speed and direction of movement • Methods – A Cop can run, shoot, respond to being hit in the head, can draw itself on a screen CIS 219/CIS 220 13
Classes • Each Object is an object of a particular class – bob. The. Cop is an object of class Cop • When you write programs you define classes • When you define a class you say – what data the objects of that class will have – what the object can do (methods) – What the class can do (static methods) CIS 219/CIS 220 14
Static and non-static (dynamic? ) • Cops have positions (they belong to individual objects). The position is a non static variable • Caps have coloured uniform. The colour of the uniforms are necessary the same. That variable belongs to the class. It is static. CIS 219/CIS 220 15
An important kind of static method is a constructor Constructors are recipes for making objects An important static method is main This is called when the application is run CIS 219/CIS 220 16
Fundamental Principles of OO • Objects are autonomous and work by sending each other messages • Things should be thought about abstractly • Things should be written in ways in which they can be reused • Abstraction Hierarchies are used to make things more general more flexible and more likely to be correct • -- • I want to look at how these principles map onto a 17 simple problem from. CIS 219/CIS 220 Tuesday
• The problem: Write a method that, given two int inputs, returns the sum of their squares: for example, 3 , 4 - 25 because 32 + 42 = 25 CIS 219/CIS 220 18
A mistake: • Class Sum. Squares 1 static public void main (String [] args) { Buffered. Reader in. Line = new Buffered. Reader(new Input. Stream. Reader(System. in)); System. out. println(“enter a number”); int a = in. Line. read(); System. out. println(“enter a number”); int b = in. Line. read(); CIS 219/CIS 220 System. out. println(“answer is” + a*a + b*b); 19
Why is this a mistake? • We have not written anything that is reusable • We have not written a method that returns a value at all • Reading and writing is not part of a sum of square method CIS 219/CIS 220 20
Better, but still poor Class Sum. Squares 2 { static public void main (String [] args) { Buffered. Reader in. Line = new Buffered. Reader(System. in); System. out. println(“enter a number”); int a = in. Line. read(); System. out. println(“enter a number”); int b = in. Line. read(); System. out. println(“answer is” + sum. Of. Squares(a, b); } int sum. Squares (int x, int b) { return x*x + y*y; CIS 219/CIS 220 21
What’s wrong here • Well we now have a method we can use elsewhere, however • We can only use this class when it is the main class in the application CIS 219/CIS 220 22
Best class Sum. Squares 3 { int sum. Squares (int x, int b) { return x*x + y*y; } } --That’s it, Something a bit better will. CIS 219/CIS 220 come along in a couple of slides 23
• But, I hear you say, you cannot do anything with that. • Bah! • If you are using Blue. J you can play with it directly • And you can call it from other classes • So, you would write a separate class, called Try. Sum. Squares, that would query this CIS 219/CIS 220 24
public class Try. Sum. Squares { public static void main (String [] args) { Sum. Squares 3 test. SS = new Sum. Squares 3(); int a = Integer. parse. Int (args[0]); int b = Integer. parse. Int (args[1]); System. out. println(test. SS. sum. Squares(a, b)); } } that had I made sum. Squares static I would not need to Note make the object test. SS, but could have called the method from the class. CIS 219/CIS 220 25
• We have two classes here: Sum. Squares 3 is reusable try. Sum. Squares is only useful in one application. This is true of any class with a main method. In Laszlo, as in most OO programming books, you will find this distinction carried through. E. g. in CIS 219/CIS 220 Laszlo Chapter 3, we have Try. Point and 26
How can we make this even better • Instead of Sum. Squares 3 being its own class, we would have one permanent, reusable class that had several related methods in them. • For example, you may have a class called Int. Arith that had lots of the integer arithmetic you might use CIS 219/CIS 220 27
• A Constructor needs to assign values to the data of the object it creates • Constructors tend to be overloaded – Different versions take different data as parameters and take default values for the rest CIS 219/CIS 220 28
public class Ball { protected int diameter; protected int x. Position; protected int y. Position; protected Color colour; public Ball() { diameter = 30; x. Position = 20; y. Position = 60; colour = Color. red; } public Ball(int size) { diameter = size; x. Position = 20; y. Position = 60; colour = Color. red; dx = 5; dy = 5; } CIS 219/CIS 220 o 29
Making objects • Ball a. Ball = new Ball(); • Ball another. Ball = new Ball(); • Ball yet. Another. Ball = new Ball (77); • Creates three different ball objects: the third is a different size than the rest. CIS 219/CIS 220 30
• Classes live in class hierarchies • A Bouncing. Ball is a kind of Ball – The Bouncing. Ball class is a child class of Ball CIS 219/CIS 220 31
Methods and Abstraction • We now turn to methods and how to think of them abstractly • Object-orientation is about putting autonomous things together • What information do you need to be able to use a method? CIS 219/CIS 220 32
What information do you need to be able to use a method? 1. What it expects of its inputs 2. What it promises about its outputs 3. What other effects running it will have CIS 219/CIS 220 33
Inputs example: factorial int factorial (int n) { if (n == 0) {return 1; } else {return (n * factorial(n-1)); } } CIS 219/CIS 220 34
• What happens if I input – 1 • So with this particular version of factorial we are assuming the input is non-negative and an integer • We put that in the interface. We make a comment about it CIS 219/CIS 220 35
//input is any non-negative integer //output is the factorial of the input int factorial (int n) { if (n == 0) {return 1; } else {return (n * factorial (n-1)); } } CIS 219/CIS 220 36
//input is any non-negative integer //output is the factorial of the input int factorial 2 (int n) { int ans = 1; for (int temp =1; temp <= n; temp++) { ans = ans * temp; } return ans; } CIS 219/CIS 220 37
• Note that factorial and factorial 2 are both fine with that specification • However, they behave differently with negative inputs • If all we ever care about is the factorial of nonnegative inputs we can choose either of them though CIS 219/CIS 220 38
Recall the use of Static • We may not want to make an object every time we want to run factorial. Making the method static will change that CIS 219/CIS 220 39
static int factorial 3 (int n) { int ans = 1; for (int temp =1; temp <= n; temp++) { ans = ans * temp; } return ans; } CIS 219/CIS 220 40
Outputs and effects Recall the move method from ball void move (int dx, int dy) { this. x = x + dx; this. y = y+dy; } What is the output? CIS 219/CIS 220 41
There is no output //inputs are two integers //no outputs //effect is to translate the ball by the two inputs void move (int dx, int dy) { this. x = x + dx; this. y = y+dy; } Nothing is output but the state of the ball has changed. CIS 219/CIS 220 42
State • The state of an object is the value of its datafields • In the case of a ball it is: its position, colour, direction of movement, … • The state of an object can change without changing what object it is CIS 219/CIS 220 43
Inputs and Outputs • You can compose functions so that the outputs of one are the inputs of another factorial CIS 219/CIS 220 44
Choosing marbles If you have a bag of n marbles how many ways are there to choose k of them? Answer k!/(n!)(n-k)! CIS 219/CIS 220 45
• It is essentially: public int choose (int set. Size, int sub. Set. Size) { int top = factorial (set. Size); int bottom =factorial(sub. Set. Size)* factorial (set. Size - sub. Set. Size); return top/bottom; } And that would work fine if it was in the same file as factorial. How do we handle this with them in different classes? CIS 219/CIS 220 46
Fact 1 fact. Object = new Fact 1 (); public int choose (int set. Size, int sub. Set. Size) { int top = fact. Object. factorial (set. Size); int bottom = fact. Object. factorial(sub. Set. Size)* fact. Object. factorial (set. Size - sub. Set. Size); return top/bottom; } CIS 219/CIS 220 47
public int choose. Static (int set. Size, int sub. Set. Size) { int top = Static. Fact. factorial 3 (set. Size); int bottom =Static. Fact. factorial 3(sub. Set. Size) * Static. Fact. factorial 3 (set. Size - sub. Set. Size); return top/bottom; } CIS 219/CIS 220 48
CIS 219/CIS 220 49
• Back to the question of limiting input spaces: – Suppose we want factorial to respond to negative inputs by printing “No you idiot: input must be positive” CIS 219/CIS 220 50
public class Fact 5 { int factorial (int n) { if (n <0) {System. out. println("whoops"); } else if (n == 0) {return 1; } else {return (n * factorial(n-1)); } } CIS 219/CIS 220 51
• That gives an error because there is no return on one of the paths CIS 219/CIS 220 52
CIS 219/CIS 220 53
public class Fact 5 { int factorial (int n) { if (n <0) {System. out. println("whoops"); return 0; } else if (n == 0) {return 1; } else {return (n * factorial(n-1)); } } } CIS 219/CIS 220 54
CIS 219/CIS 220 55
- Slides: 55