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

package chapter4; import java.util.*; public class SetOfFloatingPoint { public s

ID: 3652244 • Letter: P

Question

package chapter4;
import java.util.*;
public class SetOfFloatingPoint {

public static void main(String[] args) {

float total = 0;
float count = 0;
Scanner in = new Scanner(System.in);
System.out.println("Please print a floating point number(then leave a space and type a letter and press enter): ");


while (in.hasNextFloat()) //only keeps working when the next value is a a valid floating point value
{float input = in.nextFloat();
total = total + input;
count++;
}
float average = 0;
if (count > 0)
{
average = total / count;
System.out.println("The average of the values are: " +average);
}
{
float largest = in.nextFloat();
while(in.hasNextFloat())
{float input = in.nextFloat();
if ( input > largest )
{
largest = input;
System.out.println("The maxiumum number is: " +largest);
}

float smallest = in.nextFloat();
while(in.hasNextFloat())
{float input2 = in.nextFloat();
if ( input2 > smallest )
{
smallest = input;
System.out.println("The maxiumum number is: " +smallest);
}
System.out.println(largest - smallest);
}
}
in.close();
}
}
}

Explanation / Answer

//package chapter4;
import java.util.*;
public class SetOfFloatingPoint {

    public static void main(String[] args) {

        float total = 0;
        float count = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("Please print a floating point number(then leave a space and type a letter and press enter): ");


        while (in.hasNextFloat()) //only keeps working when the next value is a a valid floating point value
        {
            float input=0;
            try{input = in.nextFloat();}
            catch(InputMismatchException e)
            {

            }
            total = total + input;
            count++;
        }
        float average = 0;
        if (count > 0)
        {
            average = total / count;
            System.out.println("The average of the values are: " +average);
        }
        {
            in.reset();
            float largest=0;
            try{largest = in.nextFloat();}
            catch (InputMismatchException e){}
            while(in.hasNextFloat())
            {
                float input=0;
                try{input = in.nextFloat();}
                catch(InputMismatchException e){}
                if ( input > largest )
                {
                    largest = input;
                    System.out.println("The maxiumum number is: " +largest);
                }

                float smallest = in.nextFloat();
                while(in.hasNextFloat())
                {float input2 = in.nextFloat();
                    if ( input2 > smallest )
                    {
                        smallest = input;
                        System.out.println("The maxiumum number is: " +smallest);
                    }
                    System.out.println(largest - smallest);
                }
            }
            in.close();
        }
    }
}