Title: A Reality Check a sort of quiz
1A Reality Checka sort of quiz
2A Story with a Moral My brother
3What Is a Computer?
4What Is a Computer Program?
5What Bits and Pieces Must a Computer Have?
6What is the Point in Having Programming Languages
like Java?
7What does javac do?
8What does java do?
9What is the trick that enables java to run on any
computer?
10There are two fundamental types of data object in
Java what are they?
11A variable in java has the name fred. What does
the java run time need to know about fred?
12I enter 1.2345 at the keyboard. What type am I
creating?
13A byte in computer memory contains00010101What
does it represent?
14A 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?
15What does new really do in Java?
16What is output by this program?
public static void main(String args) String
fred fred fred fred.toUpperCase()
System.out.println(fred)
17What is the value of x?
int x x 2 3 4 / 2 2
Why?
18A 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 has room for 8 elements
- the elements are accessed by their index
- in Java, array indices start at 0
20Declaring Arrays
- int myArray
- declares myArray to be an array of integers
- myArray new int8
- sets up 8 integer-sized spaces in memory,
labelled myArray0 to myArray7 - int myArray new int8
- combines the two statements in one line
21Assigning Values
- refer to the array elements by index to store
values in them. - myArray0 3
- myArray1 6
- myArray2 3 ...
- can create and initialise in one step
- int myArray 3, 6, 3, 1, 6, 3, 4, 1
22Iterating Through Arrays
- for loops are useful when dealing with arrays
- for (int i 0 i lt myArray.length i)
- myArrayi getsomevalue()
-
23Arrays 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.
24Declaring the Array
- 1. Declare the array
- private Student studentList
- this declares studentList
- 2 .Create the array
- studentList new Student10
- this sets up 10 spaces in memory that can hold
references to Student objects - 3. Create Student objects and add them to the
array studentList0 new Student("Cathy",
"Computing")
25Java Methods Classes
26Classes 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 -
-
27Simple class
- class Fruit
- int grams
- int cals_per_gram
-
28Methods ...
- Class Fruit
- nt grams
- int cals_per_gram
- int total_calories()
- return(gramscals_per_gram)
-
29Another 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 -
-
30Source Files
- Put
- public class Fred
-
- IN Fred.java
- A Source file can have only one public class in it
31Methods
- 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 )
-
32Method 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
33Using objects
- Here, code in one class creates an instance of
another class and does something with it - Fruit plumnew Fruit()
- int cals
- cals plum.total_calories()
- Dot operator allows you to access (public)
data/methods inside Fruit class
34Public/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
35Creating 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
36Constructors
- 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) ...
37Overloading
- Can have several versions of a method in class
with different types/numbers of arguments - Fruit()grams50
- Fruit(a,b)
- gramsacals_per_gramb
-
- By looking at arguments Java decides which
version to use
38Object Oriented Programming
instance variables
methods
messages
break()
speed 45.7 gear 3
changeGears(g)
an object
a program
39The 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
Super class
auto- matic
manual
Subclasses
draw()
draw()
40Example Russian Roulette
5/6
1/6
4/6
2/6
3/6
3/6
2/6
4/6
1/6
5/6
2
41Web of message calls...
revolver
load()
model
trigger()
trigger()
player1
player2
42Roulette
Classes
instances
Model
model
new
new
new
player1
Player
player2
Revolver
revolver
43Inheritance ...
- 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().
-
44Inheritance II
- How to use
- eg.
- Citrus lemon new Citrus()
- lemon.squeeze()
- lemon.total_calories()
- old methods exist alongside new methods
45Overriding
- 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 ...
46Libraries
- 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
47Interface vs. implementation
User only has to be familiar with the interface
of an object, not its implementation
Objects hide their functions and data
48Where 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