CSC 120 Final Exam Review

IMPORTANT NOTE:  The Final Exam is given on paper.  To best prepare oneself for the Final Exam, follow this suggested order in studying this review web page:

  1. Write solutions to these problems on paper.
  2. Solve the problems in Netbeans
  3. Only after sincerely performing the first two steps shown here, check your solutions with the answers to the problems.  The answers will be linked to the CSC 120 Schedule page on or before Friday, December 10, 2021 at 11:00 pm

Problems:

  1. Create a new Netbeans project called FinalExamReview.
    Right click on each of the following and choose Save Target As... to copy these files into your src folder.
  2. Declaring, instantiating, and using individual objects (no arrays) in the MUPanel.java file.
    1. Declare three Product variables called one, two, and three.
    2. Instantiate one using the 5-parameter constructor.
      one's stockNumber is 15, name is "Wallace and Gromit," it is taxable, it costs 25.10, and is CYAN.
    3. Instantiate two using the 1-parameter constructor.
      two's name is "XPod."
    4. Instantiate three using the no-parameter constructor.
    5. At the end of the MUPanel constructor method change the name of three to "Keychain."
      There is a method in the Product class that can be used to change a product's name.
    6. In the paintComponent method call the draw method for each of the three Products.
    7. Run & fix, before proceeding.

  3. Declaring, instantiating, and using 1-dimensional arrays of objects in the MUPanel.java file.
    1. Declare a 1-dimensional array of Product objects called arrayOfProducts.
    2. Instantiate the array with room for 5 Product objects.
    3. Instantiate each element of the array one at a time without using a loop, using these values:
           10     Slumdog Millionaire     true     22.95     MAGENTA
      15 No Country for Old Men true 21.30 GREEN
      20 A Beautiful Mind false 19.95 RED
      25 Gladiator true 15.27 YELLOW
      30 Schindler's List true 23.42 BLUE
    4. Write code that uses a loop to call the getName method for each product in the array and displays the names of all products in the Java Console.
    5. Write code that uses a loop to determine whether any product in the array is RED (actually, you'll be looking for Color.RED and don't forget that for a String or a Color or any object to test for equality you cannot use ==, but must use the equals method.) If you find at least one red product, then display "Found" in the Java Console; otherwise display "Not found." In any case there should be only one line of output in the Java Console.
    6. Write a new method to be placed in the MUPanel.java file called getAveragePrice. This method has no parameters and returns a double. The value to be retuned by this method is the average price of all products in the array.
    7. Write one line of code near the bottom of the constructor method of the MUPanel class definition that calls the getAveragePrice method. Like this:
           System.out.println("The average price is " + getAveragePrice());
    8. Run & fix, before proceeding.

  4. Writing a complete class definition for a class called Bike.
    1. There are three private data members.
      • color, which is a Color
      • price, which is a Double
      • hasGears, which is a Boolean
    2. There is one constructor method.
      • A 3-parameter constructor that uses the 3 parameters to initialize the 3 data members.
    3. Write a getColor method, a setColor method, a getPrice method, and a setPrice method.  The setPrice method should validate the parameter to ensure that it is not less than 0 or greater than 1000.  If the parameter is less than 0, the price data member should be set to 0.  If the parameter is greater than 1000, the price data member should be set to 1000.
    4. Write a toString method that (as usual) returns a String describing the bike in terms of the data members. The description must label the values returned. For example, the return value should be formatted something like this:
            Color=java.awt.Color[r=255,g=0,b=0] price=150.99 hasGears=true
      instead of
            java.awt.Color[r=255,g=0,b=0] 150.99 true
    5. Run & fix, before proceeding.

  5. Writing Methods. For each of the following, write the header for the method. You do not need to write the body of the method. You may do these on paper, but if you do decide to type them into a Netbeans project, then you will need to write an empty method body (opening and closing braces) for methods that do not return anything, and you'll need to write a method body that has some kind of return statement for methods that do return something.
    1. getSmallest method that has two Integer parameters and returns the smallest of the two parameter values.
    2. isLarge method that has one Integer parameter and returns true if the value of the parameter is greater than 500, and otherwise returns false.
    3. add100 method that has no parameters and returns nothing. This method assumes we are in a class that has a private data member called size, and this method adds 100 to the current value of size.

  6. Writing Methods.  Write the method bodies for each of the methods above.

  7. Tracing Output.
    We'll probably give you a program that declares and instantiates Balloons, Flags, or some other graphical class, and has a paintComponent method that calls the draw method of the Balloons/Flags/Whatever. We'll ask you to sketch the output displayed in the output window, and also to write the Java Console output. Probably, the Balloon/Flag/Whatever class will have System.out.println stuff in the constructor, and in the draw methods, so read carefully and trace each step as you go.

  8. Show the output of the following Java code:
        Integer num = 400;
        System.out.println( (num / 5) );
        System.out.println( (num % 5) );
        num = num / 4;
        System.out.println( (num / 5) );
        System.out.println( (num % 5) );
        num = num / 7 - 1;
        System.out.println( (num / 5) );
        System.out.println( (num % 5) );
        num *= 3;
        System.out.println( (num / 5) );
        System.out.println( (num % 5) );
    
  9. Strings and String methods.
    1. Write a method named parameterIsAQuestion that accepts one String parameter and returns a boolean value.  If the last character of the parameter is a question mark, the method should return true; if the last character of the parameter is any other character, the method should return false.

      An example of usage of this method would be:
          String firstSentence = new String("Will the Final Exam be easy?");
          String secondSentence = new String("Java is so much fun!");
      
          if ( parameterIsAQuestion(firstSentence) ) {
              System.out.println("Here is a question: " + firstSentence);
          }
          else {
              System.out.println(firstSentence + " is not a question.");
          }
      
          if ( parameterIsAQuestion(secondSentence) ) {
              System.out.println("Here is a question: " + secondSentence);
          }
          else {
              System.out.println(secondSentence + " is not a question.");
          }
      
    2. Write a method named parameterContainsAnInternalSpace that accepts one String parameter and returns a Boolean value.  If any character in the parameter is a space OTHER THAN the first or last character, the method should return true; if the parameter does not contain a space in any position OTHER THAN the first or last positions, the method should return false.

      An example of usage of this method would be:
          String firstSentence = new String("Better study for all of your Final Exams");
          String secondSentence = new String(" ThisOneWillTestAsFalse ");
      
          if ( parameterContainsAnInternalSpace(firstSentence) ) {
              System.out.println("There is an internal space in: " + firstSentence);
          }
          else {
              System.out.println(firstSentence + " has no internal spaces.");
          }
      
          if ( parameterContainsAnInternalSpace(secondSentence) ) {
              System.out.println("There is an internal space in: " + secondSentence);
          }
          else {
              System.out.println(secondSentence + " has no internal spaces.");
          }
      
    3. Show the output of the following Java code:
          String str = new String("If April showers bring May flowers, we'll have many flowers this year!");
          System.out.println( str.length() );
          System.out.println( str.charAt(4) );
          System.out.println( str.substring(3, 6) );
          System.out.println( str.substring(47) );
          System.out.println( str.indexOf("low") );
          System.out.println( str.lastIndexOf("low") );
          System.out.println( str.split("s").length );