package eecs1022.finalb; /** * This question is worth 2.5 marks. No partial marks will be awarded for this question. * * @author Franck van Breugel */ public class Question6 { /** * Returns the average of the digits occurring in the given string. You may assume that * the string is not null and contains at least one digit. * * @param s a string. * @return the average of the digits occurring in the given string. */ public static double average(String s) { int sum = 0; int count = 0; for (int i = 0; i < s.length(); i++) { char letter = s.charAt(i); if (letter >= '0' && letter <= '9') { int number = letter - '0'; sum = sum + number; count++; } } double average = sum / (double) count; return average; } /** * Tests method. You may add more test cases. * * @param args not applicable. */ public static void main(String[] args) { System.out.println(Question6.average("1234")); System.out.println(Question6.average("1a5h5x1")); System.out.println(Question6.average("12h56s9")); System.out.println(Question6.average("aaaaaa4aaaaa")); System.out.println(Question6.average("5a")); } }