470 likes | 571 Views
Writing a Program in Java. The “Hello World” Program. Want to write a program to print a message on the screen. “Hello World” – Objects and Classes. An instance of class String. An instance of class Terminal. Another instance of class String. Class String is built into Java.
E N D
The “Hello World” Program • Want to write a program to print a message on the screen.
“Hello World” – Objects and Classes An instance of class String An instance of class Terminal Another instance of class String Class String is built into Java Class Terminal is part of a collection of pre-existing classescalled tcdIO
Functions and Parameters • Given f(x) = x2 + 4x + 13 • What is f(10)? • What is f(20)? • What is f(30)? x is the “formal parameter” of function f 10 is an “actual parameter” of function f
Variables 1 A variable is a container for information • In our program Terminal window; // Used to store object representing the window • “window” is the name of a container used to store a Terminal object • Alternatively: Terminal box;
Variables 2 Every variable has a name, a type, and a single (current) value type is Terminal window name value Think of a variable as a container for a value of the specified type
Variables 3 window is a local variablebecause it is defined within a method public static void main (String args[]) { Terminal window; // store object representing terminal . . . . }
Variables 4 The value stored in a variable can be changed using assignment window Assignment operator window = new Terminal("Hello Window"); window that why it’s called a variable!
Variables 5 The value stored in a variable can be changed using assignment as often as we want window window = new Terminal("Another Window"); window
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 -123 23000 0 1000000 -7456 3990276
Integer operators 1 There are five integer operators Examples Operator Use Meaning newSalary = oldSalary + rise; count = count +1; add values of op1 and op2 op1 + op2 + newSalary = oldSalary - 100; subtract op2 from op1 op1 - op2 - area = length * breath; minutes = hours * 60; multiply op1 by op2 op1 * op2 * length = area / breath; divide op1 by op2 op1 / op2 / pounds = pence / 100 pence = pence % 100; remainder on dividing op1 by op2 % op1 % op2
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;
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
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 integer value integer value
Compilation • The text of any program needs to be translated into an executable form • The process of doing this translation is called “compilation” • The program that performs the process is called a “compiler” • Different programming languages use different compilers • Eclipse includes a Java compiler
Compilation cntd. Java program as text Executable Java program Java compiler HelloWorld.java HelloWorld.class
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
Flow of control point 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"); } We execute a single statement at a time in sequence “Address Window” In the computer On the screen object
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 On the screen
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 On the screen
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 On the screen
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 On the screen
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 On the screen
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
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” println! - "Prof. Vinny Cahill" On the screen On the screen
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” println! - "Discipline of Computer Systems" On the screen On the screen
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” println! - "School of Computer Science and Statistics" On the screen On the screen
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” println! - “Trinity College Dublin" On the screen On the screen
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” In the computer On the screen
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! - "Prof. Vinny Cahill" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }
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" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }
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! - "School of Computer Science and Statistics" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }
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! - “Trinity College Dublin" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }
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); }
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); } On the screen “Square”
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); } On the screen “Square” 10
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! 100 On the screen int calculateArea() { // step 1 // step 2 // step 3 } “Square”
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” 10 println! - “Area is: 100” On the screen void println (String text) { // step 1 // step 2 // step 3 }
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” 10 On the screen
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 ± 1.79769313486231570E+308 with 15 significant decimal digits • Values of type double can be followed by the letter ‘D’ 230.6 0.0 -1.0E8 -7.4E-5 -123D -399.02E+7F • and usually have either a decimal point or an exponent
double operators There are fourdouble operators Examples Operator Use Meaning newSalary = oldSalary + rise; distance = distance + 10.2; add values of op1 and op2 op1 + op2 + time = hours - 0.6; subtract op2 from op1 op1 - op2 - area = radius * PI; area = radius * 3.14D; multiply op1 by op2 op1 * op2 * length = area / breath; divide op1 by op2 op1 / op2 /
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 – • Operators of equal precedence are left associative • Can use brackets to change the order of evaluation
Other integer types • Java actually provides four primitive types that represent integers • 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 byte 8 bits -128 to 127 short 16 bits -32,768 to 32,7687 long 64 bits -9,233,372,036,854,775,808 to ..... Use int unless you have a really good reason not to!
Floating-point types • Java provides two primitive types that represent real numbers • Again they differ only in their size • (i.e., the range of values that they can store and their precision) float 32 bits ± 3.40282347E+38 with 7 significant decimal digits double 64 bits ± 1.79769313486231570E+308 with 15 significant decimal digits • Values of type float are followed by an ‘F’ • In general, use double rather than float
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 can’t put a square peg in a round hole!”