code:
class Chegg{
public static int getMaxOf2Ints(int a,int b)
{
return a>b?a:b;
}
public static int getMinOf2Ints(int a,int b)
{
return a<b?a:b;
}
public static int getMaxOf3Ints(int a,int b,int c)
{
if(a>b&&a>c)
return a;
else if(b>c)
return b;
return c;
}
public static void printMinOf3Ints(int a,int b,int c)
{
if(a<b&&a<c)
System.out.println("The min is
"+a);
else if(b<c)
System.out.println("The min is
"+b);
System.out.println("The min is "+c);
}
public static int getMedianOf3Ints(int a,int b,int c)
{
int x = a-b;
int y = b-c;
int z = a-c;
if(x*y > 0) return b;
if(x*z > 0) return c;
return a;
}
public static int getProdOfAllPositiveInts(int n)
{
int prod=1;
for(int i=1;i<=n;i++)
prod*=i;
return prod;
}
public static int getProdOfAllNegativeInts(int n)
{
int prod=1;
for(int i=-1;i>=n;i--)
prod*=i;
return prod;
}
public static int getCharAtIndex(String s,int index)
{
return s.charAt(index);
}
public static String getStringReversed(String s)
{
return new
StringBuilder(s).reverse().toString();
}
public static String getStringTitleCased(String s)
{
char c[]=s.toCharArray();
c[0]=Character.toUpperCase(c[0]);
for(int i=0;i<c.length-1;i++)
{
if(c[i]==' ')
{
c[i+1]=Character.toUpperCase(c[i+1]);
}
}
return new String(c);
}
public static void main(String args[])
{
System.out.println("The max of 3,5 is
"+getMaxOf2Ints(3,5));
System.out.println("The min of 3,5 is
"+getMinOf2Ints(3,5));
System.out.println("The max of 3,4,5 is
"+getMaxOf3Ints(3,4,5));
printMinOf3Ints(3,4,5);
System.out.println("The median of 3,6,5 is
"+getMedianOf3Ints(3,6,5));
System.out.println("The prod is 1 to 5
"+getProdOfAllPositiveInts(5));
System.out.println("The prod is -1 to -5
"+getProdOfAllNegativeInts(-5));
System.out.println("The character at index 3 is
"+(char)getCharAtIndex("Ezhil",2));
System.out.println("The character at index 3 is
"+getStringReversed("Ezhil"));
System.out.println("The character at index 3 is
"+getStringTitleCased("sushmanth ezhil"));
}
}
Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the...
1) Write a public static method named printArray, that takes two arguments. The first argument is an Array of int and the second argument is a String. The method should print out a list of the values in the array, each separated by the value of the second argument. For example, given the following Array declaration and instantiation: int[] myArray = {1, 22, 333, 400, 5005, 9}; printArray(myArray, ", ") will print out 1, 22, 333, 400, 5005, 9 printArray(myArray,...
Write a static method named middleValue that takes three int arguments, and returns an int. When given three different argument values, the method should return the value that is numerically between the other two values. When given three values where two or more argument values are the same, then the function should return that value. Examples: middleValue(1, 2, 3) will return 2 middleValue(5, 2, 7) will return 5 middleValue(8, 4, 6) will return 6 middleValue(1, 2, 1) will return 1...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...
public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...
Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1, 1, 1, 1}; sumOf(theArray) will return...
With using Python . Write a function that: 1. Takes two arguments and returns the product of the arguments. The return value should be of type integer. 2. Takes two arguments and returns the quotient of the arguments. The return value should be of type float. 3. Takes one argument and squares the argument. It then uses the two values and returns the product of the two arguments.(Reuse function in 1.) 4. Takes one argument and prints the argument. Use...
Write a cell method that takes a single double as an argument and returns an int. The int should be the next highest whole integer (eg 3.14 would return 4). Use math not built-in Java methods mins) Write a method that takes a string as an argument. It your string is more than 10 letters or less than 1 letter return "Bad number". Otherwise, return the telephone number equivalent of the input. Your return can be of type String Eg...
Write a ceil method that takes a single double as an argument and returns an int. The int should be the next highest whole integer (eg 3.14 would return 4). Use math, not built-in Java methods (est. 20 mins) Write a method that takes a string as an argument. If your string is more than 10 letters or less than 1 letter return "Bad number." Otherwise, return the telephone number equivalent of the input. Your return can be of type...
Create a public static method named valueG that takes an ArrayList of type Double and returns a double. This method returns the maximum result ( do not return the original value) of taking the sine of each value from the input