Question

Simple statistics

3.12 LAB: Simple statistics

Part 1

Given 4 integers, output their product and their average, using integer arithmetic.

Ex: If the input is:

8 10 5 4

the output is:

1600 6

Note: Integer division discards the fraction. Hence the average of 8 10 5 4 is output as 6, not 6.75.

Note: The test cases include four very large input values whose product results in overflow. You do not need to do anything special, but just observe that the output does not represent the correct product (in fact, four positive numbers yield a negative output; wow).

Submit the above for grading. Your program will fail the last test cases (which is expected), until you complete part 2 below.

Part 2

Also output the product and average, using floating-point arithmetic.

Output each floating-point value with three digits after the decimal point, which can be achieved as follows:
System.out.printf("%.3f", yourValue);

Ex: If the input is 8 10 5 4, the output is:

1600 6
1600.000 6.750

Note that fractions aren't discarded, and that overflow does not occur for the test case with large values.


3 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
import java.util.Scanner;

public class LabProgram {
   
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      
      int num1;
      int num2;
      int num3;
      int num4;
      int product;
      int average;
      double productD;
      double averageD;
      
      num1 = scnr.nextInt();
      num2 = scnr.nextInt();
      num3 = scnr.nextInt();
      num4 = scnr.nextInt();
      
      product = num1 * num2 * num3 * num4;
      
      average = (num1 + num2 + num3 + num4) / 4;
      
      System.out.println((int)product + " " + (int)average);
      
      productD = (double)num1 * (double)num2 * (double)num3 * (double)num4;
      
      averageD = ((double)num1 + (double)num2 + (double)num3 + (double)num4) / 4.0;
      
      System.out.printf("%.3f %.3f\n", productD, averageD);
      
   }
   
}


Add a comment
Know the answer?
Add Answer to:
Simple statistics
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Musical note frequencies

    3.11 LAB: Musical note frequenciesOn a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * rn, where n is the distance (number of keys) from that key, and r is 2(1/12). Given an initial key frequency, output that frequency and the next 4 higher key frequencies.Output each floating-point value with two digits after the decimal point, which can be achieved as follows:System.out.printf("%.2f", yourValue);Ex: If the input is:440.0(which is the A...

  • Java 6.16 LAB: Adjust list by normalizing

    When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This adjustment can be done by normalizing to values between 0 and 1, or throwing away outliers. For this program, adjust the values by dividing all values by the largest value. The input begins with an integer indicating the number of floating-point values that follow. Assume that the list will always contain fewer than 20 floating-point values. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue); Ex: If the input is: 5 30.0 50.0 10.0 100.0 65.0 the output is: 0.30 0.50 0.10 1.00 0.65  The 5 indicates that there are five floating-point values in the list, namely 30.0, 50.0, 10.0, 100.0, and 65.0. 100.0 is the largest value in the list, so each value is divided by 100.0. For coding simplicity, follow every output value by a space, including the last one. import java.util.Scanner;  public class LabProgram {    public static void main(String[] args) {       /* Type your code here. */    } }

  • Driving cost - methods

    3.14 LAB: Driving cost - methodsWrite a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975.Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method three times.Output each...

  • Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallo

    6.26 LAB: Driving cost - methodsWrite a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975.Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method three times.Output...

  • In C PLEASE 1.22 LAB: Using math functions Given three floating-point numbers x, y, and z,...

    In C PLEASE 1.22 LAB: Using math functions Given three floating-point numbers x, y, and z, output x to the power of z, x to the power of (y to the power of 2), the absolute value of y, and the square root of (xy to the power of z). Output each floating-point value with two digits after the decimal point, which can be achieved as follows: printf("%0.21f", yourValue); Ex: If the input is: 5.0 6.5 3.2 the output is:...

  • C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints...

    C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints out the answer based on arithmetic. Assume that there are no overflow, underflow and division by zero cases. Your function should be named calculator Your function takes three input parameter: two double numbers and one char operator Your function does not return anything Your function prints answer in the format specified below Your function should set precision point to 2 Note: You must use...

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing!...

    Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...

  • PLEASE WRITE IN C++ 2.25 LAB: Driving costs Driving is expensive. Write a program with a...

    PLEASE WRITE IN C++ 2.25 LAB: Driving costs Driving is expensive. Write a program with a car's miles/gallon and gas dollars/gallon (both doubles) as input, and output the gas cost for 10 miles, 50 miles, and 400 miles. Output each floating-point value with two digits after the decimal point, which can be achieved by executing cout << fixed << setprecision(2); once before all other cout statements. Ex: If the input is: 20.0 3.1599 the output is: 1.58 7.90 63.20 Note:...

  • *In Python please***** This program will display some statistics, a table and a histogram of a...

    *In Python please***** This program will display some statistics, a table and a histogram of a set of cities and the population of each city. You will ask the user for all of the information. Using what you learned about incremental development, consider the following approach to create your program: Prompt the user for information about the table. First, ask for the title of this data set by prompting the user for a title for data, and then output the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT