0% found this document useful (0 votes)
5 views5 pages

Java Telephone and Student Directory

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)
5 views5 pages

Java Telephone and Student Directory

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

import [Link].

Scanner;

public class KboatTelephoneBook

public static void main(String args[]) {

final int SIZE = 20;

Scanner in = new Scanner([Link]);

String names[] = new String[SIZE];

long telNos[] = new long[SIZE];

[Link]("Enter " + SIZE + " names and telephone numbers");

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

[Link]("Enter Name: ");

names[i] = [Link]();

[Link]("Enter telephone number: ");

telNos[i] = [Link]();

[Link]();

//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;

[Link]("Name\tTelephone Number");

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

[Link](names[i] + "\t" + telNos[i]);

import [Link];

public class Student

public static void main(String args[])

Scanner in=new Scanner([Link]);

String student[]= new String[20];

int number[]=new int[20];


[Link]("Enter name of 20 students: ");

for(int i=0;i<[Link];i++)

student[i]=[Link]();

[Link]("Enter Number of 20 students: ");

for(int j=0;j<[Link];j++)

number[j]=in

.nextInt();

[Link]("Enter your choice "

+ "\n1 for Number Search"

+ "\n2 for Names Search");

int ch = [Link]();

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

switch(ch)

case 1: [Link]("Enter Number");

int code = [Link]();

for(int i=0; i<[Link]; i++)


{

if(code == number[i])

pos = i;

if(pos != -1)

[Link]("student : " + student[pos]);

else

[Link]("Element not found");

break;

case 2: [Link]("Enter name of student");

String name = [Link]();

for(int i=0; i<[Link]; i++)

if([Link](student[i]))

pos = i;

if(pos != -1)

[Link]("student : " + student[pos]);

else

[Link]("Element not found");

break;

default: [Link]("Wrong choice");

break;
}

//Now printing student names with 'A'

[Link]("student names which start with A");

for(int i=0; i<[Link] ; i++)

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

[Link](student[i]);

Common questions

Powered by AI

The Student program implements a linear search for both searching by student number and name. In a linear search, each element in the array is checked sequentially until the desired element is found or all elements are checked. The time complexity of this approach is O(n), where n is the number of elements in the array. This is because, in the worst case, every element must be examined once. While linear search is simple to implement, it is not the most efficient for large lists, due to its linear time complexity. However, given that the program deals with only 20 elements, the performance impact is negligible .

The Student program iterates through the array of student names and utilizes the String class's 'startsWith' method to check if a name begins with the letter 'A'. For each name in the array, it calls 'student[i].startsWith("A")'. If this method returns true, indicating that the name starts with 'A', it prints the name. This operation is straightforward due to the efficiency of the 'startsWith' method in its direct comparison logic with the beginning characters of a string .

The line 'in.nextLine();' is used to consume the newline character left in the input buffer after using 'nextInt()' or 'nextLong()' when reading integers or long data types. Without this line, the Scanner may skip prompts for string input following an integer input. When 'nextInt()' or 'nextLong()' reads an integer, it does not consume the entire line; it stops at the first whitespace or newline. The subsequent 'nextLine()' call removes the newline at that instance, clearing the input buffer for future reads, ensuring correct subsequent user input processing .

The 'min' variable is used to track the index of the minimum element found during each pass of the selection sort algorithm. During each iteration of the outer loop, the algorithm assumes the first unsorted element is the minimum and stores its index in 'min'. It then iterates through the remaining unsorted elements to find a smaller element. If such an element is found, it updates 'min' with the new index. This allows the program to swap the actual minimum element into its correct position, ensuring that the array is sorted by the end of all iterations .

The KboatTelephoneBook program maintains the association between names and their corresponding telephone numbers by using parallel arrays. When a name is swapped during the selection sort process, it also swaps the corresponding telephone number in the parallel array of telephone numbers. This is done by storing the index of the minimum element, performing the element swap in the names array, and simultaneously swapping the telephone numbers using the same indices. This approach ensures that each name remains paired with its correct telephone number throughout the sorting process .

The Student program distinguishes between searching by student number and student name using a switch-case structure based on user input. After prompting the user to enter a choice (1 for number search, 2 for name search), it reads the choice with 'in.nextInt()'. In the switch-case, case '1' corresponds to searching by number, prompting the user to enter the desired student number, which is then searched within the 'number' array. Conversely, case '2' is for name-based searching, where the user inputs the student's name, and the program searches within the 'student' array. This structural distinction ensures that the program processes and directs the search logic according to user intentions accurately .

The program uses the selection sort algorithm to sort the array of names and their corresponding telephone numbers. Selection sort operates by iteratively selecting the minimum element from an unsorted subarray and swapping it with the first unsorted element. This process is repeated for all elements in the array except the last one. The time complexity of selection sort is O(n^2), where n is the number of elements to sort. This is due to the nested loop structure: for each of the n elements, another n elements may need to be checked. In the context of the KboatTelephoneBook program, the array size is fixed at 20, so while the theoretical time complexity remains O(n^2), the actual run time for 20 elements is manageable .

To search for a student by either name or number, the Student program uses a simple linear search with separate cases in a switch statement. For searching by number, the program iterates over the 'number' array and compares each element to the input number. Similarly, when searching by name, it iterates over the 'student' array, comparing each element to the input name using the 'equals' method. If a match is found, it updates the variable 'pos' to the current index. After each search, it checks if 'pos' has been changed from its initial value (-1). If 'pos' has a valid index, it prints the student's name; otherwise, it indicates that the element was not found .

The use of fixed-size arrays in both the KboatTelephoneBook and Student programs imposes significant limitations on scalability and flexibility. Fixed-size arrays require predetermined size allocation, in this case, 20 elements, which restricts the maximum data capacity and can lead to inefficient memory usage if fewer elements are used. Moreover, these arrays cannot dynamically expand or shrink based on input size, limiting the programs' adaptability to varying amounts of data. To handle more extensive or variable data efficiently, dynamic data structures such as ArrayLists or linked lists would be more appropriate, offering automatic resizing and more efficient memory use .

The Student program manages input validation by using a switch-case structure to handle different user choices for searching by number or name. Before the switch-case block, the program prompts the user to enter a choice and reads the integer input. In the switch-case, it checks if the user's input corresponds to the numbers 1 or 2 (for number search or name search, respectively). If the choice is not 1 or 2, it goes to the default case, printing 'Wrong choice' to inform the user of invalid input. This simple input validation does not prevent non-integer input errors, thus limited to handling only correct integer inputs .

You might also like