1 / 48

A Reality Check a sort of quiz

A Reality Check a sort of quiz. Dave Elliman. A Story with a Moral “My brother”. What Is a Computer?. What Is a Computer Program?. What Bits and Pieces Must a Computer Have?. What is the Point in Having Programming Languages like Java?. What does javac do?. What does java do?.

anoush
Download Presentation

A Reality Check a sort of quiz

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Reality Checka sort of quiz Dave Elliman

  2. A Story with a Moral “My brother”

  3. What Is a Computer?

  4. What Is a Computer Program?

  5. What Bits and Pieces Must a Computer Have?

  6. What is the Point in Having Programming Languages like Java?

  7. What does javac do?

  8. What does java do?

  9. What is the trick that enables java to run on any computer?

  10. There are two fundamental types of data object in Java what are they?

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

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

  13. A byte in computer memory contains00010101What does it represent?

  14. 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. What does new really do in Java?

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

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

  18. 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. myArray = 3 6 3 1 6 3 4 1 0 1 2 3 4 5 6 7 • myArray has room for 8 elements • the elements are accessed by their index • in Java, array indices start at 0

  20. 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. 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. Iterating Through Arrays • for loops are useful when dealing with arrays: for (int i = 0; i < myArray.length; i++) { myArray[i] = getsomevalue(); }

  23. 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. 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. Java Methods & Classes

  26. 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. Simple class class Fruit{ int grams; int cals_per_gram; }

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

  29. 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. Source Files • Put: public class Fred { } IN Fred.java A Source file can have only one public class in it

  31. 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. 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. 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. 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. 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. 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. 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. Object Oriented Programming methods instance variables messages break() speed = 45.7; gear = 3; changeGears(g) a program an object

  39. 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 The three principles of OOP car Super class auto- matic manual Subclasses draw() draw()

  40. 1 2 1 2 1 Example: Russian Roulette 5/6 1/6 4/6 2/6 3/6 3/6 2/6 4/6 1/6 5/6 2

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

  42. Roulette Classes instances Model model new new new player1 Player player2 Revolver revolver

  43. 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. Inheritance II • How to use … eg. Citrus lemon = new Citrus(); lemon.squeeze(); lemon.total_calories(); • old methods exist alongside new methods …

  45. 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. 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. 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. 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/tutorial/index.html

More Related