Explore BrainMass
Share

Example Java program using arrays and sort algorithms

This content was STOLEN from BrainMass.com - View the original, and get the already-completed solution here!

Write a program that allows the user to enter students' names followed by their test scores and output the following information(assume that the maximum number of students in the class is 50):

a. Class average

b. Names of all the students whose test scores are below the class average, with an appropriate message

c. Highest score and the names of all students have the highest score

example of the data.txt file format:

AAA 85
BBB 90
CCC 100
DDD 85
... ...
... ...

© BrainMass Inc. brainmass.com October 25, 2018, 12:01 am ad1c9bdddf
https://brainmass.com/computer-science/sorting/example-java-program-using-arrays-and-sort-algorithms-213629

Solution Preview

This solution shows how to use an array in Java and how to read data from a file. We use two Java classes. The first class, ScoreData, is just for storing the name and score values for a particular data item. The class constructor takes the name and score for a particular
item. There are accessor methods to get the name and score out.

Most of the work is done in the Scores class. This class contains an array of ScoreData items. The array is loaded with the ...

Solution Summary

This solution provides an example Java program that uses an array to store data and sorts the array.

$2.19
See Also This Related BrainMass Solution

Java List Arrays Example: Class SList

Use the following shell class SList, and complete its implementation as per the guidance in points 1 through 5. Read the documentation on the member function declarations carefully.

public class SList
{
// Methods
public void insert(int item);
// Pre: The list is not full
// item is not in the list
// Post: item is in the list; the items are in sorted order
public void printList()
// Post: If the list is not empty, the elements are
// printed on the screen; otherwise "The list
// is empty" is printed on the screen
public int getLength()
// Post: return value is the number of items in the list
public boolean isEmpty()
// Post: returns true if list is empty; false otherwise
public boolean isFull()
// Post: returns true if there is no more room in the
// list; false otherwise
SList(int maxItems)
// Constructor
// Post: Empty list is created with maxItems cells
// Data fields
private int numItems;
private int[] values;
}

1: Write the definitions for the member functions.

2: Write a driver program that reads values from file int.dat, stores them in the list, and prints them on the screen.

3: Add a boolean method "contains" to class SList that returns true if its parameter is in the list and false otherwise. Use the binary search algorithm.

4: Add a private variable currentItem to the list and implement these method headings. This combination allows the user to view the list items, one at a time.

public void resetList()
// Set currentItem to 0
public String next()
// Returns the item at values[currentItem]
// Increments currentItem
public boolean hasNext()
// Returns true if currentItem is not equal to numItems

5: Augment the driver in point 2 to demonstrate how to use resetList, hasNext, and next to print the list.

View Full Posting Details