PDA

View Full Version : Java Help



theone
04-25-2004, 01:44 PM
I have these extra credit java questions to do and i'm completely clueless. They look like this...

a. Write a main method to let a user enter student scores, and display the max, min, and average of the score. The input 0 signifies the end of input.

b. Write a method to find the max in an array of double values. The method signature is as follows:
public state double max(double[] a)

c. Write a method to find the max in an array of two-dimensional array of double values. The method signature is as follows:
public static double max(double [][] a)

I've searched the net but couldn't find any helpful sites, just a lot of commercial sites wanting you to download something. Does anyone know any good instructional java sites? Any help is appreciated.

TheOne

GonePostal
04-25-2004, 10:02 PM
PM me... I'll get back to you in like maybe 2 days.. I got a hardware exam tommorrow morning... But those are easy should take you only maybe a max of 20 mins to do.

GonePostal
04-25-2004, 10:41 PM
import java.io.*;
class a1{
public static void main (String args[]){
String tmp = null;
int n=0, total=0,min=9999, max =0, data = 0 ;
InputStreamReader in= new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(in);

try
{
while (true)
{
System.out.println("Enter score:");
tmp= reader.readLine();
data = Integer.parseInt(tmp);
if (data==0)
break;
n++;
total +=data;
if (data<min)
min = data;
if (data>max)
max = data;
}
System.out.println ("Max: " + max + "Min: " +min + "Average: " + total/n);

}
catch (IOException ex)
{
System.out.println("You can't type");
}


}
public static double max (double [][] a )
{
double max = 0;
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a[i].length; j++)
{
if (a[i][j]> max)
max = a[i][j];
}
}
return max;
}
}
I assuem you can figure out the middle one from the bottom one. My code comes with no garuntees :P
I just did this to clear my mind... so i can study more hardware :)

theone
04-26-2004, 07:37 AM
I appreciate the help man, it was very helpful. Thanks.

defcon
04-26-2004, 09:26 AM
What compiler to you use for java? If you read this GP, it does look syntactically like c++, but crazy struct. lol, looks interesting.

theone
04-26-2004, 09:28 AM
I use JBuilder.

aidano
04-26-2004, 10:10 AM
It's probably the official Java compiler from Sun. Most people use it.

galileo
04-26-2004, 10:12 AM
IBM Eclipse is a very good compiler. eclipse.org

aidano
04-26-2004, 11:23 AM
Not to nitpick, but Eclipse is not really a compiler. It's more of a platform for development and uses other compilers. If you're using Eclipse, you're probably using it along with Sun's Java compiler.

theone
04-28-2004, 05:41 PM
i have another question that's probably easy for you guys. Let's say i find a max in an array using:

double max = a[0];
for (int i = 1; i < a.length; i++0)
if ( a[i]>max)
max = a[i];
return max;

What changes would i make to find the min?

aidano
04-28-2004, 06:34 PM
Just one character

C'mon. I'm not telling you the answer - I know you can get it yourself by just trying to understand what the code is doing.

defcon
04-28-2004, 06:54 PM
Yes, I am GREATER then you, oh i'm sorry, I must have posted this in the wrong topic, I meant to put is in a LESSER important one :P ;)

theone
04-28-2004, 08:58 PM
I know it needs to be changed to lesser than in the if statement. I was unsure if it needed to also be switched in the for statement. Any help?

aidano
04-28-2004, 09:01 PM
Dry run the code on paper, writing out the values of all the variables for each iteration of the loop. It won't take long for a piece of code that short, and after you're done you'll understand it all.

theone
04-28-2004, 09:50 PM
Ok, I have concluded to just change it in the if statement.