CET 3640 LAB 2

Lab Description:

This is a program that will allow the user to enter numbers to compute several statistics using Classess and overloaded methods. In the class it have one setter method to accept new number and a getter to receive. When the user exit the program it will display the following

  1. First number entered
  2. Last number entered
  3. Count of number entered
  4. Count of Floating point numbers
  5. Count of Integer
  6. Lowest two numbers
  7. Highest two numbers
  8. Total Sum

Code

// main method lab 2
// calls other method to compute several statistics
import java.util.Scanner; //program uses Scanner

public class NumberStatsClass{

   public static void main(String [] args){
      //create a Scanner to obtain input from user
      Scanner input = new Scanner(System.in);

      NumberStats MyNewValue = new NumberStats();  //Create newValue objective

        float num2;
        boolean end = true;
        MyNewValue.welcome();
        num2 = input.nextInt();

        if(num2 == 1){

      while( end ){

         { System.out.print("Please enter you number: To end program enter 'n' ");

         String num1 = input.next();
         char num3 = num1.charAt(0);

         if(num3 == 'N' || num3 == 'n'){
             end = false;
             MyNewValue.displayMessage();      
         }
         else{
            end = true;
            float num = Float.parseFloat(num1);
            MyNewValue.setNewValue(num);      
         }          
   }
         // end main method
}// end NumberStatsClient
}}}
public class NumberStats
{
   private float firstNum, lastNum, maxNum, minNum, sum, count, count1, count2;

   public void welcome(){
       System.out.println("David Chen lab 2 Classes and Methods");
       System.out.println("Press 1 to enter number");
       System.out.println("Press 2 to exit the program");}

   public void setNewValue(float num){
         sum += num;
         count++;
         lastNum = num;

         if (count == 1){
            minNum   = num;
            maxNum   = num;https://openlab.citytech.cuny.edu/
            firstNum = num;
         }
         if (num < minNum){
            minNum = num;
         }
         if (num > maxNum){
            maxNum = num;
         }
         if(num%1 == 0)
         {
             count1 ++;
         }
         else
         {
             count2 ++;
         }}

   public void setFirstNum(float num){
      firstNum = num;
   }
   public float getFirstNum(){
     return firstNum;
   }
   public void setLastNum(float num){
      lastNum = num;
   }
   public float getLastNum(){
      return lastNum;
   }
   public void setMaxNum(float num){
      maxNum = num;
   }  
   public float getMaxNum(){
      return maxNum;
   }
   public void setMinNum(float num){
      minNum = num;
   }
   public float getMinNum(){
      return minNum;
   }
   public void displayMessage(){
      if(count == count)
      {
      System.out.printf("First number entered: %f \n",  getFirstNum());
      System.out.printf("Last  number entered: %f \n",  getLastNum());
      System.out.printf("Maximum             : %f \n",   getMaxNum());
      System.out.printf("Minimum             : %f \n",   getMinNum());
      System.out.printf("Average             : %f \n",     sum/count);
      System.out.printf("Sum                 : %f \n",      sum);
      System.out.printf("The total number entered: %f \n", count);
      System.out.printf("The number of integer entered: %f \n",   count1);
      System.out.printf("The number of float entered: %f \n",     count2);

   }}}

SCREENSHOT
Screenshot from 2013-10-06 23:09:10

Leave a Reply

Your email address will not be published. Required fields are marked *