Professional Documents
Culture Documents
Vivekananda Institute of Professional Studies
Vivekananda Institute of Professional Studies
Vivekananda Institute of Professional Studies
Page | 1
Himanshu Tandon
08429802019
BCA 4EB
INDEX
S.NO Date Teacher’s
. PROGRAM Signature
Write a program to print all odd numbers 12-03-21
1. between 1 to 10.
Write a program to find out factorial of a number 12-03-21
2. through recursion.
Page | 2
Himanshu Tandon
08429802019
BCA 4EB
Write a program to implement Run time 26-03-21
12 polymorphism.
Write a program to implement interface. Create an 3-04-21
interface named
Shape having area () & perimeter () as its methods.
13 Create three
classes circle, rectangle & square that implement
this interface.
Page | 4
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #1
1. Write a program to print all odd numbers between 1 to 10.
Ans.
Source Code
class oddnumber {
public static void main(String args[])
{
int n = 10;
System.out.print("Odd Numbers from 1 to "+n+" are: ");
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}
Page | 5
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 6
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #2
2. Write a program to find out factorial of a number through recursion.
Ans.
Source Code
class factorial
{
static int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[])
{
int i,fact=1;
int number=6;
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
}
Page | 7
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 8
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #3
3. Write a program to accept Command line arguments & print them.
Ans.
Source Code
class commandline
{
public static void main(String args[]){
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
Page | 9
Himanshu Tandon
08429802019
BCA 4EB
Output
PRACTICAL #4
Page | 10
Himanshu Tandon
08429802019
BCA 4EB
4. Write a program to print Fibonacci series.
Ans.
Source Code
class fibonacci
{
public static void main(String args[])
{
int n1=0,n2=1,n3,i,count=20;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}}
Output
Page | 11
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #5
Page | 12
Himanshu Tandon
08429802019
BCA 4EB
5. Write a program to obtain a number by a user & check if it’s prime or
not.
Ans.
Source Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class prime
{
public static void main(String args[]) throws IOException
{
int i,m=0,flag=0;
BufferedReader reader =new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a Number: ");
int n = Integer.parseInt(reader.readLine());
m=n/2;
if(n==0||n==1)
{
System.out.println(n+" is not prime number");
}
else
{
for(i=2;i<=m;i++)
{
if(n%i==0)
Page | 13
Himanshu Tandon
08429802019
BCA 4EB
{
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(n+" is prime number"); }
}
}
}
Output
Page | 14
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #6
6. Write a program that creates a class Accounts with following details:
Instance variables: ac_no., name, ac_name, balance
Methods: withdrawal (), deposit (), display ().Use constructors to
Page | 15
Himanshu Tandon
08429802019
BCA 4EB
initialize members.
Ans.
Source Code
class accounts
{
public int ac_no;
public String name;
public String ac_name;
public float balance;
accounts()
{
ac_no=6969;
name="abc";
ac_name="bank";
balance=5000;
}
void display()
{
System.out.println("Account number=" + ac_no);
System.out.println("Name = " +name);
System.out.println("Account name =" +ac_name);
System.out.println("balance = " +balance);
}
}
class account
{
public static void main(String args[])
{
accounts a=new accounts();
a.display();
a.deposit(50000);
a.display();
a.withdraw(11000);
Page | 17
Himanshu Tandon
08429802019
BCA 4EB
a.display();
}
}
Output
Page | 18
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #7
7. Write a program to implement constructor overloading.
Ans.
Page | 19
Himanshu Tandon
08429802019
BCA 4EB
Source Code
Output
Page | 21
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #8
8. Write a program to count the no. of objects created in a program.
Page | 22
Himanshu Tandon
08429802019
BCA 4EB
Ans.
Source Code
public class number
{
static int count=0;
number()
{
count++;
}
public static void main(String[] args)
{
number obj1 = new number();
number obj2 = new number();
number obj3 = new number();
number obj4 = new number();
System.out.println("Number of objects created:"+count);
}
}
Output
Page | 23
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #9
9. Write a program to show call by value & call by reference.
Page | 24
Himanshu Tandon
08429802019
BCA 4EB
Ans.
Call By Value
Source Code
public class tester
{
public static void main(String[] args)
{
int a = 90;
int b = 58;
System.out.println("Before swapping, a = " + a + " and b = " + b);
swapFunction(a, b);
System.out.println("\n*Now, Before and After swapping values will be
same here*:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
public static void swapFunction(int a, int b)
{
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}
Output
Page | 25
Himanshu Tandon
08429802019
BCA 4EB
Call By Reference
Source Code
public class tester
Page | 26
Himanshu Tandon
08429802019
BCA 4EB
{
public static void main(String[] args)
{
int a = 90;
int b = 58;
System.out.println("Before swapping, a = " + a + " and b = " + b);
swapFunction(a, b);
System.out.println("\n*Now, Before and After swapping values will be
same here*:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
public static void swapFunction(int a, int b)
{
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}
Output
Page | 27
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #10
10. Write a program to implement method over ridding &
method
overloading..
Page | 28
Himanshu Tandon
08429802019
BCA 4EB
Ans.
Method Overriding
Source Code
class vehicle
{
void run(){System.out.println("Vehicle is running");}
}
class bike extends vehicle
{
public static void main(String args[])
{
bike obj = new bike();
obj.run();
}
}
Output
Page | 29
Himanshu Tandon
08429802019
BCA 4EB
Method Overloading: Changing no. of arguments
Source Code
Page | 30
Himanshu Tandon
08429802019
BCA 4EB
class adder
{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class testoverloading
{
public static void main(String[] args)
{
System.out.println(adder.add(20,22));
System.out.println(adder.add(91,34,32));
}
}
Output
Page | 31
Himanshu Tandon
08429802019
BCA 4EB
Method Overloading: Changing data type of arguments
Source Code
class adder
Page | 32
Himanshu Tandon
08429802019
BCA 4EB
{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class testoverloading2
{
public static void main(String[] args)
{
System.out.println(adder.add(45,71));
System.out.println(adder.add(13.3,62.6));
}
}
Output
Page | 33
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #11
11. Create a class box having height, width, depth as the instance
variables & calculate its volume. Implement constructor
Page | 34
Himanshu Tandon
08429802019
BCA 4EB
overloading in it. Create a subclass named box_new that has
weight as an instance variable. Use super in the box_new class
to initialize members of the base class.
Ans.
Source Code
class box
{
private float depth,vol,height,width;
public box(float x,float y,float z)
{
height=x;
width=y;
depth=z;
System.out.println("height is "+height+"m");
System.out.println("width is "+width+"m");
System.out.println("depth is "+depth+"m");
}
public box()
{
height=0;
width=0;
depth=0;
}
public void volume()
{
vol=height*width*depth;
System.out.println("volume is "+vol+"m^3");
}
}
class boxnew extends box
{
private float weight;
public boxnew(float x,float y,float z,float w)
{
super(x,y,z);
weight=w;
System.out.println("weight is "+weight+"kg");
}
}
class prog11
{
Page | 35
Himanshu Tandon
08429802019
BCA 4EB
public static void main(String[]args)
{
boxnew b1=new boxnew(3,5,7,50);
b1.volume();
}
}
Output
Page | 36
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #12
12. Write a program to implement Run time polymorphism.
Page | 37
Himanshu Tandon
08429802019
BCA 4EB
Ans.
Source Code
class car
{
public void move()
{
System.out.println("Cars can move fast");
}
}
class bike extends car
{
public void move()
{
System.out.println("Bikes can move faster");
}
}
public class testbike
{
public static void main(String args[])
{
car a = new car();
car b = new bike();
a.move();
b.move();
}
}
Output
Page | 38
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #13
13. Write a program to implement interface. Create an interface
named
Page | 39
Himanshu Tandon
08429802019
BCA 4EB
Shape having area () & perimeter () as its methods. Create three
classes circle, rectangle & square that implement this interface.
Ans.
Source Code
interface Shape
{
void area();
void perimeter();
}
class circle implements Shape
{
int r = 0;
double pi = 3.14, ar = 0,p=0;
public void input(int a)
{
r = a;
}
Page | 40
Himanshu Tandon
08429802019
BCA 4EB
System.out.println("Perimeter of circle: "+p);
}
}
class rectangle implements Shape
{
int l = 0, b = 0;
double ar,pe;
l = a;
b = x;
}
public void area()
{
ar = l * b;
System.out.println("Area of rectangle: "+ar);
}
public void perimeter()
{
pe=2*(l+b);
System.out.println("Perimeter of rectangle: "+pe);
}
}
Page | 41
Himanshu Tandon
08429802019
BCA 4EB
class square implements Shape
{
int s = 0;
double ar,pe;
s = a;
}
public void area()
{
ar = s * s;
System.out.println("Area of square: "+ar);
}
public void perimeter()
{
pe=4*s;
System.out.println("Perimeter of square: "+pe);
}
}
public class prog
Page | 42
Himanshu Tandon
08429802019
BCA 4EB
{
public static void main(String[] args)
{ circle c=new circle();
square sq=new square();
rectangle rect = new rectangle();
c.input(8);
c.area();
c.perimeter();
sq.input(6);
sq.area();
sq.perimeter();
rect.input(5,3);
rect.area();
rect.perimeter();
}
}
Output
Page | 43
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #14
14. Write a program to show multiple inheritance.
Page | 44
Himanshu Tandon
08429802019
BCA 4EB
Ans.
Source Code
interface cardrive
{
void drive();
}
interface carstop
{
void stop();
}
class car implements cardrive, carstop
{
public void drive()
{
System.out.println("Car is moving");
}
public void stop()
{
System.out.println("Car is stoping");
}
}
public class multipleinheritance
{
public static void main(String args[])
{
car a = new car();
a.drive();
a.stop();
}
}
Output
Page | 45
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #15
15. Write a program to implement exception handling. Use try, catch &
finally.
Page | 46
Himanshu Tandon
08429802019
BCA 4EB
Ans.
Source Code
class main
{
public static void main (String[] args)
{
int[] myArr = new int[20];
try
{
int i = myArr[20];
Output
Page | 47
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #16
16. Create a user defined exception named “NoMatchException”
that is fired when the string entered by the user is not “india”.
Ans.
Source Code
Page | 48
Himanshu Tandon
08429802019
BCA 4EB
import java.io.DataInputStream;
import java.io.IOException;
class NoMatcher
{
public static void main(String[] a) throws NoMatchException
{
IndiaAssertComparer v = new IndiaAssertComparer("ThaiLand");
}
}
Output
Page | 49
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #17
17. Write a program to show even & odd numbers by
thread.
Ans.
Source Code
Page | 50
Himanshu Tandon
08429802019
BCA 4EB
class OddEven implements Runnable
{
public int PRINT_NUMBERS_UPTO=20;
static int n=1;
int r;
static Object lock=new Object();
OddEven(int r)
{
this.r=r;
}
t1.start();
t2.start();
}
}
Output
Page | 52
Himanshu Tandon
08429802019
BCA 4EB
Page | 53
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #18
18. Write a program that draws different color shapes on an applet .Set the
foreground & background color as red & blue.
Ans.
Source Code
import java.applet.*;
import java.awt.*;
/*
* <applet code="eighteen" width=400 height=400>
* </applet>
*/
Page | 54
Himanshu Tandon
08429802019
BCA 4EB
g.drawRect(20, 150, 60, 50);
g.fillRect(20, 150, 60, 50);
g.drawRoundRect(20, 240, 80, 60, 8, 8);
g.fillRoundRect(20, 240, 80, 60, 8, 8);
g.drawPolygon(new int[] {300,350,240,350,400}, new int[]
{300,300,350,350,325}, 5);
g.fillPolygon(new int[] {300,350,240,350,400}, new int[]
{300,300,350,350,325}, 5);
g.drawOval(350, 350, 200, 120);
g.fillOval(350, 350, 200, 120);
}
}
Page | 55
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 56
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #19
19. Write a program to show moving banner by applet.
Ans.
Source Code
import java.applet.*;
import java.awt.*;
import java.lang.Thread;
/**
* <applet code="ninteen" width=850 height=180>
* </applet>
*/
}
public void stop() {
stopFlag = true;
thread = null;
}
}
Output
Page | 58
Himanshu Tandon
08429802019
BCA 4EB
Page | 59
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #20
20. Write a program to implement Matrix multiplication by 2D
array.
Ans.
Source Code
public class MatrixMulti {
Page | 61
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #21
21. Write a program to implement Vector.
[Use: addElement(),elementAt().removeElement(),size().]
Ans.
Source Code
import java.util.Vector;
{
public static void main(String args[])
{
Vector vect1 = new Vector ();
vect1.add("A");
vect1.add("B");
vect1.add("072");
vect1.add(0,"C");
System.out.println("Vector A: " +vect1);
vect2.clear();
System.out.println("After Clear vectB : " +vect2);
System.out.println("Vector A Size: " +vect1.size());
}
}
Page | 62
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 63
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #22
22. Write a program to demonstrate the use of equals() ,trim()
,length(),substring(), compareTo() of String class.
Ans.
Source Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class twentytwo {
public static void main(String[] args) throws IOException {
InputStreamReader inp = new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(inp);
System.out.println("Enter String 1 : ");
String str1 = in.readLine();
System.out.println("Enter String 2 : ");
String str2 = in.readLine();
if(str1.equals(str2)) {
System.out.println("Both the strings are same. ");
}
else {
System.out.println("The strings aren't same. ");
}
str1 = str1.trim();
System.out.println("Trimmed string is : " + str1);
Page | 64
Himanshu Tandon
08429802019
BCA 4EB
System.out.println("Length of string 1 is : " + str1.length());
System.out.println("Length of string 2 is : " + str2.length());
Page | 65
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 66
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #23
23. Write a program to implement file handling(both reading &
writing to a file)
Ans.
Source Code
import java.io.*;
import java.util.*;
// write to a file
try {
FileWriter fileWriter = new FileWriter("txtfile.txt");
fileWriter.write("Hello I am Himanshu Tandon and we are handling files.
\nOkay bye");
fileWriter.close();
} catch (IOException e) {
Page | 67
Himanshu Tandon
08429802019
BCA 4EB
e.printStackTrace();
}
// Reading a file
System.out.println("Displaying the contents of the file: ");
File f = new File("txtfile.txt");
try {
FileReader reader = new FileReader(f);
int character;
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Page | 68
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 69
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #24
24. Write a program to implement all mouse events and mouse
motion
events.
Ans.
Source Code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* <applet code="twentyfour" height=400 width=400>
* </applet>
*/
Page | 71
Himanshu Tandon
08429802019
BCA 4EB
public void mousePressed(MouseEvent mEvent) {
msg = "MOUSE PRESSED";
dragMode = false;
repaint();
}
public void mouseReleased(MouseEvent mEvent) {
msg = "MOUSE RELEASED";
dragMode = false;
for(int i = 0; i < 9999999 ; i++) {
l.setText("MOUSE RELEASED");
}
repaint();
}
public void mouseMoved(MouseEvent mEvent) {
msg = "MOUSE MOVED";
X = mEvent.getX();
Y = mEvent.getY();
dragMode = false;
repaint();
}
public void mouseDragged(MouseEvent mEvent) {
msg = "$$";
dragMode = true;
X = mEvent.getX();
Y = mEvent.getY();
repaint();
}
}
Page | 72
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 73
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #25
25. Write a program to implement keyboard events.
Ans.
Source Code
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* <applet code="twentyfive" height=500 width=500>
* </applet>
*/
Page | 74
Himanshu Tandon
08429802019
BCA 4EB
status = "keyPressed";
int key = ke.getKeyCode();
switch(key) {
case KeyEvent.VK_UP:
Y--;
break;
case KeyEvent.VK_DOWN:
Y++;
break;
case KeyEvent.VK_LEFT:
X--;
break;
case KeyEvent.VK_RIGHT:
X++;
break;
}
repaint();
}
public void keyTyped(KeyEvent ke) {
status = "keyTyped";
repaint();
}
public void keyReleased(KeyEvent ke) {
status = "keyReleased";
repaint();
}
}
Page | 75
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 76
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #26
26. Write a program using AWT to create a simple calculator.
Ans.
Source Code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* <applet code="twentysix" height=400 width=200>
* </applet>
*/
Page | 77
Himanshu Tandon
08429802019
BCA 4EB
public void init() {
addButton.setFont(MyFont);
subButton.setFont(MyFont);
mulButton.setFont(MyFont);
divButton.setFont(MyFont);
calcButton.setFont(MyFont);
allClrButton.setFont(MyFont);
add(addButton);
add(subButton);
add(mulButton);
add(divButton);
add(calcButton);
add(allClrButton);
addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
Page | 78
Himanshu Tandon
08429802019
BCA 4EB
divButton.addActionListener(this);
calcButton.addActionListener(this);
allClrButton.addActionListener(this);
box1.setFont(MyFont);
result.setFont(MyFont);
message.setFont(MyFont);
add(result);
add(message);
setLayout(null);
Page | 79
Himanshu Tandon
08429802019
BCA 4EB
}
}
public void actionPerformed(ActionEvent ae) {
if(box1.getText().length() != 0) {
String operation = ae.getActionCommand();
executeOperation(operation);
}
if(ae.getActionCommand() == "AC")
reset();
}
public void keyPressed(KeyEvent keyEvent) {
}
public void keyReleased(KeyEvent keyEvent) {
Page | 81
Himanshu Tandon
08429802019
BCA 4EB
}
public void keyTyped(KeyEvent keyEvent) {
Page | 82
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 83
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #27
27. Create a login form using AWT controls like labels,buttons,
textboxes, checkboxes, list, checkboxgroup. The selected
checkbox item names should be displayed.
Ans.
Source Code
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
* <applet code="twentyseven" height=500 width=430>
* </applet>
*/
class User {
private String name;
private String password;
private String posting;
private String[] luggage;
public User() {
name = password = posting = "-/NA/-";
luggage = null;
}
public User(String name, String password, String posting, String[] luggage) {
this.name = name;
this.password = password;
Page | 84
Himanshu Tandon
08429802019
BCA 4EB
this.posting = posting;
this.luggage = luggage;
}
public String name() {
return this.name;
}
public String password() {
return this.password;
}
public String posting() {
return posting;
}
public String[] luggage() {
return luggage;
}
}
class MyString {
public static String myJoin(char separator, String[] sArray) {
String joinedString = new String();
for (int i = 0 ; i < sArray.length-1 ; i++) {
joinedString += sArray[i] + separator;
}
joinedString = joinedString.substring(0, joinedString.length() - 1);
return joinedString;
}
}
Page | 85
Himanshu Tandon
08429802019
BCA 4EB
class Vector {
User[] array_vect;
int Length;
public Vector() {
array_vect = null;
Length = -1;
}
public Vector(User[] arr) {
array_vect = arr;
Length = arr.length;
}
Page | 86
Himanshu Tandon
08429802019
BCA 4EB
if(array_vect[i].name().equals(item.name())) {
return true;
}
i++;
}
return false;
}
public String addElement(User element) {
if(this.Contains(element)) {
return "MESSAGE:User already exists";
} else {
User[] temp_arr = new User[++Length];
for(int i = 0 ; i < Length ; i++) {
try {
temp_arr[i] = array_vect[i];
} catch (Exception e) {
temp_arr[i] = element;
}
}
array_vect = temp_arr;
return "MESSAGE:User added.";
}
}
Page | 88
Himanshu Tandon
08429802019
BCA 4EB
System.out.print(array_vect[i].name() + " ");
}
System.out.println();
}
}
Vector userList;
Label headLabel,uNameLabel, pswdLabel;
TextField uNameTField, pswdTField;
Button signInButton,signUpButton;
CheckboxGroup dutyLocations;
Checkbox loc1,loc2,loc3,loc4;
List luggageItemList;
TextArea details;
Font font = new Font("Consolas",Font.BOLD,28);
Font font2 = new Font("Arial",Font.PLAIN,18);
boolean signup=false,signin=false,loginSuccess=false;
Page | 89
Himanshu Tandon
08429802019
BCA 4EB
headLabel.setBounds(80, 5, 320, 80);
headLabel.setFont(font);
add(headLabel);
Page | 92
Himanshu Tandon
08429802019
BCA 4EB
if(buttonName == "Sign-Up") {
signup = true;
signin = false;
SignUp(tempUser);
} else if(buttonName == "Sign-In") {
signin = true;
signup = false;
SignIn(tempUser);
}
}
public void SignIn(User tempUser) {
boolean state = userList.validateUser(tempUser);
if(state) {
showStatus("User signed in successfully");
String userData = "Name: " + tempUser.name() + "\n"
+ "Posting: " + tempUser.posting() + "\n"
+ "Luggage: " + MyString.myJoin(',', tempUser.luggage()) +
"\n";
details.setText(userData);
}
else {
Page | 93
Himanshu Tandon
08429802019
BCA 4EB
showStatus("Login info is incorrect or not available.");
}
}
}
public void mouseDragged(MouseEvent mEvent) {
}
public void mouseMoved(MouseEvent mEvent) {
//showStatus("("+mEvent.getX()+","+mEvent.getY()+")");
}
public void destroy() {
userList = null;
this.removeAll();
}
}
Page | 94
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 95
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #28
28. Write a program to show all Layout managers.
Ans.
Source Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* <applet code="twentyeight" width=400 height=400>
* </applet>
*/
b1 = new JButton("JButton1");
b2 = new JButton("JButton2");
b3 = new JButton("JButton3");
b4 = new JButton("JButton4");
fl = new FlowLayout(FlowLayout.CENTER);
getContentPane().add(p);
Page | 96
Himanshu Tandon
08429802019
BCA 4EB
p.setLayout(fl);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
}
}
Output
Page | 97
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #29
29. Write a program to show all Layout managers.
Ans.
Source Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* <applet code="twentynine" width=400 height=400>
* </applet>
*/
b1 = new JButton("JButton1");
b2 = new JButton("JButton2");
b3 = new JButton("JButton3");
b4 = new JButton("JButton4");
gl = new GridLayout(2,2);
getContentPane().add(p);
p.setLayout(gl);
Page | 98
Himanshu Tandon
08429802019
BCA 4EB
p.add(" 1",b1);
p.add(" 2",b2);
p.add(" 3",b3);
p.add(" 4",b4);
}
}
Output
Page | 99
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #30
30. Write a program to show all Layout managers.
Ans.
Source Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* <applet code="thirty" width=400 height=400>
* </applet>
*/
b1 = new JButton("JButton1");
b2 = new JButton("JButton2");
b3 = new JButton("JButton3");
b4 = new JButton("JButton4");
b5 = new JButton("JButton5");
bl = new BorderLayout();
Page | 100
Himanshu Tandon
08429802019
BCA 4EB
getContentPane().add(p);
p.setLayout(bl);
p.add(b1,BorderLayout.NORTH);
p.add(b2,BorderLayout.SOUTH);
p.add(b3,BorderLayout.EAST);
p.add(b4,BorderLayout.WEST);
p.add(b5,BorderLayout.CENTER);
}
}
Output
Page | 101
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #31
31. Write a program to show all Layout managers.
Ans.
Source Code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/**
* <applet code="thirtyone" width=400 height=400>
* </applet>
*/
b1 = new JButton("JButton1");
b2 = new JButton("JButton2");
b3 = new JButton("JButton3");
b4 = new JButton("JButton4");
cl = new CardLayout(10,10);
Page | 102
Himanshu Tandon
08429802019
BCA 4EB
getContentPane().add(p);
p.setLayout(cl);
p.add(" 1",b1);
p.add(" 2",b2);
p.add(" 3",b3);
p.add(" 4",b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
cl.next(p);
}
}
Page | 103
Himanshu Tandon
08429802019
BCA 4EB
Output
Page | 104
Himanshu Tandon
08429802019
BCA 4EB
PRACTICAL #35
35. Create a Java applet with three buttons ‘Red’,’Green’,’Blue’.
Whenever user press any button the corresponding color should
be seen as background color in an applet window.
Ans.
Source Code
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* <applet code="thirtyfive" height=400 width=400>
* </applet>
*/
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void paint(Graphics g) {
setBackground(backColor);
g.setFont(MyFont);
g.drawString(message, 60, 60);
}
public void actionPerformed(ActionEvent ae) {
String actionCommand = ae.getActionCommand();
if(actionCommand == "RED") {
message = "RED BUTTON PRESSED";
backColor = Color.red;
} else if(actionCommand == "GREEN") {
message = "GREEN BUTTON PRESSED";
backColor = Color.green;
} else if(actionCommand == "BLUE") {
message = "BLUE BUTTON PRESSED";
backColor = Color.blue;
}
Page | 106
Himanshu Tandon
08429802019
BCA 4EB
repaint();
}
}
Output
Page | 107
Himanshu Tandon
08429802019
BCA 4EB