Given the code on the next page (I had to take out the import calls to fit it all on one page, assume the proper imports have been listed), and the mouse actions sketched below draw what the frames look like at each stage.

 

 2.

1.

                                                                        3.

                                                4.                            

 

 

 

1.       Mouse Starts here

2.       Mouse moves and presses down button (directly on where the period on 2. ) but does not release

3.   Mouse moves here and then releases button (on the period again, except at location 3. )

4.   Mouse Moves to here

     (Each dotted line is five pixels on the graph)

 

 

 

 

 

 

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

 

public class myApplet extends Applet implements MouseListener

{

            int one, two, three, four, flag;

            Graphics myG;

            public void init()

            {

                        one = 5;

                        two = 40;

                        three = 30;

                        four=10;

                        flag=0;

                        addMouseListener(this);

            }

 

            public void paint(Graphics g)

            {

                         if(flag==0)

                                 g.setColor(Color.red);

                        g.fillRect(one, two, three, four);

            }

            public void mouseClicked(MouseEvent e)

            {

     

             }

 public void mousePressed(MouseEvent e)

             {

                

                        one = e.getX();

                         two = e.getY();

                         repaint();

            }

             public void mouseReleased(MouseEvent e)

             {

                           three = e.getX();

                           four = e.getY();

                          repaint();

             }

             public void mouseEntered(MouseEvent e)

            {

      

             }

              public void mouseExited(MouseEvent e)

              {

                           flag=1;

                           repaint();

             }

}


2. Object question, passing by reference (2 points each)

 

public class Drinks

{

     private String name;

           

    Drinks(String n)

    {      name = n;       }    /trying to fit this whole problem on one page

   

     public void changeName(String n){

              name = n;

     }

     public String getName()

     {

        return name;

     }

    private Drinks returnClass(Drinks d1)

    {

        Drinks newDrink = new Drinks("Grasshopper");

        return d1;

     }

      public Drinks changeStatus(Drinks one, Drinks two)

     {

          Drinks  cold = new Drinks("Pepsi");

          Drinks three = cold.returnClass(this);

          one.changeName("Coke");

          three.changeName("Beer");

          two.changeName(three.getName());

          Drinks four = one;

          System.out.println(one.getName());   // Part A  = Beer

          System.out.println(two.getName());   //Part B   = Beer     

        System.out.println(three.getName()); //Part C   = Beer

          System.out.println(four.getName());  //Part D   = Beer

          return four;

    }

 

 

    public static void main(String [] args)

    {

        Drinks green = new Drinks("Gatorade");

        Drinks red = new Drinks("Coffee");

        Drinks blue = green;

        Drinks yellow = red.changeStatus(blue, green);

       

        System.out.println(red.getName());    //part E        = Beer

        System.out.println(green.getName());  //Part F      = Beer

        System.out.println(yellow.getName()); //Part G     = Beer

        System.out.println(blue.getName());   //Part H       = Beer

       

    }   // end of main  }  //end of class

Write the answers next to the print statements

3.                         You are to write a method that will return the count of elements in the 2D array that are divisible by the variable divisible being sent in. The method should be generic so that the values of columns, rows, divisible and the values in the myArray do not matter, as long as columns and rows match up with the size of the 2D array. (If divisible = 5 and the array element is 25 then the count should be incremented by one, if the element in the array you’re looking at is 26 then you should not increment the count.) 
 
public class Output
{
    public Output(){}
 
//Write your code here:
 
 
 
  public int countElements(int ma[][], int r, int c, int d)
{
       int count = 0;
       for(int i=0; i<r; i++)
          for(int j=0; j<c; j++)
              if((ma[i][j]%d) == 0)
                   count++;
        
        return count;
}
 
 
 
 
 
 
 
  public static void main(String [] args)
  {
      Output out = new Output();
      int rows = 3, cols=4;
      int divisble = 3;
      int myArray[][] = {{34, 35, 14, 22},
                         {11, 20, 30, 33}, 
                         {34, 23, 45, 22}};                    
      int count = out.countElements(myArray, rows, cols, divisible);
     System.out.println(count);
   }
 
}

 

 

4.      Inheritance – Make sure you see all five classes

//Main Class

public class Q4InheritDriver

{

   public static void main(String [] args)

   {

       FoodPlant f1 = new FoodPlant();

       NonFoodPlant f2 = new Flower(25);

       NonFoodPlant f3 = new NonFoodPlant();

       Flower f4 = new Flower(3);

       System.out.println(f1.returnInfo()); 

       System.out.println(f2.returnInfo());  

       System.out.println(f3.returnInfo());

       f4.returnOutput();

                System.out.println(f3.total);

       System.out.println(Plant.total);

   }

}

 

//A new Class #2

 

  public class Plant

{

    protected String leaves;

    protected static int total = 0;

   Plant()

   {

       leaves = "medium";

       total++;

   }

   Plant(String l)

   {

       leaves = l;

       total++;

   }

  

   public String returnInfo()

   {

       return "Info";

   }

   public void changeLeaves(String l)

   {

       leaves = l;

   }

  

}

 


// A new Class #3

public class FoodPlant extends Plant

{

   private int calories;

   public FoodPlant(int c)

   {

      super("large");

      calories = c;

   }

    public FoodPlant()

    {

        calories = 500;

    }

   public String returnInfo()

   {

       return leaves;

   }

   public int returnLeaves()

   {

       return calories;

   }

}

 

// A new Class #4

 

public class NonFoodPlant extends Plant

{

   public NonFoodPlant()

   {

                   super("small");

                        num ++;

 

   }

   public NonFoodPlant(String l)

   {

                       num ++;

   }

   public String returnInfo()

   {

                   return super.returnInfo();

   }

}

 

 

 

 

 

 

 

 

//A new Class #5

public class Flower extends NonFoodPlant

{

   private int petals;

   public Flower()

   {

       super("Mucho");

   }

   public Flower(int n)

   {

                   petals = n;

   }

 

   public void returnOutput()

   {

                   System.out.println(petals + " and " + leaves);

   }

}

 

 

 

Inheritance Problem:  The above program outputs 6 lines of output. What is the output of the program:

 

               Give the output of the program using Exceptions:

                      Medium

               Info

               Info

                       3 and small

                       7

                       7

 

 

 

 


1.      Exception Problem

 

public class ExceptionExample

{

    public static void main(String [] args)

    {

       Test p1 = new Test();

        try{

          p1.sampleMethod();

          }

           catch(RuntimeException es)

          {

             System.out.println("Johnny Bench");

          }

          catch(Exception e)

          {

             System.out.println("Kirby Pucket");

          }

       

          finally{

             System.out.println("Manny Mota");

          }

    }

}

 

        public class Test

        {

         public Test()

        {

        }

      public void sampleMethod() throws Exception

      {

         try{

               throw new Exception("Sandy Koufax");

               }

               catch(Exception e)

               {

                  System.out.println("Babe Ruth");

                  throw new RuntimeException("Chipper Jones");

                 }

                 finally{

                       System.out.println("Cal Ripken");

                    }

      }

      }

      Babe Ruth

      Cal Ripken

      Johnny Bench

      Manny Mota

 

 

 

2.     Answer the following questions:

            A) How many return values can a constructor have?

 

                               0

               B) Write the method definition for the following method call:

                                              String test = Instruction.myMethod(new Student(“Harry”));

 

Public String myMethod(Student h)

C) How many possible values can num have in the following statement:   int num = var%5;

                      5

 

D). In part C above what is the value of num if var is 20?

                      0

 

E) What is the keyword for a variable that is not modifiable after it is initialized?

   final

 

F)     What does overloading mean in Java?

 

Methods with same name but different parameters

G) What will the output be after the next line of code is executed?

System.out.println(“CS160 \n \”Almost over\””);

 

        CS160

        “Almost Over”

 

H)    What is the html tag for center?

<center>

 

 

 

 

 

 

 

 

 


 

 

3.      You are to write the code to handle the following output given the value of a variable cheese. You may not use an if or if/else or else statement to complete the task. Mininum lines of code get more points.  Don’t write a method call, main, or any of that other stuff, assume cheese has already been declared and initialized to something.  Just write the block of code that will handle the following:

 

 

Value of variable cheese

Ouput

1

American

 

 

2

Provolone

Don’t know

 

 

3

Westminster

 

 

Anything else

Don’t know

 

 

 

 

switch(cheese)

{

       choice 1:

System.out.println(“American”);

break;

       choice 3:

System.out.println(Westminster”);

break;

       choice 2:

System.out.println(“Provolone”);

                               Default:

                                              System.out.println(“Don’t know”);

     

}