Side Navigation

X

Average Value

Write a method that returns the average of a list of integers. Average = Total Sum of All Numbers / Number of Item in the Set   public class BestOnlineLearning { public static double maxNumber(int[] list){ int sum = 0; double avg = 0.0; for(int i = 0; i< list.length; i++){ sum = sum +

Read More

Find The Maximum Value in a List

Write a method that returns the largest integer in the list. You can assume that the list has at least one element. public class BestOnlineLearning { public static int maxNumber(int[] n){ int max = n[0]; for(int i=0; i<n.length; i++){ if(n[i] > max){ max = n[i]; } } System.out.println(max); return max; } public static void main(String[]

Read More

Reverse a String

Write a method that reverses a string. For example, ‘bestOnlineLearning.net’ becomes ‘ten.gninraeLenilnOtse’. public class BestOnlineLearning { public static String reverseString(String str){ String rStr = “”; for(int i = str.length()-1; i>=0; i–){ rStr = rStr + str.charAt(i); } System.out.println(rStr); return rStr; } public static void main(String[] args) { String str = “bestOnlineLearning.net”; reverseString(str); } }  

Linear Search

Write a method that returns the index of the first occurrence of a given integer in a list. Assume that the index of the first element in the list is zero. If the number does not exist return -1. public class BestOnlineLearning { public static int linearSearch(int n, int[] list){ int index = -1; for(int

Read More

Factorial Sum

Write a method that calculates the factorial of a given number. Factorial is the product of all positive integers less than or equal to n. For example: factorial(4) = 4x3x2x1 = 24. TIP: To make it more interesting, try to do it recursively.   public class BestOnlineLearning { public static int factorialSum(int n){ int sum

Read More

Sum Multiples of Given Two Numbers

Given a number n, write a method that sums all multiples of values up to n (inclusive). public class BestOnlineLearning{ public static int sumMultiples(int first, int second, int n){ int sum = 0; for(int i = 0; i <= n; i++){ if(i%first == 0 || i%second == 0){ sum += i; } } System.out.println(“sum of

Read More

Add Two Numbers

Adding two integers public class BestOnlineLearning{ public static int addTwoNumbers(int i, int j){ return i + j; } public static void main(String[] args){ addTwoNumbers(56, 28); } }