Chapter 4 Input Output Expressions and Java Operators

19 Slides1.12 MB

Chapter 4 Input Output Expressions and Java Operators

In This Chapter you will learn Use the classes intended for Input and output Create Java Expressions Performs Computations Understand the use of Java Operators. Know the rule of operator precedence Identify the different Math class methods

Input and Output Input and output, or I/O is the communication between an information processing system, such as a computer, and the outside world, possibly a human or another information processing system.

Inputs are the signals or data received by the system and outputs are the signals or data sent from it

Scanner Class A class in the java.util package. The methods of this class are used to read data from the standard input device and store data into variables.

Methods of the scanner class Method nextDouble() Description Retrieves input as a double Example Double x objectName.nextDouble(); nextInt() nextLine() Retrieves input as an int int x objectName.nextInt(); Retrieves the next line of data String x objectName.nextLine(); and returns it as a String next() Retrieves the next complete String x objectName.next(); token as a String nextShort() Retrieves input as a short short x objectName.nextShort(); nextByte() Retrieves input as a byte bytex objectName.nextByte(); nextFloat() Retrieves input as a float float x objectName.nextFloat(); nextLong() Retrieves input as n long longx objectName.nextLong();

Printing Data with System.out Standard output is a stream designed to display text output onscreen. When you run a Java program under Windows, a special console window opens, and the standard output stream is connected to it.

The print () method Displays an output, and the insertion point stays in the current line.

The println () method moves the insertion point to the following line after the output is displayed.

The printf () method used to create an output in a specific format

Format used for the printf method Format Type of output Example Specifier %c Character A single character: %c A single character in a field of two (2) spaces: %2c %d Decimal integer number An integer %f Floating-point number A floating-point number: %f With 2 digits after the decimal point: %.2f With 2 digits after the decimal in a field of 6 spaces: %6.2f %e Exponential floating-point number A floating-point number in exponential format: %e %s String A string formatted to a field of 10 spaces: %10s With first 2 characters of the string: %.2s

Java Operators Arithmetic Operators Relational Operators(Comparison) Logical Operators Bitwise Operators Assignment Operator

Categorizing operators by the number of operands Unary operators Binary operators Ternary operators

The Math Class methods that perform a wide variety of mathematical calculations, from basic functions such as calculating an absolute value or a square root to trigonometry functions such as sin and cos (sine and cosine), to practical functions such as rounding numbers or generating random numbers.

Constants of the Math class Math Class constant Constant What it is Value PI The constant pi (π), the ratio of a circle’s 3.14159265358979 radius and diameter 3 The base of natural logarithms 2.71828182845904 E 5

Category of Math class method Mathematical functions Rounding Functions

Operator Precedence Operator precedence determines the order in which expressions are evaluated. This, in some cases, can determine the overall value of the expression

Table 4.11 Operator Precedence Operator Notes . [] () Parentheses () group expressions; dot (.) is used for access to methods and variables within objects and classes; [] is used for arrays –– ! instanceof Returns true or false based on whether the object is an instanceof the named class or any of that class’s super classes new (type)expression The new operator is used for creating new instances of classes; () in this case is for casting a value to another type. * % Multiplication, division, modulus – Addition, subtraction Bitwise left and right shift Relational comparison tests ! Equality & AND XOR OR && Logical AND Logical OR ?: Shorthand for if. then. else – * % Various assignments

Congratulations. Well done

Back to top button