Lab 14 – Inheritance and Sorting

Write a class named Student that models a college student. The Student class should include fields for the student’s first name (String), last name (String), student id (int) and major (String) along with an access methods (getters) for the fields. The class should set all of its fields in the constructor using arguments passed to the constructor.  If any of the Strings are null or the empty string or if the integer is less than or equal to 0, the constructor should throw an IllegalArgumentException.  Note which class the IllegalArgumentException class extends.

Write a class named TransferStudent that is a subclass of Student.  The class has a field that specifies the number of credits transferred in to the college (int). The constructor should set all of the class fields (and super class fields) using the values of the arguments passed into the constructor.  If the number of credits transferred in is less than 0 then the constructor should throw an IllegalArgumentException.  The class should have access methods (getters) to get the values stored in the fields but should not contain setters. Override toString() to return a String containing the data in all of the class’ and superclass’ fields, each separated by colons (:).

Write a driver (a runnable program) in a class named Lab14. The class should have three methods: sortStudents, writeStudents, and main.

The sortStudents method should take an array of Students as an argument.  The method sorts the elements in the array using the Bubble-Sort algorithm discussed in lecture so that the elements in the array are sorted in-place according to their student ids (in increasing order).

In your driver, add a method named writeStudents that has two arguments. The first argument is a file name (String) and the second argument is an array of TransferStudent objects. The function writes the first name, last name, student id, major and number of credits transferring in of each element in the array to the file named in the first argument. The file should contain one line for each object in the array with each field separated by colons.

The main method should create two objects of type TransferStudent using the following data and inserts their references into an array of TransferStudents.

    • Kim Brown is a Computer Science major with 27 credits transferring in.  Her student id is 67890.
    • John Doe is a Computer Science major with 24 credits transferring in.  His student id is 12345.

The main should then sort the elements in the array by calling sortStudents.

Finally, the main method should call the writeStudents method using the file name “students.txt” and the array of TransferStudents.


Compile, test, debug, and push to Github when complete.

Lab 13b – Travel Course

 

Part I

Write a class named InvalidDataException that extends the Exception class.

    • The class should have a constructor that takes a String (for an error message) as an argument. The constructor should pass the string to the super class’ constructor.
Part II

Write a class named TravelCourse.

    • The class should include fields for the following information:
      • catalog number: String
      • title: String
      • destination: String
    • The class should have a constructor that sets each of the fields using data that is passed to it.  If any of the arguments are null or the empty string, the constructor should throw an InvalidDataException.
    • The class should have getters for each field, but not setters.
    • The class should override toString which should return a string containing the catalog number, title, and destination; all separated by commas.
    • The class should override equals.  Two instances of TravelCourse are considered equal if their catalog numbers and titles are the same.
Part III

Write a program in a file named Lab13b.java.  The program should behave as fillows.

    • Create an array that can hold 4 references to TravelCourse objects.
    • Populate the array using the following data.  If an exception is caught, print an error to the screen and continue – do not terminate the program.
Catalog Number / Title / Destination

   FRE-310 / Exploring France / France
   FRE-310 / Exploring France / Paris
   FRE-310 / A Tour of France / France
   ITL-310 / Discovering Italy / null
    • Iterate over the array using a for-loop, and for each non-null element in the array, print to the screen the string returned by the element’s toString method.
    • Iterate over the array using a for-loop, and count how many elements in the array are equal to the first element in the array.  Print the count to the screen.

Compile, test, and when satisfied that it is correct, push to GitHub.

 

Lab 13a – Exam 3

Part I

Write a class named MenuItem that models a restaurant menu item. Include the following:

    • Fields for the menu item’s name (String), number of calories (int), and price (double).
    • A constructor that sets all 3 fields using data passed into the constructor.
        • Do not include data validation in the constructor.
    • Getters and setters for each of the fields.
        • Do not include data validation in the setters.
    • Override the toString() method and return a string containing the name, number of calories, and price, all separated by commas.
    • Override the equals() method. Two menu items are considered equal if they have the same name.
Part II

Create a file named menu.txt that contains the data shown below.  The first line of the file contains an integer that indicates how many menu items are described in the file. Each subsequent line contains the name, number of calories, and price of a menu item – separated by commas.

3
Cheese Cake,560,6.95
16 oz. Prime Rib,900,21.95
Lobster Bisque,600,5.75
Part III

Write a class named Exam3 that satisfies the following.

    • The class has a method named printMenu that takes an array of MenuItem elements as an argument and prints to the screen the name, number of calories, and price of each item in the array.
    • The class has a main method that when run:
      • Reads the data in menu.txt, and for each menu item in the file creates an instance of the MenuItem class and stores the reference to the instance in an array named menuItems.
      • Prints the menu items stored in the array named menuItems to the screen by calling printMenu.
      • Iterates, using a for-loop, over the array named menuItems and prints to the screen the name, a dash (-), and the price of each menu items in the array that has fewer than 700 calories, with each items on a separate line.

Compile, Test, and when correct, push to GitHub.

Lab 12 – Maze Game Base

Log into cs.bridgewater.edu and change your working directory to your labs directory in your repository.  Inside the labs directory create a file named Lab12.java.

The following program uses a 10×10 array of characters as a game field. The users current location in the game field is indicated by the ‘X’ character.  The program for this lab simply allows the user to move the ‘X’ character around the field.  When done you are encourage to expand upon this base program.


Inside the Lab12.java file write a program that satisfies the following:

    1. Add a method named initializePlayingField. The method has one parameter which holds a reference to a 2D array of characters.

The method sets the first element in the last row to ‘X’ and all other elements in the 2D array to the ‘=’ character.

    1. Add a method named printField.  The method has a single parameter that holds a reference to a 2D array of characters.  The method prints the elements in the 2D array to the screen, with each row on a separate line, and with the elements in a row separated by spaces.
    1. Add a method named isValidDirection.  The method has 3 parameters.  The first parameter is named row and holds an integer, the second is named col and holds an integer, and the third is named direction and holds a character.

The variables named row and col hold indices specifying the player’s current location in a (10 x 10) 2D array.  

The value in the variable named direction is ‘w’ (up), ‘a’ (left), ‘s’ (down) or ‘d’ (right) indicating where the player would like to move.  

If the location of the player’s next move in the array doesn’t take the player out of bounds return true; Otherwise return false.  For example, if the arguments are 0,0,’w’ then the user’s current location is (0,0) and ‘w’ (up) would take the user out of bounds, therefore the method returns false.

    1. Add a method named updateField. The method has four parameters. The first parameter is named matrix and holds a reference to a 2D array of characters, the second parameter is named row and holds an integer, the third parameter is named col and holds an integer, and the fourth parameter is named direction and holds a character.

The variables named row and col hold indices specifying the player’s current location in matrix.

The method moves the ‘X’ character in matrix in the direction specified in the variable named direction, replacing the ‘X’ character at the user’s current location with an ‘=’ character.

    1. In the main method do the following.
      • Declare the following variables.
      • int playerRow = 9;    // player's current row
        int playerCol = 0;    // player's current column
        char choice = '?';
        boolean isValid = false;
        String input = null;
      • Create a Scanner that can read data from the keyboard.
      • Declare a (10 x 10) 2D array of characters named field.
      • Initialize the 2D array by calling initializePlayingField.
      • Add a do-while loop that does the following while the value in the variable named choice is not ‘x’.
        • Print the game field by calling printField.
        • Ask the user to enter a direction: w (up), a (left), s (down), d (right) or x (to exit)
        • Read the user’s choice (as a String) from the keyboard and store the value in the variable named input.
        • If the length of the string entered by the user is 0, set isValid to false; Otherwise set choice equal to the first character in the string entered by the user and set isValid equal to the value returned by isValidDirection.
        • If the direction is valid, call updateField and update the values in playerRow and playerCol.
        • Reset the screen using the following code:
System.out.print("\033[H\033[2J"); 
System.out.flush();

Compile and debug.


When done,  expand the program to include mines, walls, a score board, and other game elements.


Push to GitHub.