14.8 Prog 8 Overload methods (find avg)
Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods.
The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted character is not valid, print an error message.
The program MUST have the following six methods, in additional to the main method (6 points):
2 methods named inputValue that prompts the user to input N integers or N real numbers, the header of the method should be
public static void inputValue (int[ ], int, Scanner) public static void inputValue (double[ ], int, Scanner) Input to the method: array of either int or double data type an integer (numbers in the data set) Scanner object Output of the method: None
2 method named findAvg that returns the average of the data, the header of the method should be
public static double findAvg (int[ ], int) public static double findAvg (double[ ], int) Input to the method: array of either int or double data type an integer (numbers in the data set) Output of the method: a real number (average)
2 method named printResult that prints the inputted data and average , the header of the method should be
public static void printResult (int[ ], int, double) public static void printResult (double[ ], int, double) Input to the method: array of either int or double data type an integer (numbers in the data set) a real number (average) Output of the method: None Note: use System.out.format("is %.2f", avg); to format the average to 2 decimal place.
Find the average of integer data set by invoking appropriate
methods in main method, format to 2 decimal places (2 points)
Enter a character: i Enter a number: 4 Enter 4 integers: 2 4 6 10 The average of 2 4 6 10 is 5.50
Find the average of real number data set by invoking appropriate
methods in main method, format to 2 decimal places (2 points)
Enter a character: R Enter a number: 3 Enter 3 real numbers: 3.66 5.795 7.1203 The average of 3.66 5.795 7.1203 is 5.53
If the inputted character is not valid, print an error message (1
points)
Enter a character: t t is not the valid character.
PROGRAM
/* import scanner class from util package */
import java.util.Scanner;
class average
{
/* input the integer values in array */
public static void inputValue (int in[], int n)
{
int i;
Scanner sc=new Scanner(System.in);
System.out.print("\n\nEnter " + n + "
integers:");
for(i=0;i<n;i++)
{
in[i]=sc.nextInt();
}
}
/* input the real values in array */
public static void inputValue (double rl[], int n)
{
int i;
Scanner sc=new Scanner(System.in);
System.out.print("\n\nEnter " + n + " real
numbers:");
for(i=0;i<n;i++)
{
rl[i]=sc.nextDouble();
}
}
/* calculate average of integers */
public static double findAvg(int in[], int n)
{
int i;
double a;
int s=0;
for(i=0;i<n;i++)
{
s=s+in[i];
}
/* convert the value to double
*/
a=((double)s)/n;
return a;
}
/* calculate average of real numbers */
public static double findAvg(double rl[], int n)
{
int i;
double a;
double s=0.0;
for(i=0;i<n;i++)
{
s=s+rl[i];
}
a=(s/n);
return a;
}
/* print average of integers */
public static void printResult (int in[], int n,
double avg )
{
int i;
System.out.print("The average of ");
for(i=0;i<n;i++)
{
System.out.print(in[i] + " ");
}
/* print upto 2 decimal place */
System.out.format("is %.2f", avg);
}
/* print average of real values*/
public static void printResult (double rl[], int n, double
avg)
{
int i;
System.out.print("The average of ");
for(i=0;i<n;i++)
{
System.out.print(rl[i] + " " );
}
/* print upto 2 decimal place */
System.out.format("is %.2f", avg);
}
}
public class avg_cal
{
public static void main(String[] args)
{
char ch;
int n;
double avg=0.0;
/* integer array with not more than
20 values */
int in[]=new int[20];
/* double data type array with not
more than 20 values */
double rl[]=new double[20];
/* sc object of scanner class
*/
Scanner sc=new
Scanner(System.in);
/* ob object of average class
*/
average ob=new average();
System.out.println();
/* take character from console
input */
System.out.print("Enter a character:");
ch=sc.next().charAt(0);
/* if i or I is pressed */
if(ch=='i' || ch =='I')
{
System.out.print("Enter a number:");
n=sc.nextInt();
/* call all
functions with object ob */
ob.inputValue(in,n);
avg=ob.findAvg(in,n);
ob.printResult(in,n,avg);
System.out.println();
}
/* if r or R is pressed
*/
else if(ch=='R' || ch =='r')
{
System.out.print("Enter a number:");
n=sc.nextInt();
/* call all
functions with object ob */
ob.inputValue(rl,n);
avg=ob.findAvg(rl,n);
ob.printResult(rl,n,avg);
System.out.println();
}
/* for wrong option press */
else
{
System.out.println(ch + " is not
the valid character.");
}
}
}
SCREEN SHOT
OUTPUT
14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...
Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...
Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
In Java* Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...
I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...
Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...
I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...
The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java. OverloadMethods.java: // Printing array elements using overloaded methods. public class OverloadedMethods { // method printArray to print Integer array public static void printArray(Integer[] inputArray) { // display array elements for (Integer element : inputArray) { System.out.printf("%s ", element); } System.out.printf("\n"); } // method printArray to print Double array public...
Write Java program( see IncomeTaxClassTemplate) uses a class( TaxTableToolTemplate), which has a tax table built in. The main method prompts for a salary, then uses a TaxTableTools method to get the tax rate. The program then calculates the tax to pay and displays the results to the user. Run the program with annual salaries of 10000, 50000, 50001, 100001 and -1 (to end the program) and note the output tax rate and tax to pay. a. Modify the TaxTableTools class...
Exercise 3: Work exercise 11.4, 11.12, and 11.14 into one program (to develop 3 methods for ArrayList: first method finds the maximum element in an array list; the second method computes the sum of all elements in an array list; and the third method combines (union) two lists by adding the second list to the first list). Use Integer type for all methods. See problem statements for methods signatures and sample test data. Write one separate test program to test...