Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a method that is passed in a String and returns a new String with every

ID: 3821412 • Letter: 1

Question

1) Write a method that is passed in a String and returns a new String with every pair of characters reversed. If there are an odd number of characters, the last character is unchanged. For example, “person” becomes “epsrno” “the” become “hte”

2) Write a method that is passed in a stock price and a stock quantity. Print what to sell based on the following rules

                        if stock price is over $100 sell all

Otherwise, sell half of stock only if quantity is 1000 or more, otherwise sell 10% of the stock

3) Write a method named daysInMonth that accepts a month (an integer between 1 and 12) as a parameter and returns the number of days in that month in this year. For example, the call daysInMonth(9) would return 30 because September has 30 days. Assume that the code is not being run during a leap year (that February always has 28 days).

Explanation / Answer

Here are the methods for you:

class StringJumblingStockPriceDaysInMonth
{
    //1) Write a method that is passed in a String and returns a new String with every pair
    //of characters reversed. If there are an odd number of characters, the last character
    //is unchanged. For example, “person” becomes “epsrno” “the” become “hte”
    public static String jumbleChars(String word)
    {
       String output = "";
       for(int i = 0; i < word.length(); i += 2)
       {
          if(i < word.length()-1)
              output += word.charAt(i+1);
          output += word.charAt(i);  
       }
       return output;
    }
    //2) Write a method that is passed in a stock price and a stock quantity. Print what
    //to sell based on the following rules
    // if stock price is over $100 sell all
    //       Otherwise, sell half of stock only if quantity is 1000 or more,
    //       otherwise sell 10% of the stock
    public static void saleSuggestion(double stockPrice, int stockQuantity)
    {
       if(stockPrice > 100)
           System.out.println("Sell All.");
       else if(stockQuantity >= 1000)
           System.out.println("Sell half of stock.");
       else
           System.out.println("Sell 10% of stock.");      
    }
    //3) Write a method named daysInMonth that accepts a month (an integer between 1 and 12)
    //as a parameter and returns the number of days in that month in this year. For example,
    //the call daysInMonth(9) would return 30 because September has 30 days. Assume that
    //the code is not being run during a leap year (that February always has 28 days).
    public static int daysInMonth(int month)
    {
       switch(month)
       {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:   return 31;
         
          case 4:
          case 6:
          case 9:  
          case 11:   return 30;
         
          case 2:       return 28;
         
          default:   return 0;
       }  
    }
}