Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Java Code Practice

Uploaded by

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

Java Code Practice

Uploaded by

Shivani
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

--------------------------Liner Search--------------------Java

Code---------------------------------------------

// Online Java Compiler


// Use this editor to write, compile and run your Java code online
import java.util.*;
class array {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array-->");
int n=sc.nextInt();
sc.nextLine();
String arr[]=new String[n];
for(int i=0;i<n;i++){
arr[i]=sc.next();

}
System.out.println("enter the value to be saerched-->");
String searchvalue=sc.next();

boolean Found=false;
for(int i=0;i<n;i++){
// System.out.println(arr[i]+ searchvalue);
if(arr[i].equalsIgnoreCase(searchvalue)){
System.out.println("hello");
Found=true;
}

}
if(Found==true){
System.out.println("Element found");
}
else
System.out.println("Element Not Found");

}
}

2--------------------------------sorting----------selection sort-----------------
java-------------------------------------

// Online Java Compiler


// Use this editor to write, compile and run your Java code online
import java.util.*;
class sorting {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array-->");
int n=sc.nextInt();
sc.nextLine();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();

selection(arr,n);
printarray(arr,n);

public static void selection(int arr[],int n){


for(int i=0;i<n-1;i++){
int smallest=i;
for(int j=i+1;j<n;j++){
if(arr[smallest] > arr[j]){
smallest=j;
}
}
int temp=arr[smallest];
arr[smallest]=arr[i];
arr[i]=temp;
}

public static void printarray(int arr[],int n){


System.out.println("Sorted arrar is:");
for(int i=0;i<n;i++){
System.out.println(arr[i]);
}

}
}

3.---------------------------------------------------------------Binary
Search------------------------

import java.util.Scanner;
import java.util.*;
class Main{

int BinarySearch(int arr[],int searchValue ){

int low=0;
int high=arr.length-1;
while(low<=high){
int mid=low+(high-low)/2;
if(arr[mid]==searchValue){
return mid;
}
else {
if(arr[mid]< searchValue){
low=mid+1;
}
else
high=mid-1;
}
}
return -1;

}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array-->");
int n=sc.nextInt();
sc.nextLine();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
sc.nextLine();
System.out.println("Enter the no to be searched-->");
int searchValue=sc.nextInt();

Main obj= new Main();


int result = obj.BinarySearch(arr, searchValue);
if (result == -1)
System.out.println(
"Element is not present in array");
else
System.out.println("Element is present at "
+ "index " + result);

4.--------------------------------------------------------Exception
Handlying--------------------------------------------------------------------------

import java.util.Scanner;
import java.util.*;

class MyException extends Exception{


MyException(String message){
super(message);
}
}

class Main{
public static void main(String args[]){
int r=5,s=1000;
int a[]={5,10};
int b=5;

try{
float z=(float) r / (float) s;
if(z<0.01){
throw new MyException("Number is too small");
}
int x=a[2]/b-a[1];
}
catch(ArithmeticException e){
System.out.println("Division by Zero");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("Array index Error"+ e.getMessage());
}
catch(ArrayStoreException e){
System.out.println("Wrong data type");
}
catch(MyException e){
System.out.println("Caught my Exception");
System.out.println(e.getMessage());
}
finally{
int y=a[1]/a[0];
System.out.println("y=" + y);
}

You might also like