Anyone know java?

Recommended Videos

iphonerose

New member
May 20, 2011
365
0
0
I'm a beginner to java and don't really understand methods yet. I have a couple of questions to do.

{

Q1.
Write a program that asks the user to input three numbers (doubles), and displays the sum and average. The calculation of the sum and average should be done in two separate methods, sum(n1, n2, n3) and average(n1, n2, n3), each of which takes three doubles as input parameters and returns the answer as a double. The inputting and display should all be done in the main method.
Note that the average of three numbers is the sum divided by 3, so the average method should call the sum method.
Q2.
Below is the main method of a maths exam program. It picks two random numbers in the range 1 - 50 and asks the user to add them, and then tells the user if they were correct or wrong.
Write the methods that are required to complete the program: pickNumber, askForSum and checkAnswer.
Note that you will have to search the online help to find out how to pick a random number, and then figure out how to scale it to the range you need. Note also that Blackboard might have upset my code indentation; please correct it when you copy the code.
public static void main(String args[])
{
/* Main method of multiplication exam program */
int x, y, ans;
x = pickNumber(); /* returns number in range 1 - 50 */
y = pickNumber();
ans = askForSum(x, y); /* asks user: what is x plus y, and returns user's answer converted to an int */
checkAnswer(x, y, ans); /* displays "Correct!" or "Wrong" */
System.exit(0);
}

}





And here is my paltry attempt at the first q:
{
import javax.swing.JOptionPane; // Dialog box methods
public class sum_average {


public static void main(String[] args) {

n1=Double.parseDouble(JOptionPane.showInputDialog("Please enter your first number: "));
n2=Double.parseDouble(JOptionPane.showInputDialog("Please enter your second number: "));
n3=Double.parseDouble(JOptionPane.showInputDialog("Please enter your third number: "));

JOptionPane.showMessageDialog (null, "The sum of the three numbers is " + xsum(n1, n2, n3));
JOptionPane.showMessageDialog (null, "The average of the three numbers is " + xaverage(n1, n2, n3));

}
public static double xsum (double n1, double n2, double n3){

double sum;
sum=n1+n2+n3;
return sum;

}
public static double xaverage(double n1, double n2, double n3){

double average;
average=xsum(n1, n2, n3)/3.0;
return average;
}
}
Any help would be appreciated. The errors I am encountering are that n1, n2 and n3 cannot be resolved in the main method
 

NeoFenrir

New member
Mar 28, 2008
4
0
0
It's been a little while since I've done any Java, but something that immediately jumps out at me is that you've not declared any types for your variables in main. I don't know how the JOptionPane stuff works, as little of my Java experience is with GUIs, but I imagine you still have to declare the type of a variable you're trying to get with it.
 

rabidmidget

New member
Apr 18, 2008
2,117
0
0
For q1, you may need to specify that n1, n2 and n3 are doubles when you're declaring them, eg:

Double n1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter your first number: "));

q2 seems easy enough, to get the random number you would do something along the lines of:

int randnum = Math.ceil(50 * Math.random());

making sure you import Math at the start of the class.
 

Oolok

New member
Dec 10, 2009
17
0
0
You need to declare n1, n2 and n3 as doubles in the main method.
 

Fanta Grape

New member
Aug 17, 2010
738
0
0
Son, I got you covered.



I think it runs on Flash though so can't help you with the coding.
 

TheAmazingHobo

New member
Oct 26, 2010
505
0
0
rabidmidget said:
For q1, you may need to specify that n1, n2 and n3 are doubles when you're declaring them, eg:

Double n1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter your first number: "));

q2 seems easy enough, to get the random number you would do something along the lines of:

int randnum = Math.ceil(50 * Math.random());

making sure you import Math at the start of the class.
If you do it like that, you would have to cast to int, because Math.ceil returns a double.
Also the distribution wouldn´t be ideal.

Personally, I would prefer:

Random bob = new Random();
int i = generator.nextInt(50) +1;

might be better.
 

iphonerose

New member
May 20, 2011
365
0
0
TheAmazingHobo said:
rabidmidget said:
snip
Thanks for your help, so here is my attempt on number 2
import java.util.Random;

import javax.swing.JOptionPane;



public class maths_exam {


public static void main(String[] args) {
/* Main method of multiplication exam program */
int x, y, ans;
x = pickNumber(); /* returns number in range 1 - 50 */
y = pickNumber();
ans = askForSum(x, y); /* asks user: what is x plus y, and returns user's answer converted to an int */
checkAnswer(x, y, ans); /* displays "Correct!" or "Wrong" */
System.exit(0);

}
public static Integer pickNumber(){

Random generator = new Random();

int i = generator.nextInt(50) + 1;
return i;

}
public static Integer askForSum (int x, int y){
int ans;
ans=Integer.parseInt(JOptionPane.showInputDialog("what is "+ x +" plus "+ y +"?"));
return ans;

}
public static Integer checkAnswer (int x, int y, int ans){

ans=x + y;

if (ans==askForSum(x, y)){
JOptionPane.showMessageDialog (null, "Correct!");

}
else {
JOptionPane.showMessageDialog (null, "Wrong");

}

return ans;

}
}

But when i run this, i get the dialog box asking me what is 'x' + 'y' twice. Any ideas why the user would get asked twice based on my code?
Thanks again
 

TheAmazingHobo

New member
Oct 26, 2010
505
0
0
iphonerose said:
Thanks for your help, so here is my attempt on number 2

/snip

But when i run this, i get the dialog box asking me what is 'x' + 'y' twice. Any ideas why the user would get asked twice based on my code?
Thanks again
I´m sure you can solve it on your own, just analyze where this inputDialog is called in your programm.

P.S.: As a side note, while I know it is tempting to use short identifiers for your variables and reuse existing names, try to choose names that are informative, as well as easy and fast to differentiate from each other.
It should also help you solve your specific problem.
 

Buzz Killington_v1legacy

Likes Good Stories About Bridges
Aug 8, 2009
771
0
0
You're calling askForSum, then calling checkAnswer, which includes a call to askForSum, so you're getting the dialog twice.
 

iphonerose

New member
May 20, 2011
365
0
0
Buzz Killington said:
You're calling askForSum, then calling checkAnswer, which includes a call to askForSum, so you're getting the dialog twice.
Cheers mate :)
 

Brandon237

New member
Mar 10, 2010
2,959
0
0
Sorry, I only know Pascal (Turbo Delphi).
I have actually done both of these programs as homework :p