Comprog Midterms Review
Comprog Midterms Review
Two rules to apply when evaluating mixed “Scanner input” declares an object of
expression: Scanner type named “input”. Object
name can be any declared name by a
Rule no. 1 - If the operator has the same programmer.
types of operands (either all integers or “new Scanner(System.in)” creates a
all floating-point numbers), the operator Scanner object connected to the
is evaluated according to the type of the System.in property. System.in is an
operands. Example: inputStream which is connected to the
25 / 2 * 2.0 //evaluation: 12 * 2.0 = 24.0 standard input device (the keyboard).
25.0 / 2.0 * 2 //evaluation: 12.5 * 2 =
25.0 The Scanner class is used to get values from an
Rule no. 2 - If the operator has both input device. Each value obtained is called a
types of operands (mixing integers and "token," which is a group of characters
floating-point numbers), during the separated by spaces.
calculation, the integer is temporarily Table 1 lists common methods of the Scanner
treated as a floating-point number. For class for reading various data types from the
example: keyboard. They fetch values you type, but if it's
25.0 / 2 * 2 //evaluation: 12.5 * 2 = 25.0 not the expected data type, they trigger an
LOGICAL EXPRESSION – these expressions error.
returns bolean value. Can consist of logical METHOD DESCRIPTION EXAMPLE
operators and relational expressions. For Retrieves
double x =
nextDouble() input as a
example: 14 >= 5 && 12 != 3 evaluates to true. objectName.nextDouble();
double
Retrieves int x =
nextInt()
If the expression has 2 or more logical input as an int objectName.nextInt();
operators, it is evaluated from left to right. Retrieves the
next line of
Example: 3 > 3 || 4 < 2 && 12 > 3 || 4 < 5 String x =
nextLine() data and
objectName.nextLine();
evaluates to true returns it as
a String
Parenthesis are used to group expression and Retrieves the
next complete String x =
control operators. Example: next()
token as a objectName.next();
String
(3 > 3 || 4 < 2) && (12 > 3 || 4 < 5) evaluates to Retrieves
short x =
false. nextShort() input as a
objectName.nextShort();
short
Retrieves byte x =
nextByte()
input as a byte objectName.nextByte();
Retrieves float x =
nextFloat()
input as a float objectName.nextFloat();
Retrieves long x =
nextLong()
input as a long objectName.nextLong();
The Scanner class doesn't have a "next" System.out.printf(“Price using print formatting:
method for characters. To get a single %.2f”, price); //result: Price using printf
character from the keyboard, use "next()" to formatting: 19.50
get a String, then access the first character with
With "printf," you format a double with 2
"charAt(0)." For example,
decimal places. "%.2f" is a format specifier,
"input.next().charAt(0)" gets the first character
like a placeholder for the number. "%.2f" says
of what you typed.
to show the number with 2 digits after the
char x = s.next().charAt(0); //gets the decimal point, and the "f" means it's a floating-
first character
point number.
The "next()" function gives the next word as a
string, and "charAt(0)" returns the first FORMAT
TYPE OF
character of that word. The "0" in "charAt(0)" is SPECIFIE EXAMPLE
OUTPUT
the position of the character within the word, R
and you can store it in a char variable. Single character: %c
Note: Sa math natin meron tayong nth term sa sequence %c Character A single character in
of numbers. Yung nth term natin is tinatawag na index sa a field of two (2)
programming. Yung first term or first index natin sa spaces: %2c
programming is ‘0’, kasi sa programming, nag start lagi sa Decimal
‘0’. Example: sa Input na “BSIT”, yung index 0 natin is %d integer An integer
letter ‘B’, then index 1 is ‘S’. number
Output Statements – used to send output to A floating-point
the standard output device, the display screen. number: %f
Example: System.out.print(“Hello”); and With 2 digits after
Floating-
the decimal point:
System.out.println(“Hello”); %f point
%.2f
number
print() method shows output on the With 2 digits after
same line. the decimal in a field
println() moves to the next line after of 6 spaces: %6.2f
displaying the output. Exponential A floating-point
floating- number in
%e
Concatenation - joining or combining pieces of point exponential format:
text (strings) to create a longer piece of text. number %e
A string formatted to
When displaying a string with variables, simply a field of 10 spaces:
concatenate them using the plus (+) sign. For %10s
example: %s String
With first 2
characters of the
String name = “Jess Diaz”;
string: %.2s
System.out.println(“Hello! My name is ” + name
+ “.”);
printf() method lets you format your output,
like controlling decimal places. It's like
customizing how a number looks when you
print it.
double price = 19.5;
System.out.println(“Price using println: ” +
price); // result: Price using println: 19.5