Final Exam Review

Topics Covered:

Exam Policies and Rules

Make sure you've read over Assignment & Exam Policies!

For Virtual Classes

For Face-to-Face Classes

For ALL Classes

Plan ahead!! Make sure you don't sleep in, as that is not a valid reason for missing a test. Make sure you leave for school earlier than normal in case you encounter transportation problems on the way (i.e traffic jam, broken-down bus, etc). If you drive, make sure you have a bus schedule - if your car won't work when you leave for school, you'll have the time and resources to take the bus, instead.

Exam Format

The exam will include the following types of questions:

Summary of Topics

The exam will cover everything you've learned to date, although the focus will be on the new material covered in weeks 9 to 13. You might want to review the Mid Term 1 Review and go over the textbook chapters we covered for those tests. The last third of the course covered the following textbook chapters:

Practice the "check point" questions, the chapter questions in Revel, and the coding questions at the end of each chapter!

Remember that you can get the even-numbered solutions to the programming questions. You should still do the odd-numbered questions: those are the questions I pick from for the exam questions!

Summary of Topics

This may not be a complete list. Make sure you've read th e required textbook sections!

Arrays

String Processing

Methods

Object-Oriented Programming/Design

Study Hints:

Practice Questions

[Solutions to Review]

1. Find the syntax/logic errors:

a.

The class definition:

public class MyClass {

	private int counter;
	private String name;

	public void MyClass() {
		counter = 1;
		name = "Kaluha";
	}

	public int setCounter(int counter) {
		if (counter > 0)
			counter = counter;
		else 
			throw new IllegalArgumentException("Invalid counter.");
	}

	public void getCounter(int counter) { return counter; }

	public void tostring() {
		return name + "\n";
	}
}

The main() class:

public class TestMyClass {
	
    public static void main(String[] args) {

        MyClass mc = MyClass();
        mc.counter = 5;
        for (int i=1; i<=mc; i++) {
            System.out.println(mc);
        }
    }
}

b.

public class Errors {

    public static void main(String[] args) {
        int numbers = new int[];
        for (int i=0; i<=numbers.size; i++) {
            numbers = (int)(Math.random() * 100 + 1);
        }
        Scanner in = new Scanner(System.in);
        System.out.print("Enter an index: ");
        double index = in.nextDouble();
        int value = numbers[index];
        display(value);
    }
    public static void display() {
        return Math.sqrt(value);
    }
}

2. Find the Output:

a.

public class Output {

    public static void main(String[] args) {
   
        int value1 = 15;
        int value2 = 10;
        boolean wasSwapped = swap(value1, value2);
        System.out.println(value1);
        System.out.println(value2);
        System.out.println(wasSwapped)
    }

    public static boolean swap(int min, int max) {
        if (min > max) {
            int temp = min;
            min = max;
            max = temp;
            return true;
        } else {
            return false;
        }
    }
}

b.

public class Output {

    public static void main(String[] args) {

        int[] nums = new int[5];
        Scanner in = new Scanner(System.in);
        for (int i=0; i<nums.length; i++) {
            System.out.print("Enter a value: ");
            nums[i] = in.nextInt();
        }
        for (int i=nums.length-1; i>=0; i--) {
            System.out.println(nums[i]);
        }
    }
}

Coding Questions

1. A company with a customer loyalty program assigns discounts to it's customers based on the number of purchases they've made, according to the table below:

# of PurchasesDiscount
50 or more2%
100 or more5%
200 or more7%
400 or more10%

Write a program that prompts the user for the customer's number of purchases, and the balance due on the current purchase. Then the program displays the total amount due after the discount.

Your program should include a method that accepts number of purchases and returns the discount percentage. You should also include a method that accepts the purchase amount and the discount percentage, and returns the total purchase amount with the discount applied.

2. a. Your friend runs a small roadside kiosk where they sell their own home-grown, organic produce. They sell different kinds of fruits and vegetables. For example, they sell a bag of 10 apples for $2.00 and a bag of 12 ears of corn for $3.00. Some customers don't want the whole bag of apples, corn, or whatever, so they're allowed to purchase say, 3 apples or only half a bag of corn.

The kiosk gets busy so your friend doesn't want to risk doing the math in their head and make mistakes. You offer to help by writing a small program they can use on their laptop to help do the calculations and let them know how much money to collect from customers.

You start with the Product class.

Class: Product
Data members:
- itemName : String
- itemQty : int
- itemPrice : double
Methods:
+ Product(String, int, double)
+ getItemName() : String
+ setItemName(String) : void
+ getItemQty() : int
+ setItemQty(int) : void
+ getItemPrice() : double
+ setItemPrice(double) : void
+ calcTotal(int) : double
+ toString() : String

This class has the following data members:

There is no default constructor. The 3-param constructor accpts a specific item name, quantity, and price, and intializes a new Product object with those values, if they're valid.

Each data member should have accessor and mutator methods. Mutator methods should validate the data appropriately and throw exceptions if the data isn't correct. itemName shouldn't be an empty string; itemQty and itemPrice should both be greater than 0.

A calcTotal() method accepts an integer value argument that corresponds to the number of a particular item the customer wants. This method should then return the total amount the customer pays. For example, if you sell 10 apples for $2.00 and the customer asks for 5 apples, then this method would return 1.0 (2.0/10 * 5). If the customer asks for 2 bags of 10 apples then the method would return 4.0 (2.0/10 * 20).

A toString() method returns a string representation of the Product object in the form:

itemName: q for $p.pp

..where itemName is the item name, q is the itemQty, and $p.pp is the item price, formatted with a dollar sign and two decimal places.

2. b. Write the application that uses this Product class. Create three sample product objects that your kiosk will sell. The program should then ask the client to enter the name of the item being purchased, and how many of this item they'd like. Use an if-statement to determine which of the product objects applies for the user's input (compare name of products). Use the Product object to calculate and the total amount owed for the purchase and display the amount to collect from the customer. Your program should use a loop so that the client can run this multiple times until the end of the working day.

3. Write a program that allows the user to enter a sentence. Then ask them to enter a character. Use a method called countChar() that accepts the character and the sentence as arguments and returns the number of occurrences of the character in the sentence. Display the results to the user.

4. (question 6.4)

Parallel arrays are 2 or more arrays that are related via their indexes like columns in a spreadsheet. For example, a String array firstNames[] could be parallel to another String array of the exact same size called lastNames[]: a person whose first name is element 2 in the firstNames[] array also has a last name in element 2 of the lastNames[] array. As another example, a set of arrays quantity[] and price[] could store the quantity of items and the price of each item. Element quantity[n] contains the number of ItemN and price[n] contains the price of ItemN.

Write a program that records test scores for 10 students. Record the test score and the student name and store all 10 pairs of values in two parallel arrays. Calculate/display the average of the 10 scores. Then, for each student, display if the score is above or below the average (see sample output).

Average Score:  78.2
Results:
Joe Schmoe: ABOVE Average
Sally Smith: BELOW Average
...
Fred Flintstone: BELOW Average