Professional Documents
Culture Documents
PRACTICAL 1
1. Write a JAVA Program to make a calculator using switch case. You can have 5
operations +,-,/,*,%.
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
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
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]);
}
OUTPUT:
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:
PRACTICAL 3
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 :
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
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
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
System.out.println("----- Prepared
And Executed BY Priyatosh
/BTECH CSE/University
Rollno20011653/SEM-IV/SECTION-K---
");
}
}
Output :
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 {
Output :
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 {
Output :
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 :
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:
14. Write a Java program to find the duplicate characters in the given paragraph
using eitherString or StringBuffer classmethods.
Sample Output:
Source code :
import java.util.*;
class JavaProgram {
System.out.println(string[i]); /BTECH
}
System.out.println(" ----- Prepared And Executed BY Priyatosh
CSE/University Rollno20011653/SEM-IV/SECTION-K --- ");
}
}
OUTPUT :
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
Output :
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 : + "'");
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;
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 :
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 :