In: Computer Science
The Lab 1 development assignment was largely an exercise in completing an already started implementation. The Lab 2 development assignment will call on you to implement a program from scratch. It’s an exercise in learning more about Java basics, core Java Classes and Class/ method-level modularity.
Implement a ‘runnable’ Class called “NumericAnalyzer”. Here’s the functional behavior that must be implemented.
NOTE: Don’t prompt the user for input – this is an exercise passing values to your program via the command line!
Development guidelines:
NOTE: Deriving calculations and displaying output to a Console are separate threads of responsibility, and should therefore be implemented independently of each other.
So your main() method will include a sequence of instructions similar to this:
// main() method code fragment example // Display some error message … (System.err. ) System.exit(1); } NumericAnalyzer analyzer = new NumericAnalyzer(args); analyzer.calculate(); analyzer.display(); System.exit(0); |
Your ‘test cases’ should include testing for (see sample output below –PASTE YOUR Console OUTPUT ON PAGE 5 below):
SAMPLE OUTPUT FOR TEST CASES 1, 2, & 3
|
(2) 256 Count: 1 Min: 256 Max: 256 Range: 0 Sum: 256 Mean: 256 Median: 256 Variance: 0 Standard Deviation: 0 |
(3) 2 4 8 16 32 64 128 256 512 Count: 9 Min: 2 Max: 512 Range: 510 Sum: 1,022 Mean: 113 Median: 32 Variance: 25,941 Standard Deviation: 161 |
PASTE YOUR OUTPUT HERE
0 args |
1 arg |
multiple args (at least 6) |
Sample output
2 4 8 16 32 64 128 256 512
Size: 9
Min: 2
Max: 512
Range: 510
Sum: 1,022
Mean: 113
Median: 32
Variance: 25,941
Standard Deviation: 161
-----------------------------------------------------------------
Lab 1 java code (if needed)
public class JulianCalendar { static private final int MAX_DAY = 31; static private final String[] MONTH_NAMES = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; static private final int[] MONTH_SIZES = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static private void displayDayHeading() { System.out.printf("%6s", "Day"); } static private void displayHeading() { displayDayHeading(); for (int i = 0; i < MONTH_NAMES.length; ++i) { System.out.printf("%5s", MONTH_NAMES[i]); } displayDayHeading(); } static public void display() { displayHeading(); for (int i = 0; i < MAX_DAY; i++) { System.out.printf("\n%6d", i + 1); for (int j = 0; j < MONTH_NAMES.length; j++) { int sum = 0; for (int k = 0; k < j; k++) { sum += MONTH_SIZES[k]; } if (sum + MONTH_SIZES[j] >= sum + i + 1) System.out.printf(" %03d", sum + i + 1); else System.out.printf(" %03d", 0); } System.out.printf("%6d", i + 1); } System.out.println(); } public static void main(String[] args) { display(); } }
If you have any doubts, please give me comment...
class NumericAnalyzer{
int arr[];
int size;
int average;
int median;
int min;
int max;
int sum;
int range;
int variance;
int stdDev;
public NumericAnalyzer(String args[]){
size = args.length;
arr = new int[size];
for(int i=0; i<size; i++){
arr[i] = Integer.parseInt(args[i]);
}
}
public void calcSum(){
sum = 0;
for(int i=0; i<size; i++){
sum += arr[i];
}
}
public void calcAverage(){
calcSum();
average = sum/size;
}
public void calcMedian(){
if(size%2!=0)
median = arr[size/2];
else
median = (arr[size/2]+arr[(size/2)+1])/2;
}
public void calcMin(){
min = arr[0];
for(int i=1; i<size; i++){
if(min>arr[i])
min = arr[i];
}
}
public void calcMax(){
max = arr[0];
for(int i=1; i<size; i++){
if(max<arr[i])
max = arr[i];
}
}
public void calcRange(){
range = max-min;
}
public void calcVariance(){
calcAverage();
variance = 0;
for(int i=0; i<size; i++){
variance += (average-arr[i])*(average-arr[i]);
}
variance /= size;
}
public void calcStdDev(){
calcVariance();
stdDev = (int)Math.sqrt(variance);
}
public void calculate(){
calcMin();
calcMax();
calcMedian();
calcRange();
calcStdDev();
}
public void display(){
for(int i=0; i<size; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("Count:\t\t"+size);
System.out.println("Min:\t\t"+min);
System.out.println("Max:\t\t"+max);
System.out.println("Range:\t\t"+range);
System.out.println("Sum:\t\t"+sum);
System.out.println("Mean:\t\t"+average);
System.out.println("Median:\t\t"+median);
System.out.println("Variance:\t\t"+variance);
System.out.println("Stanadard Deviation:\t"+stdDev);
}
}
public class NumericAnalyzerTest {
public static void main(String[] args) {
if (args.length == 0) {
// Display some error message … (System.err. )
System.out.println("Usage: numbers...");
System.exit(1);
}
NumericAnalyzer analyzer = new NumericAnalyzer(args);
analyzer.calculate();
analyzer.display();
System.exit(0);
}
}