Presentation is loading. Please wait.

Presentation is loading. Please wait.

G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman.

Similar presentations


Presentation on theme: "G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman."— Presentation transcript:

1 G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman

2 G51PRG-Sem2 A Story with a Moral “My brother”

3 G51PRG-Sem2 What Is a Computer?

4 G51PRG-Sem2 What Is a Computer Program?

5 G51PRG-Sem2 What Bits and Pieces Must a Computer Have?

6 G51PRG-Sem2 What is the Point in Having Programming Languages like Java?

7 G51PRG-Sem2 What does javac do?

8 G51PRG-Sem2 What does java do?

9 G51PRG-Sem2 What is the trick that enables java to run on any computer?

10 G51PRG-Sem2 There are two fundamental types of data object in Java what are they?

11 G51PRG-Sem2 A variable in java has the name fred. What does the java run time need to know about fred?

12 G51PRG-Sem2 I enter 1.2345 at the keyboard. What type am I creating?

13 G51PRG-Sem2 A byte in computer memory contains 00010101 What does it represent?

14 G51PRG-Sem2 A Simple Java Program: public static void main(String args[]) { System.out.println(“ I Love G51PRG”); } What is the meaning of the word static in this program?

15 G51PRG-Sem2 What does new really do in Java?

16 G51PRG-Sem2 What is output by this program? public static void main(String args[]) { String fred; fred = “fred”; fred.toUpperCase(); System.out.println(fred); }

17 G51PRG-Sem2 What is the value of x? int x; x = 2 * 3 + 4 / 2 + 2; Why?

18 G51PRG-Sem2 A Proper Object import java.io.*; public class ReadDouble { double readDouble() { String aLine = ""; double d = 0.0; boolean failed = false; BufferedReader input; input = new BufferedReader(new InputStreamReader(System.in)); try { aLine = input.readLine(); } catch (Exception e) { failed = true; } try { d = Double.parseDouble(aLine); } catch (Exception e) { failed = true; } if(failed) { System.out.println("Invalid Coefficient!"); System.exit(-1); } return d; } what filename should be used?

19 G51PRG-Sem2 myArray has room for 8 elements the elements are accessed by their index in Java, array indices start at 0 36316341 myArray = 01234567

20 G51PRG-Sem2 Declaring Arrays int myArray[]; declares myArray to be an array of integers myArray = new int[8]; sets up 8 integer-sized spaces in memory, labelled myArray[0] to myArray[7] int myArray[] = new int[8]; combines the two statements in one line

21 G51PRG-Sem2 Assigning Values refer to the array elements by index to store values in them. myArray[0] = 3; myArray[1] = 6; myArray[2] = 3;... can create and initialise in one step: int myArray[] = {3, 6, 3, 1, 6, 3, 4, 1};

22 G51PRG-Sem2 Iterating Through Arrays for loops are useful when dealing with arrays: for (int i = 0; i < myArray.length; i++) { myArray[i] = getsomevalue(); }

23 G51PRG-Sem2 Arrays of Objects So far we have looked at an array of primitive types. –integers –could also use doubles, floats, characters… Often want to have an array of objects –Students, Books, Loans …… Need to follow 3 steps.

24 G51PRG-Sem2 Declaring the Array 1. Declare the array private Student studentList[]; –this declares studentList 2.Create the array studentList = new Student[10]; –this sets up 10 spaces in memory that can hold references to Student objects 3. Create Student objects and add them to the array: studentList[0] = new Student("Cathy", "Computing");

25 G51PRG-Sem2 Java Methods & Classes

26 G51PRG-Sem2 Classes ARE Object Definitions OOP - object oriented programming code built from objects Java these are called classes Each class definition is coded in a separate.java file Name of the object must match the class/object name

27 G51PRG-Sem2 Simple class class Fruit{ int grams; int cals_per_gram; }

28 G51PRG-Sem2 Methods... Class Fruit{ nt grams; int cals_per_gram; int total_calories() { return(grams*cals_per_gram); } }

29 G51PRG-Sem2 Another Class public class Point { public double x, y; private attribute; public Point() { x = 0; y = 0; size = 1; } public double getSize() { return size; } public void setSize(int newSize) { size = newSize; } }

30 G51PRG-Sem2 Source Files Put: public class Fred { } INFred.java A Source file can have only one public class in it

31 G51PRG-Sem2 Methods A method is a named sequence of code that can be invoked by other Java code. A method takes some parameters, performs some computations and then optionally returns a value (or object). Methods can be used as part of an expression statement. public float convertCelsius(float tempC) { return( ((tempC * 9.0f) / 5.0f) + 32.0 ); }

32 G51PRG-Sem2 Method Signatures A method signature specifies: –The name of the method. –The type and name of each parameter. –The type of the value (or object) returned by the method. –The checked exceptions thrown by the method. –Various method modifiers. –modifiers type name ( parameter list ) [throws exceptions ] public float convertCelsius (float tCelsius ) {} public boolean setUserInfo ( int i, int j, String name ) throws IndexOutOfBoundsException {}

33 G51PRG-Sem2 Using objects Here, code in one class creates an instance of another class and does something with it … Fruit plum=new Fruit(); int cals; cals = plum.total_calories(); Dot operator allows you to access (public) data/methods inside Fruit class

34 G51PRG-Sem2 Public/private Methods/data may be declared public or private meaning they may or may not be accessed by code in other classes … Good practice: –keep data private –keep most methods private well-defined interface between classes - helps to eliminate errors

35 G51PRG-Sem2 Creating objects Following code creates an instance of the Fruit class Fruit plum; defines the plum object plum = new Fruit(); creates it in memory the content of the Fruit class must be defined in another file Fruit.java

36 G51PRG-Sem2 Constructors The line plum = new Fruit(); invokes a constructor method with which you can set the initial data of an object You may choose several different type of constructor with different argument lists eg Fruit(), Fruit(a)...

37 G51PRG-Sem2 Overloading Can have several versions of a method in class with different types/numbers of arguments Fruit(){grams=50;} Fruit(a,b){ grams=a;cals_per_gram=b; } By looking at arguments Java decides which version to use

38 G51PRG-Sem2 Object Oriented Programming instance variablesmethods an object a program speed = 45.7; gear = 3; break() changeGears(g) messages

39 G51PRG-Sem2 The three principles of OOP Encapsulation –Objects hide their functions (methods) and data (instance variables) Inheritance –Each subclass inherits all variables of its superclass Polymorphism –Interface same despite different data types car auto- matic manual Super class Subclasses draw()

40 G51PRG-Sem2 Example: Russian Roulette 1 2 1 2 1 1/6 5/6 2/6 4/6 3/6 4/6 2 5/6 3/6 2/6 1/6

41 G51PRG-Sem2 Web of message calls... model player2 player1 pass( ) revolver pass( ) revolver load() trigger()

42 G51PRG-Sem2 Roulette Model Player Revolver model player1 player2 revolver new Classes instances

43 G51PRG-Sem2 Inheritance... Important feature of OOP - new classes can be based on existing classes eg. Could define a `specialized’ type of Fruit class called Citrus … Has all methods of Fruit plus possibly some new ones eg class Citrus extends Fruit{ void squeeze(){….} }

44 G51PRG-Sem2 Inheritance II How to use … eg. Citrus lemon = new Citrus(); lemon.squeeze(); lemon.total_calories(); old methods exist alongside new methods …

45 G51PRG-Sem2 Overriding Even more powerful concept of OOP –can override the functionality of one method in a descendant class eg. Add method peel() to Fruit class. Since Citrus extends Fruit this method will also be available to an instance of Citrus But can redefine content of peel() inside of Citrus - the new definition hides the earlier...

46 G51PRG-Sem2 Libraries Java comes with libraries for creating GUIs and network applications and for embedding in Web pages- java.applet.Applet eg import java.awt.*; compile to byte code - can be run by any system with a Java interpreter - portable! Relatively robust and secure

47 G51PRG-Sem2 Interface vs. implementation User only has to be familiar with the interface of an object, not its implementation Objects hide their functions and data

48 G51PRG-Sem2 Where to Revise Eckel, B. Thinking in Java –Free online book: http://www.ibiblio.org/pub/docs/ books/eckel/ - Sun Tutorials: http://java.sun.com/docs/books/t utorial/index.html


Download ppt "G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman."

Similar presentations


Ads by Google