Java File Hritik
Java File Hritik
Practical File
Of
Object Oriented Programming Lab
(KCA – 251)
Submitted To Submitted By
Mr. Sonu Kumar HRITIK SINGH
(Assistant Professor) Roll No:- 2201940140015
MCA Department MCA – 1st Year
1|Page
INDEX
S.No Topic’s Signature
1 WAP to find the average and sum of the n
numbers using command line argument
2 WAP to Demonstrate type casting
3 WAP to find the number of arguments provided at
runtime
4 WAP to test the prime number
5 WAP to calculate simple interest and input from
user
6 WAP to create a simple class to find out the area
and perimeter of rectangle and box using super
and this keyword
7 WAP to find GCD of two numbers
8 WAP class account withdraw deposit inheritance
9 WAP to the factorial of given number using
Recursion
10 WAP to design a class using abstact methods and
classes
11 WAP to design a string class that perfrom string
method(Equal, reverse the string, change case)
12 WAP to Handle the exception using try and
multiple catch block
13 WAP that implement the Nested try statements
2|Page
14 WAP to create package that access the member of
external class as well as same package
15 WAP that import the user define package and
access the member variable of class that contained
by package
16 WAP that show the partial Implementation of
interface
17 WAP to handle the user define Exception using
throw keyword
18 WAP to create a thread that implement the
Runable interface
19 WAP to implement interthread communication
20 WAP to create a class component that show
controls and event handling on that controls.(math
calc)
21 WAP to draw the line, Rectangle, over, text, using
the graphics
22 WAP to Draw create a menu using the frame
23 WAP to create a Dialogbox
24 WAP to implement the flow layout and border
Layout
25 WAP to implement the gridlayout, Cardlayout
26 WAP of Awt demo2 given by me
27 WAP to demonstrate system clock
28 WAP to create frame that display the student
information
3|Page
1. WAP to find the average and sum of the n numbers using command line
argument
OUTPUT
4|Page
2. WAP to Demonstrate type casting
class Main {
public static void main(String[] args)
{
int num = 10;
System.out.println("The integer value: " + num);
double data = num;
System.out.println("The double value: " + data);
}
}
OUTPUT
5|Page
3. WAP to find the number of arguments provided at runtime
class Main {
public static void main(String[] args) {
for(String str: args) {
int argument = Integer.parseInt(str);
System.out.println("Argument in integer form: " + argument);
}
}
}
Javac main.java
Java main 11 23
Arguments in integer from
11
23
Int argument = Intege.parseInt(str);
6|Page
4. WAP to test the prime number
OUTPUT
3 is prime number
7|Page
5. WAP to calculate simple interest and input from user
OUTPUT
Simple Interest is: 3120.0
8|Page
6. WAP to create a simple class to find out the area and perimeter of rectangle
and box using super and this keyword
import java.util.*;
public class Rectangle {
double length;
double width;
void Area() {
double area;
area = this.length * this.width;
System.out.println("Area of rectangle is : + area); }
void Perimeter()
double perimeter;
perimeter = 2 * (this.length + this.width);
System.out.println("Perimeter of rectangle is : "+ perimeter); } }
class Use_Rectangle {
public static void main(String args[]) {
Rectangle rect = new Rectangle();
rect.length = 15.854;
rect.width = 22.65;
System.out.println("Length = " + rect.length);
System.out.println("Width = " + rect.width);
rect.Area();
rect.Perimeter(); } }
OUTPUT
Length = 15.854
Width = 22.65
Area of rectangle is : 359.09309999999994
Perimeter of rectangle is : 77.008
9|Page
7. WAP to find GCD of two numbers
public class FindGCDExample1
{
public static void main(String[] args)
{
int x = 12, y = 8, gcd = 1;
for(int i = 1; i <= x && i <= y; i++)
{
if(x%i==0 && y%i==0)
gcd = i;
}
System.out.printf("GCD of %d and %d is: %d", x, y, gcd);
}
}
OUTPUT
10 | P a g e
8. WAP class account withdraw deposit inheritance .
OUTPUT
Account Number: 10265356554
Balance :75000
Interest:8025
Amount:83025
11 | P a g e
9.WAP to the factorial of given number using Recursion
class FactorialExample{
public static void main(String args[]){
int i,fact=1;
int number=5;//It is the number to calculate factorial
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of "+number+" is: "+fact);
} }
OUTPUT
Factorial of 5 is 120.
12 | P a g e
10. WAP to design a class using abstact methods and classes
OUTPUT
Function of Base class is called
13 | P a g e
11. WAP to design a string class that perfrom string method(Equal, reverse
the string, change case).
import java.lang.*;
import java.io.*;
import java.util.*;
class ReverseString {
public static void main(String[] args)
{
String input = "java is rocking ";
StringBuilder input1 = new StringBuilder();
input1.append(input);
input1.reverse();
System.out.println(input1);
}
}
OUTPUT
Java is rocking
14 | P a g e
12.WAP to Handle the exception using try and multiple catch block
import java.util.Scanner;
public class Test{
public static void main(String args[])
{
Scanner scn = new Scanner(System.in);
try{
int n = Integer.parseInt(scn.nextLine());
if (99%n == 0)
System.out.println(n + " is a factor of 99");
}
catch (ArithmeticException ex)
{
System.out.println("Arithmetic " + ex);
}
catch (NumberFormatException ex)
{
System.out.println("Number Format Exception " + ex);
}}}
OUTPUT
15 | P a g e
13. WAP that implement the Nested try statements
class NestedTry {
public static void main(String args[])
{
try {
int a[] = { 1, 2, 3, 4, 5 };
System.out.println(a[5]);
try {
int x = a[2] / 0; }
catch (ArithmeticException e2) {
System.out.println("division by zero is not possible");
}}
catch (ArrayIndexOutOfBoundsException e1) {
System.out.println("ArrayIndexOutOfBoundsException");
System.out.println("Element at such index does not exists");
}}}
OUTPUT
ArrayIndexOutOfBoundsException
Element at such index does not exists
16 | P a g e
14. WAP to create package that access the member of external class as well as
same package.
interface GFG1Interface
{
String name = "This is the Interface of GF1";
void GFG1Interface();
}
public class GFG1
{
String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }}
package GFG2;
interface GFG3Interface
{
String name = "GFG";
public void interfaceGFG();
}
abstract class GFGabstract {
String name = "GFGAbstract";
abstract public void print();
}
public class GFG3
{
int first;
int second;
GFG3(int a, int b)
{
this.first = a;
this.second = b;
}
public int add() { return this.first + this.second; }}
package GFG2;
import GFG1.*;
public class GFG2 implements GFG3Interface {
@Override public void interfaceGFG()
{
17 | P a g e
System.out.println("This is the interface of the GFG3class"); }
public static void main(String args[]) {
GFG1 ob = new GFG1();
ob.setName("GFGsetter");
System.out.println(ob.getName());
OUTPUT
GFG Setter
18 | P a g e
15. WAP that import the user define package and access the member variable
of class that contained by package.
import package_one.ClassTwo;
import package_name.ClassOne;
public class Testing {
public static void main(String[] args){
ClassTwo a = new ClassTwo();
ClassOne b = new ClassOne();
a.methodClassTwo();
b.methodClassOne();
}}
OUTPUT
Hello there I am class two
Hello there I am class one
19 | P a g e
16. WAP that show the partial Implementation of interface
interface findArea {
void area(); }
interface GeometricalShape extends findArea {
void drawShape(); }
class Rectangle implements GeometricalShape {
public void drawShape() {
System.out.println("Drawing a rectangle"); }
public void area() {
System.out.println("Area of a rectangle is l * b"); } }
public class InterfaceInheritanceExample {
public static void main(String argvs[]) {
Rectangle rect = new Rectangle();
rect.drawShape();
rect.area(); } }
OUTPUT
Drawing a rectangle
Area of a rectangle is l * b
20 | P a g e
17. WAP to handle the user define Exception using throw keyword.
public class TestThrow2 {
public static void method() throws FileNotFoundException {
FileReader file = new FileReader("C:\\Users\\Anurati\\Desktop\\abc.txt");
BufferedReader fileInput = new BufferedReader(file);
throw new FileNotFoundException(); }
public static void main(String args[]){
try
{
method();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
System.out.println("rest of the code...");
} }
OUTPUT
At test throw2..method(testthrow.java:12)
At test throw2..main(testthrow.java:22)
Rest of the code.
21 | P a g e
18. WAP to create a thread that implement the Runable interface.
public class ExampleClass implements Runnable
{
@Override
public void run() {
System.out.println("Thread has ended");
}
public static void main(String[] args) {
ExampleClass ex = new ExampleClass();
Thread t1= new Thread(ex);
t1.start();
System.out.println("Hi");
}
}
OUTPUT
22 | P a g e
19. WAP to implement interthread communication.
class Customer{
int amount=10000;
synchronized void withdraw(int amount){
System.out.println("going to withdraw...");
if(this.amount<amount){
System.out.println("Less balance; waiting for deposit...");
try{wait();}catch(Exception e){}
}
this.amount-=amount;
System.out.println("withdraw completed...");
}
synchronized void deposit(int amount){
System.out.println("going to deposit...");
this.amount+=amount;
System.out.println("deposit completed... ");
notify();
}
}
class Test{
public static void main(String args[]){
final Customer c=new Customer();
new Thread(){
public void run(){c.withdraw(15000);}
}.start();
new Thread(){
public void run(){c.deposit(10000);}
}
.start();
}}
OUTPUT
going to deposit...10000
deposit completed...15000
23 | P a g e
20. WAP to create a class component that show controls and event handling
on that controls.(math calc).
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implements ActionListener{
TextField tf;
AEvent(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
b.addActionListener(this);//passing current instance
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true); }
public void actionPerformed(ActionEvent e){
tf.setText("Welcome"); }
public static void main(String args[]){
new AEvent(); } }
OUTPUT
24 | P a g e
21. WAP to draw the line, Rectangle, over, text, using the graphics.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class Lro extends JApplet {
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.drawLine(5,30,350,30);
g.setColor(Color.blue);
g.drawRect(5,40,90,55);
g.fillRect(100,40,90,55);
g.setColor(Color.cyan);
g.fillRoundRect(195,40,90,55,50,50);
g.drawRoundRect(290,40,90,55,20,20);
g.setColor(Color.yellow);
g.draw3DRect(5,100,90,55,true);
g.fill3DRect(100,100,90,55,false);
g.setColor(Color.magenta);
g.drawOval(195,100,90,55);
g.fillOval(290,100,90,55); } }
OUTPUT
25 | P a g e
22. WAP to Draw create a menu using the frame.
import javax.swing.*;
class MenuExample {
JMenu menu, submenu;
JMenuItem i1, i2, i3, i4, i5;
MenuExample(){
JFrame f= new JFrame("Menu and MenuItem Example");
JMenuBar mb=new JMenuBar();
menu=new JMenu("Menu");
submenu=new JMenu("Sub Menu");
i1=new JMenuItem("Item 1");
i2=new JMenuItem("Item 2");
i3=new JMenuItem("Item 3");
i4=new JMenuItem("Item 4");
i5=new JMenuItem("Item 5");
menu.add(i1); menu.add(i2); menu.add(i3);
submenu.add(i4); submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true); }
public static void main(String args[]) {
new MenuExample(); }}
OUTPUT
26 | P a g e
23. WAP to create a Dialogbox.
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static Dialog d;
DialogExample() {
Frame f= new Frame();
d = new Dialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
DialogExample.d.setVisible(false); } }
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true); }
public static void main(String args[]) {
new DialogExample(); } }
OUTPUT
27 | P a g e
24. WAP to implement the flow layout and border Layout.
import java.awt.*;
import javax.swing.*;
public class MyFlowLayout{
JFrame f;
MyFlowLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
f.setSize(300,300);
f.setVisible(true); }
public static void main(String[] args) {
new MyFlowLayout(); } }
OUTPUT
28 | P a g e
25. WAP to implement the gridlayout , Cardlayout.
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample {
JFrame frameObj;
GridLayoutExample() {
frameObj = new JFrame();
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);
frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);
frameObj.setLayout(new GridLayout());
frameObj.setSize(300, 300);
frameObj.setVisible(true); }
public static void main(String argvs[]) {
new GridLayoutExample(); } }
29 | P a g e
Cardlayout
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample1 extends JFrame implements ActionL
istener {
CardLayout crd;
JButton btn1, btn2, btn3;
Container cPane;
CardLayoutExample1() {
cPane = getContentPane();
crd = new CardLayout();
cPane.setLayout(crd);
btn1 = new JButton("Apple");
btn2 = new JButton("Boy");
btn3 = new JButton("Cat");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
cPane.add("a", btn1);
cPane.add("b", btn2);
cPane.add("c", btn3); }
public void actionPerformed(ActionEvent e) {
crd.next(cPane); }
public static void main(String argvs[]) {
CardLayoutExample1 crdl = new CardLayoutExample1();
crdl.setSize(300, 300);
crdl.setVisible(true);
crdl.setDefaultCloseOperation(EXIT_ON_CLOSE); } }
30 | P a g e
26. WAP of Awtdemo2 given by me.
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static demo2;
demo2Example() {
Frame f= new Frame();
d = new demo2 (f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
DialogExample.d.setVisible(false); }}
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true); }
public static void main(String args[]) {
new demo2 (); } }
OUTPUT
31 | P a g e
27. WAP to demonstrate system clock.
import java.time.*;
public class systemMethodDemo {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.of("Europe/Paris");
Clock clock = Clock.system(zoneId);
Instant instant = clock.instant();
ZonedDateTime time = instant.atZone(clock.getZone());
System.out.println("Instant for class is " + time.toString());
}}
OUTPUT
instant.atZone(clock.getZone.)
32 | P a g e
28. WAP to create frame that display the student information.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class GFG {
public static void StudentInfo() {
JFrame f = new JFrame("Student Details Form");
JLabel l1, l2, l3, l4, l5;
JTextField t1, t2, t3;
JComboBox j1, j2;
JButton b1, b2;
l1 = new JLabel("Student Name:");
l1.setBounds(50, 50, 100, 30);
l2 = new JLabel("College Email ID:");
l2.setBounds(50, 120, 120, 30);
l3 = new JLabel("Branch:");
l3.setBounds(50, 190, 50, 30);
l4 = new JLabel("Section:");
l4.setBounds(420, 50, 70, 30);
l5 = new JLabel("Mobile No:");
l5.setBounds(420, 120, 70, 30);
t1 = new JTextField();
t1.setBounds(150, 50, 130, 30);
t2 = new JTextField();
t2.setBounds(160, 120, 130, 30);
t3 = new JTextField();
t3.setBounds(490, 120, 130, 30);
String s1[] = { " ", "CSE", "ECE", "EEE", "CIVIL", "MEC", "Others" };
String s2[] = { " ", "Section-A", "Section-B", "Section-C", "Section-D",
"Section-E" };
j1 = new JComboBox(s1);
j1.setBounds(120, 190, 100, 30);
j2 = new JComboBox(s2);
j2.setBounds(470, 50, 140, 30);
33 | P a g e
b1 = new JButton("Save");
b1.setBounds(150, 300, 70, 30);
b2 = new JButton("close");
b2.setBounds(420, 300, 70, 30);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s1 = t1.getText();
String s2 = t2.getText();
String s3 = j1.getSelectedItem() + "";
String s4 = j2.getSelectedItem() + "";
String s5 = t3.getText();
if (e.getSource() == b1) {
try {
FileWriter w= new FileWriter("GFG.txt", true);
w.write(s1 + "\n");
w.write(s2 + "\n");
w.write(s3 + "\n");
w.write(s4 + "\n");
w.write(s5 + "\n");
w.close(); }
catch (Exception ae) {
System.out.println(ae); } }
JOptionPane
.showMessageDialog(f,"Successfully Saved"+ " The Details");
} });
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.dispose(); } });
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0); } });
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(j1);
34 | P a g e
f.add(l4);
f.add(j2);
f.add(l5);
f.add(t3);
f.add(b1);
f.add(b2);
f.setLayout(null);
f.setSize(700, 600);
f.setVisible(true); }
public static void main(String args[]) {
StudentInfo(); } }
OUTPUT
35 | P a g e