50% found this document useful (2 votes)
5K views35 pages

Slab Programs

The document contains programs and explanations for various Java concepts like slab programs, menu driven programs, pattern printing, linear search, binary search, and more. It includes sample programs to demonstrate each concept along with explanations of the variables used. The programs are written in Java and utilize concepts like classes, methods, arrays, loops, conditional statements, and more.

Uploaded by

saumya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
5K views35 pages

Slab Programs

The document contains programs and explanations for various Java concepts like slab programs, menu driven programs, pattern printing, linear search, binary search, and more. It includes sample programs to demonstrate each concept along with explanations of the variables used. The programs are written in Java and utilize concepts like classes, methods, arrays, loops, conditional statements, and more.

Uploaded by

saumya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

By –

Acknowledgement
Index

S.No Topic Page number Remark


1 slab
2 Decision making
3 Pattern printing
4 Linear search
5 Binary search
6 Selection sorting
7 Bubble sorting
8 Function overloading
9 functions
Slab programs –
Program 1 –
class slab1

public static void main(int n)

double bill=0.0d;

if(n<=100)

bill=n*3.0;

else if(n>100 && n<=200)

bill=(100*3)+(n-100)*4.5;

else if(n>200 && n<=300)

bill=(100*3)+(100*4.5)+(n-200)*5.0;

else

bill=(100*3.0)+(100*4.5)+(100*5.0)+(n-300)*6.5;

System.out.println("the telephone bill is"+(bill+250));

Variable name Data type Variable description


n integer To store number of calls
bill double To store the telephone
bill

Program 2 –
class slab2

public static void main(int c)

double r=0.0d;

if(c<=100)

r=c*1;

else if(c>100 && c<=300)

r=(100*1)+(c-100)*1.50;

else if(c>300 && c<=500)

r=(100*1)+(200*1.5)+(c-300)*2;

else

r=(100*1)+(200*1.5)+(200*2)+(c-500)*0.5;

double asc=5/100*r;

double t=r+asc;

System.out.println("Total money to be paid"+t);

}}
Variable name Data type Variable description
C integer To store number of units
T double To store total amount

Menu driven programs –


Program 1 –
import java.util.Scanner;

class des

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1 for area of a square");

System.out.println("Enter 2 for area of rectangle");

System.out.println("Enter 3 for area of a circle");

System.out.println("Enter your choice");

int choice=sc.nextInt();

switch(choice)

case 1:

System.out.println("Enter the side");


int side=sc.nextInt();

int area= side*side;

System.out.println("the area is"+area);

break;

case 2:

System.out.println("Enter the length");

int l=sc.nextInt();

System.out.println("Enter the breadth");

int b=sc.nextInt();

int area=l*b;

System.out.println("the area is"+area);

break;

case 3:

System.out.println("Enter the radius");

double r=sc.nextDouble();

double area=Math.PI*r*r;

System.out.println("the area is"+area);

}
break;

default :

System.out.println("Invalid choice");

sc.close();

Variable name Data type Variable description


choice Integer To store user’s choice
area Integer To store the area
r Double To store the radius

Program 2 –
import java.util.Scanner;

class des1

public static void main(String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter a chararcter or operator to be performed");

System.out.println("Enter + for addition");

System.out.println("Enter - for substraction");

System.out.println("Enter * for multiplication");


System.out.println("Enter / for division");

System.out.println("Enter % for remainder");

char ch= sc.next().charAt(0);

switch(ch)

case '+':

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

int a=sc.nextInt();

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

int b=sc.nextInt();

int sum=a+b;

System.out.println("the sum is :- "+sum);

break;

case '-':

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

int a=sc.nextInt();

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

int b=sc.nextInt();

int dif;
if(a>b)

dif=a-b;

else

dif=b-a;

System.out.println("the difference is :- "+dif);

break;

case '*':

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

int a=sc.nextInt();

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

int b=sc.nextInt();

int p=a*b;

System.out.println("the product is :- "+p);

break;

case '/':

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

int a=sc.nextInt();

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


int b=sc.nextInt();

int d;

if(a>b)

d=a/b;

else

d=b/a;

System.out.println("the Quotient is :- "+d);

break;

case '%':

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

int a=sc.nextInt();

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

int b=sc.nextInt();

int re;

if(a>b)

re=a%b;

else

re=b%a;

System.out.println("the remainder is :- "+re);

}
break;

default:

System.out.println("Invalid choice");

Variable name Data type Variable description


ch character To store user’s choice
sum Integer To store sum of numbers
d Integer To store the quotient

Pattern printing –
Program 1 –
class pattern1

public static void main(String args[])

int c=0,p;

for(int i=0;i<=9;i++)

for(int j=0;j<=i;j++)

p=c*j;
System.out.print(p+"\t");

System.out.println();

c++;

Variable name Data type description


c integer counter
p integer To store the product of c,j

Program 2 –
class pattern2

public static void main(String args[])

int c=1,k;

for(int i=1;i>=4;i++)

{ k=1;

for(int j=4;j>=i;j--)

System.out.print(" ");

}
for(k=1;k<=c;k++)

System.out.print(k);

for(int a=(k-1);a>=1;a--)

System.out.print(a);

System.out.println();

c++;

Variable name Data type description


c integer counter
k integer counter

Linear search –
Program 1
class linear1
{

public static void main(String n)

String a[]={"tom","jake","jimmy","austin","hart","vince","angela"};

int l=a.length;

int s=0;

for(int i=0;i<l;i++)

if(a[i].equals(n)==true)

s=1;

break;

if(s==1)

System.out.println("the name is found");

else

System.out.println("the name is not found");

Variable name Data type Description


s integer To classify the presence
of number
l integer To store the length of
array

Program 2 –
class linear2

public static void main(int n)

int a[]={1,4,5,6,5,3,6,8,9,7,55,7,7,8,8,9,5};

int l=a.length;

int c=0;

for(int i=0;i<l;i++)

if(a[i]==n)

c=1;

break;

if(c==1)

System.out.println("Element found");

else
System.out.println("Element not found");

Variable name Data type Description


c integer To classify presence of
element
l integer To store length of array
n integer Element to be found

Binary search –
Program 1 –
class binary1

public static void main(int n)

int a[]={5,10,15,20,25,30,35,40,45,50};

int l=0,u=9,m=0,flag=0;

while(l<=u)

m=(l+u)/2;

if(n>a[m])

l=m+1;
else if(n<a[m])

u=m+1;

else

flag=1;

break;

if(flag==1)

System.out.println("Element present at position"+(m+1));

else

System.out.println("Element not present");

Variable name Data type Description


flag integer To specify presence of no.
u integer Upper limit
p integer Lower limit

Program 2 –
class binary2

public static void main(int n)


{

int a[]={5,7,9,11,15,20,30,45,89,97};

int l=0,u=9,m=0,flag=0;

while(l<=u)

m=(l+u)/2;

if(n>a[m])

l=m+1;

else if(n<a[m])

u=m+1;

else

flag=1;

break;

if(flag==1)

System.out.println("Element present at position"+(m+1));

else

System.out.println("Element not present");

}
Variable name Data type Description
l integer To store length of array
u integer Upper limit
l integer Lower limit
m integer Mid term
Selection sorting –
Program 1 –
import java.util.Scanner;

class selectionSorting1

public static void main()

Scanner sc=new Scanner(System.in);

String name[]=new String[50];

int marks[]=new int[50];

for(int i=0;i<50;i++)

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

name[i]=sc.nextLine();

System.out.println("Enter computer marks");

marks[i]=sc.nextInt();

for(int j=0;j<=49;j++)

String min=name[j];

int pos=j;
for(int k=j+1;k<50;k++)

if(name[j].compareTo(min)<0)

min=name[k];

pos=k;

String t=name[j];

name[j]=name[pos];

name[pos]=t;

for(int i=0;i<50;i++)

System.out.println(name[i]+"\t"+marks[i]);

Variable name Data type Description


t integer Stores the value of
name[j]
pos integer Stores the position
Program 2 –
import java.util.Scanner;

class selectionSorting2

public static void main()

Scanner sc=new Scanner(System.in);

int values[]=new int[25];

int min,pos,temp;

for(int i=0;i<25;i++)

System.out.println("enter a value");

values[i]=sc.nextInt();

for(int i=0;i<=24;i++)

min=values[i];

pos=i;

for(int j=i+1;j<25;j++)

if(values[j]<min)
{

min=values[j];

pos=j;

temp=values[pos];

values[pos]=values[i];

values[i]=temp;

System.out.println("List in Ascending order");

for(int i=0;i<25;i++)

System.out.println(values[i]);

Variable name Data type Description


pos integer Stores position
temp integer Stores value of
values[pos]

Bubble sorting –
Program 1 –
class bubble1
{

public void min()

int a[]={5,3,8,4,9,2,1,12,98,16};

for(int i=0;i<a.length;i++)

for(int j=0;j<a.length-1-i;j++)

if(a[j]>a[j+1])

int temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

for(int k=0;k<a.length;k++)

System.out.println(a[k]);

}
Variable name Data type Description
temp integer Temporary variable

Program 2 –
import java.util.Scanner;

class bubble2

public static void main()

Scanner sc=new Scanner(System.in);

String name[]=new String[20];

for(int i=0;i<20;i++)

System.out.println("Enter the name");

name[i]=sc.nextLine();

for(int i=0;i<=19;i++)

for(int j=0;j<20-i-1;j++)

if(name[j].compareTo(name[j+1])<0)
{

String t=name[j];

name[j]=name[j+1];

name[j+1]=t;

for(int i=0;i<25;i++)

System.out.println(name[i]);

Variable name Data type Description


t String Stores name[j]

Function overloading –
Program 1 –
import java.util.Scanner;

class overload

void compare(int a,int b)

if(a>b)
System.out.println(a);

else

System.out.println(b);

void compare(char ch1,char ch2)

if((int)ch1>(int)ch2)

System.out.println(ch1);

else

System.out.println(ch2);

void compare(String s1,String s2)

if(s1.length()>s2.length())

System.out.println(s1);

else

System.out.println(s2);

Program 2 –
import java.util.Scanner;
class overload1

public void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1 for wiring");

System.out.println("Enter 2 for flooring");

System.out.println("Enter 3 for painting");

System.out.println("Enter your choice");

int choice=sc.nextInt();

switch(choice)

case 1:

System.out.println("Enter the length");

int l=sc.nextInt();

cost(l);

break;

case 2:

System.out.println("Enter the length");


int l=sc.nextInt();

System.out.println("Enter the breadth");

int b=sc.nextInt();

cost(l,b);

break;

case 3:

System.out.println("Enter the length");

int l=sc.nextInt();

System.out.println("Enter the breadth");

int b=sc.nextInt();

System.out.println("Enter the height");

int h=sc.nextInt();

cost(l,b,b);

break;

default :

System.out.println("Invalid choice");

public void cost(int a)


{

int cost=a*10;

System.out.println("the cost is"+cost);

public void cost(int a,int b)

int area=a*b;

int cost=area*20;

System.out.println("the cost of flooring is"+cost);

public void cost(int a,int b,int c)

int area=2*(a+b)*c;

int cost=area*5;

System.out.println("the cost of painting is"+cost);

Variable name Data type Description


cost integer Stores total cost
a integer Sides of room
b integer Sides of rooms

Functions –
Program 1 –
class date

int dd,mm,yy;

public date()

dd=0;

mm=0;

yy=0;

public date(int d,int m,int y)

dd=d;

mm=m;

yy=y;

public void displaydate()

System.out.println(dd+"/"+mm+"/"+yy);

public boolean isLeap()


{

if(yy%4==0)

return true;

else

return false;

Variable name Data type Description


yy integer Stores year
mm integer Stores month
dd integer Stores day

Program 2 –
class date2

public static boolean fact(int n)

int c=0;

for(int i=1;i<=n;i++)

if(n%i==0)

c++;

}
if(c==2)

return true;

else

return false;

public static void amin()

for(int j=100;j<=999;j++)

if(fact(j)==true)

System.out.print(j);

Variable name Data type Description


c integer Stores no of factors

You might also like