In: Computer Science
Ans In Java Please
Homework 6-1
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
Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values
Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values
Write a public static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values
Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value of the 3 values
Write a public static method named printMinOf3Ints that takes in 3 int arguments of type int and prints the minimum value of those 3 ints
Example: “The min is “ + minVal
Write a public static method named getProdOfAllPositiveInts that takes in 1 int argument and returns the product of all the values between 1 and that number.
If the argument is NON-positive return 0
Write a public static method named getProdOfAllNegativeInts that takes in 1 int argument and returns the product of all the values between -1 and that number.
If the argument is NON-negative return 0
Write a public static method named isProdOfAllNegativeIntsNegative that takes in 1 int argument and returns true if the product of all the values between -1 and that number is negative, and false otherwise.
Write a public static method named getCharAtIndex that takes in 2 arguments, a String s, and an int index. The method should return the char found at the index location of the string or if not found return a ‘?’
Write a public static method named getCountOfCharInString that takes in 2 arguments, a String s, and a char c. The method should return an int representing the number of times the char was found within the string.
Write a public static method named getStringReversed that takes in 1 argument of type String and returns the String in reverse order.
Write a public static method named getStringTitleCased that takes in 1 argument of type String and capitalizes the first letter of each word in the String, then returns the title cased string.
Example:
Input: “the dog ate my homework!” Returns: “The Dog Ate My Homework!”
Input: “tHe Dog atE My HOMEwoRk!” Returns: “The Dog Ate My Homework!”
Input: “THE DOG ATE MY HOMEWORK!” Returns: “The Dog Ate My Homework!”
Please complete all the Participation and Challenge activities in the above sections. This work must be completed in your textbook ZYBooks -- CMP-167: Programming Methods I
No other forms of submission will be accepted.
CODE -
import java.util.Scanner;
public class Homework6Methods
{
// Function to return maximum of 2 integer values passed as arguments
public static int getMaxOf2Ints(int num1, int num2)
{
int maxVal;
if (num1 > num2)
maxVal = num1;
else
maxVal = num2;
return maxVal;
}
// Function to return minimum of 2 integer values passed as arguments
public static int getMinOf2Ints(int num1, int num2)
{
int minVal;
if (num1 < num2)
minVal = num1;
else
minVal = num2;
return minVal;
}
// Function to return maximum of 3 integer values passed as arguments
public static int getMaxOf3Ints(int num1, int num2, int num3)
{
int maxVal;
if (num1>num2 && num1>num3)
maxVal = num1;
else if (num2>num1 && num2>num3)
maxVal = num2;
else
maxVal = num3;
return maxVal;
}
// Function to return median of 3 integer values passed as arguments
public static int getMedianOf3Ints(int num1, int num2, int num3)
{
int medianVal;
if ((num2>num1 && num1>num3) || (num3>num1 && num1>num2))
medianVal = num1;
else if ((num1>num2 && num2>num3) || (num3>num2 && num2>num1))
medianVal = num2;
else
medianVal = num3;
return medianVal;
}
// Function to print minimum of 3 integer values passed as arguments
public static void printMinOf3Ints(int num1, int num2, int num3)
{
int minVal;
if (num1<num2 && num1<num3)
minVal = num1;
else if (num2<num1 && num2<num3)
minVal = num2;
else
minVal = num3;
System.out.println("The min is " + minVal);
}
// Function to return product of all positive integers between 1 and an integer value passed as argument.
// It returns 0 if the argument is non-positive integer.
public static int getProdOfAllPositiveInts(int num)
{
int prodVal;
if (num<1)
prodVal = 0;
else
{
prodVal = 1;
for(int i=1; i<=num; i++)
prodVal = prodVal*i;
}
return prodVal;
}
// Function to return product of all negative integers between -1 and an integer value passed as argument.
// It returns 0 if the argument is non-negative integer.
public static int getProdOfAllNegativeInts(int num)
{
int prodVal;
if (num>-1)
prodVal = 0;
else
{
prodVal = 1;
for(int i=-1; i>=num; i--)
prodVal = prodVal*i;
}
return prodVal;
}
// Function to return True if product of all negative integers between -1 and an integer value passed as argument is negative
// It returns False otherwise
public static boolean isProdOfAllNegativeIntsNegative(int num)
{
int prodVal = getProdOfAllNegativeInts(num);
if (prodVal<0)
return true;
else
return false;
}
// Function to return character at the index (passed as argument) in the string (passed as argument).
public static char getCharAtIndex(String s, int index)
{
if (index > s.length() || index < 0)
return '?';
return s.charAt(index);
}
// Function to return count of occurances of a character (passed as argument) in the string (passed as argument).
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;
}
// Function to return reverse of a string (passed as argument).
public static String getStringReversed(String s)
{
String finalStr = "";
int j=0;
for (int i=s.length()-1; i>=0; i--)
{
finalStr += s.charAt(i);
}
return finalStr;
}
// Function to return the string after capitalizing the first letter of each word in the string passed as argument.
public static String getStringTitleCased(String s)
{
String newStr = "";
if (s.charAt(0) >= 97 && s.charAt(0) <= 122)
newStr += (char)(s.charAt(0) - 32);
else
newStr += s.charAt(0);
for(int i=1; i<s.length(); i++)
{
if (s.charAt(i) != ' ')
{
if (s.charAt(i) >= 65 && s.charAt(i) <= 90)
newStr += (char)(s.charAt(i) + 32);
else
newStr += s.charAt(i);
}
else
{
newStr += s.charAt(i);
i++;
if (s.charAt(i) >= 97 && s.charAt(i) <= 122)
newStr += (char)(s.charAt(i) - 32);
else
newStr += s.charAt(i);
}
}
return newStr;
}
// Main method to test our functions
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("\nEnter two numbers: ");
int num1 = keyboard.nextInt();
int num2 = keyboard.nextInt();
System.out.println("\nThe max is " + getMaxOf2Ints(num1, num2));
System.out.println("The min is " + getMinOf2Ints(num1, num2));
System.out.print("\nEnter three numbers: ");
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
int num3 = keyboard.nextInt();
System.out.println("\nThe max is " + getMaxOf3Ints(num1, num2, num3));
System.out.println("The median is " + getMedianOf3Ints(num1, num2, num3));
printMinOf3Ints(num1, num2, num3);
System.out.print("\nEnter a positive number: ");
int num = keyboard.nextInt();
System.out.println("\nThe product of all postive numbers from 1 to the number is " + getProdOfAllPositiveInts(num));
System.out.print("\nEnter a negative number: ");
num = keyboard.nextInt();
System.out.println("\nThe product of all negative numbers from -1 to the number is " + getProdOfAllNegativeInts(num));
if (isProdOfAllNegativeIntsNegative(num))
System.out.println("Product of all negative numbers from -1 to the number is negative");
else
System.out.println("Product of all negative numbers from -1 to the number is not negative");
keyboard.nextLine();
System.out.print("\nEnter a string: ");
String s = keyboard.nextLine();
System.out.print("Enter the index: ");
int index = keyboard.nextInt();
System.out.println("\nCharacter at the given index is " + getCharAtIndex(s, index));
System.out.print("\nEnter a character: ");
char c = keyboard.next().charAt(0);
System.out.println("\nThere are " + getCountOfCharInString(s, c) + " occurances of the character in the string");
System.out.println("String in reverse order: " + getStringReversed(s));
System.out.println("String in Title Case: " + getStringTitleCased(s));
keyboard.close();
}
}
SCREENSHOTS -
CODE -
OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.