0% found this document useful (0 votes)
14 views7 pages

6

Uploaded by

rohitm04122002
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)
14 views7 pages

6

Uploaded by

rohitm04122002
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/ 7

LAB 6

Aim: Write a java program to illustrate the use of various primitive data types.

Theory: Java is statically typed and also a strongly typed language because, in Java, each type of data (such as
integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming
language and all constants or variables defined for a given program must be described with one of the Java
data types. Primitive Data Types: such as boolean, char, int, short, byte, long, float, and double.

Algorithm:

Step 1: Start.

Step 2: Create a public class ‘PrimitiveDataTypes’.

Step 3: Declare variables using all primitive data types.

Step 4: Assign value to each variable.

Step 5: Print all the primitive data type’s variable output.

Step 6: End.

Source Code:

public class PrimitiveDataTypes {

public static void main(String[] args) {

byte byteVar = 127;

short shortVar = 32000;

int intVar = 2000000000;

long longVar = 9000000000000000000L;

float floatVar = 3.14f;

double doubleVar = 3.14159;

char charVar = 'A';

boolean boolVar = true;

System.out.println("Byte variable: " + byteVar);

System.out.println("Short variable: " + shortVar);

System.out.println("Int variable: " + intVar);

System.out.println("Long variable: " + longVar);

System.out.println("Float variable: " + floatVar);

System.out.println("Double variable: " + doubleVar);

System.out.println("Char variable: " + charVar);

System.out.println("Boolean variable: " + boolVar);

Output:
Byte variable: 127

Short variable: 32000

Int variable: 2000000000

Long variable: 9000000000000000000

Float variable: 3.14

Double variable: 3.14159

Char variable: A

Boolean variable: true

Aim: Write a java program to illustrate automatic type promotion.

Theory: The name Type Promotion specifies that a small size datatype can be promoted to a large size
datatype. i.e., an Integer data type can be promoted to long, float, double, etc. This Automatic Type
Promotion is done when any method which accepts a higher size data type argument is called with the smaller
data type.

Algorithm:

Step 1: Start.

Step 2: Create a public class ‘TypePromotionExample’.

Step 3: Declare variables and assign values.

Step 4: Display the result.

Step 5: End.

Source Code:

public class TypePromotionExample {

public static void main(String[] args) {

byte byteVar = 10;

short shortVar = 100;

int intVar = 1000;

long longVar = 10000L;

float floatVar = 3.14f;

double doubleVar = 3.14159;

int result1 = byteVar * shortVar;

long result2 = intVar * longVar;

float result3 = floatVar * result1;

double result4 = doubleVar * result2;

System.out.println("Result 1: " + result1);


System.out.println("Result 2: " + result2);

System.out.println("Result 3: " + result3);

System.out.println("Result 4: " + result4);

Output:

Result 1: 1000

Result 2: 10000000

Result 3: 3140.0

Result 4: 3.14159E7
Aim: Write a java program to illustrate explicit type casting.

Theory: In Java, type casting is a method or process that converts a data type into another data type in both
ways manually and automatically. The automatic conversion is done by the compiler and manual conversion
performed by the programmer.

Algorithm:

Step 1: Start.

Step 2: Create a public class ‘TypeCasting’.

Step 3: Declare two variables and assign values.

Step 4: Perform explicit type casting from double to int.

Step 5: Display the output.

Step 6: End.

Source Code:

public class TypeCasting {

public static void main(String[] args) {

double doubleVar = 3.14;

int intVar;

intVar = (int) doubleVar;

System.out.println("Original double value: " + doubleVar);

System.out.println("After explicit casting to int: " + intVar);

Output:

Original double value: 3.14

After explicit casting to int: 3


Aim: Write a java program to illustrate the use of various escape sequences.

Theory: In Java, if a character is preceded by a backslash (\) is known as Java escape sequence or escape
characters. It may include letters, numerals, punctuations, etc. Remember that escape characters must be
enclosed in quotation marks (""). These are the valid character literals. The Java compiler interprets these
characters as a single character that adds a specific meaning to the compiler.

Algorithm:

Step 1: Start.

Step 2: Create a public class ‘EscapeSequencesExample’.

Step 3: Print a text using all escape sequences.

Step 4: End.

Source Code:

public class EscapeSequenceExample {

public static void main(String[] args) {

System.out.println("Hello\tWorld"); // Tab

System.out.println("Hello\nWorld"); // Newline

System.out.println("Hello\rWorld"); // Carriage return

System.out.println("Hello\bWorld"); // Backspace

System.out.println("Hello\fWorld"); // Form feed

System.out.println("Hello\"World"); // Double quote

System.out.println("Hello\'World"); // Single quote

System.out.println("Hello\\World"); // Backslash

Output:

Hello World

Hello

World

World

HellWorld

Hello

World

Hello"World

Hello'World

Hello\World
Aim: Write a java program to illustrate the application of modulus(%) operator. Suppose, your problem is to
display the current time at this moment in Indian Standard Time (IST).

Algorithm:

Step 1: Start.

Step 2: Obtain the total milliseconds since midnight, January 1, 1970, intotalMilliseconds
by invoking System.currentTimeMillis().

Step 3: Obtain the total seconds totalSeconds by dividing totalMilliseconds by 1000.

Step 4: Compute the current second from totalSeconds % 60.

Step 5: Obtain the total minutes totalMinutes by dividing totalSeconds by 60.

Step 6: Compute the current minute from totalMinutes % 60.

Step 7: Obtain the total hours totalHours by dividing totalMinutes by 60.

Step 8: Compute the current hour from totalHours % 24.

Step 9: Add an offset of 5 hours 30 minutes to obtain the current time in IST (since IST is 5 hours 30
minutes ahead of GMT).

Step 10: End.

Source Code:

import java.util.TimeZone;

public class CurrentTimeIST {

public static void main(String[] args) {

long totalMilliseconds = System.currentTimeMillis();

long totalSeconds = totalMilliseconds / 1000;

long currentSecond = totalSeconds % 60;

long totalMinutes = totalSeconds / 60;

long currentMinute = totalMinutes % 60;

long totalHours = totalMinutes / 60;

long currentHour = totalHours % 24;

currentHour += 5;

currentMinute += 30;

if (currentMinute >= 60) {

currentMinute -= 60;

currentHour += 1;

if (currentHour >= 24) {

currentHour -= 24;
}

System.out.printf("Current time in IST: %02d:%02d:%02d", currentHour, currentMinute, currentSecond);

Output:

Current time in IST: 20:40:44

You might also like