Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.

Similar presentations


Presentation on theme: "© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen."— Presentation transcript:

1 © Vinny Cahill 1 Writing a Program in Java

2 © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.

3 © Vinny Cahill 3 Hello World – Objects and Classes An instance of class Terminal An instance of class String Another instance of class String Class String is built into Java Class Terminal is part of a collection of pre-existing classes called tcdIO

4 © Vinny Cahill 4 Functions and Parameters l Given f(x) = x 2 + 4x + 13 l What is f(10)? l What is f(20)? l What is f(30)? x is the formal parameter of function f 10 is an actual parameter of function f

5 © Vinny Cahill 5 Variables 1 A variable is a container for information l In our program Terminal window; // Used to store object representing the window l window is the name of a container used to store a Terminal object l Alternatively: Terminal box;

6 © Vinny Cahill 6 Variables 2 Every variable has a name, a type, and a single (current) value window type is Terminal name value Think of a variable as a container for a value of the specified type

7 © Vinny Cahill 7 Variables 3 window is a local variable because it is defined within a method public static void main (String args[]) { Terminal window; // store object representing terminal.. }

8 © Vinny Cahill 8 Variables 4 The value stored in a variable can be changed using assignment window that why its called a variable! window window = new Terminal("Hello Window"); Assignment operator

9 © Vinny Cahill 9 Variables 5 The value stored in a variable can be changed using assignment as often as we want window window = new Terminal("Another Window");

10 © Vinny Cahill 10 Type int Type int represents positive and negative whole numbers four bytes of memory are used to store a value of type int thus, an int can contain a number in the range -2,147,483,648 to 2,147,483,647 values of type int can be written in decimal

11 © Vinny Cahill 11 Integer operators 1 There are five integer operators Operator Meaning Examples Use + op1 + op2 add values of op1 and op2 newSalary = oldSalary + rise; count = count +1; - op1 - op2 subtract op2 from op1 newSalary = oldSalary - 100; * op1 * op2 multiply op1 by op2 area = length * breath; minutes = hours * 60; / op1 / op2 divide op1 by op2 length = area / breath; % op1 % op2 remainder on dividing op1 by op2 pounds = pence / 100 pence = pence % 100;

12 © Vinny Cahill 12 Integer operators 2 The int operators are all binary operators –they all take two operands The int operators all return a single result of type int For example: int result, number1, number2; number1 = 7; number2 = 2; result = number1 / number2; result = number1 % number2;

13 © Vinny Cahill 13 Integer expressions We can have expressions that involve multiple operators pay = salary + bonus - tax; monthlyPay = salary + bonus - tax / 12; Note that the order in which operators are evaluated is important! *, /, % are always evaluated before +, - i.e. *, /, % have higher precedence than +, - monthlyPay = (salary + bonus - tax) / 12; Operators of equal precedence are evaluated left to right i.e. they are left associative

14 © Vinny Cahill 14 Average /* A program to get the average of five numbers entered by the user */ import tcdIO.*; public class Average { public static void main(String[] args) { Terminal window; // Used to store object representing the window int runningTotal, average; // used to store total window = new Terminal("Average"); runningTotal = window.readInt("Enter first number: "); runningTotal = runningTotal + window.readInt("Enter second number: "); runningTotal = runningTotal + window.readInt("Enter third number: "); runningTotal = runningTotal + window.readInt("Enter fourth number: "); runningTotal = runningTotal + window.readInt("Enter fifth number: "); average = runningTotal / 5; window.println("The average is: " + average); } integer value

15 © Vinny Cahill 15

16 © Vinny Cahill 16 Compilation l The text of any program needs to be translated into an executable form l The process of doing this translation is called compilation l The program that performs the process is called a compiler l Different programming languages use different compilers l Eclipse includes a Java compiler

17 © Vinny Cahill 17 Compilation cntd. HelloWorld.java Java program as text Java compiler HelloWorld.class Executable Java program

18 © Vinny Cahill 18 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } The program always starts executing the main method

19 © Vinny Cahill 19 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } On the screen Address Window In the computer object We execute a single statement at a time in sequence point of control

20 © Vinny Cahill 20 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } On the screen Address Window In the computer On the screen

21 © Vinny Cahill 21 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window In the computer On the screen

22 © Vinny Cahill 22 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window In the computerOn the screen

23 © Vinny Cahill 23 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window In the computerOn the screen

24 © Vinny Cahill 24 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window In the computerOn the screen

25 © Vinny Cahill 25 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } On the screen Address Window In the computer

26 © Vinny Cahill 26 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } On the screen Address Window On the screen println! - "Prof. Vinny Cahill"

27 © Vinny Cahill 27 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window On the screen println! - "Discipline of Computer Systems"

28 © Vinny Cahill 28 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window On the screen println! - "School of Computer Science and Statistics"

29 © Vinny Cahill 29 Flow of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window On the screen println! - Trinity College Dublin"

30 © Vinny Cahill 30 Transfer of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } On the screen Address Window In the computer

31 © Vinny Cahill 31 Transfer of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } On the screen Address Window On the screen println! - "Prof. Vinny Cahill" void println (String text) { // step 1 // step 2 // step 3 }

32 © Vinny Cahill 32 Transfer of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window println! - "Discipline of Computer Systems" void println (String text) { // step 1 // step 2 // step 3 } On the screen

33 © Vinny Cahill 33 Transfer of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window void println (String text) { // step 1 // step 2 // step 3 } On the screen println! - "School of Computer Science and Statistics"

34 © Vinny Cahill 34 Transfer of control public static void main(String[] args) { Terminal window; window = new Terminal("Address Window"); window.println("Prof. Vinny Cahill"); window.println("Discipline of Computer Systems"); window.println("School of Computer Science and Statistics"); window.println("Trinity College Dublin"); } Address Window void println (String text) { // step 1 // step 2 // step 3 } println! - Trinity College Dublin" On the screen

35 © Vinny Cahill 35

36 © Vinny Cahill 36 Transfer of control public static void main(String[] args) { Terminal window; Square shape; int area; window = new Terminal(Square); shape = new Square(10); area = shape.calculateArea(); window.println(Area is: + area); }

37 © Vinny Cahill 37 Transfer of control public static void main(String[] args) { Terminal window; Square shape; int area; window = new Terminal(Square); shape = new Square(10); area = shape.calculateArea(); window.println(Area is: + area); } Square On the screen

38 © Vinny Cahill 38 Transfer of control public static void main(String[] args) { Terminal window; Square shape; int area; window = new Terminal(Square); shape = new Square(10); area = shape.calculateArea(); window.println(Area is: + area); } Square On the screen 10

39 © Vinny Cahill 39 Transfer of control public static void main(String[] args) { Terminal window; Square shape; int area; window = new Terminal(Square); shape = new Square(10); area = shape.calculateArea(); window.println(Area is: + area); } 10 calculateArea! int calculateArea() { // step 1 // step 2 // step 3 } Square On the screen 100

40 © Vinny Cahill 40 Transfer of control public static void main(String[] args) { Terminal window; Square shape; int area; window = new Terminal(Square); shape = new Square(10); area = shape.calculateArea(); window.println(Area is: + area); } Square void println (String text) { // step 1 // step 2 // step 3 } println! - Area is: 100 On the screen 10

41 © Vinny Cahill 41 Transfer of control public static void main(String[] args) { Terminal window; Square shape; int area; window = new Terminal(Square); shape = new Square(10); area = shape.calculateArea(); window.println(Area is: + area); } Square On the screen 10

42 © Vinny Cahill 42 Type double Type double represents real numbers A value of type double occupies 8 bytes of memory A value of type double can store a number in the range ± E+308 with 15 significant decimal digits Values of type double can be followed by the letter D E8 -7.4E D E+7F l and usually have either a decimal point or an exponent

43 © Vinny Cahill 43 double operators There are four double operators Operator Meaning Examples Use + op1 + op2 add values of op1 and op2 newSalary = oldSalary + rise; distance = distance ; - op1 - op2 subtract op2 from op1 time = hours - 0.6; * op1 * op2 multiply op1 by op2 area = radius * PI; area = radius * 3.14D; / op1 / op2 divide op1 by op2 length = area / breath;

44 © Vinny Cahill 44 Real expressions The same rules apply to writing real expressions as to integer expressions The double operators are all binary operators that take two operands The double operators all return a single result of type double * and / have higher precedence than + and – l Operators of equal precedence are left associative l Can use brackets to change the order of evaluation

45 © Vinny Cahill 45 Other integer types l Java actually provides four primitive types that represent integers l They differ only in their size (i.e., the range of values that they can store) int 32 bits -2,147,483,648 to 2,147,483,647 short 16 bits -32,768 to 32,7687 long 64 bits -9,233,372,036,854,775,808 to..... byte 8 bits -128 to 127 Use int unless you have a really good reason not to!

46 © Vinny Cahill 46 Floating-point types l Java provides two primitive types that represent real numbers l Again they differ only in their size (i.e., the range of values that they can store and their precision) float 32 bits ± E+38 with 7 significant decimal digits In general, use double rather than float double 64 bits ± E+308 with 15 significant decimal digits Values of type float are followed by an F

47 © Vinny Cahill 47 Assignment compatibility The type of an expression must be the same as the type of the variable to which its value is being assigned You cant put a square peg in a round hole!


Download ppt "© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen."

Similar presentations


Ads by Google