Lab Description:
In this lab further knowledge of Java such as getter and setter methods and object instantiation will be applied to the previous lab to write an easier code to read. Methods are used in this case since a class’s private attributes can only be manipulated by methods and object instantiation is used to create classes for each object.
Code:
import java.util.Scanner;
public class Lab3 {public static void main (String [] args)
{
NumberStats myNumberStats = new NumberStats();
Scanner input = new Scanner( System.in);
int number;do{
System.out.println ("Enter a series of numbers.");
System.out.println ("Enter 0 when finshed entering numbers.");
number = input.nextInt();
myNumberStats.setNewNumber(number);}while (number != 0);
//do while
//display menuSystem.out.println(myNumberStats.getFirstNumber());
System.out.println(myNumberStats.getlastNumber());
System.out.println(myNumberStats.getcount());
System.out.println(myNumberStats.getmaximum());
System.out.println(myNumberStats.getminimum());
System.out.println(myNumberStats.getsum());
System.out.println(myNumberStats.getaverage());//Call all the get methods and display them
}
public static int total(int number, int sum)
{
int totalsum = number + sum;
return totalsum;
}}
*************************************************************************
import java.util.Scanner;public class NumberStats {
private int firstNumber;
private int lastnumber;
private int count;
private int maximum;
private int minimum;
private int sum;
private int average;public void setNewNumber(int n){
//first
//last
//lab2
int first = 0;
int last = 0;
int min = 0;
int number = 0;
int max = 0;
int sum = 0;
int numberAmount = 0; //keeps count of how many numbers are entered
int average = 0;//Create a Scanner object for keyboard input.
Scanner input = new Scanner(System.in);//Display general instructions.
System.out.println ("Enter a series of numbers.");System.out.println ("Enter 0 when finshed entering numbers.");
do {
//Get the numbers from user input.
number = input.nextInt();
if (numberAmount == 0){
first = number;
min = number;
max = number;}
sum = sum + number;
if(number != 0){
last = number;
numberAmount++; //value increases by one every time a new number is entered
}if (number > max)
{
max = number;}
if (number < min) { min = number; } } while (number != 0);} public int getFirstNumber() { return firstNumber; } public int getlastNumber(){ return lastnumber; } public int getcount(){ return count; } public int getmaximum(){ return maximum; } public int getminimum(){ return minimum; } public int getsum(){ return sum; } public int getaverage(){ return average; } } ******************************************************************************* import java.util.Scanner; public class Lab3 { public static void main (String [] args) { NumberStats mydoubleNumberStats = new doubleNumberStats(); Scanner input = new Scanner( System.in); double number; do{ System.out.println ("Enter a series of numbers."); System.out.println ("Enter 0 when finshed entering numbers."); number = input.nextDouble(); myNumberStats.setNewNumber(number); }while (number != 0); //do while //display menu System.out.println(mydoubleNumberStats.getFirstNumber()); System.out.println(mydoubleStats.getlastNumber()); System.out.println(mydoubleStats.getcount()); System.out.println(mydoubleStats.getmaximum()); System.out.println(mydoubleStats.getminimum()); System.out.println(mydoubleStats.getsum()); System.out.println(mydoubleStats.getaverage()); //Call all the get methods and display them } } *********************************************************************************************** import java.util.Scanner; public class mydoublenumberstats { private double firstNumber; private double lastnumber; private double count; private double maximum; private double minimum; private double sum; private double average; public void setNewNumber(double n){ //lab2 double first = 0; double last = 0; double min = 0; double number = 0; double max = 0; double sum = 0; double numberAmount = 0; //keeps count of how many numbers are entered double average = 0; //Create a Scanner object for keyboard input. Scanner input = new Scanner(System.in); //Display general instructions. System.out.println ("Enter a series of numbers."); System.out.println ("Enter 0 when finshed entering numbers."); do { //Get the numbers from user input. number = input.nextDouble(); if (numberAmount == 0){ first = number; min = number; max = number; } sum = sum + number; if(number != 0){ last = number; numberAmount++; //value increases by one every time a new number is entered } if (number > max)
{
max = number;}
if (number < min) { min = number; } } while (number != 0);} public double getFirstNumber() { return firstNumber; } public double getlastNumber(){ return lastnumber; } public double getcount(){ return count; } public double getmaximum(){ return maximum; } public double getminimum(){ return minimum; } public double getsum(){ return sum; } public double average(){ return average; } } Screenshots: