简体   繁体   中英

How do I finish the rest of this program? (Overloaded Methods)

I am trying to use a textbook and the internet to learn coding, but I am having some trouble with these types of codes. I know how to do the first part, but I just can not figure out the other part.

HERE IS THE PRACTICE ASSIGNMENT:

" You need to write a program to create random values as input for a different program that needs to be tested before it is implemented by your organization. Your boss asks you to have it create some statistics for you at the same time. You realize you are going to have to do some work with random numbers and the math class. You also know that you are going to be working with a system that sometimes has different inputs into the jobs it does...so that means you are going to have to read overloaded methods...and the best way to review something is to write a test program with it, so you decide to work it in. Create a program with the following requirements:

  1. It must have 4 randomly generated numbers in main. (You can do more if you wish if you want to play with this one.) These numbers should have values in the range 1-10.

1.a. Make sure to print their values for the user. This is the way to test and see what they are.

  1. You must write a custom method to find the average of 4 numbers, and write an overloaded version of the same method (with the same name...its an overloaded method) that takes only 3 numbers and a string value as parameters. The first version should just return the average of 4 numbers. The second one should return the average of 3 numbers and print a message saying "Finding the average of all except " + [value of the string passed in]

2.a Write test code to call each method from main. Test the first one with all four random numbers and report the results. Then test the second one passing the first three random numbers and pass the message "the fouth random number" (to tell that its the fourth one that is not in the set when it is printed.)

.....just to be clear, you are writing an overloaded method that takes 4 numbers as parameters....and a second version of the same method that takes 3 numbers and a string. If you do not have both of these methods and if they are not both the exact same name....there will be deductions on this program. [Last year there was some confusion on this as to why its being assigned and has 2 methods of the same name and what was being done here. It's an excercise to get you to write an overloaded method.]

  1. Use techniques you know and the Math class functions to report the smallest value of your four numbers. (You might want to write a separate method to do this. The min method can only check two of them at a time in this language. You will have to do a series of them in some way to find the minimum of all four and return it.)

  2. Finally...you need to send a random message to the user at the end. It should be one of the following: "Thank you for playing", "Have a Nice Day", "Goodbye Wally World", or "So Long Folks" "

HERE IS WHAT I HAVE:

import java.util.Random;

public class JavaMethods
{
    public static void main(String [] args)
    {
        Random r=new Random();
        double sum = 0; // is double so to prevent int division later on
        int amount = 4;
        int upperBound = 10;

        for (int i = 0; i < amount; i++){
            int next = r.nextInt(upperBound) + 1; // creates a random int in [1,50]
            System.out.println(next);

            sum += next; // accumulate sum of all random numbers
        }

        System.out.println("Your average is: " + (sum/amount));
    }
}

This program successfully gives me random numbers and finds the average, but how do I do the other part with the overloaded method and string?

You must write a custom method to find the average of 4 numbers, and write an overloaded version of the same method (with the same name...its an overloaded method) that takes only 3 numbers and a string value as parameters. The first version should just return the average of 4 numbers. The second one should return the average of 3 numbers and print a message saying "Finding the average of all except " + [value of the string passed in]

I think they want you to write 2 overloaded functions

double average (int a1, int a2, int a3, int a4)

which returns the average of 4 numbers. You'd copy the body of your code from the original answer you gave; the loop that computes the average.

You should make this average the 4 numbers

ADDITIONALLY

write a function

double average (int a1, int a2, int a3, String Str)

which takes the average and print a message saying "Finding the average of all except " + [value of the string passed in]

the idea of overloading means 2 separate functions, which have the same name, which have different calling parameters

I believe that's what they're looking for

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM