1 / 47

Writing a Program in Java

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.

tao
Download Presentation

Writing a Program in Java

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. Writing a Program in Java

  2. The “Hello World” Program • Want to write a program to print a message on the screen.

  3. “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

  4. 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

  5. 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;

  6. 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

  7. 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 . . . . }

  8. 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!

  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"); window

  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 -123 23000 0 1000000 -7456 3990276

  11. 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

  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. 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. 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

  15. 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

  16. Compilation cntd. Java program as text Executable Java program Java compiler HelloWorld.java HelloWorld.class

  17. 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

  18. 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

  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"); } “Address Window” In the computer On the screen On the screen

  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"); } “Address Window” In the computer On the screen On the screen

  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 On the screen

  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 computer On the screen On the screen

  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 computer On the screen On the screen

  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 computer On the screen

  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"); } “Address Window” println! - "Prof. Vinny Cahill" On the screen On the screen

  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"); } “Address Window” println! - "Discipline of Computer Systems" On the screen On the screen

  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” println! - "School of Computer Science and Statistics" On the screen On the screen

  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” println! - “Trinity College Dublin" On the screen On the screen

  29. 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

  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"); } “Address Window” println! - "Prof. Vinny Cahill" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }

  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"); } “Address Window” println! - "Discipline of Computer Systems" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }

  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! - "School of Computer Science and Statistics" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }

  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” println! - “Trinity College Dublin" On the screen On the screen void println (String text) { // step 1 // step 2 // step 3 }

  34. 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); }

  35. 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”

  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); } On the screen “Square” 10

  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); } 10 calculateArea! 100 On the screen int calculateArea() { // step 1 // step 2 // step 3 } “Square”

  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” 10 println! - “Area is: 100” On the screen void println (String text) { // step 1 // step 2 // step 3 }

  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); } “Square” 10 On the screen

  40. 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

  41. 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 /

  42. 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

  43. 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!

  44. 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

  45. 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!”

More Related