Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP in Java by Kasper B. Graversen1 OOP in Java /EBUSS Kasper B. Graversen Lecture 2.

Similar presentations


Presentation on theme: "OOP in Java by Kasper B. Graversen1 OOP in Java /EBUSS Kasper B. Graversen Lecture 2."— Presentation transcript:

1 OOP in Java by Kasper B. Graversen1 OOP in Java /EBUSS Kasper B. Graversen Lecture 2

2 OOP in Java by Kasper B. Graversen2 Course change Due to lack of computers –Labs from 9-12 in 3.15 + 3.16 –Lectures 13-16 in 3.14 –7 out of 9 homework assignments must be passed –First one will be given next Wednesday Today stops at 12 Course homepage http://www.it-c.dk/~kbilsted

3 OOP in Java by Kasper B. Graversen3 Questions/comments Did the book make any sense? Do you have a question for today’s chapter?

4 OOP in Java by Kasper B. Graversen4 On the right track Code-Compile-Execute cycle Installed and played with the compiler Smallest Java program class A { public static void main(String[] args) { System.out.println(“java is fun”); }

5 OOP in Java by Kasper B. Graversen5 On the right track 2 Stay on the right side of the tracks. Use the compiler and play with the examples the book presents Flunking-% on this type of course at IT-C is ~40%

6 OOP in Java by Kasper B. Graversen6 Course dictionary Attribute,variable:Attribute,variable: defines a property of an object Method,function:Method,function: defines functionality of an object Class:Class: Defines an object Syntax:Syntax: What we write Semantics:Semantics: The meaning of what we write

7 OOP in Java by Kasper B. Graversen7 Today’s program Documentation 101 Simple types Objects Coding style

8 OOP in Java by Kasper B. Graversen8 Comments 101 // till end of the line /* spanning multiple lines */ /** special comments spanning multiple lines */ Comments always above or to the right of what is being commented on! Nesting is not allowed!

9 OOP in Java by Kasper B. Graversen9 Today’s program Documentation 101 Simple types Objects Coding style

10 OOP in Java by Kasper B. Graversen10 The Java language “In Java everything is an object!” * * Except for simple types i.e. the car/shop

11 OOP in Java by Kasper B. Graversen11 Simple types in Java Simple types are –boolean: boolean (logiske værdier) –integer: int long (heltal) –float: float double (kommatal) –character: char (a-Z-0-9) No ambiguity! –The current speed of a car is not just “a value” its representation must be clearly expressed.

12 OOP in Java by Kasper B. Graversen12 Simple types Simple types In earlier languages Simple types was all one had It’s type was determined by its name –i,j,k,l were integers a,b,c,d were chars In “BASIC” a variable could change type runtime In “C” these could be combined in structures - like a class with no functionality

13 OOP in Java by Kasper B. Graversen13 Simple types Why simple types? –Speed, storage space Why is a string not a simple type –Because it has functionality, ie. String s = “hamburger”; System.out.print(s.substring(4,8)); gives “ urge” not “burg”

14 OOP in Java by Kasper B. Graversen14 How we count when programming We always count from 0! –Seen in particularly old literature Thus the first position is in a string is position 0 However, the length of a string is equal to the number of characters in the string, ie “foobar” == 6

15 OOP in Java by Kasper B. Graversen15 How we represent characters in a computer Each character is nothing but a integer value in the computer. These values are called ASCII values In the computer world only the first 127 values are standardized. Danish characters lies outside 127, thus it can be problematic to transfer raw text from ie Windows to Mac Try holding down the alt key and type a number between 0-255 on the numeric pad and release all keys. (65 will produce an “A”)

16 OOP in Java by Kasper B. Graversen16 How we represent simple types in a computer Is a predefined small chunk of the computers memory. These chunks are measured in “bits” and “bytes” - more on this in the next lecture.

17 OOP in Java by Kasper B. Graversen17 How to use simple types Nothing new We now know how to specify these attributes and how we can represent them. Wheel Motor Steering mechanism Body speed color price cleanness Or maybe we are just confused on a higher level?

18 OOP in Java by Kasper B. Graversen18 How would you represent the attributes? class Motor { int speed; // meter/hour } What happens if I choose meter/second? 10 m/s = 36 km/h 11 m/s = 39,6 km/h insecurity of speed = 3,6 km/h first choice insecurity = 1 m/h The motor Motor speed

19 OOP in Java by Kasper B. Graversen19 How would you represent the attributes? class Body { int price; // in $ String color; // only “blue”, “black”, “green” ??? cleanness; } How do we ensure only legal values are used? Is “bluegreen” a valid color and can the system handle it? Compare it? Etc. Sometimes the solution is to not use simple types. The body Body color price cleanness

20 OOP in Java by Kasper B. Graversen20 Comments and attributes Still using comments to specify the units are important. A unit confusion between pounds and newtons led to NASA loosing $94 million on a Mars Climate Orbiter!

21 OOP in Java by Kasper B. Graversen21 Today’s program Documentation 101 Simple types Objects Coding style

22 OOP in Java by Kasper B. Graversen22 The Java language Simple types Classes “Special classes” –Integrated in the syntax String Array –Always exists System

23 OOP in Java by Kasper B. Graversen23 Roles of objects Data container –A king (name, birthyear, deathyear, sons etc.) Functionality provider –A dictionary, converters Mix –Bank account data: balance, name,... functionality: deposit, withdraw, interest rate,... –Human

24 OOP in Java by Kasper B. Graversen24 Object theory An object is an encapsulation of attributes and functionality. Notably, it allows concealing of internal representation and supplying easy to use methods. –speedup(int km) in a car. –deposit(int cent) in a bank account –match(Colour othercar) Allows creating a new kinds of types

25 OOP in Java by Kasper B. Graversen25 Object Theory Object Internal representation Object interfaces (aka methods)

26 OOP in Java by Kasper B. Graversen26 Object Theory Plato (428-348 BC) perceived the world as flawed copies of ideas. All horses derive from the abstract idea “horse” This explained the different appearances of horses

27 OOP in Java by Kasper B. Graversen27 Object Theory In Java a class is “the idea” An object is “the copy” –formally called an instantiation of the class. Designing classes is a matter of defining static structure.

28 OOP in Java by Kasper B. Graversen28 Object Theory How to model a horse? class Horse { String name; int energy; // limits 0-2 void walk() { if(energy > 0) energy--; } void appearance() { if(energy == 0) System.out.println(“dead”); if(energy == 1) System.out.println(“drowsy”); if(energy == 2) System.out.println(“fresh”); } void eat() { energy = 2; }

29 OOP in Java by Kasper B. Graversen29 Object Theory To create a horse, we must create an object Horse h1 = new Horse(); and set appropriate values h1.name = “lucas”; h1.energy = 2; The dot-operator (.) gives access to the objects internals.

30 OOP in Java by Kasper B. Graversen30 Object Theory Formally Horse h1 = new Horse(); is Type nameOfPointer = new Type(); h1 is of the type Horse h1 has the ability to point to horse objects h1 is pointing at a freshly created object

31 OOP in Java by Kasper B. Graversen31 Object Theory Horse h1 = new Horse(); h1.name=“silas”; h1.energy=2; Horse h2 = new Horse(); h2.name=“superhorse”; h2.energy=99; h1 h2 Horse Silas 2 Horse Superhorse 99

32 OOP in Java by Kasper B. Graversen32 Object Theory int h3 = new Horse(); h3.name=“jürgen”; h3.energy=2; h1 < h2 h1 h2 Horse Silas 2 Horse Superhorse 99 h1.energy < h2.energy h1 = h2 h1 == h2

33 OOP in Java by Kasper B. Graversen33 Object Theory Horse h1 = new Horse();Horse h2 = new Horse(); h1.name=“silas”;h2.name=“superhorse”; h1.energy=2;h2.energy=99; h1.appearance(); h2.appearance(); h1.walk(); h2.walk(); h1.appearance(); h2.appearance(); h1.walk(); h2.walk(); h1.appearance(); h2.appearance(); h1.walk(); h2.walk(); h1.appearance(); h2.appearance(); Poor Silas DIED due to a simple cheat!...racing the horses

34 OOP in Java by Kasper B. Graversen34 Object Theory The program was “mechanically” correct Unexpected input made it perform faulty Programming is more than working out the “mechanics” also the input/data needs be secured within expected limits. …programs & input

35 OOP in Java by Kasper B. Graversen35 Object Theory Setting up constraints in the comments clearly doesn’t suffice! Additionally its easy to forget to initialize all attributes Solution: add a constructor …constructing valid objects Horse(String name, int e) { this.name = name; energy = e; } Easy to type, hard to read! No return type

36 OOP in Java by Kasper B. Graversen36 Object Theory But constructors can also check input against the constraints of the class …constructing valid objects Horse(String name, int energy) { this.name = name; if(energy < 3) this.energy = energy; else this.energy = 2; }

37 OOP in Java by Kasper B. Graversen37 Object Theory Constructors defines an interface to creating objects When the constructor is defined Horse foo = new Horse(); is now illegal The new operator must obey the constructor Horse foo = new Horse(“max”, 2); A stringAn integer

38 OOP in Java by Kasper B. Graversen38 Starting programs How do we get started? –Functionality resides inside objects –And can only be executed when objects are created –It takes functionality to create objects Horse h1 = new Horse(“silas”,2); Horse h2 = new Horse(“super”,99); h1.appearance();h2.appearance(); h1.walk(); h2.walk(); h1.appearance();h2.appearance(); h1.walk(); h2.walk(); h1.appearance();h2.appearance(); h1.walk(); h2.walk(); h1.appearance();h2.appearance(); Our horse race

39 OOP in Java by Kasper B. Graversen39 Starting programs Java has a bootstrapping mechanism called main() which is automatically called when the virtual machine is started with a class. The static allows Java to access the main method before object creation.

40 OOP in Java by Kasper B. Graversen40 Starting programs javac *.java java HorseRace class HorseRace { public static void main(String[] args) { Horse h1 = new Horse(“silas”, 2); Horse h2 = new Horse(“superhorse”, 99); h1.appearance(); h2.appearance(); h1.walk(); h2.walk(); h1.appearance(); h2.appearance(); h1.walk(); h2.walk(); h1.appearance(); h2.appearance(); h1.walk(); h2.walk(); h1.appearance(); h2.appearance(); } How do we get on from here?

41 OOP in Java by Kasper B. Graversen41 Order of execution One line at a time In order to use a variable/object it must first be created main(…) { beers = beers+1; int beers; }

42 OOP in Java by Kasper B. Graversen42 static is forbidden! During compilation the compiler reports class SomethingAbstract { public static void main(String[] args) { f(); } void f() {…} } A.java:5: non-static method f() cannot be referenced from a static context f(); ^ Must be read as: you forgot a new somewhere static

43 OOP in Java by Kasper B. Graversen43 Today’s program Documentation 101 Simple types Objects Coding style

44 OOP in Java by Kasper B. Graversen44 Code conventions Class names are always spelled with a starting capital letter The opposite for methods and attributes Attributes can not contain spaces so multiple words are concatenated –int theDogsMothersName

45 OOP in Java by Kasper B. Graversen45 Code conventions The codes are identical to the compiler, are they to you too? class A{public static void main(String[] args){f();}void f(){System.out.print("foo");}} class A { public static void main(String[] args) { f(); } void f() { System.out.print("foo"); }

46 OOP in Java by Kasper B. Graversen46 Code conventions class A { int a; String b; A(String b, int a){…} void b(){…} void c(){…} void d(){…} } class Horse { String name; int energy; Horse(String name, int energy){…} void walk(){…} void appearance(){…} void eat(){…} } Which do you prefer? Spend time on proper naming.

47 OOP in Java by Kasper B. Graversen47 Special classes As String is a class not a simple type, writing String s = “foo”; means String s = new String(“foo”); Horse Silas 2 Should therefore be depicted as Horse name 2 String Silas

48 OOP in Java by Kasper B. Graversen48 The end


Download ppt "OOP in Java by Kasper B. Graversen1 OOP in Java /EBUSS Kasper B. Graversen Lecture 2."

Similar presentations


Ads by Google