Question

In: Computer Science

The main method is where Java begins execution of your program. In this assignment, you will...


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
1 Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values

2Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values

3Write a public static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values

4Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value of the 3 values

5Write 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

6Write 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



7Write 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

8Write 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.

9Write 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 ‘?’

10Write 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.

11Write a public static method named getStringReversed that takes in 1 argument of type String and returns the String in reverse order.

12Write 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!”

Solutions

Expert Solution

NOTE:

All the values are directly passed as parameters.

CODE WITH EXPLANATION :


public class Main//Main class declaration
{
        public static void main(String[] args)//starting point of execution.
        {
                System.out.println("Demonstarting getMaxOf2Ints() :");
                System.out.println("The maximum of 2 and 3 is : "+getMaxOf2Ints(2,3));//returns maximum of two values ie 2,3 by calling respective function.
                System.out.println("Demonstarting getMinOf2Ints() :");
                System.out.println("The minimum of 2 and 3 is : " + getMinOf2Ints(2,3));//returns minimum of two values ie 2,3 by calling respective function.
                System.out.println("Demonstarting getMaxOf3Ints() :");
                System.out.println("The maximum of 2 , 3 and 4 is : "+  getMaxOf3Ints(2,3,4));//returns maximum of three values ie 2,3 and 4 by calling respective function.
                System.out.println("Demonstarting getMedianOf3Ints() :");
                System.out.println("The median of 2 , 3 and 4 is : "+getMeadianOf3Ints(2,3,4));//returns median of three values ie 2,3 and 4 by calling respective function.
        
        }
        public static int getMaxOf2Ints(int a,int b)//function defintion
        {
            if(a>b)//if a is greater than b 
            return a;//returns a as a is maximun and exits function.
            else //As we compare only two numbers if a is not maximun value then b is the maximun value
            return b;//returns b as b is maximun and exits function.
        }
        public static int getMinOf2Ints(int a,int b)//function defintion
        {
            if(a>b)//if a is greater than b
            return b;//returns b as b is minimun and exits function.
            else //As we compare only two numbers if a is not maximun value then b is the maximun value
            return a;//returns a as a is minimun and exits function.
        }
        public static int getMaxOf3Ints(int a,int b,int c)
        {
            if(a>b&&a>c)//if a is greater than b and a is greater than c.
            return a;//returns a as a is maximun and exits function.
            else if(a<b&&b>c)//if b is greater than a and b is greater than c.
            return b;//returns b as b is maximun and exits function.
            else//upo finally we need not have any confusion that c is the maximum value at the above 2 cases gets false.
            return c;//returns c as c is maximun and exits function.
        }
        public static int getMinOf3Ints(int a,int b,int c)//function defintion
        {
            if(a<b&&a<c)//if a is lesser than b and a is lesser than c.
            return a;//returns a as a is minimum and exits function.
            else if(a>b&&b<c)//if b is lesser than a and b is lesser than c.
            return b;//returns b as b is minimum and exits function.
            else//upon finally we need not have any confusion that c is the minimum value at the above 2 cases gets false.
            return c;//returns c as c is minimum and exits function.
        }
        public static int getMeadianOf3Ints(int a,int b,int c)//function defintion.
        {
            /*
            How to calculate the median value.
            Median is the middle value.
            for the three numbers example 5,4,2 
            the median is calculated as follows
               1)sort the values in ascending order => 2,4,5
               2)Median is the middle value.
               Therefore 4 is the median of 2,4,5
         For 3 numbers it is observed that the median ie middle element will not be maximum and also minimum.
         Hene median calculated using the above strategy.
            */
           if(a!=getMinOf3Ints(a,b,c) && a!=getMaxOf3Ints(a,b,c))//If a is not maximum an also a is not minimum 
           return a;//return a as a will be on the middle.
           else if(b!=getMinOf3Ints(a,b,c) && b!=getMaxOf3Ints(a,b,c))//If b is not maximum an also b is not minimum 
           return b;//return b as b will be on the middle.
           else //upon finally we need not have any confusion that c is the middle value at the above 2 cases gets false.
            return c;//returns c as c is median and exits function.
        }
}

OUTPUT:


Related Solutions

Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a complete Java program, including comments in both the main program and in each method,...
Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...
Write a Java test program, all the code should be in a single main method, that...
Write a Java test program, all the code should be in a single main method, that prompts the user for a single character. Display a message indicating if the character is a letter (a..z or A..Z), a digit (0..9), or other. Java's Scanner class does not have a nextChar method. You can use next() or nextLine() to read the character entered by the user, but it is returned to you as a String. Since we are only interested in the...
TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It...
TreeSetDemo IN JAVA PLEASE The following program can be done all in the main method. It demonstrates that a TreeSet eliminates duplicates and is ordered. Create a Random object with a seed of 5. Create an ArrayList numAL of type Integer Populate numAL with 10 numbers randomly selected from 0 to 6 Print out numAL Create a TreeSet numTS of type Integer Create an Iterator alIter from numAl and use it to add all the elements of numAL in numTS....
Design a complete JAVA program with the following methods:- In the main method : 3 variables...
Design a complete JAVA program with the following methods:- In the main method : 3 variables are declared :- employee id, salary and status. A method that will allow user to input employee id and salary. A method that will assign status to “Taxable’ if employee’s salary is more than 2500RM or “Not Taxable” if salary is less and equal to 2500RM. A method that will display the employee’s id , salary and its status. Write the above Using Java.
10) Create a java program that will ask for name and age with method age where...
10) Create a java program that will ask for name and age with method age where it will check the condition that id you are greater than 18 you can vote.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT