Question

put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...

put everything together in an object-oriented manner!

  1. create a class named PositiveNumberStatistics that has the following:

    • A private double 1D array instance variable named numbers.

    • A constructor that takes one parameter, a 1D double array. The constructor should throw an IllegalArgumentException with the message "Array length of zero" if the parameter has a length of 0. If the exception is not thrown, the constructor should create the numbers array and copy every element from the parameter into the numbers instance variable. However, if any of the elements are less than or equal to zero, the constructor should throw a NumberFormatException with the message "Non-positive value at index X" where X is the first index at which a non-positive value occurs.

    • A method named average that does not take any parameters and returns a double. The method should return the average of the numbers in the instance variable numbers. Note that you do not need to worry about whether the numbers instance variable has been created in this method because this method will only be usable if the constructor does not throw an exception.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the java code :

********************************************************************************************

import java.io.*;
import java.util.*;
class PositiveNumberStatistics
{
private double numbers[];
public PositiveNumberStatistics(double argument[])
{
int len = argument.length;
   if(len==0)
   {
    throw new IllegalArgumentException("Array Length of zero");
   }
   else
   {
    numbers = new double[len];
    for(int i=0;i<len;i++)
    {
     if(argument[i]<=0) {
      throw new NumberFormatException("Non-positive value at index "+ i);
     }
     else
      numbers[i]=argument[i];
    }
   }   
}
public double average()
{
int len=numbers.length;
double avg=0;
for(int i=0;i<len;i++)
    avg = avg+numbers[i];
    avg = avg/len;
   return avg;
}
   
public static void main(String args[])
{
   Scanner s = new Scanner(System.in);
   int n=s.nextInt();
   double arg[] = new double[n];
   for(int i=0;i<n;i++)
    arg[i] = s.nextDouble();
PositiveNumberStatistics pns = new PositiveNumberStatistics(arg);
   double res = pns.average();
   System.out.println(res);
}
}
********************************************************************************************

Add a comment
Know the answer?
Add Answer to:
put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named...

    JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows...

    Create a new ArrayPractice project in Eclipse and a class named ArrayPractice.java. Put everything that follows into the main method except the method arrayAverage. Create a 5 element array of doubles called grades that contains the following numbers in this order: 81.2, 92.5, 48.9, 78.8, and 45.5. Create a 7 element array of ints called numbers that contains the following numbers in this order: 12, 42, 33, 67, 92, 58, and 33. Create a 9 element array of Strings called...

  • Design a class named BankAccount containing the following data field and methods. • One double data...

    Design a class named BankAccount containing the following data field and methods. • One double data field named balance with default values 0.0 to denote the balance of the account. • A no-arg constructor that creates a default bank account. • A constructor that creates a bank account with the specified balance.  throw an IllegalArgumentException when constructing an account with a negative balance • The accessor method for the data field. • A method named deposit(double amount) that deposits...

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • 1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files)...

    1a. Create a class named Inventory - Separate declaration from implementation (i.e. Header and CPP files) 1b. Add the following private member variables - a int variable named itemNumber - an int variable named quantity - a double variable named cost - a double variable named totalCost. Total cost is defined as quantity X cost. - updateTotalCost() which takes no arguments and sets the values of the variable totalCost. 1c. Add the following public functions: - Accessor and Mutators for...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

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