123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- /**
- * <https://y.st./>
- * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org./licenses/>.
- **/
- $xhtml = array(
- 'title' => 'Unit5.java',
- 'subtitle' => 'Written in <span title="Programming 1">CS 1102</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-05-10',
- 'copyright year' => '2017',
- 'body' => <<<END
- <h2>Source code:</h2>
- <blockquote>
- <pre><code>/*
- * Copyright (C) 2017 Alex Yst
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package unit5;
- /**
- * Alex Yst's CS 1102 (Programming 1) Unit 5 submission
- *
- * This class was programmed to fill the requirements on the Unit 5 programming
- * assignment in University of the People's CS 1102 (Programming 1) course.
- *
- * @author Alex Yst
- */
- public class Unit5 {
- /**
- * This method defines a class used for stats calculation, then uses it to
- * compute some statistics.
- *
- * @param arguments This parameter is unused.
- */
- public static void main(String[] arguments) {
- /*
- * 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, and the standard deviation
- */
- class StatCalc {
- 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.
- /**
- * Add a number to the dataset. The statistics will be computed for all
- * the numbers that have been added to the dataset using this method.
- */
- public void enter(double num) {
- count++;
- sum += num;
- squareSum += num*num;
- }
- /**
- * Return the number of items that have been entered into the dataset.
- */
- public int getCount() {
- return count;
- }
- /**
- * Return the sum of all the numbers that have been entered.
- */
- public double getSum() {
- return sum;
- }
- /**
- * Return the average of all the items that have been entered.
- * The return value is Double.NaN if no numbers have been entered.
- */
- public double getMean() {
- return sum / count;
- }
- /**
- * Return the standard deviation of all the items that have been entered.
- * The return value is Double.NaN if no numbers have been entered.
- */
- public double getStandardDeviation() {
- double mean = getMean();
- return Math.sqrt( squareSum/count - mean*mean );
- }
- } // end class StatCalc
- StatCalc myStatCalc = new StatCalc();
- myStatCalc.enter(5);
- myStatCalc.enter(7);
- myStatCalc.enter(12);
- myStatCalc.enter(23);
- myStatCalc.enter(3);
- myStatCalc.enter(2);
- myStatCalc.enter(8);
- myStatCalc.enter(14);
- myStatCalc.enter(10);
- myStatCalc.enter(5);
- myStatCalc.enter(9);
- myStatCalc.enter(13);
- System.out.println("The following number set has the following statistics.");
- System.out.println("Number Set: 5, 7, 12, 23, 3, 2, 8, 14, 10, 5, 9, 13");
- System.out.println("Count: " + myStatCalc.getCount());
- System.out.println("Mean: " + myStatCalc.getMean());
- System.out.println("Standard Deviation: " + myStatCalc.getStandardDeviation());
- }
- }</code></pre>
- </blockquote>
- <h2>Output:</h2>
- <blockquote>
- <pre>run:
- The following number set has the following statistics.
- Number Set: 5, 7, 12, 23, 3, 2, 8, 14, 10, 5, 9, 13
- Count: 12
- Mean: 9.25
- Standard Deviation: 5.539629951540085
- BUILD SUCCESSFUL (total time: 0 seconds)</pre>
- </blockquote>
- END
- );
|