1)I am lost with the search algorithm and how to output the result.
2)How to output the average and the grades below 50.
I do not know how to go about the linear search algorithm.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class pdemo {
private String[] names;
private double[] grade;
public int Nos;
private String num;
public char a=0,b=0,c=0,x=0;
private BufferedReader stdin;
Scanner on = new Scanner(System.in);
public static final int NOT_FOUND = -1;
public pdemo()
throws IOException {
stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.printf("%s\n", "Welcome Enter the Number of student taking This Couse");
num = stdin.readLine(); Nos = Integer.parseInt(num);
names = new String[Nos]; grade=new double[Nos];
}
public void inputNames()
throws IOException {
for (int i = 0;
i < names.length;
i++) {
System.out.print("Enter name: ");
names[i] = stdin.readLine();
}
}
public void inputGrades()
throws IOException {
for(int i=0;
i<grade.length;
i++) {
System.out.println("Enter the grade ");
grade[i]=on.nextDouble();
}
}
public void trav()
throws IOException {
for (int da=0;
da<names.length;
da++) {
System.out.println( da+ ":"+ names[da]+" :"+grade[da]);
}
}
public void sele()
throws IOException {
System.out.println("CHOSE FORM THE LIST OF OPERATION");
System.out.printf("%s\n %s\n %s\n ",
"a = Search for a particular Student",
"b=Find the average of all Student",
"c= Find all student with the marks blow 50");
String in = stdin.readLine();
}
}
Here are the codes I am having problems with.
linear search algorithm
public void search(String[] names, double[] grade,String num,int i)
throws IOException {
if(a==a) {
System.out.println("What is the name of the student u are looking for?");
names[i] = stdin.readLine();
}
{
int low = 0;
int high = names.length - 1;
int mid;
while( low <= high ) {
mid = ( low + high ) / 2;
if (names[mid].compareTo( num)< 0)
low = mid + 1;
else if (names[ mid ].compareTo( num) > 0)
high = mid - 1;
else
return mid;
}
return NOT_FOUND;
}
}
System.out.println(names[i]+""+grade[i]);
}
public void average(String [] names, double [] grade,int num) {
if (b==b) {
double ave=0;
for(int i=0;
i < num;i++) {
ave+=grade[i];
}
System.out.println(ave);
}
}
public void below(String [] names, double [] grade,int num)
{
{
for(int i=0;
i <
num;i++) {
if(grade[i] < 50) {
System.out.println(names[i]+grade[i]);
}
}
}
}
import java.io.*;
public class Main {
public static int n;
private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
public static void main(String[] args)
throws IOException {
pdemo demo = new pdemo();
demo.inputNames();
demo.inputGrades();
demo.trav();
demo.sele();
{
}
I have a problem here too:
demo.search(args, grade, null, n);
demo.average(args, grade, null, n);
demo.below(args, grade, null, n);