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

0_java Record Work 2020-2023-Semester III

This document is a practical record for the Bachelor of Computer Science program at Guru Nanak College, detailing various Java programming exercises completed during the 2021-2022 academic year. It includes a list of application programs and applet programs, along with sample code for each exercise. The document serves as a certification of the practical work done by students in the Computer Science department.

Uploaded by

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

0_java Record Work 2020-2023-Semester III

This document is a practical record for the Bachelor of Computer Science program at Guru Nanak College, detailing various Java programming exercises completed during the 2021-2022 academic year. It includes a list of application programs and applet programs, along with sample code for each exercise. The document serves as a certification of the practical work done by students in the Computer Science department.

Uploaded by

Pradeep Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

GURU NANAK COLLEGE

(AUTONOMOUS)
Chennai – 600 042.

BACHELOR OF COMPUTER SCIENCE

[DEPARTMENT OF COMPUTER SCIENCE]

2021 - 2022
Name :

Reg. No :

Year : II Semester: III

Subject Code :

Subject : PROGRAMMING IN JAVA PRACTICAL


GURU NANAK COLLEGE (AUTONOMOUS)
Chennai – 600 042.

DEPARTMENT OF COMPUTER SCIENCE


BONAFIDE CERTIFICATE
NAME :

REG. NO :

CLASS :

This is to certify that this is the bonafide record of the practical


work done in _________________________________________at
Guru Nanak College Computer Lab, during the Year 2021-2022 .

Staff-In-Charge Head of the Department

Submitted for the


B.Sc., Computer Science Practical Examination held on
at Guru Nanak College, Chennai – 42.

Internal Examiner External Examiner


INDEX

S. DATE PROGRAM TITLE PAGE NO SIGN


NO.
APPLICATION PROGRAMS

1. SIMPLE AND COMPOUND INTEREST

2. LARGEST OF THREE NUMBERS

3. ILLUSTRATION OF CLASS AND OBJECT

4. GENERATE RANDOM NUMBERS

5. CALENDAR CLASS

6. ILLUSTRATION OF CONSTRUCTOR

7. METHOD OVERLOADING

8. ILLUSTRATION OF INHERITANCE

9. METHOD OVERRIDING

10. PACKAGES

11. ILLUSTRATION OF THREAD

ILLUSTRATION OF EXCEPTION
12.
HANDLING

APPLET PROGRAMS

13. VARIOUS SHAPES USING APPLET

14. POINT CLASS MANIPULATION

15. HUMAN FACE

AWT CONTROLS – CHECKBOX, CHOICE,


16.
RADIO BUTTON, LABEL AND TEXTBOX

17. FONT STYLE AND DIFFERENT COLORS

18. PANELS AND LAYOUTS


1. SIMPLE AND COMPOUND INTEREST
import java.io.*;
class interest
{
public static void main(String args[]) throws IOException
{
System.out.println("Simple & Compound Interest");
String s;
double p,n,r,si,ci;
InputStreamReader ips=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ips);
System.out.println("Enter the Principle Amount");
s=br.readLine();
p=Double.parseDouble(s);
System.out.println("Enter the Period of Time in Months");
s=br.readLine();
n=Double.parseDouble(s);
System.out.println("Enter the rate of interest");
s=br.readLine();
r=Double.parseDouble(s);
si=(p*n*r)/100;
ci=(p*(Math.pow((1+(r/100)),n)))-p;
System.out.println("Simple Interest for the Principle amount"+p+ "is" +si);
System.out.println("Compound Interest for the principle amount"+p+"is"+ci);
}
}

OUTPUT
2. LARGEST OF THREE NUMBERS
import java.io.*;
class largest
{
public static void main(String args[])throws IOException
{
String s;
int a,b,c,result;
InputStreamReader ipps=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ipps);
System.out.println("Enter number 1");
s=br.readLine();
a=Integer.parseInt(s);
System.out.println("Enter number 2");
s=br.readLine();
b=Integer.parseInt(s);
System.out.println("Enter number 3");
s=br.readLine();
c=Integer.parseInt(s);
if((a>b)&&(a>c))
{
System.out.println("Result:"+a+"is greater then"+b+"and"+c);
}
else if((b>a)&&(b>c))
{
System.out.println("Result:"+b+"is greater than"+a+"and"+c);
}
else
System.out.println("Result:"+c+"is greater than"+a+"and"+b);
}
}

OUTPUT
3. ILLUSTRATION OF CLASS AND OBJECT
class rectangle
{
int length,width;
void getdata(int x,int y)
{
length=x;
width=y;
}
int rectarea()
{
int area=length*width;
return(area);
}
}
class rectarea
{
public static void main(String args[])
{
int area1,area2;
rectangle rect1=new rectangle();
rectangle rect2=new rectangle();
rect1.length=15;
rect1.width=10;
area1=rect1.length*rect1.width;
rect2.getdata(20,12);
area2=rect2.rectarea();
System.out.println("area1="+area1);
System.out.println("area2="+area2);
}
}

OUTPUT
4. GENERATE RANDOM NUMBERS
import java.util.Random;
public class generateRandom
{
public static void main(String args[])
{
Random rand = new Random();
int rand_int1 = rand.nextInt(1000);
int rand_int2 = rand.nextInt(1000);
System.out.println("Random Integers: "+rand_int1);
System.out.println("Random Integers: "+rand_int2);
double rand_dub1 = rand.nextDouble();
double rand_dub2 = rand.nextDouble();
System.out.println("Random Doubles: "+rand_dub1);
System.out.println("Random Doubles: "+rand_dub2);
}
}

OUTPUT
5. CALENDAR CLASS
import java.util.*;
public class Calendar2 {
public static void main(String[] args)
{
Calendar calendar = Calendar.getInstance();
System.out.println("Current Calendar's Year: " + calendar.get(Calendar.YEAR));
System.out.println("Current Calendar's Day: " + calendar.get(Calendar.DATE));
System.out.println("Current MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("Current SECOND: " + calendar.get(Calendar.SECOND));
}
}

OUTPUT
6. ILLUSTRATION OF CONSTRUCTOR

import java.io.*;
class rectangle {
int length,width;
rectangle(int x,int y)
{
length=x;
width=y;
}
int rectarea(){
return(length*width);
}
}
class rectanglearea{
public static void main(String args[])throws IOException
{
rectangle rect1=new rectangle(15,10);
int area1=rect1.rectarea();
System.out.println("Area1="+area1);
}
}

OUTPUT
7. METHOD OVERLOADING

class overloaddemo{
void test(){
System.out.println("No Parameters");
}
void test(int a)
{
System.out.println("a:"+a);
}
void test(int a,int b)
{
System.out.println("a and b:"+a+" "+b);
}
double test(double a)
{
System.out.println("double a:"+a);
return a*a;
}
}
class overload{
public static void main(String args[]){
overloaddemo ob=new overloaddemo();
double result;
ob.test();
ob.test(10);
ob.test(10,20);
result=ob.test(123.25);
System.out.println("Result of ob.test(123.25):"+result);
}
}

OUTPUT
8. ILLUSTRATION OF INHERITANCE
class room
{
int length;
int breadth;
room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}
}
class sroom extends room
{
int height;
sroom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
class inher
{
public static void main(String args[])
{
sroom room1=new sroom(14,12,10);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("Area1="+area1);
System.out.println("Volume="+volume1);
}
}

OUTPUT
9. METHOD OVERRIDING

class A{
int i,j;
A(int a,int b){
i=a;
j=b;
}
void show(){
System.out.println("i and j:"+i+" "+j);
}
}
class B extends A{
int k;
B(int a,int b,int c){
super(a,b);
k=c;
}
void show(){
System.out.println("k:"+k);
}
}
class override{
public static void main(String args[]){
B subob=new B(1,2,3);
subob.show();
}
}

OUTPUT
10. PACKAGES
classA.java

package package1;
public class classA
{
public void displayA()
{
System.out.println("Class A");
}
}

Packagetest1.java

import package1.*;
class packagetest1
{
public static void main(String args[])
{
classA objA=new classA();
objA.displayA();
}
}

OUTPUT
11. ILLUSTRATION OF THREAD

class NewThread extends Thread{


NewThread(){
super("Demo Thread");
System.out.println("Child Thread:"+this);
start();
}
public void run(){
try{
for(int i=5;i>0;i--)
{
System.out.println("Child Thread:" +i);
Thread.sleep(500);
}}
catch(InterruptedException e){
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}}
class ExtendThread
{
public static void main(String args[])
{
new NewThread();
try{
for(int i=5;i>0;i--)
{
System.out.println("Main Thread:" +i);
Thread.sleep(1000);
}}
catch(InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting");
}}
OUTPUT
12. ILLUSTRATION OF EXCEPTION HANDLING

class exceptiontest{
public static void main(String args[])
{
int d,a;
try{
d=0;
a=42/d;
System.out.println("This will not be printed");
}catch(ArithmeticException e)
{
System.out.println("Division by Zero");
}
System.out.println("After catch statement");
}
}

OUTPUT
13. VARIOUS SHAPES USING APPLET
import java.awt.*;
import java.applet.*;
/*
<applet code="LineRect.java" width=250 height=200>
</applet>
*/
public class LineRect extends Applet{
public void paint(Graphics g){
g.drawLine(10,10,10,50);
g.drawLine(20,30,50,30);
g.drawRect(10,60,40,30);
g.fillRect(60,10,30,80);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(20,110,60,30,5,5);
g.drawLine(100,10,230,140);
g.drawLine(100,140,230,10);
}}

OUTPUT
14. POINT CLASS MANIPULATION

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="impoint.java" width=200 height=200>
</applet>*/
public class impoint extends Applet
{
Point p;
int w=0;
int h=0;
int x1=100;
int y1=100;
Image theImage;
public void init()
{
p=new Point(10,10);
theImage=getImage(getDocumentBase(),"Tulips.jpg");
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
p.move(me.getX(),me.getY());
x1=p.x;
y1=p.y;
w+=5;
h+=5;
repaint();
}});}
public void paint(Graphics g)
{
g.drawImage(theImage,x1,y1,w,h,this);
showStatus(getCodeBase().toString());
}}

OUTPUT
15. HUMAN FACE

import java.awt.*;
import java.applet.*;
/*
<applet code="smile" width=550 height=450>
</applet>
*/
public class smile extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.yellow);
g.fillOval(200,150,200,200);
g.setColor(Color.black);
g.fillOval(240,200,20,20);
g.fillOval(335,200,20,20);
g.drawLine(300,230,300,260);
g.drawArc(250,250,100,60,180,180);
g.setColor(Color.yellow);
g.fillOval(393,200,28,28);
g.fillOval(179,200,28,28);
}
}

OUTPUT
16. AWT CONTROLS – CHECKBOX, CHOICE, RADIO
BUTTON, LABEL AND TEXTBOX

CHECKBOX

import java.awt.*;
public class CheckboxExample
{
CheckboxExample(){
Frame f= new Frame("Checkbox Example");
Checkbox checkbox1 = new Checkbox("C++");
checkbox1.setBounds(100,100, 50,50);
Checkbox checkbox2 = new Checkbox("Java", true);
checkbox2.setBounds(100,150, 50,50);
f.add(checkbox1);
f.add(checkbox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxExample();
}
}
OUTPUT
CHECKBOXGROUP

import java.awt.*;
public class CheckboxGroupExample
{
CheckboxGroupExample(){
Frame f= new Frame("CheckboxGroup Example");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox checkBox1 = new Checkbox("C++", cbg, false);
checkBox1.setBounds(100,100, 50,50);
Checkbox checkBox2 = new Checkbox("Java", cbg, true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckboxGroupExample();
}
}
OUTPUT
LABEL

import java.awt.*;
class LabelExample{
public static void main(String args[]){
Frame f= new Frame("Label Example");
Label l1,l2;
l1=new Label("First Label.");
l1.setBounds(50,100, 100,30);
l2=new Label("Second Label.");
l2.setBounds(50,150, 100,30);
f.add(l1); f.add(l2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

OUTPUT
TEXT FIELD

import java.awt.*;
class TextFieldExample
{
public static void main(String args[])
{
Frame f= new Frame("TextField Example");
TextField t1,t2;
t1=new TextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
t2=new TextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

OUTPUT
CHOICE

import java.awt.*;
import java.awt.event.*;
public class ChoiceExample
{
ChoiceExample(){
Frame f= new Frame();
Choice c=new Choice();
c.setBounds(100,100, 75,75);
c.add("Item 1");
c.add("Item 2");
c.add("Item 3");
c.add("Item 4");
c.add("Item 5");
f.add(c);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new ChoiceExample();
}
}

OUTPUT

\
17. FONT STYLE AND DIFFERENT COLORS

import java.awt.*;
import java.applet.*;
/*
<applet code="fontsize" width=300 height=400>
</applet>
*/
public class fontsize extends Applet
{Font f;
int size=8;
int flag=0;
public void paint(Graphics g){
if(flag==0){
g.setColor(Color.green);
f=new Font("Courier New",Font.BOLD,size);
g.setFont(f);
}
else
{g.setColor(Color.red);
f=new Font("Times New Roman",Font.BOLD,size);
g.setFont(f);
}
g.drawString("Welcome",0,400);
flag=1-flag;
size++;
try{
Thread.sleep(500);
}catch(Exception e){
}
repaint();
}}
OUTPUT
18. PANELS AND LAYOUTS
import java.awt.*;
import java.applet.*;
/*
<applet code="BorderLayoutJavaExample" width=550 height=450>
</applet>
*/
class BorderLayoutExample extends Frame
{
BorderLayoutExample()
{
setLayout(new BorderLayout());
add(new Button("NORTH"),BorderLayout.NORTH);
add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("EAST"),BorderLayout.EAST);
add(new Button("WEST"),BorderLayout.WEST);
add(new Button("CENTER"),BorderLayout.CENTER);
}
}
class BorderLayoutJavaExample
{
public static void main(String args[])
{
BorderLayoutExample frame = new BorderLayoutExample();
frame.setTitle("BorderLayout in Java Example");
frame.setSize(400,150);
frame.setVisible(true);
}

OUTPUT

You might also like