Hi everyone!
I am having a complete brain freeze on how to start writing the program. This is the problem.
Write a program that accepts a list of students’ name along with their final
score (out of 100). Next the program should display a menu to allow the user
get the following functionalities:
Display the range of the score list (The range is the difference between
the minimum and the maximum score)
Display the average score
Display the list sorted by score
Display students’ name whose score is below the average
Display students’ name whose score is above the average
Display students’ name whose score is below a given score
Display students’ name whose score is above a given score
Exit program
I know I will have to use parallel arrays for the student's name and their final score. I just don't know where to begin.
Do you think I should use methods to perform the functions as asked in the display menu.
Any feedback will be appreciated. Thank You!
17 Replies - 6106 Views - Last Post: 11 April 2010 - 04:11 PM
Replies To: using Parallel array in program
#2
Re: using Parallel array in program
Posted 10 April 2010 - 08:45 AM
why use parrallel arrays?
better create a Student class, and have a List of Student Objects, as the instructions require.
the Student Constructor will get a name and the final score:
then, you can add as much Students to a List and iterate the List to get average score, and all other data you need.
better create a Student class, and have a List of Student Objects, as the instructions require.
the Student Constructor will get a name and the final score:
public class Student {
private String name;
private int score;
public Student(String name, int score){
this.name = name;
this.score = score;
}
//more methods...
then, you can add as much Students to a List and iterate the List to get average score, and all other data you need.
#3 Guest_Elizabeth*
Re: using Parallel array in program
Posted 10 April 2010 - 09:31 AM
Thank you for your input. Will this looks great, we have not learned about objects yet so I really don't know how to use them.
#4
Re: using Parallel array in program
Posted 10 April 2010 - 09:58 AM
I see.. if you cannot use classes, than you have to use parrallel arrays as you said.
first think if you know the size of the arrays. if not, use a List implementation.
just fill the arrays or Lists (depand on what you choose), and use methods to perform the functions of the instruction.
However, it is really hard to say without seeing some code you wrote (to see your line of thought)
first think if you know the size of the arrays. if not, use a List implementation.
just fill the arrays or Lists (depand on what you choose), and use methods to perform the functions of the instruction.
However, it is really hard to say without seeing some code you wrote (to see your line of thought)
#5
Re: using Parallel array in program
Posted 10 April 2010 - 11:55 AM
@Elizabeth: I've written a tutorial on class design called Moving Away From Parallel Arrays. In it, I explain why classes are a better choice than parallel arrays, and how to design and use them in a simple inventory program. If you are required to sort the Students, then using an array of Student objects will be much easier than using parallel arrays, as you can make use of the Arrays.sort() method. Just make sure that your Student class implements the Comparable<Student> interface. Try to use the Object-Oriented model vs. the parallel array model. We will be happy to help you with the code you write. 
@japanir: Parallel arrays are one thing, but parallel Lists (or other dynamic collections) are even worse practice, as it is very easy to mismatch the data due to the dynamic resizing.
@japanir: Parallel arrays are one thing, but parallel Lists (or other dynamic collections) are even worse practice, as it is very easy to mismatch the data due to the dynamic resizing.
This post has been edited by macosxnerd101: 10 April 2010 - 11:56 AM
#6 Guest_Elizabeth*
Re: using Parallel array in program
Posted 10 April 2010 - 12:55 PM
macosxnerd101, on 10 April 2010 - 10:55 AM, said:
@Elizabeth: I've written a tutorial on class design called Moving Away From Parallel Arrays. In it, I explain why classes are a better choice than parallel arrays, and how to design and use them in a simple inventory program. If you are required to sort the Students, then using an array of Student objects will be much easier than using parallel arrays, as you can make use of the Arrays.sort() method. Just make sure that your Student class implements the Comparable<Student> interface. Try to use the Object-Oriented model vs. the parallel array model. We will be happy to help you with the code you write. 
@japanir: Parallel arrays are one thing, but parallel Lists (or other dynamic collections) are even worse practice, as it is very easy to mismatch the data due to the dynamic resizing.
@japanir: Parallel arrays are one thing, but parallel Lists (or other dynamic collections) are even worse practice, as it is very easy to mismatch the data due to the dynamic resizing.
Thanks. will do.
#7 Guest_Elizabeth*
Re: using Parallel array in program
Posted 10 April 2010 - 07:29 PM
I emailed my professor and asked if I can use classes and he said no
so I am still stuck on using parallel arrays this is what i have so far which is not much. How do display a list stored my score with the students name.
package assignments;
import javax.swing.*;
import javax.swing.JOptionPane;
//Write a program that accepts a list of students’ name along with their final
//score (out of 100). Next the program should display a menu to allow the user
//get the following functionalities:
//Display the range of the score list (The range is the difference between
//the minimum and the maximum score)
//Display the average score
[b]//Display the list sorted by score[/b]
//Display students’ name whose score is below the average
//Display students’ name whose score is above the average
//Display students’ name whose score is below a given score
//Display students’ name whose score is above a given score
//Exit program
public class Program4 {
public static void main(String[] args){
int num = Integer.parseInt(JOptionPane.showInputDialog("How many students do you want to enter?(-1 to quit)"));
if(num == -1)
{ JOptionPane.showMessageDialog(null,"Bye-bye!");
}
String[] name = new String[num];
int[] score = new int[num];
String menu = "";
int menuItem = 0;
for(int k = 0; k < num; k++)
{
String inputname = JOptionPane.showInputDialog("Name of Student");
name[k] = inputname;
int inputscore = Integer.parseInt(JOptionPane.showInputDialog("Score of Student"));
score[k] = inputscore;
}
while(true){
menu = JOptionPane.showInputDialog(" Main Menu\n 1) Find Students' Range \n 8)Exit");
menuItem = Integer.parseInt(menu);
switch(menuItem){
case 1: //find the range
int rangeNUM = range(score);
JOptionPane.showMessageDialog(null, rangeNUM);
break; //jump out of the switch
case 2:
break;
case 3:
// Display the list sorted by score
case 8: //exit
System.exit(0);
}
}
}
public static int range(int[] score)
{
int min = score[1];
for(int i = 0; i < score.length; i++)
{
if(score[i] < min)
min = score[i];
}
int max = score[1];
for(int ii = 0; ii < score.length; ii++)
{
if(score[ii] > max)
max = score[ii];
}
return max-min;
}
}
#8
Re: using Parallel array in program
Posted 10 April 2010 - 08:05 PM
If you know that name[0] correlates to score[0], you can use a for loop, printing out name[i] and score[i] in the body of the for loop.
#9 Guest_Elizabeth*
Re: using Parallel array in program
Posted 10 April 2010 - 08:26 PM
I do not know how to do just that. I don't know how to return an array that will keep track of the original entry of the scores arranged in order.
Please, I need help ASAP.
Thanks guys!
Please, I need help ASAP.
Thanks guys!
#10
Re: using Parallel array in program
Posted 10 April 2010 - 08:31 PM
Are you sure? Below, you seem to have no problems using a for loop to access corresponding elements in parallel arrays. If you want to work with a JOptionPane, you can concatenate this data into a single String object to display after the loop. Hint- you may find the \n escape sequence helpful, as it adds a line break.
for(int k = 0; k < num; k++)
{
String inputname = JOptionPane.showInputDialog("Name of Student");
name[k] = inputname;
int inputscore = Integer.parseInt(JOptionPane.showInputDialog("Score of Student"));
score[k] = inputscore;
}
#11
Re: using Parallel array in program
Posted 10 April 2010 - 08:36 PM
@macosxnerd101 - I don't think the problem is with printing it out, it is with sorting the one array, and keeping the other matched up.
@Elizabeth - In your sort method, when you swap two elements in your score array, make sure to swap the same two indexes in your name array. So if you end up having to swap score[0] and score[1], you also need to swap name[0] and name[1]
@Elizabeth - In your sort method, when you swap two elements in your score array, make sure to swap the same two indexes in your name array. So if you end up having to swap score[0] and score[1], you also need to swap name[0] and name[1]
#12
Re: using Parallel array in program
Posted 10 April 2010 - 08:40 PM
Quote
How do display a list stored my score with the students name.
@erik.price: That was the question I was going on.
@Elizabeth: There are tons of examples of sorting algorithms in our Java Snippets section. I have a feeling that Selection Sort may be the easiest to understand and implement, though you may also want to look at Bubblesort (least efficient) and possibly Insertion Sort (most efficient out of these three).
#13 Guest_Elizabeth*
Re: using Parallel array in program
Posted 11 April 2010 - 10:16 AM
I used selection sort and got the scores to display in ascending order in case 3 but I don't know how to write code for displaying the students name in the same display box. Here is what I have
package assignments;
import javax.swing.*;
import javax.swing.JOptionPane;
//Write a program that accepts a list of students’ name along with their final
//score (out of 100). Next the program should display a menu to allow the user
//get the following functionalities:
//Display the range of the score list (The range is the difference between
//the minimum and the maximum score)
//Display the average score
//Display the list sorted by score
//Display students’ name whose score is below the average
//Display students’ name whose score is above the average
//Display students’ name whose score is below a given score
//Display students’ name whose score is above a given score
//Exit program
public class Program4 {
public static void main(String[] args){
int num = Integer.parseInt(JOptionPane.showInputDialog("How many students do you want to enter?(-1 to quit)"));
if(num == -1)
{ JOptionPane.showMessageDialog(null,"Bye-bye!");
}
String[] name = new String[num];
int[] score = new int[num];
String menu = "";
int menuItem = 0;
for(int k = 0; k < num; k++)
{
String inputname = JOptionPane.showInputDialog("Name of Student");
name[k] = inputname;
int inputscore = Integer.parseInt(JOptionPane.showInputDialog("Score of Student"));
score[k] = inputscore;
}
while(true){
menu = JOptionPane.showInputDialog(" Main Menu\n 1) Find Students' Range \n2) Display average score \n3 Display the list sorted by score \n 8)Exit");
menuItem = Integer.parseInt(menu);
switch(menuItem){
case 1: //find the range
int rangeNUM = range(score);
JOptionPane.showMessageDialog(null, rangeNUM);
break; //jump out of the switch
case 2:
double avgNUM = average(score);
JOptionPane.showMessageDialog(null, avgNUM);
break;
case 3:
// Display the list sorted by score
int[] list = new int [score.length];
list = selectionSort(score);
String display = "Scores";
for (int k= 0; k < score.length; k++)
{
display = display + "\n" +list[k];
}
JOptionPane.showMessageDialog(null, display);
break;
case 8: //exit
System.exit(0);
}
}
}
public static int range(int[] score)
{
int min = score[1];
for(int i = 0; i < score.length; i++)
{
if(score[i] < min)
min = score[i];
}
int max = score[1];
for(int ii = 0; ii < score.length; ii++)
{
if(score[ii] > max)
max = score[ii];
}
return max-min;
}
public static double average(int [] score)
{
double sum = 0.0;
for (int k = 0; k < score.length; k++)
sum += score[k];
return sum/score.length;
}
public static int[] selectionSort(int[] list)
{
for (int i = list.length - 1; i >= 1; i--) {
// Find the maximum in the list[0..i]
int currentMax = list[0];
int currentMaxIndex = 0;
for (int j = 1; j <= i; j++) {
if (currentMax < list[j]) {
currentMax = list[j];
currentMaxIndex = j;
}
}
// Swap list[i] with list[currentMaxIndex] if necessary;
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
return list;
}
}
#14
Re: using Parallel array in program
Posted 11 April 2010 - 10:52 AM
Well, what you should do is, whenever you switch the position of two indexes in list[] (e.g. switch list[1] with list[4]) you should also switch the position of the name[] (e.g. switch name[1] with name[4]). Basically, this would allow you to set up a for loop that prints out list[i] then name[i] and they would all be lined up in the proper order, meaning the name of the student would be in the same position as their score in list[]. Did that make sense?
#15 Guest_Elizabeth*
Re: using Parallel array in program
Posted 11 April 2010 - 11:10 AM
Luckless, on 11 April 2010 - 09:52 AM, said:
Well, what you should do is, whenever you switch the position of two indexes in list[] (e.g. switch list[1] with list[4]) you should also switch the position of the name[] (e.g. switch name[1] with name[4]). Basically, this would allow you to set up a for loop that prints out list[i] then name[i] and they would all be lined up in the proper order, meaning the name of the student would be in the same position as their score in list[]. Did that make sense?
Yes that did make sense but I don't know HOW to switch the position of the name so that it matches with the score.
|
|

New Topic/Question
Reply
MultiQuote










|