You are on page 1of 4

Work Sheet – 6

(Array – Key)

Question 1:

import java.util.*;
class Ques1
{
static void test(int n)
{
Scanner in = new Scanner(System.in);
String name[] = new String[n];
int rno[] = new int[n];
int sub1[] = new int[n];
int sub2[] = new int[n];
int sub3[] = new int[n];
System.out.println("Enter the Details : ");
for(int i = 0;i<n;i++)
{
System.out.println("Enter the name ");
name[i]=in.nextLine();
System.out.println("Enter the Roll No ");
rno[i] = in.nextInt();
System.out.println("Enter the First Subject marks ");
sub1[i]=in.nextInt();
System.out.println("Enter the Second Subject marks ");
sub2[i]=in.nextInt();
System.out.println("Enter the Third Subject marks ");
sub3[i]=in.nextInt();
int tot = sub1[i]+sub2[i]+sub3[i];
int avg = tot/3;
if(avg>=85&&avg<=100)
System.out.println("Excellent ");
if(avg>=75&&avg<84)
System.out.println("Distinction ");
if(avg>=60&&avg<75)
System.out.println("First Class ");
if(avg>=40&&avg<60)
System.out.println("Pass ");
if(avg<40)
System.out.println("Poor ");
}
}
}

Question 2:

import java.util.*;
class buble_strng2
{
static void main()
{
Scanner cy=new Scanner(System.in);
String m[]=new String[20];
String t;
System.out.println("Enter names in the array : ");
for(int i=0;i<m.length;i++)
{
System.out.print("m["+i+"] :");
m[i]=cy.nextLine();
}
for(int i=0;i<m.length-1;i++)
{
for(int j=i+1;j<m.length;j++)
{
if(m[i].compareTo(m[j])<0)
{
t=m[i];
m[i]=m[j];
m[j]=t;
}
}
}
System.out.println("Sorted array : ");
for(int i=0;i<m.length;i++)
{
System.out.println(m[i]);
}
}
}

Question 3:

import java.util.*;
public class City3
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String Name[]=new String[10];
int STD[]=new int[10];
int flag=0,i;
String n,N="";
int S=0;
System.out.println("Enter the names of the cities and their STDs");
for(i=0;i<10;i++)
{
Name[i]=sc.next();
STD[i]=sc.nextInt();
}
System.out.println("Enter the name of the city to be searched: ");
n=sc.next();
for(i=0;i<10;i++)
{
if(n.equals(Name[i]))
{
flag=1;
N=Name[i];
S=STD[i];
break;
}
}
if(flag==1)
System.out.println("Search Successful. The name of the city is: "+N+"
and it's STD is: "+S);
else
System.out.println("Search Unsuccessful. No such city in the list.");
}
}

Question 4:

import java.util.*;
public class bsearchnamesQ4
{
public static void display()
{
Scanner sc=new Scanner(System.in);
String A[]=new String[10];
int flag=0,L,U,M;
String n;
L=0;
U=4;
M=0;
System.out.println("Enter the elements");
for(int i=0;i<10;i++)
{
A[i]=sc.next();
}
System.out.println("Enter the search element");
n=sc.next();
while(L<=U)
{
M=(L+U)/2;
if(n.compareTo(A[M])>0)
L=M+1;
else
if(n.compareTo(A[M])<0)
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");
}
}

You might also like