CSE 201 Homework 5


Read Chapter 5 in the textbook, and then answer the following questions.

  1. What are the differences between procedures (void methods) and functions (value-returning methods)?
  2. 
           
  3. For each of the following tasks, design a class (i.e., static) method heading with an appropriate name, parameters, and return type (or void). You do not need to write the method body. For example, the heading for a method that computes and returns the largest of two real numbers could be something like this
        private static double max(double x, double y).
    1. The task is to convert a given integer grade (0..100) into a corresponding letter grade.
    2. 
            
    3. The task is to compute the state tax on an item, given the cost of the item and the tax rate.
    4. 
            
    5. Given three names, the task is to output them in aphabetical order.
    6. 
            
    7. Given a sentence, the task is to print each word in the sentence on a separate line of output.
    8. 
            
    9. Given a string and a character, the task is return the string obtained by removing all occurrences of the given character from the given string.
    
           
  4. What is the output produced by the following programs?
    1. public class ChangeParam
      {
          public static void main(String[] args)
          {
              int i = 1;
              double x = 3.4;
              String s = "hi!";
      
              System.out.println(i + " " + x + " " + s);
              changeUs(i, x, s);
              System.out.println(i + " " + x + " " + s);
          }
      
          private static void changeUs(int j, double y, String t)
          {
              j = 7;
              y = -1.2;
              t = "there";
              System.out.println(j + " " + y + " " + t);
          }
      }
    2. 
              
    3. public class TraceMe
      {
          public static void main(String[] args)
          {
              printBox(5, 3);
          }
      
          private static void printBox(int rows, int columns)
          {
              int i = 0;
              while (i < rows)
              {
                  int j = 0;
                  while (j < columns)
                  {
                      if ((i % 2) == (j % 2))
                      {
                          System.out.print('A');
                      }
                      else
                      {
                          System.out.print('B');
                      }
                      j = j + 1;
                  }
                  System.out.println();
                  i = i + 1;
              }
          }
      }
    4. 
              
    5. public class ItsAMistery
      {
          public static void main(String[] args)
          {
              mistery("123456789");
          }
      
          private static void mistery(String str)
          {
              int i = 0;
              while (i < str.length())
              {
                  System.out.print(str.charAt((i + 5) % str.length()));
                  i = i + 1;
              }
          }
      }
    
           
  5. Fill in the code for the following class methods.
  6. 
        
    1. The following method returns the position of the first occurrence of a given character in a given string. The position starts from 0 for the first character in the string. The method should return -1 if the character does not appear in the string. For example, the call indexOf('t',"iteration") would return 1, and the call indexOf('s',"iteration") would return -1. (You are not allowed to use the String indexOf method.)
      public static int indexOf(char c, String str)
      {
          // fill this in
      }
    2. Given two String parameters, the following method returns true if they are the same string of characters, and false otherwise. Note: You cannot use the equals String method; in other words, you actually have to write the code that compares the two strings character by character.
      public static boolean stringsAreEqual(String s1, String s2)
      {
          // fill this in
      }