will be the construction of a program that reads in a sequence of numbers (not n
ID: 3624941 • Letter: W
Question
will be the construction of a program that reads in a sequence of numbers (not necessary integers) from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers.Then compute the minimum number, the sum of numbers at odd indexes (1,3,5,...) of the array, the sum of negative numbers, and count the number of positive numbers, using recursion. Thus you will create recursive methods findMin, computeSumAtOdd, computeNegativeSum, and countPositive in Assignment9 class and they will be called by a main method.
Specifically, the following recursive methods must be implemented (These methods should not contain any loop):
public static double findMin(double[] numbers, int startIndex, int endIndex)
public static double computeSumAtOdd(double[] numbers, int startIndex, int endIndex)
public static double computeNegativeSum(double[] numbers, int startIndex, int endIndex)
public static int countPositive(double[] numbers, int startIndex, int endIndex)
If these methods are implemented using a Loop or any Static Variable, points will be deducted (from the test cases) even if your program passes test cases. DO NOT use any Static Variables.
The program should output the results of those calculations to standard output. Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:
The minimum number is 0.00#
The sum of the numbers at odd indexes is 0.00
The sum of the negative numbers is 0.###
The total count of positive numbers is 0
To format numbers as above, you should utilize the DecimalFormat class (java.text package).
Do not output a prompt to query for the numbers. The number 0 is included in the sequence of numbers and should be included in all of your calculations.
Explanation / Answer
please rate - thanks
import java.io.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class resursion
{public static void main(String[] args)throws IOException
{int i=0;
NumberFormat decimal3 = new DecimalFormat("#0.000");
NumberFormat decimal2 = new DecimalFormat("#0.00");
NumberFormat decimal0 = new DecimalFormat("#0");
double[]s=new double[100];
String inputString;
BufferedReader input=new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Enter number "+(i+1)+"( 0 to exit): ");
inputString=input.readLine();
s[i]=Double.parseDouble(inputString);
while(s[i]!=0)
{i++;
System.out.print("Enter number "+(i+1)+" 0 to exit): ");
inputString=input.readLine();
s[i]=Double.parseDouble(inputString); }
System.out.println("Minimum number: "+decimal2.format(findMin(s,0,i-1)));
System.out.println("Sum of Negative numbers: "+decimal3.format(computeNegativeSum(s,0,i-1)));
System.out.println("Number of Positive numbers: "+decimal0.format(countPositive(s,0,i-1)));
System.out.println("Sum of the numbers at odd indices: "+decimal2.format(computeSumAtOdd(s,1,i-1)));
}
public static double findMin(double[] numbers, int startIndex, int endIndex)
{int mid;
double top,bot;
if(startIndex==endIndex)
return numbers[startIndex];
mid=(startIndex+endIndex)/2;
top=findMin(numbers,startIndex,mid);
bot=findMin(numbers,mid+1,endIndex);
if(top<=bot)
return top;
else
return bot;
}
public static double computeNegativeSum(double[] numbers, int startIndex, int endIndex)
{if(startIndex==endIndex)
if (numbers[startIndex]<0)
return numbers[startIndex];
else
return 0;
else
if(numbers[startIndex]<0)
return numbers[startIndex] + computeNegativeSum(numbers, startIndex+1, endIndex);
else
return computeNegativeSum(numbers, startIndex+1, endIndex);
}
public static double computeSumAtOdd(double[] numbers, int startIndex, int endIndex)
{if(startIndex==endIndex)
if (startIndex%2==1)
return numbers[startIndex];
else
return 0;
else
if(startIndex%2==1)
return numbers[startIndex] + computeSumAtOdd(numbers, startIndex+1, endIndex);
else
return computeSumAtOdd(numbers, startIndex+1, endIndex);
}
public static int countPositive(double[] numbers, int startIndex, int endIndex)
{
if(startIndex==endIndex)
if (numbers[startIndex]>0)
return 1;
else
return 0;
else
if(numbers[startIndex]>0)
return 1 + countPositive(numbers, startIndex+1, endIndex);
else
return countPositive(numbers, startIndex+1, endIndex);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.