Hello all, this is my HW problem that my teacher will grade by using the output
ID: 3632347 • Letter: H
Question
Hello all, this is my HW problem that my teacher will grade by using the output posted below. He gave me hints below, but I'm still stuck. When answering, please try not use advance coding schemes as I'm in a beginner Java class. I've tried using while, do loops and if & else statements but I'm stumped as I can't make it loop the first question for inputting numbers if the number isn't matching 3 or 4. Also, make sure you use overloading in the methods. I won't forget to rate the best answer, thanks!Please write a class and calculate three or four numbers' average with overloading method.
(Hint: first step:
Please input how many numbers(3 or 4)?:
If you input 1 or 5(wrong number), and shows "You input a wrong number, please input a correct number(3 or 4): " again.
second step:
If you input "4" number, and then please input what are these four numbers; If you input "3" number, and then please input what are these three numbers.
third step:
Please calculate the average of these three numbers with three variables input overloading method, and if the numbers are four, and you should use four variables input overloading method.)
I will use two ways to test your codes as below:
-------------------------------------------------
Please input how many numbers(3 or 4)?:
3
Please input these three numbers:
3
6
4
You input: 3, 6 and 4. The average is 4.3
-------------------------------------------------
Please input how many numbers(3 or 4)?:
5
You input a wrong number, please input a correct number(3 or 4):
3
Please input these three numbers:
3
4
5
You input: 3, 4, and 5. The average is 4.0
Explanation / Answer
// File : methods/avg3/AvgThreeOverloaded.java // Description: Averages three numbers -- meaningless, but // Purpose: Show an overloaded method, getDouble, with three definitions, // differing in the number of parameters. // Issues : Input isn't checked for legality (non-null number) because // the point is to show overloading. // Author : Fred Swartz - 2007-01-11 - placed in public domain import javax.swing.*; public class AvgThreeOverloaded { //============================================================== main public static void main(String[] args) { //... Read three numbers using the three different methods. // Using three different methods is only to show overloading. double n1 = getDouble(); double n2 = getDouble("Enter the second number."); double n3 = getDouble("Enter last number.", 0.0, 100.0); double average = (n1 + n2 + n3) / 3.0; displayString("Average is " + average); } //========================================================= getDouble // I/O convenience method to read a double value. // This version of the getDouble method simply calls on another // version passing it a generic input message. private static double getDouble() { return getDouble("Enter a number"); } //========================================================= getDouble // I/O convenience method to read a double value given a prompt. // This version of getDouble displays the user supplied prompt. private static double getDouble(String prompt) { String tempStr; tempStr = JOptionPane.showInputDialog(null, prompt); return Double.parseDouble(tempStr); } //========================================================= getDouble // I/O convenience method to read a double value in a range. // It builds a new prompt and calls another version to get // the value, looping until a value in the range is found. private static double getDouble(String prompt, double low, double high) { double result; String rangePrompt = prompt + " Value must be in range " + low + " to " + high; //... Read and loop back if the number is not in the right range. do { result = getDouble(rangePrompt); } while (result high); return result;Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.