Question

In: Computer Science

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

  1. Write a public static method named getMaxOf2Ints that takes in 2 int arguments and returns the Maximum of the 2 values
  2. Write a public static method named getMinOf2Ints that takes in 2 int arguments and returns the Minimum of the 2 values
  3. Write apublic static method named getMaxOf3Ints that takes in 3 int arguments and returns the Maximum of the 3 values
  4. Write a public static method named getMedianOf3Ints that takes in 3 int arguments and returns the Median Value of the 3 values
  5. 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
  6. 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
  7. 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
  8. 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.
  9. 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 ‘?’
  10. 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.
  11. Write a public static method named getStringReversed that takes in 1 argument of type String and returns the String in reverse order.
  12. 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!”

Solutions

Expert Solution

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


Related Solutions

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 Write a public static method...
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).
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.
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...
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
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?
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.
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...
It is important to note that we are assuming that so far there has been NO...
It is important to note that we are assuming that so far there has been NO governmental action regarding the virus. So, there has been no quarantining, there have been no “Stay-at-Home” orders, there have been no orders closing businesses, and, generally, there have been no decisions by a governmental entity regarding the virus.Draw a graph to explain your answer. First, explain (in words) what an “efficient” outcome is in the present context. Then, present an economic argument, using a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT