17 Statement: assignment statement: assigning an expression value 18 Age next year 19 Type: String: conversion: from int 21 Age next year 22 The full

Size: px
Start display at page:

Download "17 Statement: assignment statement: assigning an expression value 18 Age next year 19 Type: String: conversion: from int 21 Age next year 22 The full"

Transcription

1 List of Slides 1 Title 2 Chapter 3: Types, variables and expressions 3 Chapter aims 4 Section 2: Example:Age next year 5 Aim 6 Design: hard coding 7 Age next year 8 Type 9 Type: int 10 Variable 11 Variable: int variable 12 Age next year 13 Statement: assignment statement 14 Statement: assignment statement: assigning a literal value 15 Age next year 16 Expression: arithmetic 0-0

2 17 Statement: assignment statement: assigning an expression value 18 Age next year 19 Type: String: conversion: from int 21 Age next year 22 The full AgeNextYear code 23 Trying it 24 Coursework: Hard coded YearsBeforeRetirement 25 Section 3: Example:Age next year a common misconception 26 Aim 27 Age next year a common misconception 28 Trying it 29 Changing my age 30 Section 4: Example:Age next year with a command line argument 31 Aim 32 Standard API: Integer: parseint() 33 Age next year with a command line argument 34 Trying it 35 Coursework: Command line YearsBeforeRetirement 0-1

3 36 Section 5: Example:Finding the volume of a fish tank 37 Aim 38 Variable: a value can be assigned when a variable is declared 39 Finding the volume of a fish tank 40 Code clarity: layout: splitting long lines 41 Finding the volume of a fish tank 42 The full FishTankVolume code 43 Trying it 44 Trying it 45 Trying it 46 Trying it 47 Trying it 48 Coursework: FieldPerimeter 49 Section 6: Example:Sum the first N numbers incorrectly 50 Aim 51 Sum the first N numbers incorrectly 52 Sum the first N numbers incorrectly 53 Expression: brackets and precedence 0-2

4 56 Sum the first N numbers incorrectly 57 The full SumFirstN code 58 Trying it 59 Trying it 60 Trying it 61 Coursework: FishTankMaterials 62 Section 7: Example:Disposable income 63 Aim 64 Disposable income 65 Expression: associativity 69 Disposable income 70 Disposable income 71 Type: String: literal: escape sequences 73 Disposable income 74 The full DisposableIncome code 75 Trying it 76 Coursework: ThreeWeights 77 Section 8: Example:Sum the first N numbers correctly 0-3

5 78 Aim 79 Expression: arithmetic: int division truncates result 80 Sum the first N numbers correctly 81 Expression: arithmetic: associativity and int division 82 Sum the first N numbers correctly 83 Trying it 84 Trying it 85 Coursework: RoundPennies 86 Section 9: Example:Temperature conversion 87 Aim 88 Type: double 89 Variable: double variable 90 Standard API: Double: parsedouble() 91 Temperature conversion 92 Expression: arithmetic: double division 93 Temperature conversion 94 Type: String: conversion: from double 95 Temperature conversion 0-4

6 96 The full CelsiusToFahrenheit code 97 Trying it 98 Coursework: FahrenheitToCelsius 99 Concepts covered in this chapter 0-5

7 Title Java Just in Time John Latham October 2, 2018 October 2, 2018 Java Just in Time - John Latham Page 1(0/0)

8 Chapter 3 Types, variables and expressions October 2, 2018 Java Just in Time - John Latham Page 2(0/0)

9 Chapter aims Introduce some more Java concepts: There are different kinds of values types. Values can be stored variables. How? assignment statements. We meet arithmetic expressions and arithmetic operators. With operator precedence and operator associativity. October 2, 2018 Java Just in Time - John Latham Page 3(0/0)

10 Section 2 Example: Age next year October 2, 2018 Java Just in Time - John Latham Page 4(0/0)

11 Aim AIM: To introduce the concepts of type, int, variable, expression and assignment statement. We also find out how to convert a number to a string, and discover what it means for data to be hard coded. October 2, 2018 Java Just in Time - John Latham Page 5(0/0)

12 Design: hard coding Input data might be command line arguments obtained via user interface maybe a GUI from files. Sometimes input data built into the program hard coded. e.g. haven t written code that obtains the data yet e.g. such data only rarely/never changes. October 2, 2018 Java Just in Time - John Latham Page 6(0/0)

13 Age next year 001: public class AgeNextYear 002: { 003: public static void main(string[] args) 004: { October 2, 2018 Java Just in Time - John Latham Page 7(0/0)

14 Type Different kinds of data numbers text data images etc.. The kind of a data item is its type. October 2, 2018 Java Just in Time - John Latham Page 8(0/0)

15 Type: int The type int is integers e.g etc.. October 2, 2018 Java Just in Time - John Latham Page 9(0/0)

16 Variable A variable entity that can hold data. Has name, value and type. Similar to variables in algebra not quite the same thing. Name: carefully chosen by programmer reflects meaning of thing it represents in relation to problem does not change during program run. Value: can be set and changed at run time variable. Java compiler maps variable names to computer memory locations. Type: what kind of data is allowed. October 2, 2018 Java Just in Time - John Latham Page 10(0/0)

17 Variable: int variable All variables declared in a variable declaration before use. Programmer states type and name, e.g.: int noofpeoplelivinginmystreet; noofpeoplelivinginmystreet is an int variable. Note semi-colon. At run time noofpeoplelivinginmystreet can hold an integer can be changed, but always an int. Name reflects intended meaning programmer writes code to ensure value reflects meaning. Convention: variable names start with a lower case letter first letter of subsequent words capitalized. October 2, 2018 Java Just in Time - John Latham Page 11(0/0)

18 Age next year 005: int myagenow; 006: int myagenextyear; October 2, 2018 Java Just in Time - John Latham Page 12(0/0)

19 Statement: assignment statement An assignment statement is a statement. Gives value to a variable or change existing value. New value and variable must have matching types. October 2, 2018 Java Just in Time - John Latham Page 13(0/0)

20 Statement: assignment statement: assigning a literal value An assignment statement can assign a literal value (constant) to a variable, e.g: noofpeoplelivinginmystreet = 47; Note use of single equal sign. 47 is an int so okay if noofpeoplelivinginmystreet is an int variable. October 2, 2018 Java Just in Time - John Latham Page 14(0/0)

21 Age next year 007: myagenow = 18; October 2, 2018 Java Just in Time - John Latham Page 15(0/0)

22 Expression: arithmetic Can have arithmetic expressions as in maths: literal values e.g. integer literals 1, 18 variables must be already declared operators, e.g. arithmetic operators binary infix operators: +, -, *, / unary prefix operators: +, - When evaluated each variable replaced with current value. E.g. if noofpeoplelivinginmystreet contains 47 then noofpeoplelivinginmystreet + 4 evaluates to 51. October 2, 2018 Java Just in Time - John Latham Page 16(0/0)

23 Statement: assignment statement: assigning an expression value More generally: assignment statement can have expression. E.g., assume then int noofpeopletoinvitetothestreetparty; noofpeopletoinvitetothestreetparty = noofpeoplelivinginmystreet + 4; when executed evaluates noofpeoplelivinginmystreet + 4 puts result in noofpeopletoinvitetothestreetparty. October 2, 2018 Java Just in Time - John Latham Page 17(0/0)

24 Age next year 008: myagenextyear = myagenow + 1; October 2, 2018 Java Just in Time - John Latham Page 18(0/0)

25 Type: String: conversion: from int The operator + used for both addition and concatenation an overloaded operator. If at least one operand is a text data string then concatenation, else addition. If only one is a string, other is converted to string before concatenation. Note difference between an integer and string of decimal digits. E.g. integer literal 123 is an int "123" is a text data string 3 separate characters. October 2, 2018 Java Just in Time - John Latham Page 19(0/0)

26 Type: String: conversion: from int E.g. assume noofpeopletoinvitetothestreetparty has value 51 System.out.println("Please invite " + noofpeopletoinvitetothestreetparty); produces: Please invite converted to "51" "Please invite " concatenated with "51" result passed to System.out.println(). For convenience a separate version of System.out.println() takes a single int, e.g. System.out.println(noOfPeopleToInviteToTheStreetParty); Same effect as: System.out.println("" + noofpeopletoinvitetothestreetparty); October 2, 2018 Java Just in Time - John Latham Page 20(0/0)

27 Age next year 009: System.out.println("My age now is " + myagenow); 010: System.out.println("My age next year will be " + myagenextyear); 011: } 012: } October 2, 2018 Java Just in Time - John Latham Page 21(0/0)

28 The full AgeNextYear code 001: public class AgeNextYear 002: { 003: public static void main(string[] args) 004: { 005: int myagenow; 006: int myagenextyear; 007: myagenow = 18; 008: myagenextyear = myagenow + 1; 009: System.out.println("My age now is " + myagenow); 010: System.out.println("My age next year will be " + myagenextyear); 011: } 012: } October 2, 2018 Java Just in Time - John Latham Page 22(0/0)

29 Trying it Console Input / Output $ javac AgeNextYear.java $ java AgeNextYear My age now is 18 My age next year will be 19 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 23(0/0)

30 Coursework: Hard coded YearsBeforeRetirement (Summary only) Write a program to determine how many years you have before you retire! October 2, 2018 Java Just in Time - John Latham Page 24(0/0)

31 Section 3 Example: Age next year a common misconception October 2, 2018 Java Just in Time - John Latham Page 25(0/0)

32 Aim AIM: To clarify the relationship between variables and assignment statements. October 2, 2018 Java Just in Time - John Latham Page 26(0/0)

33 Age next year a common misconception Common misconception: assignment statements are equations. Not helped by use of single equal sign! If they are, then order doesn t matter! 001: public class AgeNextYear 002: { 003: public static void main(string[] args) 004: { 005: int myagenow; 006: int myagenextyear; 007: myagenextyear = myagenow + 1; 008: myagenow = 18; 009: System.out.println("My age now is " + myagenow); 010: System.out.println("My age next year will be " + myagenextyear); 011: } 012: } October 2, 2018 Java Just in Time - John Latham Page 27(0/0)

34 Trying it Console Input / Output $ javac AgeNextYear.java AgeNextYear.java:7: variable myagenow might not have been initialized myagenextyear = myagenow + 1; ˆ 1 error $ _ Run Compiler checks variable has been given value before use. October 2, 2018 Java Just in Time - John Latham Page 28(0/0)

35 Changing my age Can change the value of a variable. 001: public class AgeNextYear 002: { 003: public static void main(string[] args) 004: { 005: int myagenow; 006: int myagenextyear; 007: myagenow = 18; 008: myagenextyear = myagenow + 1; 009: myagenow = 60; Coffee time: What would be the result? 010: System.out.println("My age now is " + myagenow); 011: System.out.println("My age next year will be " + myagenextyear); 012: } 013: } October 2, 2018 Java Just in Time - John Latham Page 29(0/0)

36 Section 4 Example: Age next year with a command line argument October 2, 2018 Java Just in Time - John Latham Page 30(0/0)

37 Aim AIM: To introduce the idea of converting a command line argumentintoan intandusingthevalueinaprogram. October 2, 2018 Java Just in Time - John Latham Page 31(0/0)

38 Standard API: Integer: parseint() Often want to turn a text data string representation of an integer into that number. E.g. turn "123" into 123. A simple way: Integer.parseInt("123"); Integer is a class in the API: has method called parseint. E.g. int firstargument; firstargument = Integer.parseInt(args[0]); takes first command line argument computes number it represents (if it does run time error otherwise) stores that in firstargument. October 2, 2018 Java Just in Time - John Latham Page 32(0/0)

39 Age next year with a command line argument 001: public class AgeNextYear 002: { 003: public static void main(string[] args) 004: { 005: int agenow; 006: int agenextyear; 007: 008: agenow = Integer.parseInt(args[0]); 009: agenextyear = agenow + 1; 010: 011: System.out.println("Your age now is " + agenow); 012: System.out.println("Your age next year will be " + agenextyear); 013: } 014: } October 2, 2018 Java Just in Time - John Latham Page 33(0/0)

40 Trying it Console Input / Output $ javac AgeNextYear.java $ java AgeNextYear 60 Your age now is 60 Your age next year will be 61 $ java AgeNextYear 18 Your age now is 18 Your age next year will be 19 $ java AgeNextYear John Exception in thread "main" java.lang.numberformatexception: For input string: "J ohn" at java.lang.numberformatexception.forinputstring(numberformatexception. java:48) at java.lang.integer.parseint(integer.java:449) at java.lang.integer.parseint(integer.java:499) at AgeNextYear.main(AgeNextYear.java:8) $ _ Run October 2, 2018 Java Just in Time - John Latham Page 34(0/0)

41 Coursework: Command line YearsBeforeRetirement (Summary only) Write a program to determine how many years the user has before he or she retires. October 2, 2018 Java Just in Time - John Latham Page 35(0/0)

42 Section 5 Example: Finding the volume of a fish tank October 2, 2018 Java Just in Time - John Latham Page 36(0/0)

43 Aim AIM: To reinforce the use of command line arguments and expressions, and introduce the idea of splitting up lines of code which are too long, whilst maintaining their readability. We also see thatavariable canbe given a value when it is declared. October 2, 2018 Java Just in Time - John Latham Page 37(0/0)

44 Variable: a value can be assigned when a variable is declared We can declare a variable and give it a value at the same time. E.g. int noofhousesinmystreet = 26; Coffee time: Could we have already used that idea in this Chapter? October 2, 2018 Java Just in Time - John Latham Page 38(0/0)

45 Finding the volume of a fish tank 001: public class FishTankVolume 002: { 003: public static void main(string[] args) 004: { 005: int width = Integer.parseInt(args[0]); 006: int depth = Integer.parseInt(args[1]); 007: int height = Integer.parseInt(args[2]); 008: int volume = width * depth * height; October 2, 2018 Java Just in Time - John Latham Page 39(0/0)

46 Code clarity: layout: splitting long lines Long source code lines are a bad idea: more horizontal eye movement to scan the code use horizontal scroll bar, or have wide/fullscreen window when printed will truncate or at least line wrap Keep source code lines shorter than 80 characters. Long statements split into separate lines. Carefully chosen places Human readers scan down the left hand side of the code. If line continues previous, make obvious at start. Use indentation. Split line before symbol not normally used to start a statement. Code read many more times than written... October 2, 2018 Java Just in Time - John Latham Page 40(0/0)

47 Finding the volume of a fish tank Split at carefully chosen places. Use of indentation. 009: System.out.println("The volume of a tank with dimensions " 010: + "(" + width + "," + depth + "," + height + ") " 011: + "is " + volume); 012: } 013: } October 2, 2018 Java Just in Time - John Latham Page 41(0/0)

48 The full FishTankVolume code 001: public class FishTankVolume 002: { 003: public static void main(string[] args) 004: { 005: int width = Integer.parseInt(args[0]); 006: int depth = Integer.parseInt(args[1]); 007: int height = Integer.parseInt(args[2]); 008: int volume = width * depth * height; 009: System.out.println("The volume of a tank with dimensions " 010: + "(" + width + "," + depth + "," + height + ") " 011: + "is " + volume); 012: } 013: } October 2, 2018 Java Just in Time - John Latham Page 42(0/0)

49 Trying it Tests that show command line argument order not important. Console Input / Output $ java FishTankVolume The volume of a tank with dimensions (10,20,30) is 6000 $ java FishTankVolume The volume of a tank with dimensions (10,30,20) is 6000 $ java FishTankVolume The volume of a tank with dimensions (20,10,30) is 6000 $ java FishTankVolume The volume of a tank with dimensions (20,30,10) is 6000 $ java FishTankVolume The volume of a tank with dimensions (30,10,20) is 6000 $ java FishTankVolume The volume of a tank with dimensions (30,20,10) is 6000 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 43(0/0)

50 Trying it Show effect of one dimension being zero. Console Input / Output $ java FishTankVolume The volume of a tank with dimensions (0,20,30) is 0 $ java FishTankVolume The volume of a tank with dimensions (10,0,30) is 0 $ java FishTankVolume The volume of a tank with dimensions (10,20,0) is 0 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 44(0/0)

51 Trying it Coffee time: How about this next test? Is the result correct? Is it meaningful? Console Input / Output $ java FishTankVolume The volume of a tank with dimensions (10,-20,-30) is 6000 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 45(0/0)

52 Trying it Coffee time: If we are taking program testing seriously, then the whole point of it is to try and find situations that break the program, rather than prove that it works. In what sense are the next two tests successful? October 2, 2018 Java Just in Time - John Latham Page 46(0/0)

53 Trying it Console Input / Output $ java FishTankVolume Exception in thread "main" java.lang.numberformatexception: For input string: "1 0.75" at java.lang.numberformatexception.forinputstring(numberformatexception. java:48) at java.lang.integer.parseint(integer.java:458) at java.lang.integer.parseint(integer.java:499) at FishTankVolume.main(FishTankVolume.java:5) $ java FishTankVolume Exception in thread "main" java.lang.numberformatexception: For input string: "1 0.0" at java.lang.numberformatexception.forinputstring(numberformatexception. java:48) at java.lang.integer.parseint(integer.java:458) at java.lang.integer.parseint(integer.java:499) at FishTankVolume.main(FishTankVolume.java:5) $ _ Run October 2, 2018 Java Just in Time - John Latham Page 47(0/0)

54 Coursework: FieldPerimeter (Summary only) Write a program to determine how much fence is needed to surround a rectangular field. October 2, 2018 Java Just in Time - John Latham Page 48(0/0)

55 Section 6 Example: Sum the first N numbers incorrectly October 2, 2018 Java Just in Time - John Latham Page 49(0/0)

56 Aim AIM: To introduce the principle of operator precedence, and have a program containing a bug. October 2, 2018 Java Just in Time - John Latham Page 50(0/0)

57 Sum the first N numbers incorrectly 001: public class SumFirstN 002: { 003: public static void main(string[] args) 004: { 005: int n = Integer.parseInt(args[0]); October 2, 2018 Java Just in Time - John Latham Page 51(0/0)

58 Sum the first N numbers incorrectly Formula: find average of numbers 1 to n multiply by n i.e.: 1+n 2 n October 2, 2018 Java Just in Time - John Latham Page 52(0/0)

59 Expression: brackets and precedence Java expressions can have round brackets. Define structure. E.g. (2 + 4) * (4 * 8) different structures, different values: 48 and 34. October 2, 2018 Java Just in Time - John Latham Page 53(0/0)

60 Expression: brackets and precedence Show structure as expression trees. (2 + 4) * (4 * 8) * / \ + 8 / \ / \ 2 * / \ 4 8 October 2, 2018 Java Just in Time - John Latham Page 54(0/0)

61 Expression: brackets and precedence No brackets? * 8 Rules to fill in missing brackets. 4 above is being pulled by + and *. Which one wins? Varying levels of operator precedence * and / have higher precedence than + and * 8 evaluates to 34. October 2, 2018 Java Just in Time - John Latham Page 55(0/0)

62 Sum the first N numbers incorrectly If operators evaluated left to right would write: 1 + n / 2 * n But division and multiplication higher precedence than addition. 006: int sumoffirstn = (1 + n) / 2 * n; 007: System.out.println("The sum of the first " + n + " numbers is " 008: + sumoffirstn); 009: } 010: } Coffee time: When computing the value of sumoffirstn, do you think the division is done before the multiplication, or vice versa? Does it matter? October 2, 2018 Java Just in Time - John Latham Page 56(0/0)

63 The full SumFirstN code 001: public class SumFirstN 002: { 003: public static void main(string[] args) 004: { 005: int n = Integer.parseInt(args[0]); 006: int sumoffirstn = (1 + n) / 2 * n; 007: System.out.println("The sum of the first " + n + " numbers is " 008: + sumoffirstn); 009: } 010: } October 2, 2018 Java Just in Time - John Latham Page 57(0/0)

64 Trying it Console Input / Output $ java SumFirstN 1 The sum of the first 1 numbers is 1 $ java SumFirstN 2 The sum of the first 2 numbers is 2 $ java SumFirstN 3 The sum of the first 3 numbers is 6 $ java SumFirstN 4 The sum of the first 4 numbers is 8 $ java SumFirstN 5 The sum of the first 5 numbers is 15 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 58(0/0)

65 Trying it Console Input / Output $ java SumFirstN 10 The sum of the first 10 numbers is 50 $ java SumFirstN 11 The sum of the first 11 numbers is 66 $ java SumFirstN 50 The sum of the first 50 numbers is 1250 $ java SumFirstN 51 The sum of the first 51 numbers is 1326 $ java SumFirstN 100 The sum of the first 100 numbers is 5000 $ java SumFirstN 101 The sum of the first 101 numbers is 5151 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 59(0/0)

66 Trying it Some of these results are wrong! Coffee time: Figure out which ones are right and which are wrong, and see if you can spot a pattern, leading you to suggest what the problem might be. We know the formula is right, so you can still use it to work out what the answers should have been. The error lies somewhere in our implementation of the formula maybe something there doesn t behave as you might expect it to? October 2, 2018 Java Just in Time - John Latham Page 60(0/0)

67 Coursework: FishTankMaterials (Summary only) Take a program with bugs in it, and fix them. October 2, 2018 Java Just in Time - John Latham Page 61(0/0)

68 Section 7 Example: Disposable income October 2, 2018 Java Just in Time - John Latham Page 62(0/0)

69 Aim AIM: To introduce operator associativity. We also take a look at the string literal escape sequences. October 2, 2018 Java Just in Time - John Latham Page 63(0/0)

70 Disposable income 001: public class DisposableIncome 002: { 003: public static void main(string[] args) 004: { 005: int salary = Integer.parseInt(args[0]); 006: int mortgage = Integer.parseInt(args[1]); 007: int bills = Integer.parseInt(args[2]); October 2, 2018 Java Just in Time - John Latham Page 64(0/0)

71 Expression: associativity Some expressions cannot be disambiguated just by operator precedence. E.g The 7 is being fought over by two operators with same precedence. October 2, 2018 Java Just in Time - John Latham Page 65(0/0)

72 Expression: associativity Two possible structures: 10 OP1 (7 OP2 3) (10 OP1 7) OP2 3 OP1 / \ 10 OP2 / \ 7 3 OP2 / \ OP1 3 / \ 10 7 October 2, 2018 Java Just in Time - John Latham Page 66(0/0)

73 Expression: associativity Does it make a difference? Expression Value (10 + 7) (7 + 3) 20 (10 + 7) (7-3) 14 (10-7) (7 + 3) 0 (10-7) (7-3) 6 Yes when first operator is -. October 2, 2018 Java Just in Time - John Latham Page 67(0/0)

74 Expression: associativity Java operators also have operator associativity. +, -, * and / have left associativity when two equal precedence operators fight over an operand? The left one wins. Expression Implicit brackets Value (10 + 7) (10 + 7) (10-7) (10-7) * and / also have equal precedence (higher than + and -). October 2, 2018 Java Just in Time - John Latham Page 68(0/0)

75 Disposable income Coffee time: Figure out why "I earn " evaluates to "I earn ", whereas "I am " + ( ) becomes "I am 21". October 2, 2018 Java Just in Time - John Latham Page 69(0/0)

76 Disposable income 008: int disposableincome = salary - (mortgage + bills); Coffee time: Alternatively, we could have written our expression as salary - mortgage - bills. Convince yourself that this would produce the same result, whereas the expression salary - mortgage + bills would be wrong. October 2, 2018 Java Just in Time - John Latham Page 70(0/0)

77 Type: String: literal: escape sequences Use escape sequence \n to have new line character in string literal. E.g. System.out.println("This text\nspans three\nlines."); produces: This text spans three lines. Note: System.out.println() always produces line separator carriage return character followed by new line character on Windows. October 2, 2018 Java Just in Time - John Latham Page 71(0/0)

78 Type: String: literal: escape sequences Sequence Name Effect \b Backspace Moves the cursor back one place, so the next character will over-print the previous. \t Tab (horizontal tab) Moves the cursor to the next tab stop. \n New line (line feed) Moves the cursor to the next line. \f Form feed Moves to a new page on many (text) printers. \r Carriage return Moves the cursor to the start of the current line, so characters will over-print those already printed. \" Double quote Without the backslash escape, this would mark the end of the string literal. \ Single quote This is just for consistency we don t need to escape a single quote in a string literal. \\ Backslash Well, sometimes you want the backslash character itself. October 2, 2018 Java Just in Time - John Latham Page 72(0/0)

79 Disposable income 009: System.out.println("Your salary:\t" + salary 010: + "\nyour mortgage:\t" + mortgage 011: + "\nyour bills:\t" + bills 012: + "\ndisposable:\t" + disposableincome); 013: } 014: } October 2, 2018 Java Just in Time - John Latham Page 73(0/0)

80 The full DisposableIncome code 001: public class DisposableIncome 002: { 003: public static void main(string[] args) 004: { 005: int salary = Integer.parseInt(args[0]); 006: int mortgage = Integer.parseInt(args[1]); 007: int bills = Integer.parseInt(args[2]); 008: int disposableincome = salary - (mortgage + bills); 009: System.out.println("Your salary:\t" + salary 010: + "\nyour mortgage:\t" + mortgage 011: + "\nyour bills:\t" + bills 012: + "\ndisposable:\t" + disposableincome); 013: } 014: } October 2, 2018 Java Just in Time - John Latham Page 74(0/0)

81 Trying it Console Input / Output $ java DisposableIncome Your salary: Your mortgage: Your bills: 4665 Disposable: 9374 $ java DisposableIncome Your salary: Your mortgage: Your bills: 3665 Disposable: 2638 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 75(0/0)

82 Coursework: ThreeWeights (Summary only) Write a program to show what weights can be weighed using a balance scale and three given weights. October 2, 2018 Java Just in Time - John Latham Page 76(0/0)

83 Section 8 Example: Sum the first N numbers correctly October 2, 2018 Java Just in Time - John Latham Page 77(0/0)

84 Aim AIM: To introduce the fact that integer division produces a truncated result. We then look at the interaction between that and operator associativity. October 2, 2018 Java Just in Time - John Latham Page 78(0/0)

85 Expression: arithmetic: int division truncates result The division operator uses integer division when given two integers. throws away any remainder. E.g. 8 / 2 is 4 9 / 2 is 4 Always rounds towards zero: 15 / 4 is 3, not 3.75 nor 4. October 2, 2018 Java Just in Time - John Latham Page 79(0/0)

86 Sum the first N numbers correctly Previous implementation: (1 + n) / 2 * n only works if n is odd. Ensure multiplication done before division must work: sum of the first n whole numbers is a whole number! October 2, 2018 Java Just in Time - John Latham Page 80(0/0)

87 Expression: arithmetic: associativity and int division * and / have equal operator precedence and left associativity. But / with integers truncates. E.g. Expression Implicit brackets Value 9 * 4 / 2 (9 * 4) / / 2 * 4 (9 / 2) * 4 16 October 2, 2018 Java Just in Time - John Latham Page 81(0/0)

88 Sum the first N numbers correctly Simplest bug fix: swap order of divide and multiply. Coffee time: Convince yourself that this will always avoid the problem for this program. 001: public class SumFirstN 002: { 003: public static void main(string[] args) 004: { 005: int n = Integer.parseInt(args[0]); 006: int sumoffirstn = (1 + n) * n / 2; 007: System.out.println("The sum of the first " + n + " numbers is " 008: + sumoffirstn); 009: } 010: } October 2, 2018 Java Just in Time - John Latham Page 82(0/0)

89 Trying it Console Input / Output $ java SumFirstN 1 The sum of the first 1 numbers is 1 $ java SumFirstN 2 The sum of the first 2 numbers is 3 $ java SumFirstN 3 The sum of the first 3 numbers is 6 $ java SumFirstN 4 The sum of the first 4 numbers is 10 $ java SumFirstN 5 The sum of the first 5 numbers is 15 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 83(0/0)

90 Trying it Console Input / Output $ java SumFirstN 10 The sum of the first 10 numbers is 55 $ java SumFirstN 11 The sum of the first 11 numbers is 66 $ java SumFirstN 50 The sum of the first 50 numbers is 1275 $ java SumFirstN 51 The sum of the first 51 numbers is 1326 $ java SumFirstN 100 The sum of the first 100 numbers is 5050 $ java SumFirstN 101 The sum of the first 101 numbers is 5151 $ _ Run October 2, 2018 Java Just in Time - John Latham Page 84(0/0)

91 Coursework: RoundPennies (Summary only) Write a program to help a child determine whether she has enough pennies to go shopping! October 2, 2018 Java Just in Time - John Latham Page 85(0/0)

92 Section 9 Example: Temperature conversion October 2, 2018 Java Just in Time - John Latham Page 86(0/0)

93 Aim AIM: To introduce the double type and some associated concepts, including converting to and from strings, and double division. October 2, 2018 Java Just in Time - John Latham Page 87(0/0)

94 Type: double The type double is reals. E.g. 0.0, , Uses double precision storage technique. real numbers only approximated: stored in finite memory space. double precision uses twice as much memory per number than older single precision technique. much more precise than single precision. October 2, 2018 Java Just in Time - John Latham Page 88(0/0)

95 Variable: double variable Can declare double variables variables of type double. E.g. double meanageofpeoplelivinginmyhouse; Value of meanageofpeoplelivinginmyhouse can change but must always be a double. E.g October 2, 2018 Java Just in Time - John Latham Page 89(0/0)

96 Standard API: Double: parsedouble() Often want to turn a text data string representation of a real into that number. E.g. turn " " into A simple way: Double.parseDouble(" "); Double is a class in the API: has method called parsedouble. E.g. double firstargument = Double.parseDouble(args[0]); takes first command line argument computes number it represents (if it does run time error otherwise) stores that in firstargument. October 2, 2018 Java Just in Time - John Latham Page 90(0/0)

97 Temperature conversion 001: public class CelsiusToFahrenheit 002: { 003: public static void main(string[] args) 004: { 005: double celsiusvalue = Double.parseDouble(args[0]); October 2, 2018 Java Just in Time - John Latham Page 91(0/0)

98 Expression: arithmetic: double division / uses double division with double result if at least one operand is double. E.g. Expression Result Type of Result 8 / 2 4 int 8 / double 9 / 2 4 int 9 / double 9.0 / double 9.0 / double October 2, 2018 Java Just in Time - John Latham Page 92(0/0)

99 Temperature conversion Celsius / Fahrenheit relationship: F = 9 5C : double fahrenheitvalue = celsiusvalue * 9 / ; October 2, 2018 Java Just in Time - John Latham Page 93(0/0)

100 Type: String: conversion: from double Java concatenation operator also converts double to a string. E.g. "" has the value "123.4". October 2, 2018 Java Just in Time - John Latham Page 94(0/0)

101 Temperature conversion 007: System.out.println("Temperature " + celsiusvalue + " Celsius" 008: + " in Fahrenheit is " + fahrenheitvalue + "."); 009: } 010: } Coffee time: Whatdoyouthinkwouldhappenifwedeclaredthevariable fahrenheitvalue as an int instead of a double? October 2, 2018 Java Just in Time - John Latham Page 95(0/0)

102 The full CelsiusToFahrenheit code 001: public class CelsiusToFahrenheit 002: { 003: public static void main(string[] args) 004: { 005: double celsiusvalue = Double.parseDouble(args[0]); 006: double fahrenheitvalue = celsiusvalue * 9 / ; 007: System.out.println("Temperature " + celsiusvalue + " Celsius" 008: + " in Fahrenheit is " + fahrenheitvalue + "."); 009: } 010: } October 2, 2018 Java Just in Time - John Latham Page 96(0/0)

103 Trying it Console Input / Output $ java CelsiusToFahrenheit 0 Temperature 0.0 Celsius in Fahrenheit is $ java CelsiusToFahrenheit 37.0 Temperature 37.0 Celsius in Fahrenheit is $ java CelsiusToFahrenheit 100 Temperature Celsius in Fahrenheit is $ java CelsiusToFahrenheit Temperature Celsius in Fahrenheit is 0.0. $ java CelsiusToFahrenheit Temperature Celsius in Fahrenheit is E-14. $ java CelsiusToFahrenheit Freezing Exception in thread "main" java.lang.numberformatexception: For input string: "F reezing" at sun.misc.floatingdecimal.readjavaformatstring(floatingdecimal.java:12 24) at java.lang.double.parsedouble(double.java:510) at CelsiusToFahrenheit.main(CelsiusToFahrenheit.java:5) $ _ Run October 2, 2018 Java Just in Time - John Latham Page 97(0/0)

104 Coursework: FahrenheitToCelsius (Summary only) Write a program to convert a temperature from Fahrenheit to Celsius. October 2, 2018 Java Just in Time - John Latham Page 98(0/0)

105 Concepts covered in this chapter Each book chapter ends with a list of concepts covered in it. Each concept has with it a self-test question, and a page reference to where it was covered. Please use these to check your understanding before we start the next chapter. October 2, 2018 Java Just in Time - John Latham Page 99(0/0)

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w

17 Hello world 18 Type: String: literal 19 Standard API: System: out.println() 20 Hello world 21 Statement 22 Statement: simple statements are ended w List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs are divided into classes 7 Class: public class 8 Class:

More information

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar

List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs ar List of Slides 1 Title 2 Chapter 2: Sequential execution and program errors 3 Chapter aims 4 Section 2: Example:Hello world 5 Aim 6 Class: programs are divided into classes 7 Class: public class 8 Class:

More information

Java Just in Time: task questions

Java Just in Time: task questions Java Just in Time: task questions John Latham October 24, 2017 Contents 1 Chapter 1 Introduction 4 1.6 Section / task 1.6 Our first Java program..................... 4 1.7 Section / task 1.7 Our second

More information

26 Age history with a separate method 27 Method: void methods 28 Age history with a separate method 29 Age history with a separate method 33 Trying it

26 Age history with a separate method 27 Method: void methods 28 Age history with a separate method 29 Age history with a separate method 33 Trying it List of Slides 1 Title 2 Chapter 8: Separate methods and logical operators 3 Chapter aims 4 Section 2: Example:Age history with two people 5 Aim 6 Age history with two people 11 Trying it 12 Coursework:

More information

20 Minimum bit width 21 Minimum bit width 22 Design: pseudo code 24 Minimum bit width 25 Minimum bit width 26 Standard API: Math: pow() 27 Minimum bit

20 Minimum bit width 21 Minimum bit width 22 Design: pseudo code 24 Minimum bit width 25 Minimum bit width 26 Standard API: Math: pow() 27 Minimum bit List of Slides 1 Title 2 Chapter 5: Repeated execution 3 Chapter aims 4 Section 2: Example:Minimum tank size 5 Aim 6 Minimum tank size 7 Execution: repeated execution 8 Statement: assignment statement:

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

40 Absolute difference 42 Trying it 43 Trying it 44 Concepts covered in this chapter 0-2

40 Absolute difference 42 Trying it 43 Trying it 44 Concepts covered in this chapter 0-2 List of Slides 1 Title 2 Chapter 4: Conditional execution 3 Chapter aims 4 Section 2: Example:Oldest spouse 1 5 Aim 6 Execution: conditional execution 7 Expression: boolean 8 Expression: boolean: relational

More information

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 COMP-202: Foundations of Programming Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015 Announcements Midterm Exams on 4 th of June (12:35 14:35) Room allocation will be announced soon

More information

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0)

Title. Java Just in Time. John Latham. October 25, October 25, 2017 Java Just in Time - John Latham Page 1(0/0) List of Slides 1 Title 2 Chapter 6: Control statements nested in loops 3 Chapter aims 4 Section 2: Example:Film certificate age checking the whole queue 5 Aim 6 Statement: statements can be nested within

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

Title. Java Just in Time. John Latham. November 5, November 5, 2018 Java Just in Time - John Latham Page 1(0/0)

Title. Java Just in Time. John Latham. November 5, November 5, 2018 Java Just in Time - John Latham Page 1(0/0) List of Slides 1 Title 2 Chapter 9: Consolidation of concepts so far 3 Chapter aims 4 Section 2: Java concepts 5 Aim 6 Java concepts 7 Type: long 8 Type: short 9 Type: byte 10 Type: char 11 Type: char:

More information

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2 CONTENTS: Compilation Data and Expressions COMP 202 More on Chapter 2 Programming Language Levels There are many programming language levels: machine language assembly language high-level language Java,

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

4 WORKING WITH DATA TYPES AND OPERATIONS

4 WORKING WITH DATA TYPES AND OPERATIONS WORKING WITH NUMERIC VALUES 27 4 WORKING WITH DATA TYPES AND OPERATIONS WORKING WITH NUMERIC VALUES This application will declare and display numeric values. To declare and display an integer value in

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 02 Data Types and Statements MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Constants Data Types

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */ Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,

More information

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasketEnhanced { public static void main(string[]

More information

Introduction to C# Applications

Introduction to C# Applications 1 2 3 Introduction to C# Applications OBJECTIVES To write simple C# applications To write statements that input and output data to the screen. To declare and use data of various types. To write decision-making

More information

MODULE 02: BASIC COMPUTATION IN JAVA

MODULE 02: BASIC COMPUTATION IN JAVA MODULE 02: BASIC COMPUTATION IN JAVA Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

Chapter 2 ELEMENTARY PROGRAMMING

Chapter 2 ELEMENTARY PROGRAMMING Chapter 2 ELEMENTARY PROGRAMMING Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk ١ Objectives To write Java programs to perform simple

More information

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators 1 Professor: Sana Odeh odeh@courant.nyu.edu Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators Review What s wrong with this line of code? print( He said Hello ) What s wrong with

More information

Basics of Java Programming

Basics of Java Programming Basics of Java Programming Lecture 2 COP 3252 Summer 2017 May 16, 2017 Components of a Java Program statements - A statement is some action or sequence of actions, given as a command in code. A statement

More information

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

More information

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

DATA TYPES AND EXPRESSIONS

DATA TYPES AND EXPRESSIONS DATA TYPES AND EXPRESSIONS Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment Mathematical

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review

More information

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance. 2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

More information

Programming Practice (vs Principles)

Programming Practice (vs Principles) Programming Practice (vs Principles) Programming in any language involves syntax (with rules, parentheses, etc) as well as format (layout, spacing, style, etc). Syntax is usually very strict, and any small

More information

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args) Built-in data types logical AND logical OR logical NOT &&! public static void main(string [] args) Fundamentals of Computer Science Keith Vertanen Copyright 2013 Variables Variables and data types Stores

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

Topic 4 Expressions and variables

Topic 4 Expressions and variables Topic 4 Expressions and variables "Once a person has understood the way variables are used in programming, he has understood the quintessence of programming." -Professor Edsger W. Dijkstra Based on slides

More information

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science Built-in data types logical AND logical OR logical NOT &&! public static void main(string [] args) Fundamentals of Computer Science Variables Overview Allows us to store and compute on data For now we'll

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

Variables and data types

Variables and data types Survivor: CSCI 135 Variables Variables and data types Stores information your program needs Each has a unique name Each has a specific type Java built-in type what it stores example values operations String

More information

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Building Java Programs. Chapter 2: Primitive Data and Definite Loops Building Java Programs Chapter 2: Primitive Data and Definite Loops Copyright 2008 2006 by Pearson Education 1 Lecture outline data concepts Primitive types: int, double, char (for now) Expressions: operators,

More information

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi... Contents 1 Introduction 3 1.1 Java, the beginning.......................... 3 1.2 Java Virtual Machine........................ 4 1.3 A First Program........................... 4 1.4 BlueJ.................................

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Expressions and Casting

Expressions and Casting Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0

More information

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

AP Computer Science Unit 1. Programs

AP Computer Science Unit 1. Programs AP Computer Science Unit 1. Programs Open DrJava. Under the File menu click on New Java Class and the window to the right should appear. Fill in the information as shown and click OK. This code is generated

More information

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 CSE 1001 Fundamentals of Software Development 1 Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018 Identifiers, Variables and Data Types Reserved Words Identifiers in C Variables and Values

More information

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value Lecture Notes 1. Comments a. /* */ b. // 2. Program Structures a. public class ComputeArea { public static void main(string[ ] args) { // input radius // compute area algorithm // output area Actions to

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M tutor Isam M. Al Jawarneh, PhD student isam.aljawarneh3@unibo.it Mobile Middleware

More information

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution Chapter 2: Elementary Programming CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Motivations In the preceding chapter, you learned how to

More information

CS11 Java. Fall Lecture 1

CS11 Java. Fall Lecture 1 CS11 Java Fall 2006-2007 Lecture 1 Welcome! 8 Lectures Slides posted on CS11 website http://www.cs.caltech.edu/courses/cs11 7-8 Lab Assignments Made available on Mondays Due one week later Monday, 12 noon

More information

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. Chapter 2 Elementary Programming 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical problems

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

69 Section 7: Example:Single times table with exception catching 70 Aim 74 Single times table with exception catching

69 Section 7: Example:Single times table with exception catching 70 Aim 74 Single times table with exception catching List of Slides 1 Title 2 Chapter 15: Exceptions 3 Chapter aims 4 Section 2: Example:Age next year revisited 5 Aim 6 Age next year revisited 7 Age next year revisited 8 Exception 9 Trying it 10 Trying it

More information

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1 CPE 101 mod/reusing slides from a UW course Overview (4) Lecture 4: Arithmetic Expressions Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity

More information

CHAPTER 2 Java Fundamentals

CHAPTER 2 Java Fundamentals CHAPTER 2 Java Fundamentals Copyright 2016 Pearson Education, Inc., Hoboken NJ Chapter Topics Chapter 2 discusses the following main topics: The Parts of a Java Program The print and println Methods, and

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions: Boolean Expressions Now we have the ability to read in some information, calculate some formulas and display the information to the user in a nice format. However, the real power of computer programs lies

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

Getting started with Java

Getting started with Java Getting started with Java Magic Lines public class MagicLines { public static void main(string[] args) { } } Comments Comments are lines in your code that get ignored during execution. Good for leaving

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations. Objectives Chapter 2 Primitive Data Types and Operations Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs,

More information

Chapter 2 Author Notes

Chapter 2 Author Notes Chapter 2 Author Notes Good Programming Practice 2.1 Every program should begin with a comment that explains the purpose of the program, the author and the date and time the program was last modified.

More information

Basic Programming Elements

Basic Programming Elements Chapter 2 Basic Programming Elements Lecture slides for: Java Actually: A Comprehensive Primer in Programming Khalid Azim Mughal, Torill Hamre, Rolf W. Rasmussen Cengage Learning, 2008. ISBN: 978-1-844480-933-2

More information

Basic Programming Language Syntax

Basic Programming Language Syntax Java Created in 1990 by Sun Microsystems. Free compiler from Sun, commercial from many vendors. We use free (Sun) Java on UNIX. Compiling and Interpreting...are processes of translating a high-level programming

More information

Advanced Computer Programming

Advanced Computer Programming Programming in the Small I: Names and Things (Part II) 188230 Advanced Computer Programming Asst. Prof. Dr. Kanda Runapongsa Saikaew (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University

More information

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013 Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

COMP-202: Foundations of Programming

COMP-202: Foundations of Programming COMP-202: Foundations of Programming Lecture 3: Basic data types Jackie Cheung, Winter 2016 Review: Hello World public class HelloWorld { } public static void main(string[] args) { } System.out.println("Hello,

More information

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language Chapter 1 Getting Started Introduction To Java Most people are familiar with Java as a language for Internet applications We will study Java as a general purpose programming language The syntax of expressions

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

JAVA Programming Concepts

JAVA Programming Concepts JAVA Programming Concepts M. G. Abbas Malik Assistant Professor Faculty of Computing and Information Technology University of Jeddah, Jeddah, KSA mgmalik@uj.edu.sa Programming is the art of Problem Solving

More information

Full file at C How to Program, 6/e Multiple Choice Test Bank

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 2 Data types type: A category or set of data

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford

SSEA Computer Science: Track A. Dr. Cynthia Lee Lecturer in Computer Science Stanford SSEA Computer Science: Track A Dr. Cynthia Lee Lecturer in Computer Science Stanford Topics for today Introduce Java programming language Assignment and type casting Expressions Operator precedence Code

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java Organisational issues Inf1-OOP Course Overview Perdita Stevens, adapting earlier version by Ewan Klein School of Informatics January 11, 2014 Why Java? Hello, World! in Java Built-in Types Integers Floating-Point

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 Copyright 2009 by Pearson Education Data and expressions reading: 2.1 self-check: 1-4 videos: Ch. 2 #1 Copyright

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

Chapter 2: Java Fundamentals

Chapter 2: Java Fundamentals Chapter 2: Java Fundamentals Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 2 discusses the following main topics: The Parts of a Java

More information

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals Chapter 2: Java Fundamentals Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 2 discusses the following main topics: The Parts of a Java

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

More information

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon

Who and what can help? Inf1-OP. Lecturer: Timothy Hospedales TA: Natalia Zon Who and what can help? Inf1-OP Lecturer: Timothy Hospedales TA: Natalia Zon Course Overview Web: http://www.inf.ed.ac.uk/teaching/ courses/inf1/op/ Timothy Hospedales, adapting earlier version by Perdita

More information