[go: up one dir, main page]

0% found this document useful (0 votes)
12 views47 pages

file

The document is a project file for Object Oriented Programming in Java submitted by a student at Indira Gandhi Delhi Technical University for Women. It includes an index of various lab experiments and programming assignments covering topics like basic operations, control statements, file handling, exception handling, and class creation. Each section outlines specific programming tasks with corresponding outputs and examples of Java code implementations.

Uploaded by

tanu144btcsai22
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)
12 views47 pages

file

The document is a project file for Object Oriented Programming in Java submitted by a student at Indira Gandhi Delhi Technical University for Women. It includes an index of various lab experiments and programming assignments covering topics like basic operations, control statements, file handling, exception handling, and class creation. Each section outlines specific programming tasks with corresponding outputs and examples of Java code implementations.

Uploaded by

tanu144btcsai22
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/ 47

Indira Gandhi Delhi Technical University for Women

(Established by Govt. of Delhi vide Act 09 of 2012)


Kashmiri Gate, Delhi – 110006

Project File

OBJECT ORIENTED PROGRAMMING - JAVA


OOPJ
(BAI-102)

Submitted to: Submitted by:


Mrs. Shweta Tanu Raman
14401172022
2nd Semester
Index

S. no. Experiment Date Signature

1. Lab – 1 : Basic questions 23.03.23

2. Lab – 2 : Pattern and loop 29.03.23

3. Lab – 3: Switch statement 06.04.23

4. Lab – 4 : Calendar and matrix 26.04.23

5. Lab – 5 : Constructor 03.05.23

6. Lab – 6 : Inheritance 21.05.23

7. Lab – 7 : Constructor and overriding 31.05.23

8. Lab – 8 : File handling 07.06.23

9. Lab – 9 : Exception handling 14.06.23


Q1. WAP to print whether a given number is even or odd.

Output:

Q2. WAP to swap two numbers without taking a 3rd variable.


Output:

Q3. WAP to find a maximum of 3 numbers.

Output:
Q4. WAP to print the grace of a student as per given marks
‘A’ for marks>80
‘B’ marks between 70-80
‘C’ marks between 60-70
‘D’ marks between 50-60
‘F’ for marks less than 50.

Output:
Q5. WAP to find the roots of a quadratic equation.

Output:
Q6. Print (a+(b*c))/(b-c) taking a, b, c as input.

Output:

Q7. Print table of a number taken from the user.


Output:

Q8. Convert rupees, taken as input, to paise.


Output:

Q9. Print following paTern


Output:
Q10. Create calculator.
import java.util.Scanner;

class Ten

{
public static void main(String []args)
{
System.out.println("Calculator");
int a, b, c;
Scanner scanner = new Scanner(System.in);
System.out.println("First value: ");
a = scanner.nextInt();
System.out.println("Second value: ");
b = scanner.nextInt();
System.out.println("Function to be performed (+, -, *, /)");
char op = scanner.next().charAt(0);
if (op == '+')
{
c = a + b;
System.out.print(c);
}
else if (op == '-')
{
c = a - b;
System.out.print(c);
}
else if (op == '*')
{
c = a * b;
System.out.print(c);
}
else if (op == '/')
{
c = a * b;
System.out.print(c);
}
else
{
System.out.println("Can't perform the specified action.");
}
}
}

Output:
Q11. Create calculator using Switch.
import java.util.Scanner;

public class Eleven {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("First value: ");
int a = scanner.nextInt();
System.out.println("Second value: ");
int b = scanner.nextInt();
System.out.println("Enter the operator \n1 for sum \n2 for difference \n3
for product \n4 for division: ");
int op = scanner.nextInt();
int result;
switch (op) {
case 1:
result = a + b;
System.out.println("The sum is: " + result);
break;

case 2:
result = a - b;
System.out.println("The difference is: " + result);
break;

case 3:
result = a * b;
System.out.println("The product is: " + result);
break;

case 4:
if (b != 0) {
result = a / b;
System.out.println("The division is: " + result);
} else {
System.out.println("Error");
}
break;

default:
Output:

Q12. WAP to convert temp from degree Celsius to degree Fahrenheit.

Output:
Q13. WAP to print whether a given number is positive, negative or
zero.

Output:

Q14. WAP to find whether a year is a leap or not.


Output:

Q15. WAP to find whether a given character is a vowel or consonant.


Output:

Q16. WAP to print the name of the month and the number of days for
given no (1-12). E.g.- 3- March 31 days.
import java.util.Scanner;
class Sixteen
{
public static void CheckYear(){
int Year;
System.out.print("Write the year: ");
Scanner scanner = new Scanner(System.in);
Year = scanner.nextInt();
if (Year%4==0){
if (Year%400==0){
System.out.print("February : 29 days");
}
else if (Year%100==0){
System.out.print("February : 28 days");
}
else {
System.out.print("February : 29 days");
}
}
else {
System.out.print("February : 28 days");
}
scanner.close();
}
public static void main(String []args)
{
int Number;
System.out.print("Write a Number between 1 and 12: ");
Scanner scanner = new Scanner(System.in);
Number = scanner.nextInt();
if (Number == 1){
System.out.print("January : 31 days");
}
else if (Number == 2){
CheckYear();
}
else if (Number == 3){
System.out.print("March : 31 days");
}
else if (Number == 4){
Output:
Q17. Create a calendar, enter date to get its previous date, next date,
and day.
import java.util.Scanner;

public class Seventeen {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the date, DD: ");
int day = scanner.nextInt();
System.out.print("Enter the month, MM: ");
int month = scanner.nextInt();
System.out.print("Enter the year, YYYY:
"); int year = scanner.nextInt();

int daysInMonth = 0;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
daysInMonth = 31;
break;
case 4: case 6: case 9: case 11:
daysInMonth = 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
break;
}

int previousDay = day - 1;


int previousMonth = month;
int previousYear = year;
if (previousDay < 1) {
previousMonth--;
if (previousMonth < 1) {
previousYear--;
previousMonth = 12;
}
previousDay = daysInMonth;
}

int nextDay = day + 1;


int nextMonth = month;
int nextYear = year;
if (nextDay > daysInMonth) {
nextMonth++;
if (nextMonth > 12) {
nextYear++;
nextMonth = 1;
}
Output:

Q18. Use 2D array to print matrix, perform addition and


multiplication on it with another matrix.
int[][] matrix2 = new int[rows2][columns2];
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < columns2; j++) {
matrix2[i][j] = scanner.nextInt();
}
}

// Print
System.out.println("First matrix:");
printMatrix(matrix1);
System.out.println("Second matrix:");
printMatrix(matrix2);

// Addition
if (rows1 == rows2 && columns1 == columns2) {
int[][] sumMatrix = new int[rows1][columns1];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns1; j++) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("Sum of matrices:");
printMatrix(sumMatrix);
} else {
System.out.println("Matrices cannot be added.");
}

// Multiplication
if (columns1 == rows2) {
int[][] productMatrix = new int[rows1][columns2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
for (int k = 0; k < columns1; k++) {
productMatrix[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
System.out.println("Product of matrices:");
printMatrix(productMatrix);
} else {
System.out.println("Matrices cannot be multiplied.");
}
}

public static void printMatrix(int[][] matrix) {


for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
Output:
Q19. Create a class triangle having color, type, side 1,2,3, depending
on the type of triangle, call constructor and calculate and print area.

public class Nineteen {


String color;
double side1;
double side2;
double side3;

Nineteen(String color,double side1, double side2, double side3) {


this.color = color;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

double calculateArea() {
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}

void printArea() {
double area = calculateArea();
System.out.println("The area of the triangle is: " + area);
}

void getType(){
if(side1==side2 && side2==side3){
System.out.println("Equilateral");
}
else{
if(side1!=side2 && side2!=side3 && side3!=side1){
System.out.println("Scalene");
}
else{
System.out.println("Isosceles");
}
}

}
public static void main(String[] args) {
// Example usage
Nineteen triangle = new Nineteen("Red", 5.0, 7.0, 8.0);
triangle.printArea();
triangle.getType();
}
}
Output:

Q20. WAP to create a class Account having account no., name,


balance, rate of interest, type (savings and current)
Write code for possible operagons:
1. Create account
2. Check Balance
3. Update Balance
4. Withdraw
5. Deposit
6. Exit
public void withdraw(Float amount) {
if (amount > balance) {
System.out.println("Insufficient balance. Withdrawal failed.");
} else {
balance -= amount;
System.out.println("Withdrawn Amount: " + amount);
System.out.println("Updated Balance: " + balance);
}
}

public void deposit(Float amount) {


balance += amount;
System.out.println("Deposited Amount: " + amount);
System.out.println("Updated Balance: " + balance);
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
Twenty account = null;

int choice;
do {
System.out.println("----- Account Menu ");
System.out.println("1. Create Account");
System.out.println("2. Check Balance");
System.out.println("3. Update Balance");
System.out.println("4. Withdraw");
System.out.println("5. Deposit");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter Account Number: ");
int accountNumber = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Name: ");
String name = scanner.nextLine();
System.out.print("Enter Initial Balance: ");
Float balance = scanner.nextFloat();
System.out.print("Enter Interest Rate: ");
Float interestRate = scanner.nextFloat();
scanner.nextLine(); // Consume the newline character
System.out.print("Enter Account Type (Savings/Current): ");
String accountType = scanner.nextLine();
account = new Twenty(accountNumber, name, balance,
interestRate, accountType);
System.out.println("Account Created Successfully!");
break;

case 2:
if (account != null) {
account.checkBalance();
} else {
System.out.println("No account found");
}
break;

case 3:
if (account != null) {
System.out.print("Enter the
amount to update the
");
balance:

Float amount =
scanner.nextFloat();
account.updateBalance(amoun
t);
} else {
System.out.println("No
account found");
}
break;

case 4:
if (account != null) {
System.out.print("Enter the amount to withdraw: ");
Float amount = scanner.nextFloat();
account.withdraw(amount);
} else {
System.out.println("No account found");
}
break;

case 5:
if (account != null) {
System.out.print("Enter the amount to deposit: ");
Float amount = scanner.nextFloat();
account.deposit(amount);
} else {
System.out.println("No account found");
}
break;

case 6:
System.out.println("Exiting");
break;

default:
System.out.println("Invalid choice");
}

System.out.println();

} while (choice != 6);


scanner.close();
}
}
Output:
Q21. Create base class distance, give private parameter d1. Create
constructors for add, subtract and print. Two derived classes
Dinch(convert d1 to inches) Dmiles( convert d1 to miles), give them
the same constructors. Use override to print and add.
public class TwentyOne {
public static void main(String[] args) {
class Distance {
double d1;

Distance(double d1) {
this.d1 = d1;
}

double getD1() {
return d1;
}

void setD1(double d1) {


this.d1 = d1;
}

void add(Distance other) {


d1 += other.getD1();
}

void subtract(Distance other) {


d1 -= other.getD1();
}

void print() {
System.out.println("Distance: " + d1 + " units");
}
}

class Dinch extends Distance {


Dinch(double d1) {
super(d1);
}

void print() {
System.out.println("Distance: " + getD1() + " inches");
}

void add(Distance other) {


double inches = ((Dinch) other).getD1();
d1 += inches;
}
}

class Dmiles extends Distance {


Output:
Q22. Create an employee class with private members name address
age gender taken as input, use constructor to initialize values to the
variable, display all. Derive employee class into full time employee,
with members salary and designation (display all) and part time
employee with members work hour and rate per hour from which pay
can be calculated (work hour * rate per hour), display pay.
import java.util.Scanner;
class Employee {
String name, address, gender;
int age;

public Employee(String name, String address, int age, String gender) {


this.name = name;
this.address = address;
this.age = age;
this.gender = gender;
}

public void display() {


System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
}
}

class FullTimeEmployee extends Employee {


Float salary;
String designation;

FullTimeEmployee(String name, String address, int age, String gender, Float


salary, String designation) {
super(name, address, age, gender);
this.salary = salary;
this.designation = designation;
}

@Override
public void display() {
super.display();
System.out.println("Salary: " + salary);
System.out.println("Designation: " + designation);
}
}

class PartTimeEmployee extends Employee {


Float workHours;
Float ratePerHour;
PartTimeEmployee(String name, String address, int age, String gender, Float
workHours, float ratePerHour) {
super(name, address, age, gender);
this.workHours = workHours;
this.ratePerHour = ratePerHour;
}

float calculatePay() {
return workHours * ratePerHour;
}

@Override
public void display() {
super.display();
Float pay = calculatePay();
System.out.println("Pay: " + pay);
}
}

public class TwentyTwo{


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter details for Full-Time Employee:");


System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Address: ");
String address = scanner.nextLine();
System.out.print("Age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Gender: ");
String gender =
scanner.nextLine();
System.out.print("Salary: $");
Float salary =
scanner.nextFloat();
scanner.nextLine();
System.out.print("Designation: ");
String designation = scanner.nextLine();
System.out.println();

FullTimeEmployee fullTimeEmployee = new FullTimeEmployee(name, address,


age, gender, salary, designation);
System.out.println("Full-Time Employee Details:");
fullTimeEmployee.display();

System.out.println();

System.out.println("Enter details for Part-Time Employee:");


System.out.print("Name: ");
String pname = scanner.nextLine();
System.out.print("Address: ");
String paddress = scanner.nextLine();
System.out.print("Age: ");er.next;
Output:
Q23. Define a class motor vehicle having members model name,
model no. and model price having method display() to display the
name, no. and price. Define another class Car that inherits the class
Motor vehicle which has the member disRate and methods display()
to display name, no., price, disRate and discount() to compute
discount rate. Create the classes with suitable constructors and
test for 2 objects.
import java.util.Scanner;

class MotorVehicle{

Scanner scan = new Scanner(System.in);


String ModelName;
int ModelNumber;
int ModelPrice;
void read(){
System.out.print("Model Name = ");
ModelName = scan.nextLine();
System.out.print("Model Number = ");
ModelNumber = scan.nextInt();
System.out.print("Model Price = ");
ModelPrice = scan.nextInt();
}
void display(){
System.out.println("Model Name : " + ModelName + " Model Number : " +
ModelNumber + " Model Price : " + ModelPrice);
}
}

class Car extends MotorVehicle{


int disRate;
void read(){
super.read();
System.out.print("Discount Rate = ");
disRate = scan.nextInt();
}
void display(){
super.display();
System.out.println("Discount Rate : " + disRate);
}
void discount(){
System.out.println(ModelPrice - (ModelPrice*disRate/100));
}
}
public class TwentryThree{
public static void main(String[] args) {
MotorVehicle A = new MotorVehicle();
Output:

Q24. Create an interface shape having function read, print and


calcarea with no parameters and return type as void implement the
interface in classes circle , triangle and rectangle. B) create an array of
ten objects instantiate to be of type circle, triangle, rectangle as per
user's choice.
System.out.println("What is the value of radius? ");
r = scan.nextInt();
scan.close();
}
public void print(){
System.out.println("Area of the Circle is : 3.14 * r * r ");
}
public void calArea(){
System.out.println(3.14*r*r);
}

class Triangle implements Shapes{


int b, h;
public void read(){
Scanner scan = new Scanner(System.in);
System.out.println("What is the value of height? ");
h = scan.nextInt();
System.out.println("What is the value of base? ");
b = scan.nextInt();
scan.close();
}
public void print(){
System.out.println("Area of the Triangle is : b*h/2 ");
}
public void calArea(){
System.out.println(b*h/2);
}
}

class Rectangle implements Shapes{


int l, b;
public void read(){
Scanner scan = new Scanner(System.in);
System.out.println("What is the value of length? ");
l = scan.nextInt();
System.out.println("What is the value of breadth? ");
b = scan.nextInt();
scan.close();
}
public void print(){
System.out.println("Area of the Rectangle is : l*b ");
}
public void calArea(){
System.out.println(l*b);
}
}

class TwentyFour{
public static void main(String[] args) {
Output:
Q25. An educational institution wishes to maintain a database of its
employees. The database is divided into several classes whose
hierarchical relationships are shown in the figure below. The figure
also shows the minimum information required for each class. Specify
all the classes and define methods to create the database and retrieve
individual information as and when required. Implement this
application by creating multiple classes and storing them in different
files. Also, write a driver class called college and illustrate the
execution of this application.

class Staff{
String code, name;
Staff(String code, String name){
this.code = code;
this.name = name;
}
void print(){
System.out.println("Code is : " + code);
System.out.println("Name is : " + name);
}
}

class Teacher extends Staff{


String subject, publication;
Teacher(String code, String name, String subject, String publication){
super(code, name);
this.subject = subject;
this.publication = publication;
}
void print(){
super.print();
System.out.println("Subject is : " + subject);
System.out.println("Publication is : " + publication);
}
}

class Typist extends Staff{


int speed;
Typist(String code, String name, int speed){
super(code, name);
this.speed = speed;
}
void print(){
super.print();
System.out.println("Speed is : " + speed);
}
}

class Officer extends Staff{


String grade;
Officer(String code, String name, String grade){
super(code, name);
this.grade = grade;
}
void print(){
super.print();
System.out.println("Speed is : " + grade);
}
}

class Regular extends Typist{


Regular(String code, String name, int speed){
super(code, name, speed);
}
}

class Casual extends Typist{


int wage;
Casual(String code, String name, int speed, int wage){
super(code, name, speed);
this.wage = wage;
}
void print(){
super.print();
System.out.println("Daily Wage is : " + wage);
}
}

public class TwentyFive{


public static void main(String[] args) {
Staff A = new Staff("ABC", "me");
Teacher B = new Teacher("DEF", "Riya", "OOPS", "NA");
Typist C = new Typist("GHJ", "Priya", 60);
Officer D = new Officer("KJN","Den", "A");
Casual E = new Casual("HJHMN", "John", 70, 10000);
A.print();
B.print();
C.print();
D.print();
E.print();

}
}
Output:

Q26. Create a file to read input from file 1 and copy its content to file
2.
import java.io.*;
public class copycontent {
public static void main(String[] args) throws IOException {
File F1 = new File ("F1.txt");
File F2 = new File ("F2.txt");
FileInputStream fis = new FileInputStream(F1);
FileInputStream fis1 = new FileInputStream(F2);
int num = fis.available();
int num1 = fis1.available();
byte b1[] = new byte[num];
fis.read(b1);
byte b2[] = new byte[num1];
fis1.read(b2);
String s = new String(b1);
String s1 = new String(b2);
System.out.println("File 1 Contents");
System.out.println(s);
System.out.println("File 2 Contents");
System.out.println(s1);
s1 = s;
fis.close();
fis1.close();

FileOutputStream fos = new FileOutputStream(F1);


fos.write(b1);
FileOutputStream fos1 = new FileOutputStream(F2);
fos1.write(b2);
System.out.println("New File 2 Content = " + s1);
fos.close();
fos1.close();
}
}

Output:

_________________________________________________________________
Q27. WAP to count the no. of blank spaces from file and delete them

import java.io.*;
public class blank {
public static void main(String[] args) throws IOException {
File F1 = new File("F1.txt");
FileInputStream fis = new FileInputStream(F1);
int num = fis.available();
byte b[] = new byte[num];
fis.read(b);
String s = new String(b);
System.out.println("File Contents");
System.out.println(s);
byte b1[] = new byte[num];
String bs = " ";
int count = 0;
for (int i = 0; i < num; i++) {
int j = 0;
if (s.charAt(i) == ' ') {
count++;
} else {
b[j] = (byte) s.charAt(i);
j++;
bs = bs + s.charAt(i);
}
}

FileOutputStream fos = new FileOutputStream(F1);


fos.write(b1);
System.out.println("No. of spaces deleted = " + count);
System.out.println("New Content = " + bs);
fos.close();
}
}

Output:
Q28. WAP to read a file and count the number of vowels.
import java.io.*;
public class vowelcount {
public static void main(String[] args) throws IOException {
File F1 = new File("F1.txt");
FileInputStream fis = new FileInputStream(F1);
int num = fis.available();
byte b[] = new byte[num];
fis.read(b);
String s = new String(b);
System.out.println("File Contents");
System.out.println(s);
byte b1[] = new byte[num];
int count = 0;
for (int i=0; i < num; i++){
if(s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O'
||s.charAt(i) == 'U' || s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i)
== 'i' ||
s.charAt(i) == 'o' || s.charAt(i) == 'u')
{
countv++;
}
}

FileOutputStream fos = new FileOutputStream(F1);


fos.write(b1);
System.out.println("No. of vowels in file = " + count);
fos.close();
}
}

Output:
Q29. WAP to read two files and concatenate the input into the third
file.
import java.io.*;
public class concatenate {
public static void main(String[] args) throws IOException {
File F1 = new File ("F1.txt");
File F2 = new File ("F2.txt");
File F3 = new File ("F3.txt");
FileInputStream fis = new FileInputStream(F1);
FileInputStream fis1 = new FileInputStream(F2);
FileInputStream fis2 = new FileInputStream(F3);
int num = fis.available();
int num1 = fis1.available();
int num2 = fis2.available();
byte b1[] = new byte[num];
fis.read(b1);
byte b2[] = new byte[num1];
fis1.read(b2);
byte b3[] = new byte[num2];
fis2.read(b3);
String s = new String(b1);
String s1 = new String(b2);
String s2 = new String(b3);
s2= s+s1;
System.out.println("File 1 Contents");
System.out.println(s);
System.out.println("File 2 Contents");
System.out.println(s1);
System.out.println("File 3 Contents (concatenated file1 and file2)");
System.out.println(s2);
fis.close();
fis1.close();
fis2.close();

FileOutputStream fos = new FileOutputStream(F1);


fos.write(b1);
FileOutputStream fos1 = new FileOutputStream(F2);
fos1.write(b2);
FileOutputStream fos2 = new FileOutputStream(F2);
fos1.write(b3);

fos.close();
fos1.close();
fos2.close();
}
}
Output:

Q30. WAP to read a file and replace a vowel with blank space.
import java.io.*;
public class intro {
public static void main(String[] args) throws IOException {
File F1 = new File("F1.txt");
FileInputStream fis = new FileInputStream(F1);
int num = fis.available();
byte b[] = new byte[num];
fis.read(b);
String s = new String(b);
System.out.println("File Contents: ");
System.out.println(s);
byte b1[] = new byte[num];
int countv = 0;
String bs = " ";
for (int i=0; i < num; i++){
if(s.charAt(i) == 'A' || s.charAt(i) == 'E' || s.charAt(i) == 'I' || s.charAt(i) == 'O'
||s.charAt(i) == 'U' || s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i)
== 'i' ||
s.charAt(i) == 'o' || s.charAt(i) == 'u')
{
countv++;

int count = 0;
for (int j = 0; j < num; j++) {
int k = 0;
if (s.charAt(j) == ' ') {
count++;
} else {
b[k] = (byte) s.charAt(j);
k++;
bs = bs + s.charAt(j);
}
}
}
}

FileOutputStream fos = new FileOutputStream(F1);


fos.write(b1);
System.out.println("No. of vowels in file = " + countv);
System.out.println("New File Content: " + bs);
fos.close();
}
}

Output:

_________________________________________________________________
Q31. WAP to create a file to save employee details, designagon,
salary, department. Write the details in the file as an object and read
and print.
import java.io.*;
class Employee_Details implements Serializable{
String Name;
String Designation;
int Salary;
String Department;
Employee_Details (String n, String des, int s, String dep){
this.Name = n;
this.Designation = des;
this.Salary = s;
this.Department = dep;
}
void Read_Details (String n, String des, int s, String dep){
Name = n;
Designation = des;
Salary = s;
Department = dep;
}
void Print_Details(){
System.out.println("Name of the Employee" + Name);
System.out.println("Designation of the Employee" + Designation);
System.out.println("Salary of the Employee" + Salary);
System.out.println("Department of the Employee" + Department);
}
}
public class main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Employee_Details e1 = new Employee_Details("abc", "Manager", 10000, "Sales");
Employee_Details e2 = new Employee_Details("def", "Manager", 100000, "Marketing");
FileOutputStream fos = new FileOutputStream ("Employee_Details");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(e1);
oos.close();

FileInputStream fis = new FileInputStream("Employee_Details");


ObjectInputStream ois = new ObjectInputStream (fis);
e2 = (Employee_Details) ois.readObject();
System.out.println("Name of the Employee: " + e2.Name);
System.out.println("Designation of the Employee: " + e2.Designation);
System.out.println("Salary of the Employee: " + e2.Salary);
System.out.println("Department of the Employee: " + e2.Department);
}
}
Output:

Q32. WAP that reads two integers and runs a loop for calculating ab .
You must check if b=0, then throw an exception and handle it by a/b.
Catch the exception and let the code continue until there is no
exception. Give ‘Hi’ and ‘Bye’ statements for every block of code.

import java.util.*;

public class lab {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Hi Main!");
System.out.println(" ");
while (true) {
try {
System.out.println("Hi try!");
System.out.println(" ");
System.out.print("Enter the value of a: ");
int a = sc.nextInt();
System.out.print("Enter the value of b: ");
int b = sc.nextInt();

if (b == 0) {
throw new ArithmeticException("Cannot divide by zero!");
}
int result = 1;
for (int i = 1; i <= b; i++) {
result *= a;
}
System.out.println("a^b = " + result);
System.out.println("Bye try!");
System.out.println(" ");
break;
}
catch (ArithmeticException a) {
System.out.println("Hi catch!");
System.out.println(" ");
System.out.println("Exception: " + a.getMessage());
System.out.println("Handled by performing a/b.");
System.out.println("Answer: Infinity");

System.out.println("Bye catch!");
System.out.println(" ");
}
}
System.out.println("Bye Main!");
}
}

Output:

You might also like