Fall Semester 2022-2023 INT201 Object Oriented Programming Tutorial Sheet#1 - Solution Week Starting, September 5, 2022 Ex#1 Write a method that takes as input a sentence and returns the number of vowels in the sentence. Write a driver program to test your method. Vowel letters are (a, e, i, o u). import java.util.*; import static java.lang.Character.*; public class TutorialSheet1_EX1 { static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { String text; int counter=0; System.out.println("Enter a sentence:"); text = keyboard.nextLine(); counter = countVowels(text); System.out.println("Number of vowel letters:" + counter); } public static int countVowels(String str) { int len = str.length(); int count =0; char ch; for (int i=0; i<len; i++){ ch = str.charAt(i); ch=toUpperCase(ch); switch (ch) { case 'A': case 'E': case 'I': case 'O': case 'U':count++; break; default: } } return (count); }//End of method countVowels } Ex#2 Write a method that takes three integers as parameters (, int a, int b, and int c ) that represent the coefficients of a quadratic equation and return the largest of the two roots of the quadratic equation. The roots are computed using the following formulas: x1 = (-b + square root (b2 – 4*a*c))/(2*a) x2 = (-b - square root (b2 – 4*a*c))/(2*a) Write a driver program to test your method. In running your program, use the following values for the coefficients: Case 1: a = 1, b = -8, c = 15 Case 2: a = 1, b = -4, c = 4 Case 2: a = 10, b = -17, c = 6 import java.util.*; import static java.lang.Math.*; public class TutorialSheet1_EX2{ public static Scanner user = new Scanner(System.in); public static void main(String[] args){ int a, b, c; double maxRoot; System.out.println("Enter a, b, and c coefficients of quadratic equation: "); a = user.nextInt(); b = user.nextInt(); c = user.nextInt(); maxRoot = findMaxRoot(a, b, c); if(maxRoot != -9999) System.out.println("Max root: " + maxRoot); else System.out.println("Roots are complex"); } public static double findMaxRoot(int ca, int cb, int cc){ double x1, x2, max; double dis; dis = cb*cb-4.0*ca*cc; if(dis >=0){ x1 = (-cb - sqrt(dis))/(2.0*ca); x2 = (-cb + sqrt(dis))/(2.0*ca); max = x1; if(x2 > max) max = x2; } else max=-9999.0; return(max); } } Ex#3 Write a method that takes two words as parameters (passed as of type String) and returns the number of characters in the smaller of the two words. Write a driver program to test your method. import java.util.Scanner; import java.lang.*; public class TutorialSheet1_EX3 { static Scanner s = new Scanner(System.in); public static void main(String[] args) { String word1, word2; int numChar; System.out.println("Enter first word: "); word1 = s.next(); System.out.println("Enter second word: "); word2 = s.next(); numChar = countSmallerWord(word1,word2); System.out.println("The number of characters in the smaller word: " + numChar); } // End of main public static int countSmallerWord(String str1, String str2) { if(str1.length() < str2.length()) return str1.length(); else return str2.length(); } // End of method compareStrings } Ex#4 Write a method that generates three random integers between 1 and 50 and returns the average of the three integers. Write a driver program to test your method. import java.util.Scanner; import static java.lang.Math.*; public class TutorialSheet1_EX4 { public static void main(String[] args) { System.out.printf("Average of three random numbers: %2.4f \n",averageRandom()); } public static double averageRandom() { int n1, n2, n3; double avg; n1 = (int)(random()*50)+1; n2 = (int)(random()*50)+1; n3 = (int)(random()*50)+1; avg = (n1+n2+n3)/3.0; return avg; }// End of method averageRandom } Ex#5 Write a method that takes two parameters of type double (the first represents interest rate and the second represents investment) and returns the value of the investment after n years where n is an integer parameter passed to the method (the method has three parameters). Write a driver program to test your method. Note: In = I1*(1+interestRate)n, where In: investment after n years, I1 : initial investment Test your method with the following data: Interest rate = 0.065, investment =10,000, and n = 10. import java.util.*; import static java.lang.Math.*; public class TutorialSheet1_EX5 { public static Scanner user = new Scanner(System.in); public static void main(String[] args) { double investment, rate; double final_amount; int years; double maxRoot; System.out.println("Enter initial investment, interest rate, and years: "); investment = user.nextDouble(); rate = user.nextDouble(); years = user.nextInt(); final_amount = computeAmount(investment, rate, years); System.out.printf("Amount after %3d years is %10.2f\nx", years,final_amount); } public static double computeAmount(double invest, double intRate, int numYears){ double amount; amount = invest *pow(1.0+intRate,numYears); return amount; } }