0% found this document useful (0 votes)
52 views

Detain 2019-Aug-01 Program 6

The document contains the code for a binary search program that searches for a string in an array of strings. It defines a BinaryString class with methods to enter strings into an array in ascending order, perform a binary search on the array to find a target string, and indicate whether the string was found or not. The main method gets user input for the strings to populate the array and the target string to search for, and calls the binary search method to check if the target is in the array.

Uploaded by

Naman Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Detain 2019-Aug-01 Program 6

The document contains the code for a binary search program that searches for a string in an array of strings. It defines a BinaryString class with methods to enter strings into an array in ascending order, perform a binary search on the array to find a target string, and indicate whether the string was found or not. The main method gets user input for the strings to populate the array and the target string to search for, and calls the binary search method to check if the target is in the array.

Uploaded by

Naman Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Detain 2019-Aug-01 Program 6

//If Else rent Calculator


class Detain
{
String name, address;
int n, telo;
double rent, amt;
Detain(String N, String a, int nn, int t, double r)
{
name=N;
address=a;
telo=t;
rent=r;
n=nn;
amt=0.0d;
}
public void compute()
{
if(n<=100)
{
amt=rent;
}
else if(n>100&&n<=200)
{
amt=0.6*(n-100)+rent;
}
else if(n>200&&n<=300)
{
amt=0.8*(n-200)+60+rent;
}
else
{
amt=(n-300)*1+140+rent;
}
}
public void display()
{
System.out.println(" Name : "+name);
System.out.println(" Address : "+address);
System.out.println("Telephone No. : "+telo);
System.out.println("No. of calls : "+n);
System.out.println("Rental Charges : "+rent);
System.out.println("Total Amount : "+amt);
}
}
Output:
Enter Name
Naman
Enter Address
HII
Enter no. of calls
313
Enter telephone No.
1234567891
Enter rent
40.5
Name : Naman
Address : HII
Telephone No. : 1234567891
No. of calls : 313
Rental Charges : 40.5
Total Amount : 193.5
Switch_2 2019-Aug-01 Program 7
//Perfect or Prime
import java.util.*;
public class Switch2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ch=0; int n=0;
System.out.println("1: Check for Prime No.");
System.out.println("2: Check for Prefect No.");
System.out.println("Enter a no. to be checked");
n=sc.nextInt();
System.out.println("Enter choice 1 or 2");
ch=sc.nextInt();
switch(ch)
{
case 1 :
{
int f=0;
for(int i=2; i<=n/2; i++)
{
if(n%i!=0)
{
f=1;
}
}
if(f==1)
{
System.out.println("No. is a Prime No.");
}
else
{
System.out.println("No. is not a Prime No.");
}
}
break;
case 2 :
{
int S=0; int N=0;
N=n;
for(int i=1; i<n; i++)
{
if(n%i==0)
{
S=S+i;
}
}
if(S==N)
System.out.println("No. is a Perfect No.");
else
System.out.println("No. is not a Perfect No.");
}
}
}
}
Output:
1: Check for Prime No.
2: Check for Prefect No.
Enter a no. to be checked
7
Enter choice 1 or 2
1
No. is a Prime No.

1: Check for Prime No.


2: Check for Prefect No.
Enter a no. to be checked
45
Enter choice 1 or 2
2
No. is not a Perfect No.
Function_Overloading 2019-Aug-01 Program 8
//String Overloading
import java.util.*;
class FunctionOverloading
{
String S;
char c;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
S=sc.nextLine();
}
void charcount()
{
int b=0;
for(int i=0; i<S.length(); i++)
{
c=S.charAt(i);
if(c==' ')
b++;
}
System.out.println("No. of Blank Spaces "+b);
}
int charcount(int v)
{
for(int j=0; j<S.length(); j++)
{
c=S.charAt(j);

if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||
c=='o'||c=='u')
{
v++;
}
}
System.out.println("No. of Vowels "+v);
return v;
}
public static void main(String args[])
{
FunctionOverloading f=new FunctionOverloading();
f.input();
f.charcount();
int v=0;
f.charcount(v);
}
}

Output:
Enter a String
I am a good boy.
No. of Blank Spaces 4
No. of Vowels 6
Series_Overloading 2019-Aug-01 Program 9
//series with overloading
import java.util.*;
class SeriesOverloading
{
double S;
void series()
{
double s=0.0d;
for(int i=2; i<=11; i++)
{
s=1/(Math.pow(2,i));
}
System.out.println("Sum of Series 1 "+s);
}
void series(int a)
{
Scanner sc=new Scanner(System.in);
int f=1; int n=0;
System.out.println("Enter N");
n=sc.nextInt();
for(int j=1; j<=n; j++)
{
for(int b=2; b<=n; b++)
{
for(int i =1; i<=b; i++)
{
f=f*i;
S=S+a/f;
f=1;
}
}
}
System.out.println("Sum of Series 2 "+S);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
SeriesOverloading p=new SeriesOverloading();
p.series();
int x=0;
System.out.println("Enter x");
x=sc.nextInt();
p.series(x);
}
}

Output:
Enter x
5
Enter N
7
Sum of Series 2 378.00048828125
Sum of Series 1 4.8828125E-4
Selection_Sort 2019-Aug-01 Program 10
import java.util.*;
class SelectionSort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[5];
int t=0; int s=0; int p=0;
for(int i=0; i<5; i++)
{
System.out.println("Enter an array");
a[i]=sc.nextInt();
s=a[i];
p=i;
for(int j=i+1; j<5; j++)
{
if(a[j]<s)
{
s=a[j];
p=j;
}
}
t=a[i];
a[i]=a[p];
a[p]=t;
}
for(int i=0; i<5; i++)
{
System.out.println(a[i]+", ");
}
}
}

Output:
Enter an array
54
Enter an array
53
Enter an array
23
Enter an array
65
Enter an array
2
2,
23,
53,
54,
65,
Bubble_Sort 2019-Aug-01 Program 11
import java.util.*;
class BubbleSort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String a[]=new String[5];
String t=" ";
for(int i=0; i<5; i++)
{
System.out.println("Enter an array");
a[i]=sc.nextLine();
}
for(int i=0; i<5; i++)
{
for(int j=i+1; j<=5-1; j++)
{
if(a[i].compareTo(a[j])>0)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(int i=0; i<5; i++)
{
System.out.print(a[i]+", ");
}
}
}

Output:
Enter an array
1
Enter an array
2
Enter an array
5
Enter an array
7
Enter an array
3
1, 2, 3, 5, 7
Binary 2019-Aug-01 Program 12
//Binary Search
import java.util.*;
class Binary
{
int f;
int l;
int u;
int a[];
Binary()
{
l=0;
u=0;
}
void enter_data()
{
int a[]=new int[10];
Scanner sc=new Scanner(System.in);
for(int i=0; i<10; i++)
{
System.out.println("Enter 10 No. in Asending order");
a[i]=sc.nextInt();
}
u=9;
}
void binsarch(int v)
{
int m=0;
m=(l+u)/2;
while(m!=0)
{
if(m>v)
{
l=m;
}
else if(m<v)
{
u=m;
}
else
{
f++;
}
}
if(f==1)
{
System.out.println("Int Found ");
}
else
{
System.out.println("Not Found");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Binary b=new Binary();
b.enter_data();
System.out.println("Enter a No.");
int v=sc.nextInt();
b.binsarch(v);
}
}

Output:
Enter 10 No. in Asending order
1
Enter 10 No. in Asending order
2
Enter 10 No. in Asending order
3
Enter 10 No. in Asending order
4
Enter 10 No. in Asending order
5
Enter 10 No. in Asending order
6
Enter 10 No. in Asending order
7
Enter 10 No. in Asending order
8
Enter 10 No. in Asending order
9
Enter 10 No. in Asending order
10
Enter a No.
5
Int Found
BinaryString 2019-Aug-01 Program 13
//Binary Search
import java.util.*;
class BinaryString
{
int f; String a[];
int l;
int u;
BinaryString()
{
l=0;
u=0;
}
void enter_data()
{
String a[]=new String[10];
Scanner sc=new Scanner(System.in);
for(int i=0; i<10; i++)
{
System.out.println("Enter 10 String in Asending order");
a[i]=sc.nextLine();
}
u=9;
}
void binsarch(String v)
{
int m=0;
m=(l+u)/2;
while(m!=0)
{
if(v.compareTo(a[m])<0)
{
l=m;
}
else if(v.compareTo(a[m])>0)
{
u=m;
}
else
{
f++;
}
}
if(f==1)
{
System.out.println("String Found ");
}
else
{
System.out.println("Not Found");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
BinaryString b=new BinaryString();
b.enter_data();
System.out.println("Enter a Name");
String v=sc.nextLine();
b.binsarch(v);
}
}

Output:
Enter 10 String in Asending order
Naman
Enter 10 String in Asending order
Devi
Enter 10 String in Asending order
Ambar
Enter 10 String in Asending order
Vivek
Enter 10 String in Asending order
Sup
Enter 10 String in Asending order
harry
Enter 10 String in Asending order
peter
Enter 10 String in Asending order
parker
Enter 10 String in Asending order
jain
Enter 10 String in Asending order
ahuja
Enter a Name.
jain
String Found
Array_2D 2019-Aug-01 Program 14
class Array2D
{
public void Sum(int a[][])
{
int Sum=0;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
Sum=Sum+a[i][j];
}
}
System.out.println("Sum="+Sum);
}
public static void main(String args[])
{
Array_2D A=new Array_2D();
int a[][]={ { 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 } };
}
}

Output:
Sum= 515
Function_2D 2019-Aug-01 Program 15
//2D Array
class Function2D
{
int n[][];
public void Search(int a[][], int n, int x)
{
int i=0; int j=n-1;
while (i < n && j >= 0)
{
if (a[i][j] == x)
{
System.out.print("n Found at " + i + " " + j);
}
else if(a[i][j]>x)
{
j--;
}
else if(a[i][j]<x)
{
i++;
}
else
{
System.out.println("No. entered not found");
}
}
}
public static void main(String args[])
{
Function2D A=new Function2D();
int a[][]={ { 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 } };
A.Search(a,40,37);
}
}

Output:
n found at (3,3)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy