0% found this document useful (0 votes)
5 views13 pages

Java Programs 1

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)
5 views13 pages

Java Programs 1

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

Date: - 28/07/25

Ques 1- Write a Java program to demonstrate the concept of class and object.

// Class definition
class Car {
// Data members (variables)
String color;
int speed;

// Method
void displayInfo() {
System.out.println("Car color: " + color);
System.out.println("Car speed: " + speed + " km/h");
}
}

// Main class to run the program


public class Main {
public static void main(String[] args) {
// Creating an object of class Car
Car myCar = new Car();

// Assigning values to the object's variables


myCar.color = "Red";
myCar.speed = 120;

// Calling method using object


myCar.displayInfo();
}
}

Output:-
Car color: Red
Car speed: 120 km/h
Ques 2: - Write a Java program to demonstrate the difference between
instance variables and local variables.
// Class to demonstrate instance and local variables
class Student {
// Instance variables (declared inside class, but outside methods)
String name;
int age;

// Method using local variables


void displayDetails() {
// Local variable (declared inside the method)
String college = "ABC College";

// Printing instance variables


System.out.println("Student Name: " + name);
System.out.println("Student Age: " + age);

// Printing local variable


System.out.println("College Name: " + college);
}
}

public class Main {


public static void main(String[] args) {
// Creating object
Student s1 = new Student();

// Assigning values to instance variables


s1.name = "John";
s1.age = 21;

// Calling method
s1.displayDetails();
}
}

Output:-
Student Name: John
Student Age: 21
College Name: ABC College
Ques 3: - Write a Java program to demonstrate the use of all 8 primitive data
types in Java.

public class DataTypesExample {


public static void main(String[] args) {
// Integer types
byte b = 100; // 1 byte, range: -128 to 127
short s = 10000; // 2 bytes, range: -32,768 to 32,767
int i = 100000; // 4 bytes
long l = 10000000000L; // 8 bytes (L is required)

// Floating-point types
float f = 5.75f; // 4 bytes (f is required)
double d = 19.99; // 8 bytes

// Character type
char c = 'A'; // 2 bytes, stores a single character

// Boolean type
boolean isJavaFun = true; // 1 bit (true or false)

// Printing all values


System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("int: " + i);
System.out.println("long: " + l);
System.out.println("float: " + f);
System.out.println("double: " + d);
System.out.println("char: " + c);
System.out.println("boolean: " + isJavaFun);
}
}

Output:-
byte: 100
short: 10000
int: 100000
long: 10000000000
float: 5.75
double: 19.99
char: A
boolean: true
Ques 4 Write a Java program to demonstrate the use of all major types of
operators in Java, including Arithmetic, Relational, Logical, Bitwise,
Assignment, Unary, and Ternary operators.

public class OperatorsExample {


public static void main(String[] args) {
// 1. Arithmetic Operators
int a = 10, b = 5;
System.out.println("Arithmetic Operators:");
System.out.println("a + b = " + (a + b)); // 15
System.out.println("a - b = " + (a - b)); // 5
System.out.println("a * b = " + (a * b)); // 50
System.out.println("a / b = " + (a / b)); // 2
System.out.println("a % b = " + (a % b)); // 0

// 2. Relational (Comparison) Operators


System.out.println("\nRelational Operators:");
System.out.println("a == b: " + (a == b)); // false
System.out.println("a != b: " + (a != b)); // true
System.out.println("a > b: " + (a > b)); // true
System.out.println("a < b: " + (a < b)); // false
System.out.println("a >= b: " + (a >= b)); // true
System.out.println("a <= b: " + (a <= b)); // false

// 3. Logical Operators
boolean x = true, y = false;
System.out.println("\nLogical Operators:");
System.out.println("x && y: " + (x && y)); // false
System.out.println("x || y: " + (x || y)); // true
System.out.println("!x: " + (!x)); // false

// 4. Bitwise Operators
System.out.println("\nBitwise Operators:");
System.out.println("a & b = " + (a & b)); // 0
System.out.println("a | b = " + (a | b)); // 15
System.out.println("a ^ b = " + (a ^ b)); // 15
System.out.println("~a = " + (~a)); // -11

// 5. Assignment Operators
int c = 20;
System.out.println("\nAssignment Operators:");
c += 5; // c = c + 5
System.out.println("c += 5: " + c); // 25
c *= 2; // c = c * 2
System.out.println("c *= 2: " + c); // 50

// 6. Unary Operators
System.out.println("\nUnary Operators:");
int p = 5;
System.out.println("++p = " + (++p)); // 6 (pre-increment)
System.out.println("p-- = " + (p--)); // 6 (then p becomes 5)
System.out.println("p = " + p); // 5

// 7. Ternary Operator
System.out.println("\nTernary Operator:");
int max = (a > b) ? a : b;
System.out.println("Max of a and b is: " + max); // 10
}
}
Output:-
Arithmetic Operators:
a + b = 15
a-b=5
a * b = 50
a/b=2
a%b=0

Relational Operators:
a == b: false
a != b: true
a > b: true
a < b: false
a >= b: true
a <= b: false

Logical Operators:
x && y: false
x || y: true
!x: false

Bitwise Operators:
a&b=0
a | b = 15
a ^ b = 15
~a = -11

Assignment Operators:
c += 5: 25
c *= 2: 50

Unary Operators:
++p = 6
p-- = 6
p=5

Ternary Operator:
Max of a and b is: 10
Ques 5 Write a Java program to demonstrate the use of all conditional
statements in Java including if, if-else, if-else-if ladder, nested if, and switch
statement.

public class ConditionalStatementsExample {


public static void main(String[] args) {
int number = 10;

// 1. if statement
if (number > 0) {
System.out.println("The number is positive.");
}

// 2. if-else statement
if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}

// 3. if-else-if ladder
if (number < 0) {
System.out.println("The number is negative.");
} else if (number == 0) {
System.out.println("The number is zero.");
} else {
System.out.println("The number is positive.");
}

// 4. Nested if statement
if (number > 0) {
if (number < 100) {
System.out.println("The number is positive and less than 100.");
}
}

// 5. switch statement
int day = 2;
System.out.println("Day " + day + " of the week is:");
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Another day");
}
}
}

Output:-

The number is positive.


The number is even.
The number is positive.
The number is positive and less than 100.
Day 2 of the week is:
Tuesday

You might also like