This is a java programming question If you could help me code it and explain why
ID: 3561030 • Letter: T
Question
This is a java programming question
If you could help me code it and explain why you did that code it would help a lot!! Thanks
Write a program that reads in a sequence of numbers of integers from keyboard until 0 is read, and store them in an array. You may assume that there will not be more than 100 numbers.
Then find the maximum number, the sum of positive numbers, and count the number of negative numbers. You need to write three methods: findMax, computePositiveSum, and countNegative.
Here are the method to be implemented :
public static int findMax(int[] numbers, int i)
public static int computePositiveSum(int[] numbers, int i)
public static int countNegative(int[] numbers, int i)
The program should output the results of those calculations to standard output. The numbers could be delimited by any kind of white space, i.e. tabs, spaces, and lines. (Use the Scanner class) Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted using the following format:
The maximum number is
The sum of the positive numbers is
The total number of negative numbers is
Do not prompt to query for the numbers. The number 0 is excluded in the sequence of numbers and should not be included in your calculations.
Explanation / Answer
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
//array of 10 numbers
int numbers[] = new int[100];
for(int i=0;i<100;i++)
{
numbers[i]=0;
}
System.out.println("Enter numbers:-");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;n!=0;i++)
{
numbers[i]=n;
n=sc.nextInt();
}
sc.close();
System.out.println("Largest Number is : " + findMax(numbers));
System.out.println("The sum of the positive numbers is : " + computePositiveSum(numbers));
System.out.println("The total number of negative numbers is : " + countNegative(numbers));
}
public static int findMax(int[] numbers)
{
int largest = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largest)
largest = numbers[i];
}
return largest;
}
public static int computePositiveSum(int[] numbers)
{
int sum = 0;
for(int i=0; i< numbers.length; i++)
{
if(numbers[i]>=0)
sum+=numbers[i];
}
return sum;
}
public static int countNegative(int[] numbers)
{
int count = 0;
for(int i=0; i< numbers.length; i++)
{
if(numbers[i] < 0)
count++;
}
return count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.