115k views
1 vote
Write a static method named evenNumbers that accepts a string of text as a parameter. Assume that the text is a series of integers, and process this text and report various statistics about the integers. Report the total number of numbers, the sum of the numbers, the total count of even numbers and the percent of even numbers. For example, if the string is the following: "5 7 2 8 9 10 12 98 7 14 20 22"

User Mclin
by
5.3k points

2 Answers

6 votes

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

// Import input/output and utility libraries

import java.io.*;

import java.util.*;

Declare class/

public class Numbers {

// Main method goes here

public static void main(String[] args) throws FileNotFoundException

{

// Read input from text file evenFile.txt; Assume file name is evenFile.txt

Scanner input = new Scanner(new File("evenFile.txt"));

// Call static method here

evenNumbers(input);

}

// Static method evenNumbers begins here

public static void evenNumbers(Scanner input)

{

// Declare and initialise Variables

String Nums = input.nextLine(); // Read all number in txt file

Scanner numbers = new Scanner (Nums);

int count = 0; // Number count

int evens = 0; // Even count

int sum = 0; // Summation of numbers

// Check if number is integer. If yes, continue the below operations

while (numbers.hasNextInt())

{

int num = numbers.nextInt();

count++;

sum = sum + num;

if(num % 2 == 0) // check for even

{

evens++;

}

}

System.out.println("Total numbers= " + count); // Print total number

System.out.println("Sum of numbers = " + sum); // Print summation of numbers

System.out.println("Total evens = " + evens); // Print total even

//Calculate and print percentage of even number

double percent = ((double)evens/count)*100.0);

System.out.println("Percent of even number = " +percent);

}

}

// End of program

User Bob Esponja
by
5.5k points
6 votes

Answer:

The output must follow this:

Total numbers = 12

Sum of numbers = 214

Total evens = 8

Percent evens = 66.66666666666667

Therefore, the method produce the total output of:

Total output=12

Sum of numbers= 12

Total events= 8

Percent even= 66.66666412353516

Step-by-step explanation:

public class StringArray {

public static void evenNumbers(String str)

{

String[] parts = str.split(" ");

int[] ints = new int[parts.length];

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

ints[i] = Integer.parseInt(parts[i]);

}

System.out.println("Total numbers ="+ints.length);

int sum=0;

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

sum+=ints[i];

System.out.println("Sum of numbers ="+ints.length);

int count=0;

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

{

if(ints[i]%2==0)

count++;

}

System.out.println("Total evens="+count);

float l=ints.length;

double per=(count*100)/l;

System.out.println("Percent evens="+per);

}

public static void main(String[] args) {

String str="5 7 2 8 9 10 12 98 7 14 20 22";

evenNumbers(str);

}

}

User TheWiseBro
by
5.5k points