Question

Write two Java classes. A Numbers class has five fields -- maximum, minimum, sum, count, and...

Write two Java classes.

A Numbers class has five fields -- maximum, minimum, sum, count, and average.

  • An input() method will prompt for and input a sequence of double inputs, negative to quit, calculating and updating the five fields as the doubles are input.
  • A getMax() method returns the maximum of the doubles.
  • A getMin() method returns the minimum of the doubles.
  • A getSum() method returns the sum of the doubles.
  • A getCount() method returns the number of doubles that were input.
  • A getAverage() method returns the average of the doubles.
  • A toString() method returns a String that shows all of the fields, nicely labeled.

A Driver class contains a main method.

  • It instantiates a Numbers object, calls its input() method and prints out the toString() method.
  • It allows the user to repeat as desired.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;
class Numbers
{
   private double maximum,minimum,sum,average;
   private int count;
  

   public void Input()
   {
       System.out.println("Enter the numbers .Enter negative numbers to end");
       Scanner input = new Scanner(System.in);
       double n;
       sum = 0;
       count = 0;
       maximum = 0;
       minimum = 9999;
       do
       {
           n = input.nextDouble();
           if(n < 0)
           break;
           sum = sum+n;
           count++;
           if(n> maximum)
           maximum = n;
           if(minimum > n)
           minimum = n;
       }while(n>= 0);
       average = sum/count;
   }
   public double getMax()
   {
       return maximum;
   }
   public double getMin()
   {
       return minimum;
   }
   public double getSum()
   {
       return sum;
   }
   public int getCount()
   {
       return count;
   }
   public double getAverage()
   {
       return average;
   }
   public String toString()
   {
       return "Sum = "+getSum()+"\nMaximum = "+getMax()+"\nMinimum = "+getMin()+"\nCount = "+getCount()+"\nAverage = "+getAverage();
   }
}
class Driver
{
   public static void main (String[] args)
   {
       Numbers num = new Numbers();
       num.Input();
      
       System.out.println(num);
   }
}

Output:

Enter the numbers .Enter negative numbers to end
56.7
34.5
87.3
66.1
43.9
-1
Sum = 288.5
Maximum = 87.3
Minimum = 34.5
Count = 5
Average = 57.7

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Write two Java classes. A Numbers class has five fields -- maximum, minimum, sum, count, and...
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
  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • In java How to get started; Create a class called Card. The card has three fields;...

    In java How to get started; Create a class called Card. The card has three fields; a value , a suit and a face. The card class has a constructor that takes three values for the three fields. Create a no-args constructor. The Card class has three get methods to return the values of each of the fields. The Card class has a toString( ) method. The Card class has a compareTo( ) method that uses the value of the...

  • Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1...

    Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1 - 100) and returns the maximum number, minimum number, average of all numbers, and prints a Bar Chart to show the number distribution (1-9, 10-19,20-29, …80-89, 90-100). Note; maximum number, minimum number, average number, and the Bar Chart must use implemented as separate methods in a separate class. Method call should pass an array as an argument. Methods should accept an array as an...

  • Need help figuring this out Write a program that reads in a sequence of numbers (doubles)...

    Need help figuring this out Write a program that reads in a sequence of numbers (doubles) from standard input until 0 is read, and stores them in an array, This part is done using iteration (loop). You may assume that the maximum size of the array will not be more than 100. Your program computes the maximum number stored in the array, the count of negative numbers, and computes the sum of positive numbers, using recursion. You need to have...

  • Lab 1: InheritanceTest Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube....

    Lab 1: InheritanceTest Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private data of Point should be the x-y coordinates, the private data of Square should be the sideLength, and the private data of Cube should be depth. Provide applicable accessor methods, mutator methods, toString() methods, area() method, and volume() method to all classes. Write a program...

  • Write a Java program which allows the user to perform simple tasks on a calculator. A...

    Write a Java program which allows the user to perform simple tasks on a calculator. A series of methods allows the user to select an operation to perform and then enter operands. The first method displays a menu, giving the user the choice of typing in any one of the following: +, -, *, /, or % representing the usual arithmetic operators (Each operation should be done on numbers negative(-) to positive(+), positive(+) to negative(-), negative(-) to negative(-), and positive(+)...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • Write a Java class called BankAccount (Parts of the code is given below), which has two...

    Write a Java class called BankAccount (Parts of the code is given below), which has two fields name (String) and balance (double), two constructors and five methods getName(), getBalance(), deposit (double amount), withdraw(double amount) and toString(). The first constructor should initialize name to null and balance to 0. The second constructor initializes name and balance to the parameters passed. deposit method deposits the amount to the account causing the current balance to increase, withdraw method withdraws the amount causing the...

  • write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.t...

    write a complete Java program with comments in main and in each method. Data: The input data for this program is given as two columns of numbers. All data will be entered from a fle named input.txt and all output will go to the screen Assume there will not be more than 100 7 23.56 16 88.12 10 75.1 Design a Java class with a main method that does the following 1) Reads the data into two arrays of doubles,...

  • Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in thr...

    Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have 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