Laboratory Activity

import java.io.*;

class method0 {
public static void main(String args[]) throws IOException{
int x=0, num1=0, num2=0,num3=0;

InputStreamReader in= new InputStreamReader(System.in);
BufferedReader stndin = new BufferedReader(in);
String line;

System.out.print("Input an integer: ");
System.out.flush();
line = stndin.readLine();
num1 = Integer.parseInt(line);

System.out.print("Input an integer: ");
System.out.flush();
line = stndin.readLine();
num2 = Integer.parseInt(line);

System.out.print("Input an integer: ");
System.out.flush();
line = stndin.readLine();
num3 = Integer.parseInt(line);

System.out.println("\nThe number is = "+num1);
System.out.println("The number is = "+num2);
System.out.println("The number is = "+num3);
}
}
_________________________________________________________________

/* this program demonstrates how to input data from a method */

import java.io.*;

class method1 {

public static int inputdata(int x) throws IOException {
InputStreamReader in= new InputStreamReader(System.in);
BufferedReader stndin = new BufferedReader(in);
String line;

System.out.print("Input any integer: ");
System.out.flush();
line = stndin.readLine();
x = Integer.parseInt(line);
return x;
}

public static void main(String args[]) throws IOException{
int num1=0, num2=0, num3=0;
num1 = inputdata(num1);
num2 = inputdata(num2);
num3 = inputdata(num3);
System.out.println("1st number = "+num1);
System.out.println("2nd number = "+num2);
System.out.println("3rd number = "+num3);
}
}


----------------------------------------------------------------------------------------

/* this program demonstrates how to define method which return
a value to the calling method. This program will also
compute the largest and smalles element within the main
method. */

import java.io.*;

class method2 {

// reads a value for x and returns it to the calling method
public static int inputdata(int x) throws IOException {
InputStreamReader in= new InputStreamReader(System.in);
BufferedReader stndin = new BufferedReader(in);
String line;

System.out.print("Input any integer: ");
System.out.flush();
line = stndin.readLine();
x = Integer.parseInt(line);
return x;
}

public static void main(String args[]) throws IOException{
int num1=0, num2=0,num3=0, large=0, small=0;

num1 = inputdata(num1);
num2 = inputdata(num2);
num3 = inputdata(num3);

large = num1;
if (large < large="num2;" large="num3;" small =" num1;">num2) small=num2;
if (small>num3) small=num3;
System.out.println("\nThe largest is = "+large);
System.out.println("The smallest is = "+small);
}
}

________________________________________________________________

import java.io.*;

public class method3{

// reads a value for x and returns it to the calling method
public static int inputdata(int x) throws IOException {
InputStreamReader in= new InputStreamReader(System.in);
BufferedReader stndin = new BufferedReader(in);
String line;

System.out.print("Input any integer: ");
System.out.flush();
line = stndin.readLine();
x = Integer.parseInt(line);
return x;
}



static int findmax(int x, int y, int z){
int largest = x;
if (largest < largest =" y;" largest =" z;" smallst =" x;"> y) smallst = y;
if (smallst > z) smallst = z;
return smallst;
}

public static int findmid(int x, int y, int z,int largest, int smallst)
{

if ((x > smallst) && (x <> smallst) && (y <> smallst) && (z < x="0," y="0," z="0," largest="0," smallst="0,middle=" x =" inputdata(x);" y =" inputdata(y);" z =" inputdata(z);" largest =" findmax(x,y,z);" smallst =" findmin(x,y,z);" middle =" findmid(x,y,z,largest,smallst);" is ="=" is ="=" is ="=" if="" large="num2;">

Exercise
A statistic is a number that summarizes some property of a set of data.Common statistics include the mean (also known as the average) and the standard deviation (which tells how spread out the data are from the mean). A class called StatCalc that can be used to compute these statistics, as well as the sum of the items in the dataset and the number of items in the dataset. You can read the source code for this class in the file StatCalc.java. If calc is a variable of type StatCalc, then the following methods are defined:

* calc.enter(item); where item is a number, adds the item to the dataset.
* calc.getCount() is a function that returns the number of items that have been added to the dataset.
* calc.getSum() is a function that returns the sum of all the items that have been added to the dataset.
* calc.getMean() is a function that returns the average of all the items.
* calc.getStandardDeviation() is a function that returns the standard deviation of the items.

Typically, all the data are added one after the other calling the enter() method over and over, as the data become available. After all the data have been entered, any of the other methods can be called to get statistical information about the data. The methods getMean() and getStandardDeviation() should only be called if the number of items is greater than zero.

Modify the current source code, StatCalc.java, to add instance methods getMax() and getMin(). The getMax() method should return the largest of all the items that have been added to the dataset, and getMin() should return the smallest. You will need to add two new instance variables to keep track of the largest and smallest items that have been seen so far.

Test your new class by using it in a program to compute statistics for a set of non-zero numbers entered by the user. Start by creating an object of type StatCalc:

StatCalc calc; // Object to be used to process the data.
calc = new StatCalc();

Read numbers from the user and add them to the dataset. Use 0 as a sentinel value (that is, stop reading numbers when the user enters 0). After all the user's non-zero numbers have been entered, print out each of the six statistics that available from calc.


Pseudocode

/*
Computes and display several statistics for a set of non-zero
numbers entered by the user. (Input ends when user enters 0.)
This program uses StatCalc.java.0
*/

public class SimpleStats {

public void enter(double num) {

}

public int getCount() {
// Return number of items that have been entered.
return count;
}

public double getSum() {
// Return the sum of all the items that have been entered.
return sum;
}

public double getMean() {
// Return average of all the items that have been entered.
// Value is Double.NaN if count == 0.
return sum / count;
}

public double getStandardDeviation() {
// Return standard deviation of all the items that have been entered.
// Value will be Double.NaN if count == 0.
double mean = getMean();
return Math.sqrt( squareSum/count - mean*mean );
}

public double getMin() {
// Return the smallest item that has been entered.
// Value will be infinity if no items have been entered.
return min;
}

public double getMax() {
// Return the largest item that has been entered.
// Value will be -infinity if no items have been entered.
return max;
}

} // end class StatCalc

public static void main(String[] args) {


} // end main()

} // end SimpleStats



Post a Comment

0 Comments