CSC 211 Final

Due: Saturday March 21, 11:59pm

No Late Finals will be accepted.

This exam has 2 parts a written part and a programming part.

For the written part type your answers into a text file. For the programming part you must write a complete java program.

Turn in

Upload the written part using the link final

Upload the program using the fprogram1

at COL

Note: When submitting the program you must submit the .java file NOT the .class file.

This is an exam. You can use your book but you must do your own work. No help from tutors or anyone.

Written Part(Worth 90% )

  1. True or false, the following are valid Java Identifiers? If false you must explain!
          
    a)	number1  
           
    b)	level
    
    c)	4sale
    
    d)	my   String
    
  2. True or false, the following are legal Java statements. If false you must explain !
    
    a)   int x = 6.5;
    
    b)   if ( x = 100)
           ++x;
    
    c)   if ( x < 30 ) 
           ++x;
           System.out.println( x + “ is less than 30 “ );                     
         else
           System.out.println( x + “ is bigger than or equal to 30 “ );
    
    
  3. For each of the following, what is the value of z?
    Given int a = 2, b = 5, c = 9;
    a) z =  c/b – b/a;
    
    b)z =  c % b  - a
    
  4. Given the following declarations, trace the output of the listed conditional statements.
    int  a = 10,  b = 20;
    
    a) if ( a > 30 ||  a < 15)
         System.out.println(“executed if “);
       else 
         System.out.println(“executed else  “);  
    
    b) if ( b > 30 )  {
          if ( a > 20 )
             System.out.println(“Second if “);
          else 
             System.out.println( “First else“);
       }
       else
        System.out.println(“Second else “);
    
     c)    if( b >= 0 &&  a < 10)
             System.out.println(“executed if”);
           else
             System.out.println(“execute else”);
    
    
  5. For each one what is output to the console?
    
    a)  for( int  i = 0;  i <= 6; i += 3) 
          System.out.println("i is " + i);
    
    b)  int x = 20;
         if( x % 2 == 0 ) 
           System.println("X is odd");
         else
           System.out.println("X is even");
    
  6. The following for loop will print out the first 20 squares. Convert the for loop into a while loop.
    for(int i = 1; i <= 20; i++)
       System.out.println(i + " squared is " + i * i);
    
  7. Explain the difference between a while statement and a do-while.
  8. Given:
    final double rate = 6.25;
    char             pass =  'P';
    
    

    True or false, the following statements are correct. If false, you must Explain!

    a)    rate  =  9.35;
    
    
    b)  pass  =  "pass";
    
  9. What is the outout ?
    int [] X = {22,44,88};
    int [] Y = {11,33,55};
    Y = X;
    System.out.println( Y[0] );
    System.out.println( X[ X.length – 1] );
    
  10. Let data be a reference to an array of integers, write the code that will compute the average value of the array. You can assume there is at least one element in the array. That is data[0] contains an integer.
  11. The String class from the Java API contains the following method:
    public int lastIndexOf (char ch) 
    that returns the index, within the string through which the method was called,
    of the last occurrence of the specified character.
    If the parameter character does not appear in the string,
    the method returns -1.
    
    YOUR TASKS:
    
    You must complete the code that follows, so that it will do the following:
    
    
    1.     Read a String from the user, using the Scanner class,
    and store it in a variable called s1
    
    2. Using a call to the method lastIndexOf of the String class described above,
    find the index in s1 of the last occurrence of the character ‘a’ and store
    the result in a variable called lastIndex
    
    3. Display with a JOptionPane or print to terminal the results of Step 2 above.
    
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    public class Problem1
    {
      public static void main (String[] args)
      {
        Scanner console = new Scanner (System.in);
    
      }
    } 
    
  12. In a program you are writing you have a recurrent need to check arrays of integers for the presence of pairs of adjacent integers that differ by one in increasing order (e.g. 4,5 or 27,28) To this end you decide to write a method that does the following:
    
    
    1.Takes an array of integers as a parameter 
    
    
    2. Checks for the presence in the array of at least one pair of adjacent
    increasing integers, i.e. two integers right next to each other in the array
    that differ by one, with the smaller one appearing first from left to right
    (2,3 or 45,46,....) 
    
    3. Returns a boolean variable that has been set to true if the array contains
    at least one such pair, or that has been set to false if it does not
    contain any such pairs.
    
    YOUR TASKS:  
    
    
    1. Complete the code of the proposed method found below, according to the
    specifications given above. 
    
    
    2.Propose a suite of three arrays of integers of length 5 that would be
    good candidates to test the correctness of your method
    
    3.For each of the arrays you proposed in Step 2  give a clear explanation
    for why you think the chosen array is a significant test case
    
    private boolean hasWantedPair(int[] nums)
    {
    
    boolean hasPair  = false;                    
       
    
    return hasPair;
    
    }
    
    

Programming Part(Worth 10%)

  1. Write an application that accepts a person's weight and displays the number of calories the person needs in one day using the formula
    calories = bodyWeight * 19;
    

    Name your class Calories

    You must use a method (function) to do the computation. In order to recieve credit you have to use a function to do the computation. No function , no credit

    You must run the program in a loop allowing the user to terminate the program.