• 0

[JAVA] Diving Competition


Question

Hey there,

I'm working on this assignment where I have to write a program that allows the user to enter eight judges' scores and then outputs the points received by the contestant. Each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. The points should be between 1 and 10, 1 being the lowest and 10 being the highest. Since we're on the array chapter, I have to use an array for this.

Right now, I defined the array and I'm able to get the input (values), but I don't think it's right. How exactly can I prompt the user and store the input I get into the array and continue with the next steps?

Many thanks! :]

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

Can we see the code you've got so far? I'm probably not going to be of much help as I'm learning like you but I might be able to, you never know...

Link to comment
Share on other sites

  • 0

You need some method of getting the input, from a text box or the console (stdin? ). Look into "input/output" as a search term..

You then need to repeat it x times to get each value. I presume you know about for loops? Usually it has an index, i, that acts as a counter. You can use that as an index to the array during each iteration ...

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

array[i] = /* input taken from command line/where ever .. so this input-getting method is repeated each iteration */

}

I hope that helps, it's hard to tell how much you know .. and I don't want to just give you the answer straight off :p

Edited by Persephone
Link to comment
Share on other sites

  • 0
Can we see the code you've got so far? I'm probably not going to be of much help as I'm learning like you but I might be able to, you never know...

Actually, I don't have much written yet, still going through some points I wrote in class when the teacher was explaining the question.. So far, I've just defined the array and I'm about to get the input values and store them into the array using a for loop.

You need some method of getting the input, from a text box or the console (stdin? ). Look into "input/output" as a search term..

You then need to repeat it x times to get each value. I presume you know about for loops? Usually it has an index, i, that acts as a counter. You can use that as an index to the array during each iteration ...

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

array[i] = /* input taken from command line/where ever .. so this input-getting method is repeated each iteration */

}

This of course means your array has to have a size of 6, it's being accessed at (0,1,2,3,4,5)

I hope that helps, it's hard to tell how much you know .. and I don't want to just give you the answer straight off :p

Yeah, I'm going to use the console. I'm planning on using for loops for the input, thanks for the code, I have an idea on how to write it now. Also, I was already planning on using a for loop for the calculation as well.

sum = 0;

for(i = 0; i< size; i++)
	sum = sum + list[i];

I'm having difficulty putting everything in order now. Could you please just give me clear directions in how to write the program? I'm not asking for the answers, just some guidelines to help me out. :)

Link to comment
Share on other sites

  • 0

First off, remember to declare your variables the first time you use them - i.e.

int sum = 0;
sum+= 5;

and

for(int i = 0; i < array.length; i++) {}

As for actually programming it, you say you need to ignore the highest and lowest value and only use the middle ones - in that case, you could possibly do that in the for loop itself:

array.sort();
for(int i = 1; i < array.length - 1; i++) {
   // addition code here (see below)
}

Also another hint, you can save yourself some coding time and code mess while adding by using += like follows:

int num = 4;
num += 3;
System.out.println(num);

Will output 7.

Edited by bmaher
Link to comment
Share on other sites

  • 0

You have the main components to put it together... I'm starting to get the feeling that you've slacked off a bit.

One thing a good programmer needs is to be able to a) read resources and b) find them :) I know it's a bit confusing at first but you ought to read your course notes. Most programming classes start with a "Hello World" and a few other example programs. Have a read through your notes or Google..

To work out the ordering of your code do this:

Write in bullet points each task it has to perform, in their logical order.

Then translate that into code.

So I imagine it's declaring variables, filling the array, processing the array, spitting out the answers..

Link to comment
Share on other sites

  • 0
First off, remember to declare your variables the first time you use them - i.e.

int sum = 0;
sum+= 5;

and

for(int i = 0; i < array.length; i++) {}

As for actually programming it, you say you need to ignore the highest and lowest value and only use the middle ones - in that case, you could possibly do that in the for loop itself:

array.sort();
for(int i = 1; i < array.length - 1; i++) {
   // addition code here (see below)
}

Also another hint, you can save yourself some coding time and code mess while adding by using += like follows:

int num = 4;
num += 3;
System.out.println(num);

Will output 7.

Thank you very much, this really helped, I really appreciate it. :]

Link to comment
Share on other sites

  • 0
You have the main components to put it together... I'm starting to get the feeling that you've slacked off a bit.

One thing a good programmer needs is to be able to a) read resources and b) find them :) I know it's a bit confusing at first but you ought to read your course notes. Most programming classes start with a "Hello World" and a few other example programs. Have a read through your notes or Google..

To work out the ordering of your code do this:

Write in bullet points each task it has to perform, in their logical order.

Then translate that into code.

So I imagine it's declaring variables, filling the array, processing the array, spitting out the answers..

It's not that I'm slacking off.. Just needed some guidelines to help me understand how to write the program. I looked up some stuff online and found a couple of useful tutorials that helped. Wrote points on what I should do first and so on. But like you said, I do have the main components, I'm going over my notes again right now. I know what I'm doing now, thanks to all who replied, I really benefited from the information you provided.

So, thanks so much, your help is very appreciated. :]

Link to comment
Share on other sites

  • 0
It's not that I'm slacking off.. Just needed some guidelines to help me understand how to write the program. I looked up some stuff online and found a couple of useful tutorials that helped. Wrote points on what I should do first and so on. But like you said, I do have the main components, I'm going over my notes again right now. I know what I'm doing now, thanks to all who replied, I really benefited from the information you provided.

So, thanks so much, your help is very appreciated. :]

Ok.. I didn't mean to be mean ... but no one learns to program without effort and all too often I am asked by first years at my uni "I'm stuck, plz to help me" when the answer is in their notes :p I guess I am oversensitive to it these days :p

Good luck with it :)

Link to comment
Share on other sites

  • 0
Ok.. I didn't mean to be mean ... but no one learns to program without effort and all too often I am asked by first years at my uni "I'm stuck, plz to help me" when the answer is in their notes :p I guess I am oversensitive to it these days :p

Good luck with it :)

Oh no, it's alright.. No harm done. ;)

I agree with what you're saying. You're right, and learning how to program does require a lot of effort.

Thanks again. :)

Link to comment
Share on other sites

  • 0

package test;

import java.io.*;
import java.util.*;

public class scores {

	static int[] scores;
	static int temp, min, max;

	public static void main (String[] args) {



	String input = null;
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	scores = new int[8];

	for(int i=0; i < 8; i++)	{
	try {
		System.out.println("Please enter the score: ");
	   input = br.readLine();
	   temp = Integer.parseInt(input);
	   scores[i] = temp;
	   /*if(min>temp)min=temp;
	   if(max<temp)max=temp;*/

	} catch (IOException ioe) {
	}
}
	Arrays.sort(scores);

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

		if(i==0 || i==7){}

		else
		System.out.println(scores[i] + ", ");

	}


}
}

Hope that helps

Link to comment
Share on other sites

  • 0
package test;

import java.io.*;
import java.util.*;

public class scores {

	static int[] scores;
	static int temp, min, max;

	public static void main (String[] args) {



	String input = null;
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	scores = new int[8];

	for(int i=0; i < 8; i++)	{
	try {
		System.out.println("Please enter the score: ");
	   input = br.readLine();
	   temp = Integer.parseInt(input);
	   scores[i] = temp;
	   /*if(min>temp)min=temp;
	   if(max<temp)max=temp;*/

	} catch (IOException ioe) {
	}
}
	Arrays.sort(scores);

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

		if(i==0 || i==7){}

		else
		System.out.println(scores[i] + ", ");

	}


}
}

Hope that helps

Wow, thank you very much for the help. The methods you've used is too advanced for me now, it's because we haven't covered them in class yet.

But thanks again, I really appreciate it, and very sorry for the trouble. :)

Link to comment
Share on other sites

  • 0
import java.util.Arrays;

public class lol {

  public static void main(final String[] args) {
	int[] scores = null;
	if (args.length != 8) {
	  throw new IllegalArgumentException("Wrong number of arguments!");
	}
	try {
	  scores = new int[8];
	  byte index = 0;
	  for (final String vote : args) {
		if ((Integer.parseInt(vote) < 1) || (Integer.parseInt(vote) > 10)) {
		  throw new IllegalArgumentException("Invalid vote number!");
		}
		scores[index++] = Integer.parseInt(vote);
	  }
	  Arrays.sort(scores);
	  scores = Arrays.copyOfRange(scores, 1, 7);
	  System.out.println(lol.sum(scores));
	} catch (final NumberFormatException e) {
	  System.err.println("The arguments must be integers!");
	}
  }

  private static final int sum(final int[] array) {
	int result = 0;
	for (final int vote : array) {
	  result += vote;
	}
	return result;
  }
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.