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: Java Fundamentals Input and Output statements Standard Output Window • Using System.out, we can output multiple lines of text to the standard output window. • The exact style of standard output window depends on the Java tool you use. Page 2 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP The println Method • We use println instead of print to skip a line. int x = 123, y = x + x; System.out.println( "Hello, Dr. Caffeine.“ ); System.out.print( " x = “ ); System.out.println( x ); System.out.print( " x + x = “ ); System.out.println( y ); System.out.println( " THE END“ ); Page 3 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Commonly Used Escape Sequences 4 Commonly Used Escape Sequences • Example System.out.println(“The string \”sunny\” contains five character”); The string “sunny” contains five characters Page 5 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Formatting Output with printf System.out.printf("Hello there!"); Consists of only the format string and the statement: System.out.printf("There are %.2f inches in %d centimeters.%n",centimeters / 2.54, centimeters); Suppose the value of centimeter is 150, the output is: • Consists of both the format string and argumentList. • %.2f and %d are called format specifiers. By default, there is a one-to-one correspondence between format specifiers and the arguments in argumentList. • The first format specifier, %.2f, is matched with the first argument, which is the expression centimeters / 2.54. • 6 Formatting Output with printf • The second format specifier, %d, is matched with the • • • • second argument, which is centimeters. The format specifier %n positions the insertion point at the beginning of the next line. If centimeters = 150 150/2.54 =59.05511811023 The o/p would be : There are 59.06 inches in 150 centimeters The output of a printf statement is right-justified by default. To force the output to be left-justified, negative column widths may be used. 7 Example1 public class Example3_6 { public static void main (String[] args) { int num = 763; double x = 658.75; String str = "Java Program."; System.out.println("123456789012345678901234567890"); System.out.printf ( "%5d%7.2f%15s%n", num, x, str); } } System.out.printf ("%15s%6d%9.2f %n", str, num, x); 8 Example1 Page 9 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Example2 public class Example3_7 { public static void main (String[] args) { int num = 763; double x = 658.75; String str = "Java Program."; System.out.println("123456789012345678901234567890"); System.out.printf("%-5d%-7.2f%-15s ***%n", num, x, str); System.out.printf("%-15s%-6d%-9.2f ***%n", str, num, x); } } 10 Example2 11 Formatting Output with printf 12 Standard Input Page 13 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Standard Input • To input primitive data values, we use the Scanner class. • 4 steps are needed to be able to use input primitive: – Step 1: import the Scanner class: • import Java.util.Scanner; – Step 2 : declaring a reference variable of a Scanner • Scanner read ; //we named the object read – Step 3: creating an instance of the Scanner • read = new Scanner (System.in); – Step 4: use specific methods to enter data • int x = read.nextInt(); Page 14 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Example 1 import java.util.Scanner; 2 Scanner input ; // declaring the reference variable of a Scanner 3 int area ,length, width; // declaring variables to store entries 4 input = new Scanner (System.in); // creating an instance 5 length = input.nextInt(); //reading the length from the keyboard 6 width = input.nextInt(); //reading the width from the keyboard 7 area = length * width ; // computing the area // displaying the result 8 System.out.println(“the legnth is ”+ lenght); 9 System.out.println(“the width is ”+ width); 10 System.out.println(“the area is ”+ area); Page 15 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Common Scanner Methods • Method Example Scanner input = new Scanner (System.in); nextByte( ) byte b = input.nextByte( ); nextDouble( ) double d = input.nextDouble( ); nextFloat( ) float f = input.nextFloat( ); nextInt( ) int i = input.nextInt( ); nextLong( ) long l = input.nextLong( ); nextShort( ) short s = input.nextShort( ); next() String str = input.next(); Page 16 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Input • Reading a Single Character if ch is a char variable. To input A into ch, you can use the following statement: ch = input.next().charAt(0); 17