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
10.Program to demonstrate constructor overloading
11.Program to demonstrate an Array
12.Program to demonstrate multidimensional array
13.Program to demonstrate Strings
14.Program to demonstrate inheritance
15.Program to demonstrate method overriding
16.Program to demonstrate abstract class
17.Program to demonstrate reading console input
18. Program to demonstrate interfaces
19. Program to demonstrate packages
20.Program to demonstrate exceptional handling
21.Program to demonstrate creating a thread by extending Thread class
22.Program to demonstrate creating a thread by implementing
Runnable interface
23.Program to demonstrate AWT controls
24.Programs to demonstrate Layout Manager
25.Program to demonstrate Events
26.Program 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){
System.out.println("even number");
}else{
System.out.println("odd number");
}
}
}
Output: odd number
b) if-else-if
// Java program to illustrate if-else-if ladder
import java.io.*;
class GFG {
public static void main(String[] args)
{
// initializing expression
int i = 20;
// condition 1
if (i == 10)
System.out.println("i is 10\n");
// condition 2
else if (i == 15)
System.out.println("i is 15\n");
// condition 3
else if (i == 20)
System.out.println("i is 20\n");
else
System.out.println("i is not present\n");
System.out.println("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: System.out.println("1");break;
case 2: System.out.println("2");break;
case 3: System.out.println("3");break;
default:System.out.println("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){
System.out.println(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++){
System.out.println(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{
System.out.println(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
System.out.println(s1.id);//accessing member through reference variable
System.out.println(s1.name);
}}
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);
System.out.println("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) {
System.out.println("Sum of two=" + (a * b));
}
void mul(int a, int b, int c) {
System.out.println("Sum of three=" + (a * b * c));
}
}
class Polymorphism {
public static void main(String args[]) {
Multiply m = new Multiply();
m.mul(6, 10);
m.mul(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()
{System.out.println(id+" "+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
Output:
111,Karan
222,Aryan
10)Program to demonstrate constructor overloading
public class Student {
//instance variables of the class
int id;
String name;
Student(){
System.out.println("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();
System.out.println("\nDefault Constructor values: \n");
System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);
System.out.println("\nParameterized Constructor values: \n");
Student student = new Student(10, "David");
System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
}
}
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<a.length;i++)//length is the property of array
System.out.println(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++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
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");
System.out.println(s1);
System.out.println(s2);
System.out.println(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();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output:
Programmer salary is:40000.0
Bonus of Programmer is:10000
15)Program to demonstrate method overriding
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:
Vehicle is running
16)Program to demonstrate abstract class
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely..");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}
Output:
running safely..
17)Program to demonstrate reading console input
import java.io.Console;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("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(){System.out.println("Hello");}
public static void main(String args[]){
A6 obj = new A6();
obj.print();
}
}
Output:
Hello
19)Program to demonstrate Packages
import java.util.Scanner; // import the Scanner class
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
String userName;
// Enter username and press Enter
System.out.println("Enter username");
userName = myObj.nextLine();
System.out.println("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){System.out.println(e);}
System.out.println("rest of the code...");
}
}
Output:
java.lang.ArithmeticException: / by zero
rest of the code...
21)Program to demonstrate creating a thread by extending Thread
class
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Output:
thread is running...
22)Program to demonstrate creating a thread by implementing
Runnable interface
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
t1.start();
}
}
Output:
thread is running...
23)Program to demonstrate AWT controls
import java.awt.*;
public class AwtProgram1 {
public AwtProgram1()
{
Frame f = new Frame();
Button btn=new Button("Hello World");
btn.setBounds(80, 80, 100, 50);
f.add(btn); //adding a new Button.
f.setSize(300, 250); //setting size.
f.setTitle("JavaTPoint"); //setting title.
f.setLayout(null); //set default layout for frame.
f.setVisible(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 java.awt.*;
import javax.swing.*;
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
f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction
f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}}
Output:
25)Program to demonstrate Events
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
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();
awtControlDemo.showEventDemo();
}
private void prepareGUI(){
mainFrame = new Frame("Java AWT Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new Label();
headerLabel.setAlignment(Label.CENTER);
statusLabel = new Label();
statusLabel.setAlignment(Label.CENTER);
statusLabel.setSize(350,100);
controlPanel = new Panel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private void showEventDemo(){
headerLabel.setText("Control in action: Button");
Button okButton = new Button("OK");
Button submitButton = new Button("Submit");
Button cancelButton = new Button("Cancel");
okButton.setActionCommand("OK");
submitButton.setActionCommand("Submit");
cancelButton.setActionCommand("Cancel");
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
cancelButton.addActionListener(new ButtonClickListener());
controlPanel.add(okButton);
controlPanel.add(submitButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "OK" )) {
statusLabel.setText("Ok Button clicked.");
}
else if( command.equals( "Submit" ) ) {
statusLabel.setText("Submit Button clicked.");
}
else {
statusLabel.setText("Cancel Button clicked.");
}
}
}
}
Output:
26)Program to demonstrate applets
import java.awt.*;
import java.applet.*;
/*
<applet code ="StatusWindow" width=300 height=50>
</applet>
*/
public class StatusWindow extends Applet
{
public void init ()
{
setBackground (Color.cyan);
}
public void paint (Graphics g)
{
g.drawString ("This is in the applet window", 10, 20);
showStatus ("This is shown in the status window.");
}
}
Output: