JavaLab Sanjay
JavaLab Sanjay
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:
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:
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
Output:
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;
}
}
}
s.close();
obj.sum(a);
}
Output:
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;
return percentage;
}
}
class Student1 {
private Score[] sem = new Score[4];
}
Output:
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;
sem[i].input();
sem[i].max_min();
}
}
}
public class Expt6 {
public static void main(String[] args) {
Student1 obj = new Student1();
obj.semester();
}
}
Output:
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:
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;
if (s3.equals(rev))
System.out.println(s3 + " is a palindrome");
else
System.out.println(s3 + " is not a palindrome");
}
}
}
Output:
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;
Arrays.sort(arr);
System.out.print("The sorted array is: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(" " + arr[i]);
}
}
mt.join();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Exercise 12
Aim:
Program:
import java.io.Console;
class ReadPasswordTest{
Console c=System.console();
char[] ch=c.readPassword();
Output:
Exercise 13
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:
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:
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