BCA SEM III-Laboratory
Subject: Java Programming Lab
List of programs
1. Programs on if-else, if-else-if
2. Program on switch
3. Program on while
4. Program on for loop
5. Program on do-while
6. Program to demonstrate class concept
7. Program to demonstrate methods
8. Program to demonstrate method overloading
9. Program to demonstrate constructors
[Link] to demonstrate constructor overloading
[Link] to demonstrate an Array
[Link] to demonstrate multidimensional array
[Link] to demonstrate Strings
[Link] to demonstrate inheritance
[Link] to demonstrate method overriding
[Link] to demonstrate abstract class
[Link] to demonstrate reading console input
18. Program to demonstrate interfaces
19. Program to demonstrate packages
[Link] to demonstrate exceptional handling
[Link] to demonstrate creating a thread by extending Thread class
[Link] to demonstrate creating a thread by implementing
Runnable interface
[Link] to demonstrate AWT controls
[Link] to demonstrate Layout Manager
[Link] to demonstrate Events
[Link] to demonstrate applets
1) Programs on if-else, if-else-if
a) if-else
public class IfElseExample {
public static void main(String[] args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
[Link]("even number");
}else{
[Link]("odd number");
}
}
}
Output: odd number
b) if-else-if
// Java program to illustrate if-else-if ladder
import [Link].*;
class GFG {
public static void main(String[] args)
{
// initializing expression
int i = 20;
// condition 1
if (i == 10)
[Link]("i is 10\n");
// condition 2
else if (i == 15)
[Link]("i is 15\n");
// condition 3
else if (i == 20)
[Link]("i is 20\n");
else
[Link]("i is not present\n");
[Link]("Outside if-else-if");
}
}
Output: i is 20
Outside if-else-if
2) Program on switch
public class SwitchExample {
public static void main(String[] args) {
int number=2;
switch(number){
case 1: [Link]("1");break;
case 2: [Link]("2");break;
case 3: [Link]("3");break;
default:[Link]("Not in 1, 2 or 3");
}
}
}
Output: 2
3) Program on while
public class WhileExample {
public static void main(String[] args) {
int i=1;
while(i<=10){
[Link](i);
i++;
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10
4) Program on for loop
public class ForExample {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
[Link](i);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10
5) Program on do-while
public class DoWhileExample {
public static void main(String[] args) {
int i=1;
do{
[Link](i);
i++;
}while(i<=10);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
6) Program to demonstrate class concept
class Student{
int id;//field or data member or instance variable
String name;
public static void main(String args[]){
Student s1=new Student();//creating an object of Student
[Link]([Link]);//accessing member through reference variable
[Link]([Link]);
}}
Output: 0
null
7) Program to demonstrate methods (how to define a method and how to call it)
public class ExampleMinNumber {
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
[Link]("Minimum Value = " + c);
}
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
Output: Minimum value = 6
8) Program to demonstrate method overloading
class Multiply {
void mul(int a, int b) {
[Link]("Sum of two=" + (a * b));
}
void mul(int a, int b, int c) {
[Link]("Sum of three=" + (a * b * c));
}
}
class Polymorphism {
public static void main(String args[]) {
Multiply m = new Multiply();
[Link](6, 10);
[Link](10, 6, 5);
}
}
Output: Sum of two=60
Sum of three=300
9)Program to demonstrate constructors
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display()
{[Link](id+" "+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
[Link]();
[Link]();
}
}
Output:
111,Karan
222,Aryan
10)Program to demonstrate constructor overloading
public class Student {
//instance variables of the class
int id;
String name;
Student(){
[Link]("this a default constructor");
}
Student(int i, String n){
id = i;
name = n;
}
public static void main(String[] args) {
//object creation
Student s = new Student();
[Link]("\nDefault Constructor values: \n");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
[Link]("\nParameterized Constructor values: \n");
Student student = new Student(10, "David");
[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
}
}
Output:
this a default constructor
Default Constructor values:
Student Id : 0
Student Name : null
Parameterized Constructor values:
Student Id : 10
Student Name : David
11)Program to demonstrate Array
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//printing array
for(int i=0;i<[Link];i++)//length is the property of array
[Link](a[i]);
}
}
Output:
10
20
70
40
50
12)Program to demonstrate multidimensional array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
[Link](arr[i][j]+" ");
}
[Link]();
}
}}
Output:
123
245
445
13)Program to demonstrate Strings
public class StringExample{
public static void main(String args[]){
String s1="java";
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);
String s3=new String("example");
[Link](s1);
[Link](s2);
[Link](s3);
}}
Output:
java
strings
example
14)Program to demonstrate inheritance
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
[Link]("Programmer salary is:"+[Link]);
[Link]("Bonus of Programmer is:"+[Link]);
}
}
Output:
Programmer salary is:40000.0
Bonus of Programmer is:10000
15)Program to demonstrate method overriding
class Vehicle{
void run(){[Link]("Vehicle is running");}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
[Link]();
}
}
Output:
Vehicle is running
16)Program to demonstrate abstract class
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){[Link]("running safely..");}
public static void main(String args[]){
Bike obj = new Honda4();
[Link]();
}
}
Output:
running safely..
17)Program to demonstrate reading console input
import [Link];
class ReadStringTest{
public static void main(String args[]){
Console c=[Link]();
[Link]("Enter your name: ");
String n=[Link]();
[Link]("Welcome "+n);
}
}
Output:
Enter your name: Ram
Welcome Ram
18)Program to demonstrate interfaces
interface printable{
void print();
}
class A6 implements printable{
public void print(){[Link]("Hello");}
public static void main(String args[]){
A6 obj = new A6();
[Link]();
}
}
Output:
Hello
19)Program to demonstrate Packages
import [Link]; // import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner([Link]);
String userName;
// Enter username and press Enter
[Link]("Enter username");
userName = [Link]();
[Link]("Username is: " + userName);
}
}
Output:
Enter username
John
Username is: John
20)Program to demonstrate Exceptional Handling
public class JavaExceptionExample{
public static void main(String args[]){
try{
int data=100/0;
}catch(ArithmeticException e){[Link](e);}
[Link]("rest of the code...");
}
}
Output:
[Link]: / by zero
rest of the code...
21)Program to demonstrate creating a thread by extending Thread
class
class Multi extends Thread{
public void run(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
[Link]();
}
}
Output:
thread is running...
22)Program to demonstrate creating a thread by implementing
Runnable interface
class Multi3 implements Runnable{
public void run(){
[Link]("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
[Link]();
}
}
Output:
thread is running...
23)Program to demonstrate AWT controls
import [Link].*;
public class AwtProgram1 {
public AwtProgram1()
{
Frame f = new Frame();
Button btn=new Button("Hello World");
[Link](80, 80, 100, 50);
[Link](btn); //adding a new Button.
[Link](300, 250); //setting size.
[Link]("JavaTPoint"); //setting title.
[Link](null); //set default layout for frame.
[Link](true); //set frame visibility true.
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AwtProgram1 awt = new AwtProgram1(); //creating a frame.
}
}
Output:
24)Program to demonstrate Layout Manager
import [Link].*;
import [Link].*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
[Link](b1, [Link]); // b1 will be placed in the North Direction
[Link](b2, [Link]); // b2 will be placed in the South Direction
[Link](b3, [Link]); // b2 will be placed in the East Direction
[Link](b4, [Link]); // b2 will be placed in the West Direction
[Link](b5, [Link]); // b2 will be placed in the Center
[Link](300, 300);
[Link](true);
}
public static void main(String[] args) {
new Border();
}}
Output:
25)Program to demonstrate Events
package [Link];
import [Link].*;
import [Link].*;
public class AwtControlDemo {
private Frame mainFrame;
private Label headerLabel;
private Label statusLabel;
private Panel controlPanel;
public AwtControlDemo(){
prepareGUI();
}
public static void main(String[] args){
AwtControlDemo awtControlDemo = new AwtControlDemo();
[Link]();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
[Link](400,400);
[Link](new GridLayout(3, 1));
[Link](new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
[Link](0);
}
});
headerLabel = new Label();
[Link]([Link]);
statusLabel = new Label();
[Link]([Link]);
[Link](350,100);
controlPanel = new Panel();
[Link](new FlowLayout());
[Link](headerLabel);
[Link](controlPanel);
[Link](statusLabel);
[Link](true);
}
private void showEventDemo(){
[Link]("Control in action: Button");
Button okButton = new Button("OK");
Button submitButton = new Button("Submit");
Button cancelButton = new Button("Cancel");
[Link]("OK");
[Link]("Submit");
[Link]("Cancel");
[Link](new ButtonClickListener());
[Link](new ButtonClickListener());
[Link](new ButtonClickListener());
[Link](okButton);
[Link](submitButton);
[Link](cancelButton);
[Link](true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = [Link]();
if( [Link]( "OK" )) {
[Link]("Ok Button clicked.");
}
else if( [Link]( "Submit" ) ) {
[Link]("Submit Button clicked.");
}
else {
[Link]("Cancel Button clicked.");
}
}
}
}
Output:
26)Program to demonstrate applets
import [Link].*;
import [Link].*;
/*
<applet code ="StatusWindow" width=300 height=50>
</applet>
*/
public class StatusWindow extends Applet
{
public void init ()
{
setBackground ([Link]);
}
public void paint (Graphics g)
{
[Link] ("This is in the applet window", 10, 20);
showStatus ("This is shown in the status window.");
}
}
Output: