You are on page 1of 32

Java Programming Lab Manual

PRACTICAL 1
1. Write a JAVA Program to make a calculator using switch case. You can have 5
operations +,-,/,*,%.

Source Code: import


java.util.*; public
class Main
{

public static void main(String[] args )


{
Scanner sc=new Scanner(System.in);
System.out.print(" Enter The First Number : ");
inta=sc.nextInt();
System.out.print(" Enter The Second Number : ");
intb=sc.nextInt();
System.out.println(" 1:Addition");
System.out.println(" 2:Subtraction");
System.out.println(" 3:Multiplication");
System.out.println(" 4:Division");
System.out.print(" ENTER YOUR CHOICE : ");
intn=sc.nextInt();
int res=0;

switch(n)
{
case 1:
res=a+b;
break;
case2:
res=a-b;
break;
case3:
res=a*b;
break;
case4:
res=a/b;
break;
default :
S
ys
te /BTECH
m.
ou
t.p
ri
nt
ln
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
("
In
va
Java Programming Lab Manual

OUTPUT:

QUESTION 1

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL2
Q3.Write a JAVA Program to print Floyd'sTriangle.

Source Code:
import java.util.*;
public class Main
{
public
static void
main(Strin
g[] args )
{
Scanner sc=new Scanner(System.in);
System.out.print(" Enter a Number : ");
int n=sc.nextInt();
int i=0,j=0,k=1;
for(i=1;i<=n;i++)
{
for(j=1;j<
=i;j++)
{
System.out.print(" "+k);
k++;
System.out.println(" Prepared And BY Priyatosh /BTECH
}
Executed CSE/University Rollno 20011653/
SEM-IV/SECTION-K"); System.out.print("\n");
}}
}

OUTPUT:
QUESTION 3

Priyatosh

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

3..Given a list of marks ranging from 0 to 100. Write a JAVA Program to compute and
print the number of students who have obtained marks
a. in the range of81-100
b. in the range of61-80
c. in the range of41-60
d. in the range of0-40.
The program should use a minimum number of if statements.

Source Code:
import java.util.*;
public class Main
{
public static
void
main(String[]
args )
{
Scanner sc=new Scanner(System.in);
System.out.print("\n");
System.out.print(" Enter Total Student : ");
int n=sc.nextInt();
int[] arr=new int[4];
Arrays.fill(arr, 0);
while(n>0)
{
System.out.print(" Input The Marks : ");
int n1=sc.nextInt();
if(n1>81&&n1<=100)
arr[0]++;
else if(n1>61&&n1<=80)
arr[1]++;
else if(n1>41&&n1<=60)
arr[2]++;
else if(n1>0&&n1<=40)
arr[3]++;
n--;
}
System.out.format(" Student Recived Marks Between 81 to 100 %d \n",arr[0]);
System.out.format("
System.out.println(" Student Recived
-----Prepared AndMarks Between
Executed 61 to 80 %d /BTECH
BY Priyatosh \n",arr[1]);
System.out.format("
CSE/University Student Recived Marks Between
Rollno20011653/SEM-IV/SECTION-K -- "); 41 to 60 %d \n",arr[2]);
System.out.format("
} Student Recived Marks Between 0 to 40 %d \n",arr[3]);
}

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

OUTPUT:

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

4.Write a JAVA Program to implement various stringfunctions.


Source Code:

import java.util.*;
public class Main
{
public static
void
main(String[]
args)
{
String first="",second="";
System.out.print("\n");
Scanner sc=new
Scanner(System.in);
System.out.println(" String Operation");
System.out.println();
System.out.print(" Enter the first Sting: ");
first=sc.nextLine();
System.out.print(" Enter the second Sting:
");
second=sc.nextLine();
System.out.println(" The strings are: "+first+" , "+second);
System.out.println(" The length of the first string is :"+first.length());
System.out.println(" The length of the second string is :"+second.length());
System.out.println(" The concatenation of first and second string is :"+first.concat("
"+second));
System.out.println(" The first character of " +first+" is: "+first.charAt(0));
System.out.println(" The uppercase of " +first+" is:
"+first.toUpperCase()); System.out.println(" The lowercase of " +first+" is:
"+first.toLowerCase()); System.out.println(" -----Prepared And Executed
BY Priyatosh /BTECH
CSE/University Rollno20011653/SEM-IV/SECTION-K -- ");
}
}
OUTPUT:

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL 3

5.Write a JAVA Program to compute power of 2 using


forloop.
Source Code:

import java.util.*;
public class Main
{
public static
void
main(String
args[]) {
int exponent, base, power = 1;
Scanner sc = new Scanner(System.in);
System.out.print("\n");
System.out.print(" Enter The Base Number : ");
base = sc.nextInt();
System.out.print(" Enter The Exponent Number : ");
exponent = sc.nextInt();
for (int i = 0; i < exponent; i++) {
power = power * base;
System.out.println("----- Prepared And Executed BY Priyatosh /BTECHCSE/University
}
Rollno20011653/SEM-IV/SECTION-K ---");
System.out.println("
} Power = " +
power);
}
Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

6.An educational institution wishes to maintain a database of its employees. The


database is divided into a number of classes whose hierarchical relationships are
shown in below figure. The figure also shows the minimum information required
foreachclass.Specifyalltheclassesanddefinemethodstocreatethedatabaseand
retrieve individual information as and whenrequired.

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL 4
7 Design a class to represent a bank account. Include the following members-
Data Members-
*Name of the Depositor
*Type of account
*Account Number
*Balance amount in the account
Methods-
*To assign initial values.
*To deposit an amount
*To withdraw an amount after checking balance.
*To display the name and balance.
Source code :

import java.util.*;
classAccount{
String Name;
String acctyp;
long accnmb;
intbamount;
void
initial(String
name,String
type,long
nmb,int amt)
{
Name=name;
acctyp=type;
accnmb=nmb;

bamount=amt
;
}
void deposit(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter The AmountToDeposit...... ");
int i=sc.nextInt();
bamount=bamount+i;
}
void withdraw(){
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
Scanner sc=new Scanner(System.in);
System.out.println("Enter The AmountToWithdraw ......");
int i=sc.nextInt();
Java Programming Lab Manual

void display()
{
System.out.println("Name : "+Name);
System.out.println("AccountType : "+acctyp);
System.out.println("AccountNumber : "+accnmb);
System.out.println("Balance Amount : "+bamount);
}
}

class JavaProgram {
public static void main(String args[])
{
Account ac =new Account();
ac.initial("Priyatosh","saving",123456789,3000);
ac.display();
ac.deposit();
ac.withdraw()
; ac.display();
System.out.pri BY
ntln("\n-----
/BTECH Priyatosh ");
CSE/University Rollno20011653/SEM-IV/SECTION-K---
} Prepared
}
And
Output :

Executed

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

8.Write a JAVA Program to implement method overloading in functioncalc().


Source code:
import java.util.*;
class Calculate{
int res;
void calc(int
a,int b)
{
res = a+b;
System.out.println("RESULT :"+res);
}
int calc()
{
int a=6,b=8;
res=a+b;
return res;
}
void calc(int
a)
{
int b=8;

res=a+b;
System.
out.print
ln("RES
ULT :"+
res);
}
}
class
JavaProgram
{
public static void main(String args[]) {
int res;
Calculate c=new Calculate();
c.calc(4,7);
res=c.calc();
System.out.println("RESULT :"+res);
c.calc(8);
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
System.out.println(" -----
Prepared And
Java Programming Lab Manual

OUTPUT

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL 5
9.Create a class “Student” having following instance variables andmethods.
Instance variables: ID, Name, Branch, city and university

While creating constructors with one, two, three, four and five arguments reuse the
constructors by construction chaining.
Source code :
import java.util.*;
class JavaProgram{
String name;
int Id;
String
Branch;
String city;
Stringuniversity;
JavaProgram(String A)
{
System.out.println(
"NAME : "+A);
}
JavaProgram(String
A,int B)
{
this(A);
System.out.println("ID : "+B);
}
JavaProgram(String A,int
B,String C)
{
this(A,B);
System.out.println("BRANCH : "+C);
}
JavaProgram(String A,int B,String
C,String D)
{
this(A,B,C);
System.out.println("CITY : "+D);
}
JavaProgram(String A,int B,String
C,String D,String E)
{
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
// invokes parameterized constructor 2
this(A,B,C,D);
System.out.println("UNIVERSITY :
Java Programming Lab Manual

public static void main(String args[])


{
// invokes parameterized constructor 3
JavaProgram t=new
JavaProgram("Priyatosh",20012705,"CSE"
,"DEHRADUN","GEHU");

System.out.println("----- Prepared
And Executed BY Priyatosh
/BTECH CSE/University
Rollno20011653/SEM-IV/SECTION-K---
");
}
}
Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

10. Create a class “Shape” having area() method to calculate area. Overload thearea()
method for shapes like triangle, rectangle andcircle.
Source code
import java.util.*;
class Shape{
float res;
void
area(float
l,float b)
{
res= l*b;
System.ou
t.println
("Area
of
rectangl
e :"+res
);
}
floatarea()
{
float r=6;
float res;
float pi=3.14f;
res=pi*r*r;
return res;
}
void area(int b)
{
float h=8;
float res=0.5f*h*b;
System.out.println("Area of triangle :"+res);
}
}
class JavaProgram {

public static void main(String args[]) {


float res;
Shape c =new Shape();
c.area(4,7);
res=c.area();
System.out.println("Area of
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
circle :"+res); c.area(8);
System.out.println("----- Prepared And
Executed BY Priyatosh
Java Programming Lab Manual

Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL 6
11. Create two dimensional integer array and insert, search and traverse thisarray.
Note: Use Scanner class to insert data.
Source code :
import java.util.*;
class JavaProgram {

public static void main(String args[]) {

Scanner sc=new Scanner(System.in);


int arr[]=new
int[5]; int i;
System.out.println("Enter the elements in array
"); for(i=0;i<5;i++)
arr[i]= sc.nextInt();
System.out.println("ENTER THE ELEMENT TO SEARCH :
"); int srch=sc.nextInt();
int count=0;
for(i=0;i<5;i+
+)
{
count=0;
if(arr[i]==srch
)
{
count
=1;
break;
}
}
if(count==
0)
{
System.o
ut.println("E
LEMENT
NOT
FOUND ");
}
else{
System.o
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
ut.println("E
LEMENT
FOUND ");
Java Programming Lab Manual

Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

12. A cloth showroom has announced the following seasonal discounts on purchase
ofitems:
Discount
Purchas
e Mill Handloo
Amount Cloth m Items

1-100 --- 5%
101-200 5% 7.50%
210-300 7.50% 10%
Above 300 10% 15%
Write a program using switch and if statements to compute the net amount to
be paid by a customer.
Source code :
import java.util.*;
class JavaProgram extends Marks {
float inputmill(float amt)
{
if(amt>=101&&amt<=200)
{
amt=amt*(5/100f);
}
else
if(amt>=201&&amt<=300)
{
amt=amt*(7.50f/100f);
}
else
{
amt=amt*(10/100f);
}
return amt;
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
Java Programming Lab Manual

}
float inputhandloom(float amt)
{
if(amt>=1&&amt<=100)
{
amt=amt*(5/100f);
}
else if(amt>=101&&amt<=200)
{
amt=amt*(7.50f/100f);
}
else if(amt>=201&&amt<=300)
{
amt=amt*(10/100f);
}
else
{
amt=amt*(15/100f);
}
return amt;
}
public static void main(String args[])
{
JavaProgram in=new JavaProgram();
Scanner sc=new
Scanner(System.in); int c=0;
while(c==0)
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
Java Programming Lab Manual

{
System.out.println("press 1.For Mill");
System.out.println("press 2.for handloom");
int option=sc.nextInt();
System.out.println("Enter the purchased amount ");
float amt=sc.nextFloat();
switch(option)
{
case 1 :
if(amt>=1&&amt<=100)
{
amt=amt*(5/100f);
}else{ amt=in.inputmill
(amt);
}
System.out.println("TOTAL AMOUNT : "+amt);
break;
case 2:
amt=in.inputhandloom(amt);
System.out.println("TOTAL AMOUNT : "+amt);
break;
}
System.out.println("Do you want to continue 0 for YES and 1forNO

"); c=sc.nextInt();
}System.out.println("\n -----Prepared And Executed BY Priyatosh
/BTECH CSE/University Rollno20011653/SEM-IV/SECTION-K --- ");
}
KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
Java Programming Lab Manual

}
Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL 7

13. Create a Java program to perform survey on four different model of Maruti (Maruti -
K10,Zen-Astelo,Wagnor,Maruti-SX4)ownedbypersonlivinginfourmetrocities(Delhi,
Mumbai,Chennai & Kolkatta). Display tabulated report like format givenbelow:

Maruti-K10 Zen-Astelo Wagnor Maruti-SX4


Delhi
Mumbai
Chennai
Kolkatta

Calculate numbers of cars of different model in each metro city.


Source code :
import java.util.*;
class JavaProgram {
public static void
main(String
args[])
{
Scanner sc=new Scanner(System.in);
int arr[][]= new int[4][4];
int i,j;
int citycode,carcode;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
arr[i][j]=0;
int c=1;
do{
System.out.println("PRESS 0 for DELHI ");
System.out.println("PRESS 1 for MUMBAI ");
System.out.println("PRESS 2 for KOLKATA ");
System.out.println("PRESS 3 for CHENNAI ");
System.out.println("ENTER THE CITY CODE ");
citycode=sc.nextInt();
System.out.println("PRESS 0 for K-10 ");
System.out.println("PRESS 1 for ZEN-ASTELO ");
System.out.println("PRESS 2 for WAGNOR ");
System.out.println("PRESS 3 for SWIFT ");
System.out.println("ENTER THE CAR CODE ");
carcode=sc.nextInt();
arr[citycode][carcode]++;
System.out.println("DO YOU WANT TO CONTINUE ? 1 for yes 0 for no ");
c=sc.nextInt();
}while(c==1) ;
KAMAL MULANI /BTECH CSE/2018403/
System.out.println("\t SEM-IV/SECTION-K
K-18\t ZENASTELO WAGRNOR TSX-4");
Java Programming Lab Manual
for( i=0;i<4;i++)
{
if(i==0)
System.out.print("DELHI
"); else if(i==1)
System.out.print("Mumbai ");
elseif(i==2)
System.out.print("Kokata
"); else if(i==3)
System.out.print("Chennai
"); for(j=0;j<4;j++)
{
if(j==1||j==2)
{
System.out.print(arr[i][j]+"\t
");
}
else{
System.out.print(arr[i][j]);
System.out.print("\t
");
}
}
System.out.println("\n---- Prepared And Executed BY Priyatosh /BTECH
System.out.print("\n");
CSE/University Rollno20011653/SEM-IV/SECTION-K --- ");
}
}
}

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

14. Write a Java program to find the duplicate characters in the given paragraph
using eitherString or StringBuffer classmethods.

Sample Output:

The given String is : Graphic Era


After removing duplicates characters the new string is : GraphicE

Source code :
import java.util.*;
class JavaProgram {

public static void main(String[] args) {


String string1 = "Great responsibility";
int count;
char string[] = string1.toCharArray();
System.out.println("Duplicate characters in a given string: ");
for(int i = 0; i <string.length; i++) {
count = 1;
for(int j = i+1; j <string.length; j+
+) {
if(string[i] == string[j] && string[i] != ' ') {
count++;
string[j] = '0';
}
}
if(count > 1 && string[i] != '0')

System.out.println(string[i]); /BTECH
}
System.out.println(" ----- Prepared And Executed BY Priyatosh
CSE/University Rollno20011653/SEM-IV/SECTION-K --- ");
}
}

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

OUTPUT :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL 8
15. Write a Java program implementing students of marks usingInheritance.
Source code:
class Marks{
int maths;
int english;
int hindi;
int science;

voidinput()
{
4Scanner
sc=new
Scanner
(System
.in);
System.out
.println(
"INPUT
THE
MARKS
MATHS
ENGLIS
H
HINDI
SCIENC
E ");
maths=sc.nextInt();
english=sc.nextInt();
hindi=sc.nextInt();
science=sc.nextInt()
;
}
}

class JavaProgram
extends Marks
{ void display()
{
System.out.println("MARKS IN ");
System.out.println("MATHS
KAMAL : "+maths);
MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K
System.out.println("ENGLISH :
"+english); System.out.println("HINDI :
Java Programming Lab Manual

System.out.println("\n-----Prepared And Executed BY Priyatosh /BTECH CSE/University Rollno20011653/


SEM-IV/SECTION-K ");
}
}

Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

16..Write a Java program to count the occurrences of a given string in another given string.
aa' has occured 3 times in 'abcd abc aabc baa abcaa'
Source code :
import java.util.*;
class JavaProgram {
public static void
main(String
args[])
{
Scanner sc=new
Scanner(Syste
m.in);
String main_string = "abcd abc aabc baa abcaa";
String sub_string = "aa";
int position = 0; int
ctr = 0;
int n =
sub_string.lengt
h();
while ((position = main_string.indexOf(sub_string, position)) != -1) {
System.out.println("\n
position = position +----Prepared
n; And Executed BY Priyatosh /BTECHCSE/University
Rollno20011653/SEM-IV/SECTION-K---
ctr++; ");

} }
}
System.out.println(sub_string + "' has occured " + ctr + " times in '" +
main_string
Output : + "'");

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

PRACTICAL 9
17. Define an exception called “NoMatchException” that is thrown when a string is not equal to
“India”. Write a program in Java that uses this exception.
Souce code :
import java.util.*;
class NoMatchException extends Exception
{ public NoMatchException(String
message){
super(message);
}
}
class IndiaAssertComparer {

private String s;

IndiaAssertComparer(String s) throws NoMatchException {


this.s = s;

if (s.equals("India"))
{ System.out.print("Matched!\
n");
} else {
throw new
} NoMatchException("Not
class JavaProgram
Matched!\n"); {
}
public static void main(String[] a) throws NoMatchException
} { IndiaAssertComparer v = new
IndiaAssertComparer("America"); /BTECH
System.out.println("\n ---- Prepared And Executed BY Priyatosh
CSE/University Rollno20011653/SEM-IV/SECTION-K ---");
}
}

Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

Q18. Apply following functions on StringBuffer object "HELLO"


(i) Append"Java"
(ii) Insert "Java" at index1
(iii) Replace with "Java" with characters between index 1 to2
(iv) Delete characters between index 1 and2

(v) Reverse the string "HELLO"


Source code:
import java.util.*;
class JavaProgram{
public static void
main(String[] a)
throws
NoMatchExcepti
on {
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");
System.out.println(sb);

sb.insert(1,"Java");//now original string is changed


System.out.println(sb);
sb.replace(1,3,"Java");
System.out.println(sb);
sb.delete(1,3);
System.out.println(sb)
; sb.reverse();
System.out.println(sb)
;
}
}Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K


Java Programming Lab Manual

19. Create a jagged array having three rows. Where 1strow contains 3 columns, 2ndrow contains 4
columns and 3rdrow contains 2 columns. Insert and traverse it.
Source code :
import java.util.*;
class JavaProgram
{
public static
void
main(String[]
args)
{
int r = 3;
int arr[][] = new
int[r][]; arr[0] = new
int[3]; arr[1] = new
int[4]; arr[2] = new
int[2];
int count = 0;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j <
arr[i].length;
j++) arr[i][j] = count++;
System.out.println("Contents of 2D Jagged
Array"); for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length;
j++) System.out.print(arr[i][j]
+ " ");
System.out.println();
} }
}
Output :

KAMAL MULANI /BTECH CSE/2018403/ SEM-IV/SECTION-K

You might also like