0% found this document useful (0 votes)
63 views7 pages

Student Rank Finder in Java

The document defines two classes - Record and Rank. Record stores the names and ranks of 50 students. It has methods to read the values, and display the names and ranks. The Rank subclass finds the highest rank and name. It overrides the display method to print the top rank and name with the highest rank. The highest method finds the index of the topmost rank without sorting, and stores it in the index variable.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views7 pages

Student Rank Finder in Java

The document defines two classes - Record and Rank. Record stores the names and ranks of 50 students. It has methods to read the values, and display the names and ranks. The Rank subclass finds the highest rank and name. It overrides the display method to print the top rank and name with the highest rank. The highest method finds the index of the topmost rank without sorting, and stores it in the index variable.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question 7:-

A super class record has been defined to store the names and ranks of 50 students.

Define a

subclass Rank to find the highest rank along with the name.

The details of both the classes are given below:

Class name: record

Data members/instance variables:

name[] :to store the names of the students.

rnk[]: to store the ranks of the students.

Members functions

record(): constructor to initialise data members members.

void readvalue():to store the names and ranks and ranks.

void display(): display the names and the corresponding rank.

Class Name: Rank

Data members/instance variable:

Index: integer to store the index of top most rank.

Member functions/methods:

rank():constructor to invoke the base class.

constructor to initialise index

void highest():find the index/location of the topmost rank and store it in index without

sorting the array.

void display (): display the names and ranks along with the name having the topmost

rank.
ALGORITHM:-

class record

Step 1: start

Step 2: a single dimension array name and rank of size 50 are initialised

Record ()

Step 1: start

Step 2: a local variable i of int type is initialise

Step 3: initialise the data members with their initials

Step 4: end

void readValues ()

Step 1: start

Step 2: accept the name and rank of the students from the user

Step 3: end

void display ()

Step 1: start

Step 2: print the name of the students and ranks with suitable headings

Step 3: end

class ended

class Rank extends Record

Step 1: start

Step 2: an instance variable index is initialised

Rank ()

Step 1: start

Step 2: initialise the data member with its initial

Step 3: end
void highest ()

Step 1: start

Step 2: find the index of the topmost rank and store it in index without sorting

Step 3: end

void display ()

Step 1: start

Step 2: print the names and ranks of the students with the name having the topmost

rank

Step 3:

end
import [Link].*;

class Record

String name[] = new String[50];

int rnk[]=new int[50];

Record()

for(int i=0;i<50;i++)

name[i]="";

rnk[i]=0;

void readValues()

Scanner sc=new Scanner([Link]);

for(int i=0;i<50;i++)

[Link]("ENTER NAME OF THE STUDENT");

name[i]=[Link]();

[Link]("ENTER RANK OF THE STUDENT");

rnk[i]=[Link]();

void display()

[Link]("NAME\t\t"+"RANK");

for(int i=0;i<50;i++)
[Link](name[i]+"\t\t"+rnk[i]);

class Rank extends Record

int index;

Rank()

index=0;

void highest()

for(int i=0;i<50;i++)

if(rnk[i]<rnk[index])

index=i;

void display()

[Link]();

[Link](“THE TOP MOST RANK:\t”+rnk[index]);

[Link](“NAME WITH TOP MOST RANK:\t”+name[index]);

}
OUTPUT:-
ENTER NAME OF THE STUDENT:

SUMAN

ENTER RANK OF THE STUDENT:

11

ENTER NAME OF THE STUDENT:

AMAN

ENTER RANK OF THE STUDENT:

ENTER NAME OF THE STUDENT:

ASHOK

ENTER RANK OF THE STUDENT:

ENTER NAME OF THE STUDENT:

NITIN

ENTER RANK OF THE STUDENT:

ENTER NAME OF THE STUDENT:

MOHIT

ENTER RANK OF THE STUDENT:

NAME
SUMAN
AMAN
ASHOK
NITIN
MOHIT
RANK
11
4
7
2
9
THE TOPMOST RANK: 2

NAME WITH TOPMOST RANK: NITIN


Variable Description Table:-

Variable Data Type Description

name[ ] String String to store name of the


student

rnk[ ] int Integers to store the rank of the


student

index int Integers to store a number

Common questions

Powered by AI

Encapsulation is achieved in the given classes by keeping the data members private to the class and controlling access to them via public methods. This design ensures that the 'name', 'rnk', and 'index' variables can only be modified through specific methods like 'readValues', 'highest', and 'display'. Encapsulation is important because it protects the integrity of the object’s state and prevents unauthorized or unexpected changes to its data, facilitating maintenance and reducing bugs .

The Rank class determines the student with the highest rank using the 'highest' method. This method iterates over the 'rnk' array, comparing each student's rank with the current highest rank stored at 'index'. The 'index' variable is updated to the current student's index if their rank is lower than the rank at the current 'index'. This approach ensures that the student with the lowest numerical rank (topmost rank) is identified without sorting the array .

The use of primitive arrays for storing student names and ranks presents limitations in terms of flexibility and scalability. A more efficient implementation could involve using ArrayLists or other dynamic data structures, allowing for storing an arbitrary number of students without being constrained by a predefined size. Dynamic data structures also facilitate more modern operations such as sorting, filtering, and transformations, thereby enhancing the program's flexibility and maintainability .

Inheritance is used in this structure by having the Rank class extend the Record class. This means that the Rank class inherits all the properties and methods of the Record class, allowing Rank to utilize and override its parent's functionality. In this way, Rank can access the 'name' and 'rnk' arrays from Record, while also adding its own functionality like the 'index' variable and methods such as 'highest' specifically for determining the topmost rank .

The 'super.display()' call in the Rank class's display method is significant because it invokes the 'display' method of the superclass Record, thereby first executing the logic to print the names and ranks of all students. This call ensures that the inherited behavior is preserved before extending it with the additional functionality specific to Rank, which is displaying the topmost rank and the name of the corresponding student .

If ranks are not stored as unique values and multiple students have the same topmost rank, only the first occurrence of this topmost rank would be identified by the current implementation of the 'highest' method in the Rank class. This limitation could lead to incomplete information when multiple students qualify for the top rank. To address this issue, the algorithm could be enhanced to store indices of all students sharing the top rank, which would allow for comprehensive display of all top-ranked students .

The 'display' method in the Record class outputs the name and rank of each student. The Rank class inherits this method but overrides it to also display the student with the topmost rank and their rank. Polymorphism is demonstrated here as the Rank class provides a specific implementation of the 'display' method, enhancing the basic functionality from the Record class. This allows objects of Rank to behave differently when invoking 'display', compared to base class instances .

The Rank class uses 'int index' instead of directly storing the name to efficiently manage and reference the student with the topmost rank. By storing the index, the class can directly access both the student's name and rank from the 'name' and 'rnk' arrays without duplication of data, thus maintaining a single source of truth and potentially reducing memory overhead, especially in larger datasets .

The current design assumes a fixed array size of 50 for storing student names and ranks, which leads to a potential issue of exceeding capacity when handling more students. This could result in ArrayIndexOutOfBoundsException when attempting to store or retrieve student data beyond the 50th element. To handle more than 50 students flexibly, the design should use a dynamic data structure, such as an ArrayList, instead of a fixed-size array .

To optimize the process of adding or updating student data, the current system could adopt a dynamic data structure like ArrayLists, which allows resizing automatically. This change would enable insertion at any index with shifting of existing elements more efficiently than manually managing a fixed-size array. Furthermore, implementing a HashMap where names are keys, and ranks are values, would enable constant time complexity for updates and retrievals, significantly optimizing performance over linear searches required when using an array .

You might also like