0% found this document useful (0 votes)
9 views

JavaLab Sanjay

The document describes Java programs to calculate the area and perimeter of a circle, simple and compound interest, professional tax calculation, sum of even and odd numbers in an array, student score calculation over multiple semesters, and finding the largest and smallest number in a set.

Uploaded by

Sanjay Sahoo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JavaLab Sanjay

The document describes Java programs to calculate the area and perimeter of a circle, simple and compound interest, professional tax calculation, sum of even and odd numbers in an array, student score calculation over multiple semesters, and finding the largest and smallest number in a set.

Uploaded by

Sanjay Sahoo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH

Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 1

Aim: Develop an Object Oriented Program to find the area and perimeter of a circle.
Program:

import java.util.*;
class circle {
float radius, area, peri,pie = 3.141f;
public void getdata() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the radius:");
radius = sc.nextFloat();
sc.close();
}
public void display() {
area = pie * radius * radius;
peri = 2 * pie * radius;
System.out.println("Area= " + area + "\n" + "Perimeter= " + peri);
}
}
public class Expt1 {
public static void main(String[] args) {
circle1 h = new circle1();
h.getdata();
h.display();
}
}

Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 2
Aim: Develop an interest calculator program to find simple interest payable monthly, compound
interest payable annually compounded quarterly. Use keyboard inputs for interest rate and
principal amount.
Program:
import java.util.Scanner;
import java.text.DecimalFormat;
public class Expt2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the principal: ");
double principal = input.nextDouble();
System.out.print("Enter the rate: ");
double rate = input.nextDouble();
System.out.print("Enter the time: ");
double time = input.nextDouble();
System.out.print("Enter number of times interest is compounded: ");
double interest = (principal * time * rate) / 100;
rate=rate/100;
double c_interest = principal * (Math.pow((1 + rate / 4), (time * 4))) - principal;
DecimalFormat ft = new DecimalFormat("₹##,###.##");
System.out.println("\nPrincipal: " + ft.format(principal));
System.out.println("Interest Rate: " + rate + "%");
System.out.println("Time Duration: " + time);
System.out.println("Simple Interest: " + ft.format(interest));
System.out.println("Compound Interest: " + ft.format(c_interest));
input.close();
}
}
Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 3
Aim: Define a class to calculate professional tax on a salary amount based on the following tax
rate. Use if and switch control structures.

Program:
import java.util.Scanner;
class tax {
private int tax;
public void calculate(int sal) {
if (sal <= 10000)
tax = 0;
if (sal >= 10001 && sal <= 25000)
tax = 100;
if (sal >= 25001 && sal <= 50000)
tax = 200;
if (sal >= 50001 && sal <= 75000)
tax = 300;
if (sal >= 758001 && sal <= 100000)
tax = 450;
if (sal >= 100000)
tax = 650;
System.out.println("If salary is " + sal + " then, professional tax is ₹" + tax);
}
}
public class Expt3 {
public static void main(String[] args) {
tax t = new tax();
int choice = 1, sal;
Scanner scn = new Scanner(System.in);
while (choice != 2) {
System.out.println("Enter your choice:\n1. Calculate tax\n2. Stop");
choice = scn.nextInt();
switch (choice) {
case 1:
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

System.out.println("Enter the salary amount: ");


sal = scn.nextInt();
t.calculate(sal);
break;
case 2:
System.out.println("Thank You");
break;
default:
System.out.println("Invalid Input, Please try again");
}
}
scn.close();
}
}

Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 4
Aim:
Develop a program to find the sum of even numbers and sum of odd numbers in a set of
numbers. Define a class with suitable methods to carry out the operations. Use array to store
numbers.

Program:

import java.util.Scanner;

class even_odd {
private int sumE = 0, sumO = 0;

public void sum(int[] a) {


for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
sumE = sumE + a[i];
} else {
sumO = sumO + a[i];
}
}
System.out.println("Sum of Even Numbers:" + sumE);
System.out.println("Sum of Odd Numbers:" + sumO);

}
}

public class Expt4 {


public static void main(String[] args) {
int n;
even_odd obj = new even_odd();
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements in array:");
n = s.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

}
s.close();
obj.sum(a);
}

Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise: 5
Aim:
A student scores marks in subjects in a semester. A semester has 5 or 6 subjects depending of
MCA course. Define a class called Score that contains subject code, name and marks in that
subject. Define a class called Student having an array of objects of Score class in it following
object composition. Process result of students in different semesters.
Program:
import java.util.Scanner;

class Score {
private String[] sub_code = new String[5];
private String[] sub_name = new String[5];
private int[] mark = new int[5];
private int sum = 0;
private double percentage;

public void input() {


Scanner scn = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.print("Enter the subject code: ");
sub_code[i] = scn.nextLine();
System.out.print("Enter the subject name: ");
sub_name[i] = scn.nextLine();
System.out.print("Enter the subject mark: ");
mark[i] = scn.nextInt();
scn.nextLine();
}
}

public double calc() {


for (int i = 0; i < 5; i++) {
sum = sum + mark[i];
}
percentage = ((sum / 500.0) * 100.0);

return percentage;
}
}

class Student1 {
private Score[] sem = new Score[4];

public void semester() {


ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

for (int i = 0; i < 4; i++) {


System.out.println("\nEnter the delatils for semester " + (i + 1) + "\n");
sem[i] = new Score();
sem[i].input();
System.out.printf("\nResult os student in semester %d is %.2f", (i + 1), sem[i].calc());
System.out.print("%\n");
}
}
}
public class Expt5 {
public static void main(String[] args) {
Student1 obj = new Student1();
obj.semester();
}

}
Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise: 6
Aim:
Modify the class defined in sl-6 to find largest and smallest numbers in a set of numbers.
Program:
import java.util.Scanner;
class Scores {
private String[] sub_code = new String[5];
private String[] sub_name = new String[5];
private int[] mark = new int[5];
private int max, min;

public void input() {


Scanner scn = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
System.out.print("Enter the subject code: ");
sub_code[i] = scn.nextLine();
System.out.print("Enter the subject name: ");
sub_name[i] = scn.nextLine();
System.out.print("Enter the subject mark: ");
mark[i] = scn.nextInt();
scn.nextLine();
}
}
public void max_min()
{
max=min=mark[0];
for(int i=0;i<4;i++)
{
if(max<mark[i+1])
max=mark[i+1];
else
min=mark[i+1];
}
System.out.println("Largest number is: "+max);
System.out.println("Smallest number is: "+min);
}
}
class Student {
private Scores[] sem = new Scores[4];
public void semester() {
for (int i = 0; i < 4; i++) {
System.out.println("\nEnter the delatils for semester " + (i + 1) + "\n");
sem[i] = new Scores();
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

sem[i].input();
sem[i].max_min();
}
}
}
public class Expt6 {
public static void main(String[] args) {
Student1 obj = new Student1();
obj.semester();
}

}
Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise: 7
Aim:
Define a class called SimpleMath with overloaded methods to carryout arithmetic operations
using it. Use static methods appropriately.
Program:
class SimpleMath {
public int add(int n1, int n2){
return n1+n2;
}
public int add(int n1, int n2, int n3){
return n1+n2+n3;
}
public int add(int n1, int n2, int n3, int n4){
return n1+n2+n3+n4;
}
}
public class Expt7
{
public static void main(String[] args){
SimpleMath obj = new SimpleMath();
System.out.println("Sum of two numbers: "+obj.add(10, 20));
System.out.println("Sum of three numbers: "+obj.add(10, 20, 30));
System.out.println("Sum of four numbers: "+obj.add(1, 2, 3, 4));
}
}
Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise: 8
Aim:
Redefine the Circle class in exercise 1 to use value of Pi as a constant and a variable to count
number of instances created as you go on creating objects.
Program:
import java.util.*;
class circle1 {
final float pie = 3.141f;
float radius, area, peri;
static int count = 0;
public circle1() {
count++;
}
public void getdata() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the radius:");
radius = sc.nextFloat();
}
public void display() {
area = pie * radius * radius;
peri = 2 * pie * radius;
System.out.println("Area= " + area + "\n" + "Perimeter= " + peri);
}
public static void print_instance() {
System.out.println("The number of created instances of Circle class is " + count);
}
}
public class Expt8 {
public static void main(String[] args) {
circle h1 = new circle();
circle h2 = new circle();
h1.getdata();
h1.display();
h2.getdata();
h2.display();
circle1.print_instance();
}
}

Output:
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 9
Aim:
Develop a class to perform the following tasks on a line of text
a. Count the number of words in the text
b. Searches a particular string in the text
c. Checks if the text is a palindrome
Program:

import java.util.*;
public class Expt9 {
static int wordcount(String string) {
int count = 0;

char ch[] = new char[string.length()];


for (int i = 0; i < string.length(); i++) {
ch[i] = string.charAt(i);
if (((i > 0) && (ch[i] != ' ') && (ch[i - 1] == ' ')) || ((ch[0] != ' ') && (i == 0)))
count++;
}
return count;
}

static void palindrome(String s3) {


int len = s3.length();
String rev = "";
for (int i = len - 1; i >= 0; i--)
rev = rev + s3.charAt(i);

if (s3.equals(rev))
System.out.println(s3 + " is a palindrome");
else
System.out.println(s3 + " is not a palindrome");
}

public static void main(String[] args) {


Scanner scn = new Scanner(System.in);
System.out.println("Enter a text to count the words in it:");
String string1 = scn.nextLine();
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

System.out.println(wordcount(string1) + " words.");


System.out.println("Enter a word to find in the text:");
String string2 = scn.nextLine();
System.out.println("the word " + string2 + " is found at index no.: " +
string1.indexOf(string2));
System.out.println("Enter a text to check it is palindrome or not:");
String string3 = scn.nextLine();
palindrome(string3);
scn.close();

}
}

Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise: 10
Aim:
A bank account maintains a minimum balance. If the account balance comes down below this
level due to some withdrawal then it raises warning and disallows the operation. Define a custom
exception class called “InsufficientFundException” which will be raised when such event occurs.
Also use the built-in exception class “IllegalArgumentException” which is to be raised when you
try to either withdraw or deposit an amount less than or equal to zero.
Program:
import java.util.Scanner;
class belowBalance extends Exception {
belowBalance(String message) {
super(message);
}
}
public class Expt10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your amount: ");
int a = sc.nextInt();
System.out.println("Enter deposited amount: ");
int b = sc.nextInt();
sc.close();
try {
if (a < 500) {
throw new belowBalance("Insuffient balance:)");
}
if (b <= 0) {
throw new belowBalance("IllegalArgumentException");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}}}

Output:
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 11
Aim:
Write a multithreaded program to perform following parallel operations on a set of numbers.
a) Find the largest number
b) Find the sum of the number
c) Sort the numbers
Program:
import java.util.Arrays;

public class MultiThread extends Thread {


public void run() {
int a = 14, b = 3;
int res = (a > b) ? a : b;
System.out.println("The largest number is: " + res);
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Sum of two numbers is: " + (a + b));
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e);
}
int[] arr = { 10, 20, 7, 5 };

Arrays.sort(arr);
System.out.print("The sorted array is: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(" " + arr[i]);
}
}

public static void main(String[] args) {


MultiThread mt = new MultiThread();
mt.start();
try {
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

mt.join();
} catch (Exception e) {
System.out.println(e);
}
}
}

Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 12

Aim:

Write program using console I/O

Program:

import java.io.Console;

class ReadPasswordTest{

public static void main(String args[]){

Console c=System.console();

System.out.println("Enter password: ");

char[] ch=c.readPassword();

String pass=String.valueOf(ch);//converting char array into string

System.out.println("Password is: "+pass);

Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 13

Aim: Write Programs using File I/O.

Program:

import java.io.*;
public class FISDemo1 {
public static void main(String args[]) throws FileNotFoundException, IOException{
FileInputStream fis = new FileInputStream("abc.txt");
int data;
while((data=fis.read()) != -1){
System.out.print((char)data);
}
fis.close();
}
}

Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 14
Aim: Write program using serialization

Program:
import java.io.*;
class Student implements Serializable{
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
class Persist{
public static void main(String args[]){
try{
//Creating the object
Student s1 =new Student(211,"ravi");
//Creating stream and writing the object
FileOutputStream fout=new FileOutputStream("f.txt");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
//closing the stream
out.close();
System.out.println("success");
}catch(Exception e){System.out.println(e);}
}
}
Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

Exercise 15

Aim:

In CET, two types of people are there: students and employees. As per Govt. of India, everyone
must have his or her AADHAR number for unique identification. Model these objects
appropriately using inheritance and create an array of people with several students and
employees in it. Write a program to search a student or an employee based on AADHAR number
and print its details.

Program:

import java.util.Scanner;
class Person {
private String name;
private String aadharNumber;
public Person(String name, String aadharNumber) {
this.name = name;
this.aadharNumber = aadharNumber;
}
public String getName() {
return name;
}
public String getAadharNumber() {
return aadharNumber;
}
}
class Student extends Person {
private String studentId;
public Student(String name, String aadharNumber, String studentId) {
super(name, aadharNumber);
this.studentId = studentId;
}
public String getStudentId() {
return studentId;
}
}
class Employee extends Person {
private String employeeId;
public Employee(String name, String aadharNumber, String employeeId) {
super(name, aadharNumber);
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

this.employeeId = employeeId;
}
public String getEmployeeId() { return employeeId; }
}
public class exp11 {
public static void main(String[] args) {
Person[] people = new Person[4];
Student student1 = new Student("sumit", "1234567890", "S1");
Student student2 = new Student("behera", "9876543210", "S2");
Employee employee1 = new Employee("Michael", "6666666666", "E1");
Employee employee2 = new Employee("sk", "88888888888", "E2");
people[0] = student1;
people[1] = student2;
people[2] = employee1;
people[3] = employee2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the AADHAR number to search: ");
String searchAadhar = scanner.nextLine();
boolean found = false;
for (Person person : people) {
if (person.getAadharNumber().equals(searchAadhar)) {
found = true;
if (person instanceof Student) {
Student student = (Student) person;
System.out.println("Student Details:");
System.out.println("Name: " + student.getName());
System.out.println("AADHAR Number: " + student.getAadharNumber());
System.out.println("Student ID: " + student.getStudentId());
} else if (person instanceof Employee) {
Employee employee = (Employee) person;
System.out.println("Employee Details:");
System.out.println("Name: " + employee.getName());
System.out.println("AADHAR Number: " + employee.getAadharNumber());
System.out.println("Employee ID: " + employee.getEmployeeId());
}
break; }
}
if (!found) {
ODISHA UNIVERSITY OF TECHNOLOGY AND RESEARCH
Bhubaneswar, Odisha
Department of Computer Science and Application Semester : Second
Paper : Programming with Java Lab Paper Code: PLCCA202

System.out.println("Person with the given AADHAR number not found."); }


scanner.close();
}
}
Output:

Submitted By: Sanjay Kumar Sahoo


Regd no: 2224100018

You might also like