Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMT Programming Software Applications

Similar presentations


Presentation on theme: "CMT Programming Software Applications"— Presentation transcript:

1 CMT4001 -- Programming Software Applications
Java Data Types and Operators Week 2 Dr. Xiaohong Gao TP B107, ext. 2252

2 Contents To become familiar with Java applications
To become familiar with variables in Java To become familiar with Java primitive’s data types To become familiar with arithmetic operators

3 A Simple Program 1 // Fig. 2.1: Welcome1.java
2 // A first program in Java 3 public class Welcome1 { public static void main( String args[]) { System.out.println( "Welcome to Java Programming!" ); } } Welcome to Java Programming!

4 A Simple Program: Printing a Line of Text
1 // Fig. 2.1: Welcome1.java // indicates the remainder of the line is a comment Comments are ignored by the compiler Use comments to document and describe code Can also use multiple line comments: /* ... */ /* This is a multiple line comment. It can be split over many lines */ Another line of comments that describes the program Note: line numbers are not part of the program; they are added for our reference

5 A Simple Program: Printing a Line of Text
3 A blank line Blank lines and spaces make a program more readable Blank lines, spaces, and tabs are known as whitespace characters, and are ignored by the compiler public class Welcome1 { Begin a class definition for class Welcome1 Every Java program has at least one user-defined class class keyword immediately followed by class name Keyword: words reserved for use by Java Naming classes: capitalize every word SampleClassName

6 A Simple Program: Printing a Line of Text
public class Welcome1 { Name of class called identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit Contains no spaces Examples: Welcome1, $value, _value, button7 7button is invalid Case sensitive (capitalization matters) a1 and A1 are different For now, use public keyword Certain details are not important now - mimic programs, full discussions will come later

7 A Simple Program: Printing a Line of Text
public class Welcome1 { Saving files File name is class name and .java extension Welcome1.java Left brace Begins body of every class Right brace ends definition (line 9) public static void main( String args[] ) Part of every Java application Applications begin executing at main Parenthesis indicate main is a method Java applications contain one or more methods

8 A Simple Program: Printing a Line of Text
5 public static void main( String args[] ) Exactly one method must be called main Methods can perform tasks and return information void means main returns no information For now, mimic main's first line { Left brace begins body of method definition Ended by right brace

9 A Simple Program: Printing a Line of Text
System.out.println( "Welcome to Java Programming!" ); Instructs computer to perform an action Prints string of characters between double quotes String - series characters inside double quotes White spaces in strings are not ignored by compiler System.out - standard output object Allows java to print to command window (i.e., MS-DOS prompt) Method System.out.println displays a line of text Argument inside parenthesis Entire line known as a statement All statements must end with a semicolon ;

10 A Simple Program: Printing a Line of Text
8 } Ends method definition } Ends class definition Some programmers add comments to keep track of ending braces Lines 8 and 9 could be rewritten as: 8 } // end of the method main () 9 } // end of class Welcome1 Remember that the compiler ignores comments

11 A Simple Program // Fig. 2.2: Welcome2.java
2 // Printing a line with multiple statements 3 public class Welcome2 { public static void main( String args[]) { System.out.print ( "Welcome to Java); System.out.println(“Java programming”); } System.out.print keeps the cursor on the same line, so System.out.println continues on the same line.

12 A Simple Program Line numbers 1-2: Comments 3: Blank
4: Begin class Welcome2 5: Method main 6: Begin main body 7: Method System.out.print 8: Method System.out.println 9: end main 10: end Welcome2

13 A Simple Program: Printing a Line of Text
Escape characters Backslash ( \ ) Indicates that special characters are to be output Backslash combined with a character makes an escape sequence \n - newline \t - tab Usage Can use in System.out.println or System.out.print to create new lines System.out.println( "Welcome\nto\nJava\nProgramming!" );

14 A Simple Program: Printing a Line of Text
// Fig. 2.3: Welcome3.java 2 // Printing multiple lines with just 3 // a single statements 4 5 public class Welcome3 { public static void main( String args[]) { System.out.print (“Welcome\ \nto\nJava\nProgramming!”); } 11 } Notice how a new line is output for each \n escape sequence. Welcome to Java Programming!

15 Variables A variable has three properties:
a memory location to store this value the type of data stored in the memory location the named used to refer to the memory location

16 Variables The general syntax for declaring variables is:
<data type> <variables> where variables is a sequence of identifiers separated by commas. Example: int x; int y; or int x,y;

17 Variables Variables Visual representation 45
Variable names correspond to locations in the computer's memory Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable it replaces (and destroys) previous value Reading variables from memory does not change them Visual representation number1 45

18 Data type There are six numerical data types in Java: byte – integer
short –integer int – integer long –integer float – real double - real Example: int j, k, l; float numberOne, numberTwo; long bigInteger; double bigNumber;

19 Java’s primitive data types
Size Description Integers byte 8-bit Byte-length integer short 16-bit Short integer int 32-bit Integer 64-bit long Long integer Real Numbers float 32-bit Single-precision floating-point double 64-bit Double-precision floating-point Other Types char 16-bit Unicode character A single character boolean true or false A boolean value(true or false)

20 Variables Assignment statement <variable> = <expression>;
Examples: firstNumber = 234; sum = firstNumber+ secondNumber; solution = x*x-2*x+1;

21 Variables The variables firstNumber and
int firstNumber,secondNumber; after A is executed firstNumber = 234; secondNumber = 87; firstNumber secondNumber The variables firstNumber and secondNumber are declared and set in the memory

22 Variables Values are assigned to the variables firstNumber
int firstNumber,secondNumber; after B is executed firstNumber = 234; secondNumber = 87; firstNumber 234 B secondNumber 87 Values are assigned to the variables firstNumber and secondNumber

23 Quick Check Why are the following declarations all invalid?
int a, b, a; float x, int; float w, int x; bigNumber double;

24 Another Java Application: Adding Integers
int number1; // first number to add int number2; // second number to add sum; // sum of number1 and number2 Declares variables number1, number2, and sum of type int int can hold integer values (whole numbers): i.e., 0, -4, 97 Data types float and double can hold decimal numbers

25 Another Java Application: Adding Integers
// add the numbers sum = number1 + number2; Assignment statement First calculates sum of number1 and number2 (right hand side) Next, uses assignment operator = to assign result to variable sum Read as: sum gets the value of number1 + number2

26 Another Java Application: Adding Integers
System.out.printl(“The sum is”+ sum); "The sum is " + sum Uses the operator + to "add" the string literal "The sum is" and sum Allows concatenation of a String and another data type Results in a new string If sum contains 117, then "The sum is " + sum results in the new string "The sum is 117" Note the space in "The sum is "

27 Arithmetic Arithmetic calculations are used in most programs
Use * for multiplication and / for division, +, - No operator for exponentiation Integer division truncates remainder 7 / 5 evaluates to 1 Modulus operator % returns the remainder 7 % 5 evaluates to 2 Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: (a + b + c ) / 3

28 Arithmetic

29 Decision Making: Equality and Relational Operators
< _ > =

30 Summary Java applications Variables in Java
Java primitive’s data types Arithmetic operators


Download ppt "CMT Programming Software Applications"

Similar presentations


Ads by Google