Java Lab Manual Sargun Singh Narula

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

DEPARTMENT

OF
COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL (CSE 306)


“JAVA PROGRAMMING ”

Name: SARGUN SINGH NARULA

Semester: 3RD
Branch: CSE Enrollment No. 0105cs191101cs

BACHELOR OF ENGINEERING (B.Tech) COURSE

SEMESTER – III (2020-21)


Corporate Office: Oriental Campus, Raisen Road, Bhopal - 462 022 (M.P.) India
Website: www.oriental.ac.in

1
Oriental Group of Institutes
ORIENTAL INSTITUTE OF SCIENCE AND TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING

JAVA PROGRAMMING (CSE 306)


ORIENTAL INSTITUTE OF SCIENCE & TECHNOLOGY,
BHOPAL
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LIST OF EXPERIMENTS

S. No Name of Experiment Exp. Submission Sign


Date Date
Installation , path setting and
1
compilation and running step in JAVA

WAP to display the List of even numbers.


2
Program to Display Multiplication Table
3
Display Triangle as follow: (using for
4 loops)
1
2 3
4 5 6
7 8 9 10 ... N *

To grade the students using switch and if-


5 else.
To grade the students using switch and if-
6 else.
EMI Calculator in Java - Java program to
7 calculate EMI.
Java program to find area and perimeter of
8 a circle using class.
Java program to demonstrate example of
9 static variable and static method
10 Java program to demonstrate example of
static block.

11 Java program to explain static and


constructor block.

12 Java program to demonstrate example of


static class.

13 Java program to demonstrate example of


final variable.

14 Java program to demonstrate example of


final method.
Java program to demonstrate example of
15 final class.
Java program to find differences between
16 minimum and maximum numbers in an array.
17 WAP for Arithmetic exception
Write a program to create a package in
18 java.
Write a Program to created two class A and
19 Simple. A class contains private data
member and private method. To access these
private members from outside the class and
show the output.
Write a program to implement Inheritance
20 in Java.
WAP to display the use of this keyword.
21
WAP that implements method overloading.
22
WAP that illustrates method overriding
23
WAP illustrating all uses of super
24 keywords.
Create an abstract class shape. Let
25 rectangle and triangle inherit this shape
class. Add necessary functions.
Write an application that creates a
26 package p1. Add some classes in it.

27 Write an application that shows the usage


of try, catch, throws and finally.
WAP in java to swap two integers without
28 using third variable. The swapping must be
done in a different method in a different
class.
WAP in java to handle the following
29 condition using a single try block
intarr[]={3,0};
int i=arr[0]/arr[1];
Experiment - 2
OBJECTIVE:
WAP to display the List of even numbers.

SOURCE CODE:

import java.util.*;

publicclassEvenNumbers{

publicstaticvoidmain(String[]args){

Scanner sc=new Scanner(System.in);

System.out.println("Enter number");

int n=sc.nextInt();

System.out.println("Printing Even numbers between 1 and "+n);

for(inti=1;i<=n;i++){

// if the number is divisible by 2 then it is even

if(i%2==0){

System.out.print(i+" ");

INPUT AND OUTPUT:


Enter Number
50
Output
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Experiment - 3
OBJECTIVE:
Program to Display Multiplication Table.
SOURCE CODE:
import java.util.*;
public class Java{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of N");
int n=sc.nextInt();
System.out.println("Output");
for(int i=1;i<=10;i++){
System.out.println(n+" * "+i+" = "+n*i);
}
}
}

INPUT AND OUTPUT:

Enter the value of N


5
Output
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Experiment - 4

OBJECTIVE:
Display Triangle as follow : (using for loops)
1
23
456
7 8 9 10 ...N *

SOURCE CODE:

import java.util.*;
public class Java{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of N");
int n=sc.nextInt();
System.out.println("Output");
int k=1;
boolean flag=false;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
System.out.print(k+" ");
k++;
if(k>n)
{
flag=true;
break;
}
}
if(flag)
break;
System.out.println();
}

}
}

INPUT AND OUTPUT:

Enter the value of N


15
Output
1
23
456
7 8 9 10
11 12 13 14 15
Experiment - 6

OBJECTIVE:
To grade the students using switch and if-else.

SOURCE CODE:
import java.util.Scanner;
public class GradeProgramUsingSwitchStatementExample {
   public static void main(String[] args) {
          Scanner scanner = new Scanner(System.in);
          System.out.print("Enter grade from (A, B, C or F) : ");
          String str = scanner.next();
          char grade = str.charAt(0);
          switch (grade) {
                 case 'A':
                       System.out.println("Grade A - marks >=80");
                       break;
  
                 case 'B':
                       System.out.println("Grade B - marks >=60");
                       break;
                 case 'C':
                       System.out.println("Grade C - marks >=40");
                       break;
                      
                 case 'F':
                       System.out.println("Grade F - marks <40 FAIL");
                       break;
                      
                 default : //optional
                       System.out.println("Invalid Grade");
                       break; //optional
          }
   }
}
INPUT AND OUTPUT:
Enter grade from (A, B, C or F) : A
Grade A - marks >=80
Enter grade from (A, B, C or F) : B
Grade B - marks >=60
Enter grade from (A, B, C or F) : C
Grade C - marks >=40
Enter grade from (A, B, C or F) : F
Grade F - marks <40 FAIL
Experiment - 7
OBJECTIVE:
program to calculate emisJava
SOURCE CODE:

import java.util.*;
import java.util.Scanner;
class Emi
{
public static void main(String []args)
{
Scanner a = new Scanner(System.in);
double principal, rate, time, emi;
System.out.print("Enter principal: ");
principal = a.nextFloat();
System.out.print("Enter rate: ");
rate = a.nextFloat();
System.out.print("Enter time in year: ");
time = a.nextFloat();
rate=rate/(12*100);
time=time*12;
emi= (principal*rate*Math.pow(1+rate,time))/(Math.pow(1+rate,time)-1);
System.out.print("Monthly EMI is= "+emi+"\n");
}
}
INPUT AND OUTPUT:
Enter principal: 1200
Enter rate: 20
Enter time in the year: 1
OUTPUT:
Monthly EMI is= 111.16140707649637
Experiment - 8

OBJECTIVE:
Java program to find area and perimeter of a circle using class.

SOURCE CODE:

import java.util.Scanner;
class AreaOfCircle
{
   public static void main(String args[])
    {  
      
      Scanner s= new Scanner(System.in);
        
         System.out.println("Enter the radius:");
         double r= s.nextDouble();
         double  area=(22*r*r)/7 ;
         System.out.println("Area of Circle is: " + area);      
   }
}

INPUT AND OUTPUT:

Enter the radius :


7
Area of circle : 154.0
Experiment - 12

OBJECTIVE:
Java program to demonstrate example of static class.

1)Static Class with Non Static Method:


SOURCE CODE:
import java.util.*;
public class StaticClassExample {
static int a = 0;
static class ClsInner {
//method of static class
public void dispMessage() {
a = 20;
System.out.println("Value of a: " + a);
}
}
public static void main(String[] s) {
StaticClassExample.ClsInnerobjClsInner = new
StaticClassExample.ClsInner();
objClsInner.dispMessage();
}
}
INPUT AND OUTPUT:
Value of a: 20

2)Static Class with Static Method:


SOURCE CODE:
import java.util.*;

public class StaticClassExample {


static int a = 0;
static class ClsInner {
//static method of static class
public static void dispMessage() {
a = 20;
System.out.println("Value of a: " + a);
}
}
public static void main(String[] s) {
//no need to create object
StaticClassExample.ClsInner.dispMessage();
}
}
INPUT AND OUTPUT:
Value of a: 20
Experiment - 16
OBJECTIVE:
Java program to find differences between minimum and maximum numbers in an array.

SOURCE CODE:
import java.util.*;
public class Java{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of an array");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the array elements");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}

int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;

for(int i=0;i<n;i++){
if(a[i]>max)
max=a[i];
if(a[i]<min)
min=a[i];
}
System.out.println("Output");
System.out.println(max-min);
}
}
INPUT AND OUTPUT:
Enter the size of an array
5
Enter the array elements
23 45 85 9 2
Output
Experiment - 17
OBJECTIVE:
WAP for Arithmetic exception.

SOURCE CODE:

import java.util.*;
public class Java{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter first value");
int n=sc.nextInt();
System.out.println("Enter second value");
int m=sc.nextInt();
try{
int c=n/m;
System.out.println("Output");
System.out.println("N/M = "+c);
}
catch(ArithmeticException e){
System.out.println ("Can't divide a number by 0");
}

}
}

INPUT AND OUTPUT:

Enter first value


25
Enter second value
0
Can't divide a number by 0
Experiment - 20
OBJECTIVE:
Write a program to implement Inheritance in Java.
SOURCE CODE:
import java.util.*;
class Bicycle 
{  
public int gear;
    public int speed;
    public Bicycle(int gear, int speed)
    {
        this.gear = gear;
        this.speed = speed;
    }
    public void applyBrake(int decrement)
    {
        speed -= decrement;
    }
          
    public void speedUp(int increment)
    {
        speed += increment;
    }
         public String toString() 
    {
        return("No of gears are "+gear
                +"\n"
                + "speed of bicycle is "+speed);
    } 
}
class MountainBike extends Bicycle 
{
    public int seatHeight;
    public MountainBike(int gear,int speed,
                        int startHeight)
    {
 super(gear, speed);
        seatHeight = startHeight;
    } 
    public void setHeight(int newValue)
    {
        seatHeight = newValue;
    } 
          public String toString()
    {
        return (super.toString()+
                "\nseat height is "+seatHeight);
    }
      }
  public class Test 
{
    public static void main(String args[]) 
    {
         MountainBike mb = new MountainBike(3, 100, 25);
        System.out.println(mb.toString());
                  }
}

Output:
No of gears are 3
speed of bicycle is 100
seat height is 25
Experiment -21

OBJECTIVE:
WAP to display the use of this keyword.

SOURCE CODE:

import java.util.Scanner;
class Student{  
int rollno;  
String name;  
float fee;  
Student(int rollno,String name,float fee){  
this.rollno=rollno;  
this.name=name;  
this.fee=fee;  
}  
void display(){System.out.println(rollno+" "+name+" "+fee);}  
}  
  
class TestThis2{  
public static void main(String args[]){  
Student s1=new Student(111,"ankit",5000f);  
Student s2=new Student(112,"sumit",6000f);  
s1.display();  
s2.display();  
}}  

INPUT AND OUTPUT:


Output:
111 ankit 5000
112 sumit 6000
Experiment -22

OBJECTIVE:
WAP that implements method overloading.
SOURCE CODE:
import java.util.Scanner;

class DisplayOverloading
{
public void disp(char c)
{
Scanner s= new Scanner(System.in);
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloadingobj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}

INPUT AND OUTPUT:


a
a 10
Experiment - 23
OBJECTIVE:
WAP that illustrates method overriding in java

SOURCE CODE:
import java.util.Scanner;

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();
}
}

INPUT AND OUTPUT:

Output:

Vehicle is running
Experiment -26

OBJECTIVE:
Write an application that creates a package p1. Add some classes in it.
SOURCE CODE:
import java.util.Scanner;
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
package mypack;
class B{
public static void main(String args[]){
pack.A obj = new pack.A();//using fully qualified name
obj.msg();
}
}

INPUT AND OUTPUT:

Output:Hello
Experiment -27

OBJECTIVE:
Write an application that shows the usage of try, catch, throws and
finally.
SOURCE CODE:
import java.util.Scanner;
class Division {
public static void main(String[] args)
{
int a = 10, b = 5, c = 5, result;
try {
result = a / (b - c);
System.out.println("result" + result);
}

catch (ArithmeticException e) {
System.out.println("Exception caught:Division by zero");
}

finally {
System.out.println("I am in final block");
}
}
}

INPUT AND OUTPUT:


Exception caught:Division by zero
I am in final block
Experiment -28

OBJECTIVE:
WAP in java to swap two integers without using third variable. The swapping must be done
in a different method in a different class.

SOURCE CODE:
import java.util.Scanner;
class Geeks {

public static void main(String a[])


{
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}

INPUT AND OUTPUT:

After Swapping: x =5, y=10

You might also like