Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 2 Basic Elements of Java continued…….. Increment & Decrement Operator Examples Revision common code int n = 3; int m = 4; int result; What will be the value of m and result after each of these executes? (a) result = n * ++m; //pre-increment m (b) result = n * m++; //post-increment m (c) result = n * --m; //pre-decrement m (d) result = n * m--; //post-decrement m Answers to Increment/Decrement Operator Questions (a) 1) m = m + 1;//m = 4 + 1 = 5 2) result = n * m;//result = 3 * 5 = 15 (b) 1) result = n * m;//result = 3 * 4 = 12 2) m = m + 1;//m = 4 + 1 = 5 (c) 1) m = m - 1;//m = 4 - 1 = 3 2) result = n * m;//result = 3 * 3 = 9 (b) 1) result = n * m;//result = 3 * 4 = 12 2) m = m - 1;//m = 4 - 1 = 3 Two Main Kinds of Types in Java primitive data types class types • the simplest types • cannot decompose into other types • values only, no methods • Examples: int - integer double - floating point (real) char - character • more complex • composed of other types (primitive or class types) • both data and methods • Examples: String The class String • Used to manipulate strings (methods) • String – – – – – – Sequence of zero or more characters Enclosed in double quotation marks Null or empty strings have no characters Numeric strings consist of integers or decimal numbers Length is the number of characters in a string String class has methods to operate on strings char charVariable = `a`; //single quotes String stringVariable = "a"; //double quotes String sentence = "Hello, world"; Strings and the Operator + (Concatenation) • Operator + can be used to concatenate two strings or a string and a numeric value or character • Example: Indexing Characters within a String • The index of a character within a string is an integer starting at 0 for the first character and gives the position of the character • The charAt(Position)method returns the char at the specified position • substring(Start, End)method returns the string from position Start to position End • For example: String greeting = "Hi, there!"; greeting.charAt(0)returns H greeting.charAt(2)returns , greeting.substring(4,7)returns the H i , 0 1 2 3 t h e r e ! 4 5 6 7 8 9 Parsing Numeric Strings • String to int Integer.parseInt(strExpression) • String to float Float.parseFloat(strExpression) • String to double Double.parseDouble(strExpression) Input • Named constant – – – content cannot be changed during program execution declared by using the reserved word final initialized when it is declared final double CONVERSION = 2.45; final char BLANK = ‘ ‘; • Variable (name, value, data type, size) – – – – – content may change during program execution must be declared before it can be used may not be automatically initialized if new value is assigned, old one is destroyed value can only be changed by an assignment statement or an input (read) statement Input • Standard input stream object: System.in • Input numeric data to program – • Separate by blanks, lines, or tabs To read a line of characters: 1. Create an input stream object of the class BufferedReader 2. Use the method readLine To read characters from the input stream: 1. Declare & initialise a variable to the standard input device (System.in) to read only one character at a time. InputStreamReader charReader = new InputStreamReader(System.in); 2. Declare & initialise another input stream variable to read an entire line of characters. BufferedReader keyboard = new BufferedReader(charReader); 3. Declare String variable to contain the input. String inputStr; 4. Read & assign input line of characters to variable. inputStr = keyboard.readLine(); Inputting Numeric Data • Numeric data read as a sequence of characters. • Must parse the numeric string into an appropriate number. String numString; double miles; BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); numString = keyboard.readLine(); miles = Double.parseDouble(numString); Reading a single character • Use the method read (from BufferedReader). • Returns the integer value of the character. – A returns a value of 65 • Will :. have to use a cast operator to convert this integer value in a char value. char ch; BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); ch = (char) keyboard.read(); Output • Standard output object: System.out • Methods – print – println – flush • Syntax System.out.print(stringExp); System.out.println(stringExp); System.out.flush(); Commonly Used Escape Sequences Packages, Classes, Methods, & the import Statement • Package: collection of related classes • Class: consists of methods • Method: designed to accomplish a specific task import Statement • Used to import the components of a package into a program • Reserved word • For example import java.io.*; imports the (components of the) package java.io into the program • Primitive data types and the class String – Part of the Java language – Don’t need to be imported Java Application Program • Syntax of the main method More on Assignment Statements • variable = variable * (expression); is equivalent to • variable *= expression; Similarly, • variable = variable + (expression); is equivalent to: • variable += expression; Documentation & Style • Use meaningful names for variables, classes, etc. • Use indentation and line spacing as shown in the examples in the text • Always include a “prologue” (an brief explanation of the program at the beginning of the file) • Use all lower case for variables, except capitalize internal words (eggsPerBasket) • Use all upper case for variables that have a constant value, PI for the value of pi (3.14159…) (see text for more examples) Documentation & Style • Use meaningful names for variables, classes, etc. • Use indentation and line spacing as shown in the examples in the text • Always include a “prologue” (an brief explanation of the program at the beginning of the file) • Use all lower case for variables, except capitalize internal words (eggsPerBasket) • Use all upper case for variables that have a constant value, PI for the value of pi (3.14159…) (see text for more examples) Programming Examples • Make Change Program – Input: Change in cents – Output: Equivalent change in half-dollars, quarters, dimes, nickels, and pennies import java.io.*; public class MakeChange { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); static final int HALFDOLLAR = 50; static final int QUARTER = 25; static final int DIME = 10; static final int NICKEL = 5; public static void main (String[] args) throws IOException { //declare variables int change; System.out.print("Enter the change in cents: "); System.out.flush(); change = Integer.parseInt(keyboard.readLine()); System.out.println(); System.out.println("The change you entered is " + change); System.out.println("The number of half dollars " + "to be returned are " + change / HALFDOLLAR); change = change % HALFDOLLAR; System.out.println("The number of quarters to be returned are " + change / QUARTER); change = change % QUARTER; System.out.println("The number of dimes to be returned are " + change / DIME); change = change % DIME; System.out.println("The number of nickels to be returned are " + change / NICKEL); change = change % NICKEL; System.out.println("The number of pennies to be " + "returned are " + change); } } }