AnandJAVA
AnandJAVA
LAB RECORD
22-382-0206 JAVA LAB
DEPARTMENT OF COMPUTER APPLICATIONS
COCHIN UNIVERSITY OF SCIENCE AND TECHNOLOGY
CERTIFICATE
Certified that this is a bonafide record of work done by ANAND KUMAR, Reg. No.
24100336, MCA Semester-II, during the sessions of 22-382-0206 Java Lab in the Department
of Computer Applications, Cochin University of Science and Technology.
Sl No Title
2. CHARACTER COUNT
7. STUDENT DATABASE
8. MATRIX ADDITION
9. POLYNOMIAL ADDITION
12. INHERITANCE
15. INTERFACES
16. GUI
17. EXCEPTION HANDLING
18. MULTITHREADING
Problem:
Write a Java program to print the saddle point of an integer matrix.
Program:
import java.io.*;
import java.util.Scanner;
class SaddlePoint
{
public static void main(String args[])
{
int a[][]=new int[10][10],r,c;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the row limit:");
r=sc.nextInt();
System.out.print("Enter the column limit:");
c=sc.nextInt();
System.out.println("Enter the numbers:");
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
a[i][j]=sc.nextInt();
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println();
OUTPUT:
OUTPUT-1
--------------------
Enter the row limit:3
Enter the column limit:3
Enter the numbers:
1
2
3
4
5
6
7
8
9
1 2 3
4 5 6
7 8 9
OUTPUT-2
-------------------------
Enter the row limit:3
Enter the column limit:3
Enter the numbers:
1
2
3
4
5
6
10
18
4
1 2 3
4 5 6
10 18 4
No Saddle Point
OUTPUT-3
------------------------
Enter the row limit:3
Enter the column limit:3
Enter the numbers:
4
16
12
2
8
14
1
3
6
4 16 12
2 8 14
1 3 6
CHARACTER COUNT
Problem
Program:
import java.io.*;
import java.util.Scanner;
class OccurrenceOfChar
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a string:");
String inputString=sc.nextLine();
sc.close();
int[] charOccurrences=new int[256];
for(int i=0;i< inputString.length();i++)
{
char currentChar=inputString.charAt(i);
charOccurrences[currentChar]++;
}
System.out.println("Occurrences of each character:");
for(int i=0;i< charOccurrences.length;i++)
if(charOccurrences[i]>0)
{
System.out.println((char) i + " : " +
charOccurrences[i]);
}
}
}
OUTPUT:
OUTPUT-1
---------------------------
Enter a string:Ashutosh
Occurrences of each character:
A : 1
h : 2
o : 1
s : 2
t : 1
u : 1
OUTPUT-2
----------------------------
Enter a string:Aashutosh
Occurrences of each character:
A : 1
a : 1
h : 2
o : 1
s : 2
t : 1
u : 1
OUTPUT-3
-------------------------------
Enter a string:Aashutosh kumar
Occurrences of each character:
: 1
A : 1
a : 2
h : 2
k : 1
m : 1
o : 1
r : 1
s : 2
t : 1
u : 2
Program No:3
Problem:
Program to find the roots of a quadratic equation. The coefficients and the constant are given.
Program:
import java.io.*;
import java.util.Scanner;
import java.lang.Math;
class quadratic
{
public static void main(String args[])
{
double a,b,c,d,roots,root1,root2;
System.out.print("Enter coefficient of x2:");
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
System.out.print("Enter coefficient of x:");
b=sc.nextInt();
System.out.print("Enter the constant:");
c=sc.nextInt();
d=Math.sqrt(b*b-4*a*c);
if(d==0)
{
System.out.print("Equation will have unique
solution:"+(roots=(-b+d)/(2*a)));
}
else if(d>0)
{
System.out.print("First root:"+(root1=(-
b+d)/(2*a)));
System.out.print("Second root:"+(root2=(-b-
d)/(2*a)));
}
else
{
System.out.println("Imaginary roots");
}
}
}
OUTPUT:
Enter coefficient of x2:1
Enter coefficient of x:-4
Enter the constant:4
Equation will have unique solution:2.0
Program No: 4
Program:
import java.io.*;
import java.util.Scanner;
class sum_of_two_numbers
{
public static void main(String args[])
{
double num1,num2;
System.out.print("Enter first number:");
Scanner sc=new Scanner(System.in);
num1=sc.nextDouble();
System.out.print("Enter second number:");
num2=sc.nextDouble();
//System.out.println("Sum: "+(num1 + num2));
System.out.printf("Sum: %.2f\n",num1 + num2);
}
}
OUTPUT:
Enter first number:4.6
Enter second number:2.62
Sum: 7.22
Program No: 5
Problem:
Menu driven program to calculate the area of a rectangle from its length and width and that of
a circle from its radius.
Class Design
class Rectangle
● Properties
○ length, breadth.
● Methods
○ SetSides(): To assign values to the properties
○ calcArea(): To calculate and print the area of the rectangle
area=length*breadth
print(area)
class Circle
● Properties
○ radius.
● Methods
○ SetSides(): To assign values to the properties
○ calcCirArea(): To calculate and print the area of the circle
area=3.14*radius*radius
print(area)
Program:
import java.io.*;
import java.util.Scanner;
class Rectangle
{
double length,breadth;
void setSides(double l,double b)
{
this.length=l;
this.breadth=b;
}
void calcRecArea()
{
double area;
area=length*breadth;
System.out.println("Area of the rectangle="+area);
}
}
class Circle
{
double radius;
void setSides(double r)
{
this.radius=r;
}
void calCirArea()
{
double area;
area=3.14*radius*radius;
System.out.println("Area of the circle="+area);
}
}
class Area_of_Rect_Circ
{
public static void main(String args[])
{
int choice;
double l,b,r;
Scanner sc=new Scanner(System.in);
while(true)
{
System.out.println("1:Area of the
Rectangle:\n2:Area of Circle:\n..........");
choice=sc.nextInt();
switch(choice)
{
case 1: System.out.print("Enter the length and
breadth of the rectagle:");
Rectangle rect=new Rectangle();
l=sc.nextDouble();
b=sc.nextDouble();
rect.setSides(l,b);
rect.calcRecArea();
break;
case 2: System.out.println("Enter the radius
of the circle:");
Circle cir=new Circle();
r=sc.nextDouble();
cir.setSides(r);
cir.calCirArea();
break;
case 3:System.exit(0);
}
}
}
}
OUTPUT:
1:Area of the Rectangle:
2:Area of Circle:
..........
1
Enter the length and breadth of the rectagle:2 3
Area of the rectangle=6.0
1:Area of the Rectangle:
2:Area of Circle:
..........
2
Enter the radius of the circle:
3
Area of the circle=28.259999999999998
1:Area of the Rectangle:
2:Area of Circle:
..........
3
Program No: 6
Program:
import java.util.Scanner;
class Circle_area_coordinates
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the x-coordinates of the
center:");
double centerX=sc.nextDouble();
System.out.print("Enter the y-coordinates of the
center:");
double centerY=sc.nextDouble();
OUTPUT:
STUDENT DATABASE
Problem:
Program to store student data which include name, register number and marks obtained in 4
subjects and to print the results. The result should contain name, register number, marks,
passed/failed per subject, passed/failed in the whole examination and total marks if passed in
all the subjects. The maximum total per subject is 50 and 25 is required for a pass.
Class Design
class student
● Properties
○ name, reg, m1, m2, m3, m4, sum.
● Methods
○ setter(): To assign values to the properties
○ printData(): To print the name , registration number, marks and
result.
Program:
import java.io.*;
import java.util.Scanner;
class Student
{
String name,reg;
double m1,m2,m3,m4,sum;
void setter(String name,String reg,double m1,double
m2,double m3,double m4)
{
this.name=name;
this.reg=reg;
this.m1=m1;
this.m2=m2;
this.m3=m3;
this.m4=m4;
}
void printData()
{
System.out.println("Name: "+this.name);
System.out.println("Registration No.: "+this.reg);
if(m1>=25)
{
System.out.println("Marks of subject1="+m1 +
"\tPass");
}
else
{
System.out.println("Marks of subject1="+m1 +
"\tFail");
}
if(m2>=25)
{
System.out.println("Marks of subject2="+m2 +
"\tPass");
}
else
{
System.out.println("Marks of subject1="+m2 +
"\tFail");
}
if(m3>=25)
{
System.out.println("Marks of subject3="+m3 +
"\tPass");
}
else
{
System.out.println("Marks of subject1="+m3 +
"\tFail");
}
if(m4>=25)
{
System.out.println("Marks of subject4="+m4 +
"\tPass");
}
else
{
System.out.println("Marks of subject1="+m4 +
"\tFail");
}
}
System.out.println("\n------------Student Data as
follows-------------");
for(Student stud:studData)
{
stud.printData();
}
}
}
OUTPUT:
Result:- Pass
Total Marks=176.0
Name: efg
Registration No.: R02
Marks of subject1=50.0 Pass
Marks of subject2=25.0 Pass
Marks of subject1=12.0 Fail
Marks of subject4=32.0 Pass
Result:- Fail
PROGRAM No: 8
MATRIX ADDITION
Problem:
Write a java program to add two integer matrices.
Program:
import java.util.Scanner;
class Matrix
{
private int rows;
private int cols;
private int[][] matrix;
class Main
{
if (sum != null)
{
System.out.println("Sum of matrices:");
sum.printMatrix();
}
}
}
OUTPUT:
Enter dimensions of Matrix 1 (rows cols):
3 3
Enter dimensions of Matrix 2 (rows cols):
3 3
Enter elements of the matrix:
1 1 1
2 2 2
3 3 3
Enter elements of the matrix:
7 7 7
8 8 8
9 9 9
Sum of matrices:
8 8 8
10 10 10
12 12 12
Program No: 9
Quadratic Polynomial Addition
Problem:
Write a java program to print the quadratic polynomial addition.
Program:
import java.io.*;
import java.util.Scanner;
class Polynomial
{
double quad,linear,constant;
void setValues(double a,double b,double c)
{
this.quad=a;
this.linear=b;
this.constant=c;
}
void printSelf()
{
System.out.println("X^2("+quad+")+x("+linear+")+"+constant);
}
}
class main
{
public static void main(String args[])
{
double quad,linear,constant;
Scanner s=new Scanner(System.in);
System.out.println("Polynomial-1");
System.out.println("Enter the coefficients:");
quad=s.nextDouble();
linear=s.nextDouble();
constant=s.nextDouble();
System.out.println("Polynomial-2");
System.out.println("Enter the coefficients:");
quad=s.nextDouble();
linear=s.nextDouble();
constant=s.nextDouble();
Polynomial sum=P1.add(P2);
System.out.print("Sum:");
sum.printSelf();
}
}
OUTPUT:
-------------------------
Polynomial-1
Enter the coefficients:
5 2 3
Polynomial-2
Enter the coefficients:
6 3 8
Polynomial-1
X^2(5.0)+x(2.0)+3.0
Polynomial-2
X^2(6.0)+x(3.0)+8.0
Sum:X^2(11.0)+x(5.0)+11.0
Program No: 10
METHOD OVERLOADING
Problem:
Menu driven program to calculate the perimeter of rectangles and squares. Write class
Rectangle with overloaded setter methods to assign the side values of rectangle and square
methods.
Program:
import java.io.*;
import java.util.Scanner;
class Rectangle
{
double length,breadth;
void setSides(double a)
{
this.length=a;
this.breadth=a;
}
void setSides(double a,double b)
{
this.length=a;
this.breadth=b;
}
void calcPerimeter()
{
double peri;
peri=2*(length + breadth);
System.out.println("Perimeter: "+peri);
}
}
class record10
{
public static void main(String args[])
{
int choice;
double l,b;
Scanner s=new Scanner(System.in);
while(true)
{
System.out.println("1:Perimeter of Rectangle.
\n2:Perimter of Square. \n3.Enter to
exit.\n..................");
choice=s.nextInt();
switch(choice)
{
case 1:System.out.print("Enter the lenght and
breadth of Rectangle: ");
Rectangle rect=new Rectangle();
l=s.nextDouble();
b=s.nextDouble();
rect.setSides(l,b);
rect.calcPerimeter();
break;
case 2:System.out.println("Enter the side of
square: ");
Rectangle sqr=new Rectangle();
l=s.nextDouble();
sqr.setSides(l);
sqr.calcPerimeter();
break;
case 3: System.exit(0);
}
}
}
}
OUTPUT:
1:Perimeter of Rectangle.
2:Perimter of Square.
3.Enter to exit.
..................
1
Enter the lenght and breadth of Rectangle: 20 2
Perimeter: 44.0
1:Perimeter of Rectangle.
2:Perimter of Square.
3.Enter to exit.
..................
2
Enter the side of square:
10
Perimeter: 40.0
1:Perimeter of Rectangle.
2:Perimter of Square.
3.Enter to exit.
..................
3
Program No:11
THE CONSTRUCTOR
Problem:
Implement the scenario given in question 13 by replacing the setter methods with overloaded
constructors.
Program:
import java.util.Scanner;
class Student {
protected String name;
protected int[] marks;
scanner.close();
}
}
OUTPUT:
---------------------------------------------------------------
Enter details for student without additional subject:
Name: Aman
Enter marks for 5 subjects: 50
55
66
22
22
System.out.println("Employee Data(self)");
emp.printSelf();
System.out.println("\nManager Data(self)\n......");
m.printSelf();
System.out.println("\nEmployee Data(Manager)\n.....");
m.printEmpData(emp);
}
OUTPUT:
Employee Data(self)
Name: Anu
Employee Id: emp01
Manager Data(self)
......
Name: Raju
Employee Id: emp05
Employee Data(Manager)
.....
Name: Anu
Employee Id: emp01
Program No:13
METHOD OVERRIDING
Problem:
For a school final board examination, the students should take five subjects. And, if the
student is interested, he/she can opt an additional subject thereby making it a total of six
subjects. To compute the grades the scores of all the 5 will be considered for students without
an additional subject and for the students with an additional subject, the best five marks out
of six will be considered. Implement the scenario using inheritance. Assume any suitable
formula for computing the grades
Program:
import java.util.Scanner;
@Override
public void inputMarks() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter marks for " + name + " (5
subjects):");
for (int i = 0; i < 5; i++) {
marks[i] = scanner.nextInt();
}
}
}
@Override
public void inputMarks() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter marks for " + name + " (6
subjects):");
for (int i = 0; i < 6; i++) {
marks[i] = scanner.nextInt();
}
}
}
System.out.println("\nResults:");
regularStudent.printResult();
extraSubjectStudent.printResult();
}
}
OUTPUT:
Enter name of student with 5 subjects: Aman
Enter marks for Aman (5 subjects):
55
66
45
55
15
Results:
Name: Aman
Grade: F
Name: Patel
Grade: A
Program No:14
Abstract Classes
Problem:
In an organization, there are different classes of employees.
Consider the activities of reading the employee data (Name,
Employee Id & Basic Pay) and calculating the salary.
Manager
Basic pay : Read from user
DA(Dearness allowance)=50% of basic pay
HRA(House Rent Allowance)=10000 rupees
TA (Travel Allowance)= 5000
Salary=Basic pay+DA+TA+HRA
Assistant Manager
Basic pay : Read from user(input a value that is less than the
basic pay of manager)
DA(Dearness allowance)=40% of basic pay
HRA(House Rent Allowance)=7000 rupees
TA (Travel Allowance)= 3000
Salary=Basic pay+DA+TA+HRA
Program:
import java.util.Scanner;
// Display salaries
System.out.println("\nManager Salary:");
System.out.println("Salary: $" +
manager.calculateSalary());
OUTPUT:
-----------------------------------------------
Enter details for Manager:
Name: Manjesh
Employee ID: 01
Basic Pay: 500000
Manager Salary:
Salary: $765000.0
Program:
// Define interface Shape
interface Shape {
double calcArea(double val);
double calcPerimeter(double val);
}
@Override
public double calcPerimeter(double side) {
return 4 * side;
}
}
@Override
public double calcArea(double radius) {
return PI * radius * radius;
}
@Override
public double calcPerimeter(double radius) {
return 2 * PI * radius;
}
}
OUTPUT:
----------------------------------------------------
Square - Area: 25.0, Perimeter: 20.0
Circle - Area: 28.274309999999996, Perimeter:
18.849539999999998
Program No:16
GUI
Problem:
Program to create a GUI based application to calculate the sum of two real numbers. Show
the sum on a message box.
Program:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
class GUI implements ActionListener
{
JFrame frame;
JTextField text1,text2;
GUI()
{
frame=new JFrame();
frame.setSize(250,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setTitle("My Application");
s=text2.getText();
num2=Float.parseFloat(s);
JOptionPane.showMessageDialog(null,"SUM="+(num1+num2),"Result"
,JOptionPane.PLAIN_MESSAGE,null);
}
}
public class record17 {
public static void main(String args[])
{
new GUI();
}
}
OUTPUT:
Program No:17
EXCEPTION HANDLING
Problem:
Program to implement the scenario in which a user has to enter the percentage score of
undergraduate studies. Consider two cases where a user enters a negative value and a value
greater than 100. Write custom Java exception classes and handling routines to separately
handle both these cases.
Program:
// Custom exception for negative percentage score
class NegativePercentageException extends Exception {
public NegativePercentageException(String message) {
super(message);
}
}
// Display percentage
System.out.println("Entered percentage: " +
percentage + "%");
} catch (NegativePercentageException e) {
System.out.println("Error: " + e.getMessage());
} catch (InvalidPercentageException e) {
System.out.println("Error: " + e.getMessage());
}
}
scanner.close();
return percentage;
}
}
OUTPUT:
--------------------------------------------
Output 1
Enter your percentage score: 98
Entered percentage: 98.0%
Output 2
Enter your percentage score: -89
Program:
class OddEvenPrinter {
private int max;
private boolean isOdd;
private Object lock = new Object();
oddThread.start();
evenThread.start();
}
}
OUTPUT:
----------------------------
OddThread: 1
EvenThread: 1
OddThread: 3
EvenThread: 3
OddThread: 5
EvenThread: 5
OddThread: 7
EvenThread: 7
OddThread: 9
EvenThread: 9
OddThread: 11
EvenThread: 11
OddThread: 13
EvenThread: 13
OddThread: 15
EvenThread: 15
OddThread: 17
EvenThread: 17
OddThread: 19
EvenThread: 19
Program No:19
MUTUAL EXCLUSION BY THREADS
Problem:
Program to implement mutual exclusion of threads on accessing a shared object using
synchronized().
Program:
//Synchronization in multithreading.....
import java.io.*;
class CommonObj
{
//synchronized void printMessage(String thread)
void printMessage(String thread)
{
System.out.print("{");
System.out.print("Hi.. from "+thread);
System.out.println("}");
}
}
class UserThread extends Thread
{
String name;
CommonObj Object;
UserThread(String name,CommonObj ob)
{
this.name=name;
this.Object=ob;
}
public void run()
{
synchronized(Object) //Synchronized block
{
this.Object.printMessage(this.name);
}
}
}
class lab13_03__2
{
public static void main(String[] a )
{
CommonObj obj=new CommonObj();
UserThread t_1=new UserThread("First",obj);
UserThread t_2=new UserThread("Second",obj);
UserThread t_3=new UserThread("Third",obj);
t_1.start();
t_2.start();
t_3.start();
}
}
OUTPUT:
-------------------------
{Hi.. from First}
{Hi.. from Third}
{Hi.. from Second}