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

Import Java

This Java program defines two classes - KboatTelephoneBook and Student. KboatTelephoneBook allows the user to enter names and phone numbers which are then sorted alphabetically by name and displayed. The Student class allows the user to search a list of student names and numbers either by name or number and displays the matching result. It also prints all student names starting with A.

Uploaded by

FAISAL GHEYAS
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
0% found this document useful (0 votes)
41 views

Import Java

This Java program defines two classes - KboatTelephoneBook and Student. KboatTelephoneBook allows the user to enter names and phone numbers which are then sorted alphabetically by name and displayed. The Student class allows the user to search a list of student names and numbers either by name or number and displays the matching result. It also prints all student names starting with A.

Uploaded by

FAISAL GHEYAS
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/ 5

import java.util.

Scanner;

public class KboatTelephoneBook

public static void main(String args[]) {

final int SIZE = 20;

Scanner in = new Scanner(System.in);

String names[] = new String[SIZE];

long telNos[] = new long[SIZE];

System.out.println("Enter " + SIZE + " names and telephone numbers");

for (int i = 0; i < SIZE; i++) {

System.out.print("Enter Name: ");

names[i] = in.nextLine();

System.out.print("Enter telephone number: ");

telNos[i] = in.nextLong();

in.nextLine();

//Selection Sort

for (int i = 0; i < SIZE - 1; i++) {

int min = i;

for (int j = i + 1; j < SIZE; j++) {

if (names[j].compareToIgnoreCase(names[min]) < 0) {

min = j;

}
}

String temp = names[min];

names[min] = names[i];

names[i] = temp;

long t = telNos[min];

telNos[min] = telNos[i];

telNos[i] = t;

System.out.println("Name\tTelephone Number");

for (int i = 0; i < SIZE; i++) {

System.out.println(names[i] + "\t" + telNos[i]);

import java.util.Scanner;

public class Student

public static void main(String args[])

Scanner in=new Scanner(System.in);

String student[]= new String[20];

int number[]=new int[20];


System.out.println("Enter name of 20 students: ");

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

student[i]=in.nextLine();

System.out.println("Enter Number of 20 students: ");

for(int j=0;j<number.length;j++)

number[j]=in

.nextInt();

System.out.println("Enter your choice "

+ "\n1 for Number Search"

+ "\n2 for Names Search");

int ch = in.nextInt();

int pos = -1; // to store the location if element is found

switch(ch)

case 1: System.out.println("Enter Number");

int code = in.nextInt();

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


{

if(code == number[i])

pos = i;

if(pos != -1)

System.out.println("student : " + student[pos]);

else

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

break;

case 2: System.out.println("Enter name of student");

String name = in.nextLine();

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

if(name.equals(student[i]))

pos = i;

if(pos != -1)

System.out.println("student : " + student[pos]);

else

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

break;

default: System.out.println("Wrong choice");

break;
}

//Now printing student names with 'A'

System.out.println("student names which start with A");

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

if(student[i].startsWith("A"))

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

You might also like