0% found this document useful (0 votes)
19 views10 pages

INFT231101081 Oop Lab 2

The document discusses Java variables, data types, operators and provides examples of using them. It defines Java's primitive data types and gives tasks to practice using variables, operators, and data types. The tasks include calculating light distance, using compound operators, evaluating expressions, and validating user input.

Uploaded by

Muhammad Haziq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views10 pages

INFT231101081 Oop Lab 2

The document discusses Java variables, data types, operators and provides examples of using them. It defines Java's primitive data types and gives tasks to practice using variables, operators, and data types. The tasks include calculating light distance, using compound operators, evaluating expressions, and validating user input.

Uploaded by

Muhammad Haziq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JAVA VARIABLES, DATA TYPES &

OPERATORS
Lab-02

Submitted By: Abdul Samad.


Submitted To: Ms. Kiran Yaqoob.
Roll no: INFT231101081.

LAB 02 Java Variables, Datatypes and Operators

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

The Primitive Types:


Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean.
The primitive types are also commonly referred to as simple types. These can be put in four groups:
Integers: This group includes byte, short, int, and long, which are for whole-valued signed
numbers.
Floating-point numbers: This group includes float and double, which represent numbers with
fractional precision.
Characters: This group includes char, which represents symbols in a character set, like letters and
numbers.
Boolean: This group includes boolean, which is a special type for representing true/false values.

TASK 1: Compute distance light travels in number of days.


Write a class named as “Light”. Write main function in that class which defines variables:
lightspeed, days, seconds and distance. The approximate value of “lightspeed” is 186000. Specify
number of days e.g. days = 365. Now calculate distance by using following formula: distance =
lightspeed * seconds
seconds can be calculated from number of days by using following formula:
seconds = days * 24 * 60 * 60 Output of the program should be:
In 365 days light will travel about 16070400000000 miles.
Arithmetic Compound Assignment Operators
Java provides special operators that can be used to combine an arithmetic operation with an
assignment. As you probably know, statements like the following are quite common in
programming: a = a + 4;
In Java, you can rewrite this statement as shown here: a
+= 4;
This version uses the += compound assignment operator. Both statements perform the same action:
they increase the value of a by 4.

TASK 2: Change the following program to use compound assignments:


class ArithmeticDemo {

public static void main (String[] args){

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);

}
}

TASK 3: Run the following program to find output.


class PrePostDemo {
public static void main(String[] args){
int i = 3; i++;
System.out.println(i);
++i;
System.out.println(i);
System.out.println(++i);
System.out.println(i++);
System.out.println(i);
}
}

TASK 4: Rewrite following expression in form of ternary expression

if (expression) {
number = 10;
} else {
number = -10;
}

HINT: Variable x = (expression) ? value if true : value if false

Answer: number = (expression)? 10: -10;

TASK 5: (Credit Limit Calculator) Develop a Java application that determines


whether any of several department-store customers has exceeded the credit
limit on a charge account. For each customer, the following facts are available:
a) account number
b) balance at the beginning of the month
c) total of all items charged by the customer this month
d) total of all credits applied to the customer’s account this month
e) allowed credit limit.

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 2: What is different between float and double datatypes?


Float and double both data types used to represent floating-point numbers in java .
The main difference is the precision and range the can handle. If you need more
precise decimal number you will use double and if you need less precise number you
will use floating point number.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy