Question

In: Computer Science

Ans In Java Please Homework 6-1 So far in this course, all of your programs have...

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


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

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

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

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

  1. 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

  1. 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



  1. 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

  1. 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.

  1. 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 ‘?’

  1. 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.

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

  1. 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.

Solutions

Expert Solution

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.


Related Solutions

So far in this course, all of your programs have been written to be fully contained...
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...
Below are 6 important terms from the course so far.  In your own words and in complete...
Below are 6 important terms from the course so far.  In your own words and in complete sentences, please identify four of the terms and explain each term's significance to the story of American history. Be as specific as possible. The Pueblo Rebellion The Great Awakening The Middle Passage The Spanish Mission System James Oglethorpe The Stamp Act of 1765
Reflect on and relate to what you have learned so far in the course with respect...
Reflect on and relate to what you have learned so far in the course with respect to your ability to calculate gross domestic product and its impact on the economy’s business cycle, unemployment, and inflation; using gross domestic product formulate recommendations for a government’s role in achieving full employment in an economy.
1. All of the economic thinkers that we have covered so far, including the mercantilists, the...
1. All of the economic thinkers that we have covered so far, including the mercantilists, the physiocrats, Adam Smith, Malthus, and Ricardo, have dealt with the topic of international trade as well as international trade policy. With the exception of Malthus, discuss the evolution of their ideas regarding those topics. You should mention their ideological leanings as well as the substance of their views. Do not forget to mention their policy recommendations.
For one important and interesting idea that you have learned so far in the course based,...
For one important and interesting idea that you have learned so far in the course based, write a one page essay beginning with the following statements: Although before this class I believed …….., in fact……..Or, Although it is tempting to believe……., in fact…..Or, Although it might seem that …………… is true, in fact…… Then explain why you or others could believe the idea that is fact incorrect. In other words, what is logical or compelling about this incorrect idea. Then...
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
Classify the types of financial institutions mentioned in the content of the course so far, as...
Classify the types of financial institutions mentioned in the content of the course so far, as either depository or nondepository. Explain the general difference between depository and nondepository institution sources of funds. It is often stated that all types of financial institutions have begun to offer services that were previously offered only by certain types. Consequently, many financial institutions are becoming more similar. Nevertheless, performance levels still differ significantly among types of financial institutions. Why?
This question puts together all that we have learned so far. Imagine that you have just...
This question puts together all that we have learned so far. Imagine that you have just been hired as an economist by the government, and you have been asked to “create” a model that combines the goods and market service with the Asset market. We are interested in determining the relationship between output and the exchange rate. This relationship should be represented in the space with output in the horizontal axis and the exchange rate on the vertical axis, i.e....
So far in this course, we’ve discussed the concepts of risk, security controls, and the value...
So far in this course, we’ve discussed the concepts of risk, security controls, and the value of addressing security early and throughout the development lifecycle of systems. We’ve also discussed different threats to those systems, which can result (and have resulted!) in breaches of our data. Though cybercrime laws and regulations are trying to catch up with the changing technology landscape, there are increasing concerns over our ability to retain some degree of personal privacy. For this question, and using...
So far we far we have been discussing the advantages of HIT, but have not considered...
So far we far we have been discussing the advantages of HIT, but have not considered its disadvantages. Discuss at least three (3) of the disadvantages or challenges that can be encountered by adoption of the HIT, such as EHR (Electronic Health Record) or HIE (health Information Exchange).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT