put everything together in an object-oriented manner!
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.
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);
}
}
********************************************************************************************
put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...
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...
Problem 2 1. In the src → edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named Complement with the following: • A public static method named onesComplement that takes a String as a parameter and returns a String that is the 1’s complement of the parameter. • If the String is not a valid binary number (i.e. only has 0s and 1s), you should throw an IllegalArgumentException with the message "Not a valid binary number". •...
****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 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 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 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...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to be...
Write a class named TestScores. The class constructor should accept an array test scores as its argument. The class should a method that returns the average of the test scores. If any test score is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate this with an array of five items. This is JAVA program.
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) 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...