In this lab we add a couple of more features to the previous lab, but in this one we end up using get and set, as well as separate classes.
Source: ——————————————————————————————————–
import java.util.Scanner;
public class NumberStatsClass{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
NumberStats NewNumber = new NumberStats();
float num2;
boolean end = true;
NewNumber.welcome();
num2 = input.nextInt();
if(num2 == 1){
while( end )
{
{
String num1 = input.next();
char num3 = num1.charAt(0);
if(num3 == ‘q’)
{
end = false;
NewNumber.Message();
}
else
{
end = true;
float num = Float.parseFloat(num1);
NewNumber.NewValue(num);
}}}}}}end of first class
public class NumberStats
{
private float firstNumber, lastNumber, maxNumber, minNumber, sum, count, count1, count2;
public void welcome(){
System.out.println(“Enter a 1 to input numbers, press enter after every digit. When done press q to compute.”);
System.out.println(“Enter a 2 to quit.”);}
public void NewValue(float num){
sum += num;
count++;
lastNumber = num;
if (count == 1){
minNumber = num;
maxNumber = num;
firstNumber = num;
}
if (num < minNumber){
minNumber = num;
}
if (num > maxNumber){
maxNumber = num;
}
if(num%1 == 0)
{
count1 ++;
}
else
{
count2 ++;
}
}
public void setFirstNumber(float num){
firstNumber = num;
}
public float getFirstNumber(){
return firstNumber;
}
public void setLastNumber(float num){
lastNumber = num;
}
public float getLastNumber(){
return lastNumber;
}
public void setMaxNumber(float num){
maxNumber = num;
}
public float getMaxNumber(){
return maxNumber;
}
public void setMinNumber(float num){
minNumber = num;
}
public float getMinNumber(){
return minNumber;
}
public void Message(){
if(count == count)
{
System.out.printf(“First number entered: %f \n”, getFirstNumber());
System.out.printf(“Last number entered: %f \n”, getLastNumber());
System.out.printf(“Maximum : %f \n”, getMaxNumber());
System.out.printf(“Minimum : %f \n”, getMinNumber());
System.out.printf(“Average : %f \n”, sum/count);
System.out.printf(“Sum : %f \n”, sum);
System.out.printf(“Total number(s) entered: %f \n”, count);
}}}
————————————————————————————————–
Out Put