Program Design I - Final Exam
Write all of your answers on the answer sheets provided. You may write on this test paper, but the instructor will be looking for answers on the answer sheets. During this exam you may refer to printouts of any source code you have written.
  1. (3 pts) What is a constructor?

  2. (3 pts) Name one reason it can be helpful to define a toString method for a class.

  3. Consider the following program:
    public class FuncExam
    {
      public static int superfun(int a, int [] z, Dude whatever)
      {
        a= 17; 
        z[0]= 99;
        whatever.score= 7;
        if (a < 20)
          return a;
        z[1]= 88;
        return -1;
      }
      public static void main(String [] args)
      {
        int a,b;
        int [] d= new int[4];
        Dude steve= new Dude();
        a= 1;  b= 2;  d[0]= 3; d[1]= 4;
        //here
    
        //there
        b= superfun(a,d,steve);
        System.out.println(a+" "+b+" "+d[0]+" "+d[1]+" "+steve);
      }
    }
    class Dude 
    {
      String name;
      int score;
      public String toString() { return "My score is: "+score; }
    }
    
    1. (10 pts) What is the output of the final statement in main?
    2. (2 pts) Where does execution of the program begin?
    3. (2 pts) What would be the result of placing the following statement on the line between the comments //here and //there: d[4]= 6;

  4. Suppose that the following parallel arrays are used to keep track of a music store's inventory of albums:
      String [] name= new String[1500];
      double [] price= new int[1500];
      int [] count= new int[1500];
      int i,j;
    

    The music store keeps track of the name of each album, the suggested retail price of the album, and the number of copies of that album they currently have in stock.

    1. (4 pts) Write a section of code that will set the price for every album to $19.95.
    2. (6 pts) Write a method called cheapPos that given an array of prices for the albums and given the number of elements in that array, will return position of the least expensive album.
    3. (10 pts) Write a method called showOverstocks that will display a list of albums for which there are more than 10 units currently in stock. The method should accept the necessary parameters to achieve this purpose. The list should include the name of the overstocked item, the number of units in stock, and the dollar amount represented by the entire stock for that item. The latter value is calculated by multiplying the number of items in stock by the price for that item. The list should be arranged in columns with names left justified and numbers right justified. Dollar amounts should show exactly two decimal places.
    4. (10 pts) Suppose a text file named inventory.txt contains a list of no more than 1500 album names with one album per line. Write a method called loadAlbums that given an array of album names will load the array with the albums in the file and will return the number of albums read.

  5. Write an Album class to represent the information described described in problem [*]. In particular this class should hold the album name, album price, and the number of copies of that album currently in stock. (2 pts) In addition the class should implement the following methods:
    1. (4 pts) A constructor that accepts the album name as a paramter. Initially the price should be set to $19.95 and the number in stock should be set to 5.
    2. (6 pts) A toString() method that returns the album name, price, and number in stock, each separated by spaces.

  6. Use the Album class you wrote in problem [*] to answer the following questions.
    1. (6 pts) Write a section of code that will declare an array of 1500 Albums and will create 1500 new Album objects having names “Album 1”, “Album 2”, ....
    2. (6 pts) Write a method called valueOfInventory that given an array of Album objects and and the number of elements in the array as parameters will calculate and return the total value of all albums currently in inventory. The value for a single album is calculated by multiplying the number of copies in stock for that album by its price.
    3. (12 pts) Write a method called deleteAlbum that given an album name, an array of Album objects, and the number of elements in the array as parameters, will find the specified element and delete it from the array. The method should return the new number of elements. If the specified album is not found in the array then there should be no change. Also, you should only allow deletion of an album that is no longer is stock. If the specified album is still in stock don't change the array and give an appropriate message. You may assume that the array is unordered.