1. For each of the following cases, modify the ‘Do’ class so that the program will produce the given output.

    1. Nice and 
      Good and Nice 
      Pleasant and Good 
      Good and Pleasant 
      Good and Good 
      Nice and Good 
    2. Nice 
      Good Nice 
      Pleasant Good Nice 
      Good Pleasant Good Nice 
      Good Good Pleasant Good Nice 
      Nice Good Good Pleasant Good Nice 
    3. -> Nice 
      -> Good 
      -> Pleasant 
      -> Nice 
      -> Pleasant 
      -> 
    class prg{ 
      public static void main(String[] args){ 
        Do nice, good, pleasant; 
        nice = new Do( "Nice" ); 
        good = new Do( "Good" ); 
        pleasant = new Do( "Pleasant" ); 
        nice.toPrint(); 
        good.toPrint(); 
        pleasant.toPrint(); 
        good.toPrint(); 
        good.toPrint(); 
        nice.toPrint(); 
        System.exit(0); 
    } } 
    class Do{ 
      static String save = ""; 
      String name; 
      Do( String nm ){ 
        name = nm; 
      } 
      void toPrint (){ 
        String temp; 
        temp = save.concat( " and " ); 
        temp = temp.concat( name ); 
        System.out.println( temp ); 
        save = name; 
      } 
    } 
  2. For the given program

    1. Derive the output of the program on your own.
    2. Compile the program and execute it, to verify your derivation.
    3. Remove the definition of the ‘toString’ method, and check the output of the modified program.
    class prg{ 
      public static void main(String[] args){ 
        Do people; 
        people = new Do( "Nice" ); 
        System.out.println( people );    System.out.println( people ); 
        System.out.println( people ); 
        people = new Do( "Good" ); 
        System.out.println( people ); 
        people = new Do( "Pleasant" ); 
        System.out.println( people );    System.out.println( people ); 
        System.exit(0); 
    } } 
    class Do{ 
      static String a = "Mr", b = "Ms"; 
      String name; 
      Do( String nm ){  name = nm; } 
      public String toString (){ 
        String temp; 
        temp = a; a = b; b = temp; 
        temp = a.concat( " " ); 
        temp = temp.concat( name ); 
        return temp; 
      } 
    } 
  3. Consider the given program.

    1. Figure out the behavior of the program without executing it.
    2. Modify the definition of the ‘Do’ class so that the program will produce the following output.
      Mr Nice 
      Ms Good 
      Mr Pleasant 
      Ms Good 
      Ms Good 
      Mr Nice 
    3. class prg { 
        public static void main ( String[] args ){ 
           MyClass obj; 
           obj = new MyClass(); 
           obj.myMethod ( 10 ); 
      } } 
      class MyClass { 
        static int i = 5; 
        static int i(){  return i; } 
        void myMethod ( int i ){ 
          i = i(); 
          System.out.println( i); 
      } } 
    class prg{ 
      public static void main(String[] args){ 
        Do nice, good, pleasant; 
        nice = new Do( "Nice" ); 
        good = new Do( "Good" ); 
        pleasant = new Do( "Pleasant" ); 
        System.out.println( nice );       System.out.println( good ); 
        System.out.println( pleasant );   System.out.println( good ); 
        System.out.println( good );       System.out.println( nice ); 
        System.exit(0); 
    } } 
    class Do{ 
      static String a = "Mr", b = "Ms"; 
      String name; 
      Do( String nm ){  name = nm; } 
      public String toString (){ 
        String temp; 
        temp = a; a = b; b = temp; 
        temp = a.concat( " " ); 
        temp = temp.concat( name ); 
        return temp; 
      } 
    } 
  4. Add the missing class to the given program so that the program will produce the following output.
    Dan 
    Dan eats 
    Dan reads 
    Dan reads 
    Evy reads 
    Nel reads 
    Nel thinks 
    Nel thinks 
    class prg{ 
      public static void main(String[] args){ 
        Do people; 
        people = new Do( "Dan" ); 
        System.out.println( people ); 
        Do.set( "eats" ); 
        System.out.println( people ); 
        Do.set( "reads" ); 
        System.out.println( people );    System.out.println( people ); 
        people = new Do( "Evy" ); 
        System.out.println( people ); 
        people = new Do( "Nel" ); 
        System.out.println( people ); 
        Do.set( "thinks" ); 
        System.out.println( people );    System.out.println( people ); 
        System.exit(0); 
    } } 
  5. Add the missing class to the given program so that the program will produce the following output.
    Dan 
    Dan eats 
    Dan 
    Dan eats 
    Dan drinks 
    Dan eats 
    Evy drinks 
    Evy eats 
    Evy drinks 
    Evy eats 
    Evy drinks 
    Evy thinks 
    Evy drinks 
    Evy thinks 
    Evy drinks 
    class prg{ 
      public static void main(String[] args){ 
        Do people; 
        people = new Do( "Dan" ); 
        System.out.println( people ); 
        Do.set( "eats" ); 
        System.out.println( people );    System.out.println( people ); 
        System.out.println( people ); 
        Do.set( "drinks" ); 
        System.out.println( people );    System.out.println( people ); 
        people = new Do( "Evy" ); 
        System.out.println( people );    System.out.println( people ); 
        System.out.println( people );    System.out.println( people ); 
        System.out.println( people ); 
        Do.set( "thinks" ); 
        System.out.println( people );    System.out.println( people ); 
        System.out.println( people );    System.out.println( people ); 
        System.exit(0); 
    } } 
  6. Parameter-Less Procedure Methods: Write a program which outputs the word ‘hi’ 128 times. Your program should use only method calls to get the outcome, with ‘System.out.println("hi");’ being the only call to a method not defined by you. Each of the methods you define may include at most two calls to methods.
  7. Parameter-Less Function Methods: Write a program which outputs the value 33. Your program should use only method calls to get the outcome; that is, no variables are allowed. The number ‘1’ should be mentioned exactly once, and no other number is allowed. Each of the methods you define may include at most two calls to methods.