I'm a beginner to java and don't really understand methods yet. I have a couple of questions to do.
And here is my paltry attempt at the first q:
Any help would be appreciated. The errors I am encountering are that n1, n2 and n3 cannot be resolved in the main method
{
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);
}
}
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;
}
}
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;
}
}