Question

/** this is my code, and I want to invoke the method, doubleArraySize() in main method....

/** this is my code, and I want to invoke the method, doubleArraySize() in main method.

and I need to display some result in cmd or terminal.

also, I have classes such as Average and RandomN needed for piping(pipe).

/** this is my code, and I want to invoke the meth

please help me and thank you.

**/

import java.util.Scanner;

public class StdDev {

static double[] arr;

static int N;

public static void main(String[] args) {

if(args.length==0){

arr= new double[N];

Scanner input = new Scanner(System.in);

int i=0;

while(input.hasNextDouble()){

arr[i] =

i++;

}

System.out.println("Average [" + i + "] is "+ average(arr, i));

System.out.println("StdDev ["+ i + "] is "+ StdDev(arr, i));

}

else{

int N = args.length;

arr = new double[N];

try{

for(int i=0; i<N; i++)

arr[i] = Double.parseDouble(args[i]);

System.out.println("Average [" + N+ "] is "+ average(arr, N));

System.out.println("StdDev ["+N + "] is "+ StdDev(arr, N));

}

catch (Exception e) {

System.out.println("Usage: StdDev [n0 n1 n2...n]");

}

}

}

public static double average(double[] data, int size)

{

double total=0;

for (int i = 0; i < data.length; i++)

{

total+=data[i];

}

return total/data.length;

}

public static double StdDev(double[] data, int size)

{

double mean = average(data, N);

double t = 0;

for(double a :data)

t += (a-mean)*(a-mean);

double variance=t/size;

return Math.sqrt(variance);

}

public static double StdDev(double[] data, int size, double mean ){

double t = 0;

for(double a :data)

t += (a-mean)*(a-mean);

double variance=t/size;

return Math.sqrt(variance);

}

public static double[] doubleArraySize(double[] arr){

double[] newarr = new double[ arr.length * 2 ];

for(int i = 0; i<arr.length ; i++)

newarr[i] = arr[i];

return newarr;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

import java.util.Scanner;
public class StdDev {
static double[] arr;
static int N = 1;
public static void main(String[] args) {
if(args.length==0){
arr= new double[N];
Scanner input = new Scanner(System.in);
int i=0;
while(input.hasNextDouble()){
if(i >= N)
{
arr = doubleArraySize(arr);
N *= 2;
}
arr[i] = input.nextDouble();
i++;
}
System.out.println("Average [" + i + "] is "+ average(arr, i));
System.out.println("StdDev ["+ i + "] is "+ StdDev(arr, i));

}
else{
int N = args.length;
arr = new double[N];
try{
for(int i=0; i<N; i++)
arr[i] = Double.parseDouble(args[i]);
System.out.println("Average [" + N+ "] is "+ average(arr, N));
System.out.println("StdDev ["+N + "] is "+ StdDev(arr, N));
}
catch (Exception e) {
System.out.println("Usage: StdDev [n0 n1 n2...n]");
}
}
}

public static double average(double[] data, int size)
{
double total=0;
for (int i = 0; i <size; i++)
{
total+=data[i];
}
return total/size;
}

public static double StdDev(double[] data, int size)
{
double mean = average(data, size);

double t = 0;
for(int i = 0; i < size; i++)
t += (data[i]-mean)*(data[i]-mean);

double variance=t/size;

return Math.sqrt(variance);
}
public static double StdDev(double[] data, int size, double mean ){
double t = 0;
for(double a :data)
t += (a-mean)*(a-mean);

double variance=t/size;

return Math.sqrt(variance);
}

public static double[] doubleArraySize(double[] arr){
double[] newarr = new double[ arr.length * 2 ];

for(int i = 0; i<arr.length ; i++)
newarr[i] = arr[i];

return newarr;


}
}

And the output screenshot is:

Add a comment
Know the answer?
Add Answer to:
/** this is my code, and I want to invoke the method, doubleArraySize() in main method....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at...

    I have this code but when there's 0 pennies, it doesn't output "and 0 pennies" at all, it is just left blank. package edu.wit.cs.comp1050; /** * Solution to the third programming assignment * When it runs it outputs the amount in quarters, dimes, nickels and pennies * * @author Rose Levine * */ import java.util.Scanner; public class PA1c {       /**    * Error message to display for negative amount    */    public static final String ERR_MSG =...

  • JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole...

    JAVA: amortization table: Hi, I have built this code and in one of my methods: printDetailsToConsole I would like it to print the first 3 payments and the last 3 payments if n is larger than 6 payments. Please help! Thank you!! //start of program import java.util.Scanner; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.lang.Math; //345678901234567890123456789012345678901234567890123456789012345678901234567890 /** * Write a description of class MortgageCalculator here. * * @author () * @version () */ public class LoanAmortization {    /* *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT