CS 1713 Introduction to Computer Science, Spring 2008 Practice Final Comments

There were a total of 43 points on this exam.

  1. The method should return a double.
    You should be careful about using integer division.
  2. This requires a loop. A do-while is easiest. Be careful to not get the test backwards.
  3. It is easiest to do this with a helper method.
  4. Choose your atribute names and data types carefully.
    The only modifier method you need to write is for the price.
    You do not need to write a toString method.
  5. You only need to write the constructor which takes a single String parameter.


  1. Write a function that takes an array of integers as a parameter and returns the average of the elements in the array. If the array is empty, return 0.
    public double findAverage(int[] array) {
       double sum = 0;
       if (array.length == 0)
          return 0.0;
       for (int i=0;i<array.length;i++)
          sum = sum + array[i];
       return sum/(double)array.length;
    
  2. Write a code segment that prompts the user for an integer between 1 and 10 until such an integer has been entered, and then prints this integer. Use the b>Scanner class which has a method called nextInt().
       Scanner scan = new Scanner(System.in):
       int val;
       do {
          System.out.println("Enter an integer between 1 and 10");
          val = scan.nextInt();
       } while (val < 1 || val > 10);
    
  3. a) Write a code segment that declares and allocates a 2-dimensional array of characters with 30 rows and 16 columns.
    b) Write a method that takes an arbitrary 2-dimensional array of characters as a parameter and returns true if the array has a row in which all of the characters are the same, and returns false otherwise. A row with no elements does not satisfy this condition, but a row with one element always does.
       char[][] board = new char[30][16];
    
       public boolean equalRow(char[][] board) {
          for (int i=0;i<board.length;i++)
             if (equalRow(board[i])
                return true;
          return false;
       }
       public boolean equalRow(char[] row) {
          if (row.length == 0)
             return false;
          for (int i=1;i<row.length;i++)
             if (row[i] !+ row[0])
                return false;
          return true;
       }
    
  4. Design a class to represent a computer mouse. A mouse can be wireless, or not wireless. It can be a USB mouse or not a USB mouse. A mouse has a number of buttons and a price. Write a complete class definition for this class. You should have a constructor and accessor methods for all of the fields. Write a modifier method for the price. You are to write the complete code for this class. Your goal is to write it so that if it is typed into eclipse it will compile without any errors. Use the back if necessary.
    public class ComputerMouse {
       private boolean wireless;
       private boolean usb;
       private int numButtons;
       private double price;
    
       public ComputerMouse(boolean wireless, boolean usb, int numButtons, double price) {
          this.wireless = wireless;
          this.usb = usb;
          this.numButtons = numButtons;
          this.price = price;
       }
    
       public boolean getWireless() {
          return wireless;
       }
       public boolean getUsb() {
          return usb;
       }
       public int getNumButtons() {
          return numButtons;
       }
       public double getPrice() {
          return price;
       }
       public void setPrice(double price) {
          this.price = price;
       }
    }
    
  5. A Name class has attributes firstName and lastName. Write a constructor for this class that takes a single String parameter in the form of a first name followed by an arbitrary number of blanks followed by a last name. The constructor should set the two attributes. You may assume that the first and last names do not contain blanks and the constructor parameter does not start with a blank. If the constructor parameter does not contain two names (either because it contains no blanks, or it contains no non-blank characters after the first blank), the last name should be empty.
       public Name(String fullName) {
          int ind;
          ind = fullName.indexOf(' ');
          if (ind == -1) {
             firstName = fullName;
             lastName = "";
          }
          else {
             firstName = fullName.substring(0,ind);
             lastName = fullName.substring(ind).trim();
          }
       }