INFT231101081 Oop Lab 2
INFT231101081 Oop Lab 2
OPERATORS
Lab-02
Lab Objectives:
1. Understand Java variables declaration and initialization
2. Understanding java datatypes and their usage
3. Use of operators in java
Software Required:
JDK & Notepad
int result = 1 + 2;
System.out.println(result);
result = result - 1;
System.out.println(result);
result = result * 2;
System.out.println(result);
result = result / 2;
System.out.println(result);
result = result + 8;
result = result % 7;
System.out.println(result);
}
}
if (expression) {
number = 10;
} else {
number = -10;
}
The program should input all these facts as integers, calculate the new balance (= beginning balance
+ charges – credits), display the new balance and determine whether the new balance exceeds the
customer’s credit limit. For those customers whose credit limit is exceeded, the program should
display the message "Credit limit exceeded".
Scanner Class
• There are various ways to read input from the keyboard, the java.util.Scanner class is one
of them.
• The Java Scanner class breaks the input into tokens using a delimiter that is whitespace by
default. It provides many methods to read and parse various primitive values.
• Java Scanner class is widely used to parse text for string and primitive types using regular
expression.
TASK 6: Take following line as input using Scanner class and then read them
using appropriate functions. Also print the data after reading
"10 tea 20 coffee 40.05 liters milk 30 tea biscuits"
TASK 7: User Input Validation: For any input, if the value entered is other than
1 or 2, keep looping until the user enters a correct value. Use Scanner class for
input.
QUESTIONS
Please Fill the blank space with respective answers to following questions: Question
1: Fill the following table
TYPE DEFAULT VALUES
0
byte
0
short
0
int
0L
long
0.0f
float
0.0
double
char
‘\u0000’
False
boolean
Question 3: How can we create object of the Scanner class? Write line of code.
Scanner scanner =new Scanner (System.in);
Question 4: What are the default values of java primitive datatypes. Fill the
following table.
TYPE DEFAULT VALUES
0
Byte
0
Short
0
Int
0L
Long
Float 0.0f
0.0
Double
‘\u0000’
Char
False
Boolean
Question 5: What is difference between i++ and ++i where i is a variable? please
explain your answer with example.
i++ is a post-increment and ++i is a pre-increment . For example: int i =5; int result = ++i; so answer will
be 6 because i is used as post-fix same as: int i =5; int result = i++; so,answer will be 5 because i is used as
prefix then it will incremented b 1 and become 6.
THE END