97 lines
7.0 KiB
HTML
97 lines
7.0 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Stats.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: Ubuntu}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: Ubuntu}
|
|
table {color: #888888; background-color: #313335; font-family: Ubuntu}
|
|
.literal {color: #cc7832}
|
|
.ST4 {font-family: Ubuntu; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST2 {color: #ffc66d; font-family: Ubuntu; font-style: italic}
|
|
.ST3 {color: #9876aa; font-family: Ubuntu; font-style: italic}
|
|
.ST1 {color: #808080; font-family: Ubuntu; font-weight: bold}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/MP3_CalebFontenot/src/mp3_calebfontenot/Stats.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> mp3_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.util.Scanner;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> Stats {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
|
{
|
|
<span class="comment">// Create scanner</span>
|
|
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>);
|
|
<span class="comment">// Define variables</span>
|
|
<span class="literal">double</span> mean = <span class="number">0</span>; <span class="comment">//It's twice as mean</span>
|
|
<span class="literal">double</span> inputSum = <span class="number">0</span>, numberOfTimesRun = <span class="number">0</span>, inputPow = <span class="number">0</span>, deviation = <span class="number">0</span>;
|
|
String userInput = <span class="string">""</span>;
|
|
|
|
<span class="comment">// Prompt for input</span>
|
|
<span class="literal">do</span> {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Press Y/y to enter a series of numbers or Q/q to quit.</span><span class="string">"</span>);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Current sum: </span><span class="string">"</span> + inputSum);
|
|
userInput = input.next();
|
|
<span class="literal">if</span> (userInput.toLowerCase().equals(<span class="string">"</span><span class="string">y</span><span class="string">"</span>)) {
|
|
<span class="literal">try</span> {
|
|
<span class="literal">do</span> {
|
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Please enter a series of numbers; Type '-1' to quit (no quotes, you doofus!): </span><span class="string">"</span>);
|
|
userInput = input.next();
|
|
<span class="literal">if</span> (Double.<span class="ST4">parseDouble</span>(userInput) != -<span class="number">1</span>) {
|
|
inputSum += Double.<span class="ST4">parseDouble</span>(userInput);
|
|
inputPow += Math.<span class="ST4">pow</span>(Double.<span class="ST4">parseDouble</span>(userInput), <span class="number">2</span>);
|
|
numberOfTimesRun++; <span class="comment">// counter to store number of times we've added to input sum</span>
|
|
}
|
|
<span class="literal">if</span> (numberOfTimesRun == <span class="number">0</span>) {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">No numbers entered!</span><span class="string">"</span>);
|
|
}
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Current sum: </span><span class="string">"</span> + inputSum);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Input sum^2: </span><span class="string">"</span> + inputPow);
|
|
} <span class="literal">while</span> (Double.<span class="ST4">parseDouble</span>(userInput) != -<span class="number">1</span>);
|
|
} <span class="literal">catch</span> (Exception e) {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Invalid input!</span><span class="string">"</span>);
|
|
}
|
|
|
|
} <span class="literal">else</span> {
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Invalid input!</span><span class="string">"</span>);
|
|
}
|
|
<span class="comment">// Calculate mean</span>
|
|
mean = inputSum / numberOfTimesRun;
|
|
<span class="comment">// Calculate deviation</span>
|
|
<span class="literal">if</span> (numberOfTimesRun == <span class="number">1</span>) { <span class="comment">// if the loop has only run once, don't bother calculating it, because the deviation is zero.</span>
|
|
deviation = <span class="number">0</span>;
|
|
} <span class="literal">else</span> {
|
|
deviation = Math.<span class="ST4">sqrt</span>((inputPow - (Math.<span class="ST4">pow</span>(inputSum, <span class="number">2</span>) / numberOfTimesRun)) / (numberOfTimesRun - <span class="number">1</span>));
|
|
}
|
|
System.<span class="ST3">out</span>.println();
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The mean is: </span><span class="string">"</span> + mean);
|
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">The deviation is: </span><span class="string">"</span> + deviation);
|
|
|
|
}
|
|
<span class="literal">while</span> (!(userInput.toLowerCase().equals(<span class="string">"</span><span class="string">q</span><span class="string">"</span>)));
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|