Practical No.
//Name:Shreya Krishnat Lad
//Roll.No:66
//Batch:C
Q1) write a program to cheak multiple condition using if statement along with logical operators
import java.io.*;
public class NumberRangeCheck
public static void main(String[] args)
int number = 25;
if (number > 0 && number < 50)
System.out.println("Number is between 1 and 49.");
else if (number <= 0 || number >= 100)
System.out.println("Number is out of range.");
else
System.out.println("Number is between 50 and 99.");
Output:
//Q2. Write a program to check no is even or odd.
import java.util.Scanner;
public class EvenOdd
public static void main(String[] args)
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0)
System.out.println(number + " is even.");
else
System.out.println(number + " is odd.");
scanner.close();
Output:
//Q3. Write a program to check switch-case using character datatype.
import java.util.Scanner;
public class GradeChecker
public static void main(String[] args)
Scanner scanner = new Scanner(System.in);
System.out.print("Enter grade (A-F): ");
char grade = scanner.next().charAt(0);
switch (grade)
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Very Good!");
break;
case 'C':
System.out.println("Good.");
break;
case 'D':
System.out.println("Passed.");
break;
case 'F':
System.out.println("Failed.");
break;
default:
System.out.println("Invalid grade.");
}
scanner.close();
Output:
//Q4. Write a program to display 1 to 20 numbers using for, while and do-while loop.
import java.io.*;
public class DisplayNumbers
public static void main(String[] args)
System.out.println("Using for loop:");
for (int i = 1; i <= 20; i++) {
System.out.println(i);
System.out.println("\nUsing while loop:");
int i = 1;
while (i <= 20) {
System.out.println(i);
i++;
System.out.println("\nUsing do-while loop:");
i = 1;
do {
System.out.println(i);
i++;
} while (i <= 20);
}
Output:
//Q5. Develop a program to use logical operators in do-while loop.
import java.io.*;
public class EvenOddCheck
public static void main(String[] args)
int i = 1;
do
if (i % 2 == 0 && i > 10) {
System.out.println(i + " is even and greater than 10.");
i++;
} while (i <= 20);
Output:
Practical No. 4
//Name:Shreya Krishnat Lad
//Roll.No:66
//Batch:C
//1. Write a program to show the use of all methods of String class.
import java.io.*;
import java.util.*;
class Methods
public static void main(String args[])
String str=new String("Java Programming");
System.out.println("Length:"+str.length());
System.out.println("Uppercase:"+str.toUpperCase());
System.out.println("Lowercase:"+str.toLowerCase());
System.out.println("Character At 5:"+str.charAt(5));
System.out.println("Replaced string:"+str.replace('J','C'));
System.out.println("Trimed string:"+str.trim());
System.out.println("Are Java Programming & java programming equals:"+str.equals("java programming"));
System.out.println("Are Java Programming & java programming equals:"+str.equalsIgnoreCase("java
programming"));
System.out.println("Concatenated string:"+str.concat("Step"));
System.out.println("Index of a:"+str.indexOf('a'));
System.out.println("Index of second a of string:"+str.indexOf('a',2));
System.out.println("Substring from 2:"+str.substring(2));
System.out.println("Substring from 2 to 6:"+str.substring(2,6));
}
Output:
//Q.2Write a program to implement all methods of StringBuffer class.
import java.io.*;
import java.util.*;
class Methods1
public static void main(String args[])
StringBuffer str=new StringBuffer("Jav");
System.out.println("Length:"+str.length());
str.setLength(4);
System.out.println("Length:"+str.length());
str.setCharAt(3,'a');
System.out.println("String:"+str);
System.out.println("Appended string:"+str.append(" programming language"));
System.out.println("New String="+str.insert(5,"is "));
System.out.println("String after reverse:"+str.reverse());
System.out.println(" String after Replace:"+str.replace(5,8,"***"));
Output:
Practical No.7
//Name:Shreya Krishnat Lad
//Roll.No:66
//Batch:C
//Q5. Write a program to implement different types of constructors to perform addition of complex numbers?
import java.io.*;
class Complex
double real;
double imaginary;
public Complex(double r,double i)
real=r;
imaginary=i;
public static Complex add( Complex n1,Complex n2)
{
Complex obj=new Complex(0,0);
obj.real=n1.real+n2.real;
obj.imaginary=n1.imaginary+n2.imaginary;
return obj;
public void show()
System.out.println(real+"+"+imaginary+"i");
public static void main(String args[])
Complex c1=new Complex(5,2);
Complex c2=new Complex(10,6);
System.out.println("first complex number:");
c1.show();
System.out.println("second complex number:");
c2.show();
Complex obj=add(c1,c2);
System.out.println("Addition is:");
obj.show();
Output :
Practical no 8
//Name:Shreya Krishnat Lad
//Roll.No:66
//Batch:C
//Q.1. Write a program to implement single inheritance.
import java.io.*;
class Student
void Fee()
System.out.println("Student Fee= 20000");
void Roll_No()
System.out.println("Student Roll No =66");
}
}
class Student_Name extends Student
void Name()
System.out.println("Student Name= Shreya Lad");
class College
public static void main(String args[])
Student_Name n = new Student_Name();
n.Name();
n.Roll_No();
n.Fee();
Output:
//Q.2. Write a program to implement multilevel inheritance
//Q. 3. Develop a program to implement the multilevel inheritance
import java.io.*;
class College
public void display()
System.out.println("Name of College: K.P.PATIL INSTITUTE OF TECHNOLOGY");
class Department extends College
public void show1()
{
System.out.println("Name of Department: Computer Engineering");
class Student extends Department
public void show2()
System.out.println("Studying year of Student: Second");
System.out.println("Name of Student: Shreya Lad");
public class Data
public static void main(String[] arguments)
Student std = new Student();
std.display();
std.show1();
std.show2();
Output:
Q. 4. Develop a program to calculate he room area and volume to illustrate the concept of
single inheritance.
import java.io.*;
class Room
int length,width;
Room(int a,int b)
length = a;
width = b;
void area()
{
int area = length*width;
System.out.println("The area of the room is " +area);
class Roomvol extends Room
int height;
Roomvol(int a,int b,int c)
super(a,b);
height = c;
void volume()
int volume = length*width*height;
System.out.println("The volume of the room is " +volume);
class Roomav
public static void main(String args[])
Roomvol room = new Roomvol(12,32,20);
room.area();
room.volume();
Output: