/*
An object of class StatCalc can be used to compute several simple statistics
for a set of numbers. Numbers are entered into the dataset using
the enter(double) method. Methods are provided to return the following
statistics for the set of numbers that have been entered: The number
of items, the sum of the items, the average, the standard deviation,
the maximum, and the minimum.
*/
public class Stats {
private int count; // Number of numbers that have been entered.
private double sum; // The sum of all the items that have been entered.
private double squareSum; // The sum of the squares of all the items.
private double max = Double.NEGATIVE_INFINITY; // Largest item seen.
private double min = Double.POSITIVE_INFINITY; // Smallest item seen.
int[] myList;
public int getCount() {
System.out.println(myList.size());
count = myList.size().
// Return number of items that have been entered.
return count;
}
public double getSum() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
return sum;
}
public double getMean() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
if(count > 0)
return sum / count;
else
return -1;
}
public double getMin() {
int smallest = myList[0];
int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return smallest;
}
public double getMax() {
int smallest = myList[0];
int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return largetst;
}
stats(int [] data)
{
if(data.count <= count)
{
if(myList.size() > 0)
System.arraycopy( myList, 0, data, 0, count );
}
}
stats()
{
Scanner s = new Scanner(System.in);
int count = s.nextInt();
s.nextLine(); // throw away the newline.
int [] numbers = new int[count];
Scanner numScanner = new Scanner(s.nextLine());
for (int i = 0; i < count; i++) {
if (numScanner.hasNextInt()) {
numbers[i] = numScanner.nextInt();
} else {
System.out.println(\"You didn\'t provide enough numbers\");
break;
}
}
System.arraycopy( myList, 0, numbers, 0, count );
}
stats(int size)
{
setValue(size);
}
private void setValue(int size)
{
count = size;
myList = new int[size];
}
} // end class StatCalc
public class SimpleStats {
public static void main(String[] args) {
Stat calc; // Computes stats for numbers entered by user.
calc = new Stat(100);
int arry[] = {20.20.40,30.80,,70,4,5,6,7,10}
calc.setvalue(array);
double item; // One number entered by the user.
TextIO.putln(\"Enter your numbers. Enter 0 to end.\");
TextIO.putln();
do {
TextIO.put(\"? \");
item = TextIO.getlnDouble();
if (item != 0)
calc.enter(item);
} while ( item != 0 );
TextIO.putln(\"\ Statistics about your calc:\ \");
TextIO.putln(\" Count: \" + calc.getCount());
TextIO.putln(\" Sum: \" + calc.getSum());
TextIO.putln(\" Minimum: \" + calc.getMin());
TextIO.putln(\" Maximum: \" + ca.
Web & Social Media Analytics Previous Year Question Paper.pdf
An object of class StatCalc can be used to compute several simp.pdf
1. /*
An object of class StatCalc can be used to compute several simple statistics
for a set of numbers. Numbers are entered into the dataset using
the enter(double) method. Methods are provided to return the following
statistics for the set of numbers that have been entered: The number
of items, the sum of the items, the average, the standard deviation,
the maximum, and the minimum.
*/
public class Stats {
private int count; // Number of numbers that have been entered.
private double sum; // The sum of all the items that have been entered.
private double squareSum; // The sum of the squares of all the items.
private double max = Double.NEGATIVE_INFINITY; // Largest item seen.
private double min = Double.POSITIVE_INFINITY; // Smallest item seen.
int[] myList;
public int getCount() {
System.out.println(myList.size());
count = myList.size().
// Return number of items that have been entered.
return count;
}
public double getSum() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
2. return sum;
}
public double getMean() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
if(count > 0)
return sum / count;
else
return -1;
}
public double getMin() {
int smallest = myList[0];
int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return smallest;
}
public double getMax() {
int smallest = myList[0];
3. int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return largetst;
}
stats(int [] data)
{
if(data.count <= count)
{
if(myList.size() > 0)
System.arraycopy( myList, 0, data, 0, count );
}
}
stats()
{
Scanner s = new Scanner(System.in);
int count = s.nextInt();
s.nextLine(); // throw away the newline.
int [] numbers = new int[count];
Scanner numScanner = new Scanner(s.nextLine());
for (int i = 0; i < count; i++) {
if (numScanner.hasNextInt()) {
4. numbers[i] = numScanner.nextInt();
} else {
System.out.println("You didn't provide enough numbers");
break;
}
}
System.arraycopy( myList, 0, numbers, 0, count );
}
stats(int size)
{
setValue(size);
}
private void setValue(int size)
{
count = size;
myList = new int[size];
}
} // end class StatCalc
public class SimpleStats {
public static void main(String[] args) {
Stat calc; // Computes stats for numbers entered by user.
calc = new Stat(100);
int arry[] = {20.20.40,30.80,,70,4,5,6,7,10}
calc.setvalue(array);
5. double item; // One number entered by the user.
TextIO.putln("Enter your numbers. Enter 0 to end.");
TextIO.putln();
do {
TextIO.put("? ");
item = TextIO.getlnDouble();
if (item != 0)
calc.enter(item);
} while ( item != 0 );
TextIO.putln(" Statistics about your calc: ");
TextIO.putln(" Count: " + calc.getCount());
TextIO.putln(" Sum: " + calc.getSum());
TextIO.putln(" Minimum: " + calc.getMin());
TextIO.putln(" Maximum: " + calc.getMax());
} // end main()
} // end Stats
/*
An object of class StatCalc can be used to compute several simple statistics
for a set of numbers. Numbers are entered into the dataset using
the enter(double) method. Methods are provided to return the following
statistics for the set of numbers that have been entered: The number
of items, the sum of the items, the average, the standard deviation,
the maximum, and the minimum.
*/
public class Stats {
private int count; // Number of numbers that have been entered.
private double sum; // The sum of all the items that have been entered.
6. private double squareSum; // The sum of the squares of all the items.
private double max = Double.NEGATIVE_INFINITY; // Largest item seen.
private double min = Double.POSITIVE_INFINITY; // Smallest item seen.
int[] myList;
public int getCount() {
System.out.println(myList.size());
count = myList.size().
// Return number of items that have been entered.
return count;
}
public double getSum() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
return sum;
}
public double getMean() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
if(count > 0)
return sum / count;
else
7. return -1;
}
public double getMin() {
int smallest = myList[0];
int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return smallest;
}
public double getMax() {
int smallest = myList[0];
int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return largetst;
}
8. stats(int [] data)
{
if(data.count <= count)
{
if(myList.size() > 0)
System.arraycopy( myList, 0, data, 0, count );
}
}
stats()
{
Scanner s = new Scanner(System.in);
int count = s.nextInt();
s.nextLine(); // throw away the newline.
int [] numbers = new int[count];
Scanner numScanner = new Scanner(s.nextLine());
for (int i = 0; i < count; i++) {
if (numScanner.hasNextInt()) {
numbers[i] = numScanner.nextInt();
} else {
System.out.println("You didn't provide enough numbers");
break;
}
}
System.arraycopy( myList, 0, numbers, 0, count );
}
stats(int size)
{
9. setValue(size);
}
private void setValue(int size)
{
count = size;
myList = new int[size];
}
} // end class StatCalc
public class SimpleStats {
public static void main(String[] args) {
Stat calc; // Computes stats for numbers entered by user.
calc = new Stat(100);
int arry[] = {20.20.40,30.80,,70,4,5,6,7,10}
calc.setvalue(array);
double item; // One number entered by the user.
TextIO.putln("Enter your numbers. Enter 0 to end.");
TextIO.putln();
do {
TextIO.put("? ");
item = TextIO.getlnDouble();
if (item != 0)
calc.enter(item);
} while ( item != 0 );
TextIO.putln(" Statistics about your calc: ");
10. TextIO.putln(" Count: " + calc.getCount());
TextIO.putln(" Sum: " + calc.getSum());
TextIO.putln(" Minimum: " + calc.getMin());
TextIO.putln(" Maximum: " + calc.getMax());
} // end main()
} // end Stats
Solution
/*
An object of class StatCalc can be used to compute several simple statistics
for a set of numbers. Numbers are entered into the dataset using
the enter(double) method. Methods are provided to return the following
statistics for the set of numbers that have been entered: The number
of items, the sum of the items, the average, the standard deviation,
the maximum, and the minimum.
*/
public class Stats {
private int count; // Number of numbers that have been entered.
private double sum; // The sum of all the items that have been entered.
private double squareSum; // The sum of the squares of all the items.
private double max = Double.NEGATIVE_INFINITY; // Largest item seen.
private double min = Double.POSITIVE_INFINITY; // Smallest item seen.
int[] myList;
public int getCount() {
System.out.println(myList.size());
11. count = myList.size().
// Return number of items that have been entered.
return count;
}
public double getSum() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
return sum;
}
public double getMean() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
if(count > 0)
return sum / count;
else
return -1;
}
public double getMin() {
int smallest = myList[0];
int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
13. }
stats()
{
Scanner s = new Scanner(System.in);
int count = s.nextInt();
s.nextLine(); // throw away the newline.
int [] numbers = new int[count];
Scanner numScanner = new Scanner(s.nextLine());
for (int i = 0; i < count; i++) {
if (numScanner.hasNextInt()) {
numbers[i] = numScanner.nextInt();
} else {
System.out.println("You didn't provide enough numbers");
break;
}
}
System.arraycopy( myList, 0, numbers, 0, count );
}
stats(int size)
{
setValue(size);
}
private void setValue(int size)
{
count = size;
myList = new int[size];
}
14. } // end class StatCalc
public class SimpleStats {
public static void main(String[] args) {
Stat calc; // Computes stats for numbers entered by user.
calc = new Stat(100);
int arry[] = {20.20.40,30.80,,70,4,5,6,7,10}
calc.setvalue(array);
double item; // One number entered by the user.
TextIO.putln("Enter your numbers. Enter 0 to end.");
TextIO.putln();
do {
TextIO.put("? ");
item = TextIO.getlnDouble();
if (item != 0)
calc.enter(item);
} while ( item != 0 );
TextIO.putln(" Statistics about your calc: ");
TextIO.putln(" Count: " + calc.getCount());
TextIO.putln(" Sum: " + calc.getSum());
TextIO.putln(" Minimum: " + calc.getMin());
TextIO.putln(" Maximum: " + calc.getMax());
} // end main()
} // end Stats
15. /*
An object of class StatCalc can be used to compute several simple statistics
for a set of numbers. Numbers are entered into the dataset using
the enter(double) method. Methods are provided to return the following
statistics for the set of numbers that have been entered: The number
of items, the sum of the items, the average, the standard deviation,
the maximum, and the minimum.
*/
public class Stats {
private int count; // Number of numbers that have been entered.
private double sum; // The sum of all the items that have been entered.
private double squareSum; // The sum of the squares of all the items.
private double max = Double.NEGATIVE_INFINITY; // Largest item seen.
private double min = Double.POSITIVE_INFINITY; // Smallest item seen.
int[] myList;
public int getCount() {
System.out.println(myList.size());
count = myList.size().
// Return number of items that have been entered.
return count;
}
public double getSum() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
16. return sum;
}
public double getMean() {
int sum = 0;
for (int i = 0; i < myList.length; i++)
{
sum +=mylist[i];
}
if(count > 0)
return sum / count;
else
return -1;
}
public double getMin() {
int smallest = myList[0];
int largetst = myList[0];
for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return smallest;
}
public double getMax() {
int smallest = myList[0];
int largetst = myList[0];
17. for(int i=1; i< mylist.length; i++)
{
if(mylist[i] > largetst)
largetst = numbers[i];
else if (mylist[i] < smallest)
smallest = numbers[i];
}
return largetst;
}
stats(int [] data)
{
if(data.count <= count)
{
if(myList.size() > 0)
System.arraycopy( myList, 0, data, 0, count );
}
}
stats()
{
Scanner s = new Scanner(System.in);
int count = s.nextInt();
s.nextLine(); // throw away the newline.
int [] numbers = new int[count];
Scanner numScanner = new Scanner(s.nextLine());
for (int i = 0; i < count; i++) {
if (numScanner.hasNextInt()) {
numbers[i] = numScanner.nextInt();
18. } else {
System.out.println("You didn't provide enough numbers");
break;
}
}
System.arraycopy( myList, 0, numbers, 0, count );
}
stats(int size)
{
setValue(size);
}
private void setValue(int size)
{
count = size;
myList = new int[size];
}
} // end class StatCalc
public class SimpleStats {
public static void main(String[] args) {
Stat calc; // Computes stats for numbers entered by user.
calc = new Stat(100);
int arry[] = {20.20.40,30.80,,70,4,5,6,7,10}
calc.setvalue(array);
19. double item; // One number entered by the user.
TextIO.putln("Enter your numbers. Enter 0 to end.");
TextIO.putln();
do {
TextIO.put("? ");
item = TextIO.getlnDouble();
if (item != 0)
calc.enter(item);
} while ( item != 0 );
TextIO.putln(" Statistics about your calc: ");
TextIO.putln(" Count: " + calc.getCount());
TextIO.putln(" Sum: " + calc.getSum());
TextIO.putln(" Minimum: " + calc.getMin());
TextIO.putln(" Maximum: " + calc.getMax());
} // end main()
} // end Stats