Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Program Write a class called Stats that will hold and/or compute various st

ID: 673160 • Letter: J

Question

Java Program

Write a class called Stats that will hold and/or compute various statistics. The class will work with integer values, including negative numbers and 0. There is no need to use arrays for this project. You will need loops and if statements. You will implement the following methods: void add (int n) int getMin() int getMax () int getCount) int computeSum () double computeAvg () int getNumPositive ()return the count of positive numbers seen so far int getNumNegative ()return a count of negative numbers seen so far int getNumZero () void reset () include n as a new entry in the "collection return the smallest number seen so far return the largest number seen so far return the count of numbers seen so far return the sum of the numbers seen so far return the average of the numbers seen so far return a count of zeros seen so far reset the data to reflect no entries Write a main program to exercise this class. Your program should have the usual main method, as well as a second method declared as private static void displayResults (Stats stats) f. )

Explanation / Answer

package mani;

import java.util.Scanner;

public class Luxury
{
public static void main(String[] args){
   Stats s=new Stats();
   String input="";
   int flag=0;
   Scanner scan=new Scanner(System.in);
   while(flag==0){
       do{
       System.out.println("Enter any integers or quit to quit");
       input=scan.next();
       if(input.equalsIgnoreCase("quit")){
           break;
       }else{
           s.add(Integer.parseInt(input));
       }
       }while(true);
       System.out.println(s.c.toString());
       displayResults(s);
       System.out.println("to continue enter Yes or No to quit");
       String c=scan.next();
       if(c.equalsIgnoreCase("yes")){
           continue;
       }else{
           s.reset();
           flag=1;
       }
       }
}

private static void displayResults(Stats s) {
System.out.println("Smallest Number: "+s.getMin());
System.out.println("Largest Number: "+s.getMax());
System.out.println("Count of numbers: "+s.getCount());
System.out.println("Sum of Numbers: "+s.computeSum());
System.out.println("Average of numbers: "+s.computeAvg());
System.out.println("NUmber of positive numbers: "+s.getNumPositive());
System.out.println("NUmber of negative numbers: "+s.getNumNegative());
System.out.println("NUmber of zero: "+s.getNumZero());
}

}

package mani;

import java.util.ArrayList;


public class Stats {
   ArrayList<Integer> c=new ArrayList<Integer>();
public void add(int n){
   c.add(n);
}
public int getMin(){
   int min=c.get(0);
   for(int i=0;i<c.size();i++){
       if(c.get(i)<min){
           min=c.get(i);
       }
      
   }
   return min;
}
public int getMax(){
   int max=c.get(0);
   for(int i=0;i<c.size();i++){
       if(c.get(i)>max){
           max=c.get(i);
       }
      
   }
   return max;
}
   public int getCount(){
       return c.size();
   }
   public int computeSum(){
       int sum=0;
       for(int i=0;i<c.size();i++){
          
               sum=sum+c.get(i);
          
          
       }
       return sum;
   }
   public double computeAvg(){
       return (double)computeSum()/(double)getCount();
   }
   public int getNumPositive(){
       int nPos=0;
       for(int i=0;i<c.size();i++){
           if(c.get(i)>0){
               nPos=nPos+1;
           }
          
       }
       return nPos;
   }
   public int getNumNegative(){
       int nNeg=0;
       for(int i=0;i<c.size();i++){
           if(c.get(i)<0){
               nNeg=nNeg+1;
           }
          
       }
       return nNeg;
   }
   public int getNumZero(){
       int zero=0;
       for(int i=0;i<c.size();i++){
           if(c.get(i)==0){
               zero=zero+1;
           }
          
       }
       return zero;
   }
   public void reset(){
       c.removeAll(c);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote