In: Computer Science
So far in this course, all of your programs have been written to be fully contained inside a single method named main. The main method is where Java begins execution of your program. In this assignment, you will coding other methods in addition to the main method. These additional methods will perform specific functions and, in most cases, return results. Write all your methods in a class named Homework6Methods.java
//Java code
public class Main { public static void main(String[] args) { System.out.println("Max of two: "+getMaxOf2Ints(5,9)); System.out.println("Min of two: "+getMinOf2Ints(6,7)); System.out.println("Max of three: "+getMaxOf3Ints(100,100,200)); printMinOf3Ints(100,100,200); System.out.println("Median: "+getMedianOf3Ints(100,100,200)); System.out.println("Product of all Positive numbers: "+getProdOfAllPositiveInts(5)); System.out.println("Product of all Negative numbers: "+getProdOfAllNegativeInts(-5)); System.out.println("Get character: "+getCharAtIndex("The Programming Language",5)); System.out.println("Count m in Programming: "+getCountOfCharInString("Programming",'m')); System.out.println("Programming in reverse order: "+getStringReversed("Programming")); System.out.println("Title case: "+getStringTitleCased("the dog ate my homework!")); System.out.println("Done"); } /** * * @param x * @param y * @returnthe Maximum of the 2 values */ public static int getMaxOf2Ints(int x, int y) { if(x>=y) return x; else return y; } /** * * @param x * @param y * @return the Minimum of the 2 values */ public static int getMinOf2Ints(int x, int y) { if(x<=y) return x; else return y; } /** * * @param x * @param y * @param z * @return the Maximum of the 3 values */ public static int getMaxOf3Ints(int x, int y, int z) { if(x>=y && x>=z) return x; else if(y>=x && y>=z) return y; else return z; } /** * * @param x * @param y * @param z * @return the Median Value of the 3 values */ public static int getMinOf3Ints(int x, int y, int z) { if(x<=y && x<=z) return x; else if(y<=x && y<=z) return y; else return z; } /** * * @param x * @param y * @param z * @return the Median Value of the 3 values */ public static int getMedianOf3Ints(int x, int y, int z) { int num2 = getMaxOf3Ints(x,y,z); int num1 = getMinOf3Ints(x,y,z); if(x> num1 && x<num2) return x; else if(y>num1 && y<num2) return y; else return x; } /** * prints the minimum value of those 3 ints * @param x * @param y * @param z */ public static void printMinOf3Ints(int x, int y, int z) { int minVal = getMinOf3Ints(x,y,z); System.out.println("The min is " + minVal); } /** * * @param num * @return the product of all the values between 1 and that number. */ public static int getProdOfAllPositiveInts(int num) { int product=1; if (num<0) return 0; else { for(int i=1; i<=num;i++) { product*=i; } return product; } } /** * * @param num * @returnthe product of all the values between -1 * and that number. If the argument is NON-negative return 0 */ public static int getProdOfAllNegativeInts(int num) { int product=1; if(num>0) return 0; else { for (int i = -1; i >=num ; i--) { product *=i; } return product; } } /** * * @param num * @return true if the product of all the values between -1 * and that number is negative, and false otherwise. */ public static boolean isProdOfAllNegativeIntsNegative(int num) { int result = getProdOfAllNegativeInts(num); if(result<0) return true; else return false; } /** * * @param s * @param index * @return the char found at the index location * of the string or if not found return a ‘?’ */ public static char getCharAtIndex(String s, int index) { if(index<0 || index>=s.length()) return '?'; else return s.charAt(index); } /** * * @param s * @param c * @returnan int representing the number of * times the char was found within the string. */ public static int getCountOfCharInString(String s, char c) { int count =0; for (int i = 0; i <s.length() ; i++) { if(s.charAt(i)==c) count++; } return count; } /** * * @param str * @return the String in reverse order. */ public static String getStringReversed(String str) { String reverse=""; for(int i = str.length()-1;i>=0;i--) { reverse += str.charAt(i); } return reverse; } /** * * @param str * @return the title cased string. */ public static String getStringTitleCased(String str) { StringBuilder titleCase = new StringBuilder(str.length()); boolean isTitle = true; for (char c : str.toCharArray()) { if (Character.isSpaceChar(c)) { isTitle = true; } else if (isTitle) { c = Character.toTitleCase(c); isTitle = false; } titleCase.append(c); } return titleCase.toString(); } }
//Output
//If you need any help regarding this solution ........ please leave a comment .......... thanks