JAVA Programming Concepts

Size: px
Start display at page:

Download "JAVA Programming Concepts"

Transcription

1 JAVA Programming Concepts M. G. Abbas Malik Assistant Professor Faculty of Computing and Information Technology University of Jeddah, Jeddah, KSA

2 Programming is the art of Problem Solving M. G. Abbas Malik, University of Jeddah 2

3 Starting from this chapter, you will learn how to solve practical problems programmatically. Through these problems, you will learn Java primitive data types and related subjects, such as variables, constants, data types, operators, expressions, and input and output. M. G. Abbas Malik, University of Jeddah 3

4 Writing a program involves two steps: 1. designing the Algorithm Problem Solving Step 2. Translating Coding Step the algorithm into programming instructions. Algorithm describes how a problem is solved by listing the actions needed to be taken to solve the problem. M. G. Abbas Malik, University of Jeddah 4

5 Read in the circle s radius. Compute the area using the following formula: area = radius * radius * pi (ᴫ) Display the result. Pseudo Code an informal high-level description of the operating principle of a computer program or other algorithm M. G. Abbas Malik, University of Jeddah 5

6 Start Flow Chart Graphical representation of an Algorithm Rhombus shows the input and output of the system Rectangle shows a process or computation Diamond shows a decision Arrows show the flow of the control. Input Computations - Operations Output Decisi on End M. G. Abbas Malik, University of Jeddah 6

7 Read in the circle s radius. Compute the area using the following formula: area = radius * radius * pi (ᴫ) Display the result. Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 7

8 M. G. Abbas Malik, University of Jeddah 8

9 M. G. Abbas Malik, University of Jeddah 9

10 M. G. Abbas Malik, University of Jeddah 10

11 ComputeCircleArea M. G. Abbas Malik, University of Jeddah 11

12 M. G. Abbas Malik, University of Jeddah 12

13 Every java program starts with a class public class ComputeCircleArea{ public static void main (String [] args){ // main function the starting point of the software } } M. G. Abbas Malik, University of Jeddah 13

14 public class ComputeCircleArea{ public static void main (String [] args){ // Input: Read the radius of the circle // Processing/Computation: Compute the area of the circle // Display the computed Area of the circle } } Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 14

15 A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 15

16 A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Data Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 16

17 A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Processing Actions Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 17

18 A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Processing Computations Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 18

19 A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation What are different types of DATA? How we store the DATA in our program? Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 19

20 What are different types of DATA? Numeric Data: Real Numbers (Rational Numbers Integers, Natural Numbers, Fractions, etc. and Irrational Numbers) Text Data: Characters and Strings Multimedia: Images, Sound and Video Logical Data M. G. Abbas Malik, University of Jeddah 20

21 How we store the DATA in our program? Data Types: kind of a container in which we can store only one kind of data. Numeric Integer (byte, int, short, long) Floating points (float, double) Text (char, String) Logical Data Boolean (True False) Built in Data Types of Java are called Primitive Data Types M. G. Abbas Malik, University of Jeddah 21

22 A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation How to perform different actions? How to do computations? Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 22

23 A program/algorithm consists of: 1. Data / Information 2. Processing 1. Action 2. Computation Different actions are control by the Control Unit in the CPU Computations are performed by the ALU (Arithematic Logic Unit) in the CPU Start Read the radius of the circle Compute Area: πr 2 Display the area of the circle End M. G. Abbas Malik, University of Jeddah 23

24 Variables are required to store Data/Information in a program Every variable has a name, size, data type and value. Variable is a memory area reserved in the RAM. public class ComputeCircleArea{ public static void main (String [] args){ Data Type of Variable Size is 8 Bytes Double precision } } Name of Variable // Input: Read the radius of the circle double radius; // Processing/Computation: Compute the area of the circle // Display the computed Area of the circle Default value for Double is 0.0 M. G. Abbas Malik, University of Jeddah 24

25 public class ComputeCircleArea{ public static void main (String [] args){ double radius; double area; // Declaring another variable for Area radius = 20; // Assigning a value to the radius // computing Area of the circle area = radius * radius * ; // Displaying the results System.out.println( The area of the circle with radius + radius + is + area); } } The plus sign (+) has two meanings: one for addition Operator and the other for concatenating (combining) stings. String concatenation operator, combines two strings into one. M. G. Abbas Malik, University of Jeddah 25

26 + In Java, + sign is also used as String Concatenation Operator Consider the string Java Programming String Concatenation In Java: Java + Programming + String + Concatenation String 1 + String 2 + String 3 + String 4 Java Programming String Concatenation JavaProgrammingStringConcatenation M. G. Abbas Malik, University of Jeddah 26

27 + In Java, + sign is also used as String Concatenation Operator Consider the string Java Programming String Concatenation In Java: Java Programming + String + Concatenation String 1 String 2 String Java Programming String Concatenation M. G. Abbas Malik, University of Jeddah 27

28 // Three strings are concatenated String message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2 String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character B String s1 = "Supplement" + 'B'; // s1 becomes SupplementB M. G. Abbas Malik, University of Jeddah 28

29 Compiler is a software that translates the high-level programming code into the machine code so that we can execute the code on our computers/machines. In Java, Java Compiler translates the java code into the Byte- Code first and then Byte-Code is executed on Java Virtual Machine (JVM). Compilation to byte code brings several genuine advantages. One advantage is that compiled programs are portable in the sense that they can be run on a number of different computer systems (such as Linux, Windows, Solaris, and many others) for which a JVM is available. T M. G. Abbas Malik, University of Jeddah 29

30 M. G. Abbas Malik, University of Jeddah 30

31 M. G. Abbas Malik, University of Jeddah 31

32 Register Register Register Control Unit Operating System Files Netbeans Files ComputeCircleArea.java ComputeCircleArea.class ComputeCircleArea.exe ALU M. G. Abbas Malik, University of Jeddah 32

33 public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * ; 5 System.out.println( The area of the circle with radius + radius + is + area); } } Operating System Files Netbeans Files ComputeCircleArea.java ComputeCircleArea.class ComputeCircleArea.exe A program is executed sequentially statement by statement.. M. G. Abbas Malik, University of Jeddah 33

34 public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * ; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius ComputeCircleArea.exe 0.0 M. G. Abbas Malik, University of Jeddah 34

35 public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * ; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe M. G. Abbas Malik, University of Jeddah 35

36 public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * ; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe M. G. Abbas Malik, University of Jeddah 36

37 public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * ; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe M. G. Abbas Malik, University of Jeddah 37

38 public class ComputeCircleArea{ public static void main (String [] args){ 1 double radius; 2 double area; 3 radius = 20; 4 area = radius * radius * ; 5 System.out.println( The area of the circle with radius + radius + is + area); } } A program is executed sequentially statement by statement.. Variable: radius Variable: area ComputeCircleArea.exe The area will be displayed on the screen M. G. Abbas Malik, University of Jeddah 38

39 Comments (//, /* */) Reserved words (public, void, static, class etc. ) Modifiers (public, static) Statements (System.out.println( Welcome to Java! ) Blocks (Class Block, Method Block) Classes (Welcome) Methods (At least one method is required) The main method M. G. Abbas Malik, University of Jeddah 39

40 Three types of comments in Java. Line comment: A line comment is preceded by two slashes (//) in a line. Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines. javadoc comment: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command. M. G. Abbas Malik, University of Jeddah 40

41 Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. For example, when the compiler sees the word class, it understands that the word after class is the name for the class. Some other reserved words are public, static, and void. M. G. Abbas Malik, University of Jeddah 41

42 Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public data, method, or class can be accessed by other programs. A private data or method cannot be accessed by other programs. Modifiers are discussed later in more details M. G. Abbas Malik, University of Jeddah 42

43 A statement represents an action or a sequence of actions. The statement System.out.println( ") in the program is a statement to display the area of the circle Every statement in Java ends with a semicolon (;). M. G. Abbas Malik, University of Jeddah 43

44 A pair of braces in a program forms a block that groups components of a program. public class Test { public static void main(string[] args) { System.out.println("Welcome to Java!"); } } Method block Class block M. G. Abbas Malik, University of Jeddah 44

45 The class is the essential Java construct. A class is a template or blueprint for objects. For now, though, understand that a program is defined by using one or more classes. M. G. Abbas Malik, University of Jeddah 45

46 What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In our example, the argument is ( The area of the circle with radius + radius + is + area ) You can call the same println method with a different argument to print a different message. M. G. Abbas Malik, University of Jeddah 46

47 The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method. The main method looks like this: } public static void main(string[] args) { // Statements; M. G. Abbas Malik, University of Jeddah 47

48 Variables are required to store Data/Information in a program Every variable has a name, size, data type and value. Variable is a memory area reserved in the RAM. Name of Variable public class ComputeCircleArea{ public static void main (String [] args){ Data Type of Variable Size is 8 Bytes Double precision } } // Input: Read the radius of the circle double radius; // Processing/Computation: Compute the area of the circle // Display the computed Area of the circle Default value for Double is 0.0 Identifier M. G. Abbas Malik, University of Jeddah 48

49 An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($). An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. An identifier cannot be true, false, or null. An identifier can be of any length. Examples: number1, Demo, i_j, number$4, area, volume, M. G. Abbas Malik, University of Jeddah 49

50 x = 1; // Assign 1 to variable x; radius = 1.0; // Assign 1.0 to variable radius; a = 'A'; // Assign 'A' to variable a; M. G. Abbas Malik, University of Jeddah 50

51 In General, an assignment statement is like: Single_Variable(identifier) = expression An expression can be: 1. A value x = 1; // Assign 1 to variable x; radius = 1.0; // Assign 1.0 to variable radius; a = 'A'; // Assign 'A' to variable a; M. G. Abbas Malik, University of Jeddah 51

52 In General, an assignment statement is like: Single_Variable(identifier) = expression An expression can be: 1. A value 2. Another variable x = 1; // Assign 1 to variable x; radius = x; // Assign 1.0 to variable radius; M. G. Abbas Malik, University of Jeddah 52

53 In General, an assignment statement is like: Single_Variable(identifier) = expression An expression can be: 1. A value 2. Another variable 3. An expression that can be evaluated into a single value x = 1; // Assign 1 to variable x; y = ( )*25 // Assign the value of expression to variable y; radius = x + y; // Assign addition of variables x and y to variable radius; M. G. Abbas Malik, University of Jeddah 53

54 A named constant is an identifier that represents a permanent value. final datatype CONSTANTNAME = VALUE; final double PI = ; final int SIZE = 3; M. G. Abbas Malik, University of Jeddah 54

55 Name Range Storage Size byte 2 7 (-128) to (127) 8-bits signed short 2 15 (-32768) to (32767) 16-bit signed int 2 31 ( ) to ( ) 32-bit signed long 2 63 to (i.e., to ) float double Negative range: E+38 to -1.4E-45 Positive range: 1.4E-45 to E+38 Negative range: E+308 to -4.9E-324 Positive range: 4.9E-324 to E bit signed 32-bit IEEE bit IEEE 754 M. G. Abbas Malik, University of Jeddah 55

56 Name Meaning Example Result + Addition Subtraction * Multiplicatio n 30 * 3 90 / Division 8.4 / % Remainder 20 % 3 2 M. G. Abbas Malik, University of Jeddah 56

57 5 / 2 yields an integer / 2 yields a double value % 2 yields 1 (the remainder of the division) M. G. Abbas Malik, University of Jeddah 57

58 Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days? Saturday is the 6 th day in a week A week has 7 days % 7 = 2 The 2 nd day in a week is Tuesday After 10 Days M. G. Abbas Malik, University of Jeddah 58

59 A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements: int i = 34; long x = ; double d = 5.0; M. G. Abbas Malik, University of Jeddah 59

60 An integer literal can be assigned to an integer variable as long as it can fit into the variable. A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type. An integer literal is assumed to be of the int type, whose value is between ( ) to ( ). To denote an integer literal of the long type, append it with the letter L or l. M. G. Abbas Malik, University of Jeddah 60 L is preferred because l (lowercase L) can easily be confused with 1 (the digit one).

61 Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value. We can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number. M. G. Abbas Malik, University of Jeddah 61

62 Floating-point literals can also be specified in scientific notation, for example, e+2, same as e-2, is equivalent to , and e-2 is equivalent to E (or e) represents an exponent and it can be either in lowercase or uppercase. M. G. Abbas Malik, University of Jeddah 62

63 3 + 4x 5 10 y 5 x a + b + c x x y How can write this in Java? (3+4*x)/5 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y) M. G. Abbas Malik, University of Jeddah 63

64 The result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression. M. G. Abbas Malik, University of Jeddah 64

65 M. G. Abbas Malik, University of Jeddah 65

66 Write a program that obtains hours and minutes from seconds. How can we achieve this? What is the Algorithm? M. G. Abbas Malik, University of Jeddah 66

67 Write a program that obtains hours and minutes from seconds. Input to our program Outputs to our program How to calculate the results/outputs from the input? 1. Compute minutes from the seconds 2. Compute remaining seconds 3. Compute hours from the minutes 4. Compute remaining minutes M. G. Abbas Malik, University of Jeddah 67

68 Write a program that obtains hours and minutes from seconds. Read the input seconds Compute the minutes from the seconds using the formula minutes = seconds / 60 Compute the remaining seconds using the formula seconds = seconds % 60 Compute the hours from the minutes using the formula hours = minutes / 60 Compute the remaining minutes using the formula minutes = minutes % 60 Display the results as Hours : Minutes : Seconds M. G. Abbas Malik, University of Jeddah 68

69 Current Time of Computer Gives current time in Milliseconds M. G. Abbas Malik, University of Jeddah 69

70 Current Time of Computer Converting time from Milliseconds into seconds M. G. Abbas Malik, University of Jeddah 70

71 Current Time of Computer Getting current seconds from the Total seconds M. G. Abbas Malik, University of Jeddah 71

72 Current Time of Computer Getting total minutes from the Total seconds M. G. Abbas Malik, University of Jeddah 72

73 Current Time of Computer Getting current minutes from the Total Minutes M. G. Abbas Malik, University of Jeddah 73

74 Current Time of Computer Getting total hours from the Total Minutes M. G. Abbas Malik, University of Jeddah 74

75 Current Time of Computer Getting current hours from the Total hours M. G. Abbas Malik, University of Jeddah 75

76 Current Time of Computer Displaying current time on screen M. G. Abbas Malik, University of Jeddah 76

77 Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 For Problem Analysis: What is the input of our program? What is the output of our program? fahrenheit 32 What are the calculations/action that we will perform in our program? M. G. Abbas Malik, University of Jeddah 77

78 Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 fahrenheit 32 M. G. Abbas Malik, University of Jeddah 78

79 Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 fahrenheit 32 Input to our program M. G. Abbas Malik, University of Jeddah 79

80 Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 fahrenheit 32 Outputs to our program Input to our program M. G. Abbas Malik, University of Jeddah 80

81 Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 computations fahrenheit 32 Outputs to our program Input to our program M. G. Abbas Malik, University of Jeddah 81

82 Write a program that converts a Fahrenheit degree to Celsius using the formula: celsius = 5 9 Read in the Fahrenheit degree fahrenheit 32 Calculate the Celsius degree using the above given formula Display the results on the screen M. G. Abbas Malik, University of Jeddah 82

83 Equal sign (=) is the Assignment Operator It is used to assign a value to a variable. Examples: radius = 20; area = radius * radius * ; M. G. Abbas Malik, University of Jeddah 83

84 Operator Example Equivalent += i += 8 i = i + 8 -= f -= 8.0 f = f *= i *= 8 i = i * 8 /= i /= 8 i = i / 8 %= i %= 8 i = i % 8 M. G. Abbas Malik, University of Jeddah 84

85 Operator Name Description ++var Preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++ Postincrement The expression (var++) evaluates to the original value in var and increments var by 1. --var Predecrement The expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var-- Postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1. M. G. Abbas Malik, University of Jeddah 85

86 int i = 10; int newnum = 10 * i++; Post Increment Same effect as int newnum = 10 * i; i = i + 1; // i += 1; int i = 10; int newnum = 10 * ++i; newnum = 100 Pre Increment i = 11 Same effect as i = i + 1; // i += 1; int newnum = 10 * i; newnum = 110 i = 11 M. G. Abbas Malik, University of Jeddah 86

87 Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i. M. G. Abbas Malik, University of Jeddah 87

88 The following types of expressions can be Assignment statements: variable op= expression; // Where op is +, -, *, /, or % ++variable; variable++; --variable; variable--; M. G. Abbas Malik, University of Jeddah 88

89 Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * k / 2; M. G. Abbas Malik, University of Jeddah 89

90 When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int. M. G. Abbas Malik, University of Jeddah 90

91 Implicit casting (automatically done by Java) double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) What is wrong with: int x = 5 / 2.0; M. G. Abbas Malik, University of Jeddah 91

92 Use Scanner class for console input. Need to create an object of Scanner class to read input. Scanner input = new Scanner(System.in); Scanner input declare input variable whose type is Scanner. the instruction creates a Scanner object and assigns its reference to the variable input. To invoke the method of the object input type scanner we will use nextdouble() M. G. Abbas Malik, University of Jeddah 92

93 import java.util.scanner; // Scanner class is in the java.util package public class ComputeCircleArea{ public static void main (String [] args){ double radius; double area; Scanner input = new Scanner(System.in); system.out.print( Enter a value for the radius of the circle: ); radius = input.nextdouble(); area = radius * radius * ; System.out.println( The area of the circle with radius + radius + is + area); } } M. G. Abbas Malik, University of Jeddah 93

94 Write a program that displays the sales tax with two digits after the decimal point. This program lets the user enter the interest rate, number of years, and loan amount and computes monthly payment and total payment. monthly payment = 1 loanamount monthlyinterestrate monthlyinterestrate numberofyear 12 ComputeLoan M. G. Abbas Malik, University of Jeddah 94

95 char letter = 'A'; (ASCII) char numchar = '4'; (ASCII) Four hexadecimal digits. char letter = '\u0041'; (Unicode) char numchar = '\u0034'; (Unicode) NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = 'a'; System.out.println(++ch); M. G. Abbas Malik, University of Jeddah 95

96 Java characters use Unicode, a 16-bit encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the world s diverse languages. Unicode takes two bytes, preceded by \u, expressed in four hexadecimal numbers that run from '\u0000' to '\uffff'. So, Unicode can represent characters. Unicode \u03b1 \u03b2 \u03b3 for three Greek letters M. G. Abbas Malik, University of Jeddah 96

97 Write a program that displays two Chinese characters and three Greek letters. DisplayUnicode Run M. G. Abbas Malik, University of Jeddah 97

98 Description Escape Sequence Backspace \b \u0008 Tab \t \u0009 Linefeed \n \u000a Carriage return \r \u000d Backslash \\ \u005c Single quote \ \u0027 Double quote \ \u0022 Unicode M. G. Abbas Malik, University of Jeddah 98

99 ASCII Character Set is a subset of the Unicode from \u0000 to \u007f M. G. Abbas Malik, University of Jeddah 99

100 ASCII Character Set is a subset of the Unicode from \u0000 to \u007f M. G. Abbas Malik, University of Jeddah 100

101 int i = 'a'; // Same as int i = (int)'a'; char c = 97; // Same as char c = (char)97; M. G. Abbas Malik, University of Jeddah 101

102 This program lets the user enter the amount in decimal representing dollars and cents and output a report listing the monetary equivalent in single dollars, quarters, dimes, nickels, and pennies. Your program should report maximum number of dollars, then the maximum number of quarters, and so on, in this order. ComputeChange Run M. G. Abbas Malik, University of Jeddah 102

103 The char type only represents one character. To represent a string of characters, use the data type called String. For example, String message = "Welcome to Java"; String is actually a predefined class in the Java library The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable. M. G. Abbas Malik, University of Jeddah 103

104 Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles M. G. Abbas Malik, University of Jeddah 104

105 Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program. M. G. Abbas Malik, University of Jeddah 105

106 Choose meaningful and descriptive names. Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computearea. M. G. Abbas Malik, University of Jeddah 106

107 Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeArea. Constants: Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE M. G. Abbas Malik, University of Jeddah 107

108 Indentation Indent two spaces. Spacing Use blank line to separate segments of the code. M. G. Abbas Malik, University of Jeddah 108

109 Use end-of-line style for braces. Next-line style public class Test { public static void main(string[] args) { System.out.println("Block Styles"); } } public class Test { public static void main(string[] args) { System.out.println("Block Styles"); } } End-of-line style M. G. Abbas Malik, University of Jeddah 109

110 Syntax Errors Detected by the compiler Runtime Errors Causes the program to abort Logic Errors Produces incorrect result M. G. Abbas Malik, University of Jeddah 110

111 public class ShowSyntaxErrors { } public static void main(string[] args) { } i = 30; System.out.println(i + 4); i is not declared as variable thus compiler does not recognize it M. G. Abbas Malik, University of Jeddah 111

112 public class ShowRuntimeErrors { } public static void main(string[] args) { } int i = 1 / 0; Division by ZERO M. G. Abbas Malik, University of Jeddah 112

113 Logic errors occur when a program does not perform the way it was intended to. This kind of error is done by the programmer. Wrong use of formula An error in algorithm Hard to identify such errors public class ShowLogicErrors { public static void main(string[] args) { // Add number1 to number2 int number1 = 3; int number2 = 3; number2 += number1 + number2; System.out.println("number2 is " + number2); } } M. G. Abbas Malik, University of Jeddah 113

114 Logic errors are called bugs. The process of finding and correcting errors is called debugging. A common approach to debugging is to use a combination of methods to narrow down to the part of the program where the bug is located. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program. This approach might work for a short, simple program. But for a large, complex program, the most effective approach for debugging is to use a debugger utility. M. G. Abbas Malik, University of Jeddah 114

115 Debugger is a program that facilitates debugging. You can use a debugger to Execute a single statement at a time. Trace into or stepping over a method. Set breakpoints. Display variables. Display call stack. Modify variables. M. G. Abbas Malik, University of Jeddah 115

116 We will use two ways of obtaining input. 1. Using the Scanner class (console input) 2. Using JOptionPane input dialogs (GUI input) M. G. Abbas Malik, University of Jeddah 116

117 String input = JOptionPane.showInputDialog( "Enter an input"); M. G. Abbas Malik, University of Jeddah 117

118 String string = JOptionPane.showInputDialog( null, Prompting Message, Dialog Title, JOptionPane.QUESTION_MESSAGE); M. G. Abbas Malik, University of Jeddah 118

119 There are several ways to use the showinputdialog method. For the time being, you only need to know two ways to invoke it. One is to use a statement as shown in the example: String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE); where x is a string for the prompting message, and y is a string for the title of the input dialog box. The other is to use a statement like this: JOptionPane.showInputDialog(x); where x is a string for the prompting message. M. G. Abbas Malik, University of Jeddah 119

120 The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns 123. To obtain the input as a number, you have to convert a string into a number. To convert a string into an int value, you can use the static parseint method in the Integer class as follows: int intvalue = Integer.parseInt(intString); where intstring is a numeric string such as 123. M. G. Abbas Malik, University of Jeddah 120

121 To convert a string into a double value, you can use the static parsedouble method in the Double class as follows: double doublevalue =Double.parseDouble(doubleString); where doublestring is a numeric string such as M. G. Abbas Malik, University of Jeddah 121

122 Same as the preceding program for computing loan payments, except that the input is entered from the input dialogs and the output is displayed in an output dialog. loanamount monthlyinterestrate 1 1 (1 monthlyinterestrate ) numberofyears 12 ComputeLoanUsingInputDialog Run M. G. Abbas Malik, University of Jeddah 122

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 Elementary Programming

Chapter 2 Elementary Programming 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

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

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives Chapter 2 Elementary Programming 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

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

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection

More information

Chapter 2 Primitive Data Types and Operations

Chapter 2 Primitive Data Types and Operations Chapter 2 Primitive Data Types and Operations 2.1 Introduction You will be introduced to Java primitive data types and related subjects, such as variables constants, data types, operators, and expressions.

More information

Chapter 2 Primitive Data Types and Operations

Chapter 2 Primitive Data Types and Operations Chapter 2 Primitive Data Types and Operations 2.1 Introduction You will be introduced to Java primitive data types and related subjects, such as variables constants, data types, operators, and expressions.

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

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

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

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

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

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming 2.1 Introduction You will learn elementary programming using Java primitive data types and related subjects, such as variables, constants, operators, expressions, and input

More information

Datatypes, Variables, and Operations

Datatypes, Variables, and Operations Datatypes, Variables, and Operations 1 Primitive Type Classification 2 Numerical Data Types Name Range Storage Size byte 2 7 to 2 7 1 (-128 to 127) 8-bit signed short 2 15 to 2 15 1 (-32768 to 32767) 16-bit

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

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

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University Elementary Programming CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Variables In a program, the variables store data Primitive type variables store single pieces

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

Elementary Programming

Elementary Programming Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG Learning Outcomes Learn ingredients of elementary programming: data types [numbers, characters, strings] literal

More information

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time Tester vs. Controller Elementary Programming EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG For effective illustrations, code examples will mostly be written in the form of a tester

More information

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process Entry Point of Execution: the main Method Elementary Programming EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG For now, all your programming exercises will

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

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

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

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

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

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

Important Java terminology

Important Java terminology 1 Important Java terminology The information we manage in a Java program is either represented as primitive data or as objects. Primitive data פרימיטיביים) (נתונים include common, fundamental values as

More information

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes Entry Point of Execution: the main Method Elementary Programming EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG For now, all your programming exercises will be defined within the

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

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

Chapter 02: Using Data

Chapter 02: Using Data True / False 1. A variable can hold more than one value at a time. ANSWER: False REFERENCES: 54 2. The int data type is the most commonly used integer type. ANSWER: True REFERENCES: 64 3. Multiplication,

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

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

4 Programming Fundamentals. Introduction to Programming 1 1

4 Programming Fundamentals. Introduction to Programming 1 1 4 Programming Fundamentals Introduction to Programming 1 1 Objectives At the end of the lesson, the student should be able to: Identify the basic parts of a Java program Differentiate among Java literals,

More information

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2 Java Foundations Introduction to Program Design and Data Structures 4th Edition Lewis TEST BANK Full download at : https://testbankreal.com/download/java-foundations-introduction-toprogram-design-and-data-structures-4th-edition-lewis-test-bank/

More information

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

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

More information

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

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

Lecture Set 2: Starting Java

Lecture Set 2: Starting Java Lecture Set 2: Starting Java 1. Java Concepts 2. Java Programming Basics 3. User output 4. Variables and types 5. Expressions 6. User input 7. Uninitialized Variables 0 This Course: Intro to Procedural

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

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors Outline Overview history and advantage how to: program, compile and execute 8 data types 3 types of errors Control statements Selection and repetition statements Classes and methods methods... 2 Oak A

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

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters 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 #4

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

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini 19. GUI Basics Java Fall 2009 Instructor: Dr. Masoud Yaghini Outline Displaying Text in a Message Dialog Box Getting Input from Input Dialogs References Displaying Text in a Message Dialog Box Displaying

More information

Introduction to Computers, Programs, and Java. CSE 114, Computer Science 1 Stony Brook University

Introduction to Computers, Programs, and Java. CSE 114, Computer Science 1 Stony Brook University Introduction to Computers, Programs, and Java CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 What is a Computer? A computer consists of a CPU, memory, hard disk,

More information

Full file at

Full file at MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) 25 % 1 is. 1) A) 2 B) 1 C) 0 D) 4 E) 3 2) Which of the following expressions will yield 0.5? (choose

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

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Basic Operations jgrasp debugger Writing Programs & Checkstyle Basic Operations jgrasp debugger Writing Programs & Checkstyle Suppose you wanted to write a computer game to play "Rock, Paper, Scissors". How many combinations are there? Is there a tricky way to represent

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

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

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

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

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

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

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

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

Date: Dr. Essam Halim

Date: Dr. Essam Halim Assignment (1) Date: 11-2-2013 Dr. Essam Halim Part 1: Chapter 2 Elementary Programming 1 Suppose a Scanner object is created as follows: Scanner input = new Scanner(System.in); What method do you use

More information

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives LIANMC03v3_0132221586.QXD 5/15/06 7:41 PM Page 67 CCHAPTER HAPTER 3 1 SELECTION STATEMENTS Objectives To declare boolean type and write Boolean expressions ( 3.2). To distinguish between conditional and

More information

Information Science 1

Information Science 1 Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java Chapter 3 Syntax, Errors, and Debugging Objectives Construct and use numeric and string literals. Name and use variables and constants. Create arithmetic expressions. Understand the precedence of different

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 3 Selection Statements

Chapter 3 Selection Statements Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are

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

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers and letters. Think of them

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

CS-201 Introduction to Programming with Java

CS-201 Introduction to Programming with Java CS-201 Introduction to Programming with Java California State University, Los Angeles Computer Science Department Lecture V: Mathematical Functions, Characters, and Strings Introduction How would you estimate

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

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

COMP Primitive and Class Types. Yi Hong May 14, 2015

COMP Primitive and Class Types. Yi Hong May 14, 2015 COMP 110-001 Primitive and Class Types Yi Hong May 14, 2015 Review What are the two major parts of an object? What is the relationship between class and object? Design a simple class for Student How to

More information

Section 2: Introduction to Java. Historical note

Section 2: Introduction to Java. Historical note The only way to learn a new programming language is by writing programs in it. - B. Kernighan & D. Ritchie Section 2: Introduction to Java Objectives: Data Types Characters and Strings Operators and Precedence

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Welcome to the Primitives and Expressions Lab!

Welcome to the Primitives and Expressions Lab! Welcome to the Primitives and Expressions Lab! Learning Outcomes By the end of this lab: 1. Be able to define chapter 2 terms. 2. Describe declarations, variables, literals and constants for primitive

More information

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

false, import, new 1 class Lecture2 { 2 3 Data types, Variables, and Operators 4 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4 5 } 6 7 // Keywords: 8 byte, short, int, long, char, float, double, boolean, true, false, import, new Zheng-Liang Lu Java Programming 45

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Java Notes. 10th ICSE. Saravanan Ganesh

Java Notes. 10th ICSE. Saravanan Ganesh Java Notes 10th ICSE Saravanan Ganesh 13 Java Character Set Character set is a set of valid characters that a language can recognise A character represents any letter, digit or any other sign Java uses

More information

Primitive Data Types: Intro

Primitive Data Types: Intro Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

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

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

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

CS110: PROGRAMMING LANGUAGE I

CS110: PROGRAMMING LANGUAGE I CS110: PROGRAMMING LANGUAGE I Computer Science Department Lecture 4: Java Basics (II) A java Program 1-2 Class in file.java class keyword braces {, } delimit a class body main Method // indicates a comment.

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

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Computer Programming, I. Laboratory Manual. Experiment #3. Selections 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 #3

More information

Java Programming Fundamentals. Visit for more.

Java Programming Fundamentals. Visit  for more. Chapter 4: Java Programming Fundamentals Informatics Practices Class XI (CBSE Board) Revised as per CBSE Curriculum 2015 Visit www.ip4you.blogspot.com for more. Authored By:- Rajesh Kumar Mishra, PGT (Comp.Sc.)

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

Chapter 4 Mathematical Functions, Characters, and Strings

Chapter 4 Mathematical Functions, Characters, and Strings Chapter 4 Mathematical Functions, Characters, and Strings Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited 2015 1 Motivations Suppose you need to estimate

More information

Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections

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

More information

Introduction to Java Applications; Input/Output and Operators

Introduction to Java Applications; Input/Output and Operators www.thestudycampus.com Introduction to Java Applications; Input/Output and Operators 2.1 Introduction 2.2 Your First Program in Java: Printing a Line of Text 2.3 Modifying Your First Java Program 2.4 Displaying

More information

II. Compiling and launching from Command-Line, IDE A simple JAVA program

II. Compiling and launching from Command-Line, IDE A simple JAVA program Contents Topic 01 - Java Fundamentals I. Introducing JAVA II. Compiling and launching from Command-Line, IDE A simple JAVA program III. How does JAVA work IV. Review - Programming Style, Documentation,

More information

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2017 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