PROG 10082
Object Oriented Programming 1

Mid Term 1 Review Solutions

Practice Questions

1. Using the code below...

a) Identify any syntax or logic errors in the code and suggest corrections.

b) Identify any stylistic errors or pieces of code that don't follow proper standards/conventions.

public class junk
{
public static main(String[] args) {
Scanner keys = new Scanner();
System.out.println(Enter a number:);
int somenumber = keys.next();
HalvedNumber = somenumber / 2
System.out.println("Your number doubled: " (somenumber * 2));
System.out.print("Your number halved: + halvedNumber);
}

Errors:

Standard Violations:


2. What is the output of the following program?

public class Question2 {
    public static void main(String[] args) {
        int x = 2, y = 5, z = 20;
        System.out.print("even?");
        System.out.print(z%x);
        x += z / x + y++;
        System.out.println(" x is " + x);
        System.out.println(" y is " + y);
        System.out.println(" z is " + z);
        int blah = x + y * z - y;
        System.out.println("blah? " + blah + 1);
    }
}

Output:

even?0 x is 17
 y is 6
 z is 20
blah? 1311

Notes:


3. a)Complete the tasks indicated in the comments:

import java.util.Scanner;

public class SalesJunk
{
     public static void main(String[] args)
     {
        
        // get a sales amount from the user using a Scanner object
        Scanner in = new Scanner(System.in);
        System.out.print("Enter sales amount: ");
        double salesAmt = in.nextDouble();

        // get a commission rate from the user (e.g. 5 for 5%)
        System.out.print("Enter commission rate: ");
        double commRate = in.nextDouble();

        // calculate and display (on the console) the amount of commission the
        //  sales person should be paid (sales * comm) - format to 2 decimal places
        double commissionAmt = commRate / 100 * salesAmt;
        System.out.printf("Amount of commission to pay: $%.2f\n", commissionAmt);
     }
}

4. Write a simple application to calculate the cost of making custom vertical blinds for a window. Your program should prompt the user to enter the length and width of the window in metres, and the cost per square metre of the fabric for the blinds. Your program should calculate and display the totals and taxes on the console in the following format:

Cost of your vertical blinds:
Sub Total:   $ 100.00
HST:         $  13.00
Final Total: $ 113.00

In this example, the user inputs were 5 for the length, 10 for the width, and 2 for the cost of fabric.

IPO Chart:

Input- length of window
- width of window
- cost per square metre of fabric
Processing- sub total = length * width * cost
- hst amount = .13 * sub total
- final total = hst amount + sub total
Output- sub total for vertical blinds
- amount of hst on sub total
- final total for vertical blinds

NOTE: Comments are not required on exam questions!!

import java.util.Scanner;

public class VerticalBlinds
{
     public static final double HST_RATE = .13;

     public static void main(String[] args)
     {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter length of window: ");
        double length = in.nextDouble();
        System.out.print("Enter width of window: ");
        double width = in.nextDouble();
        System.out.print("Enter cost of fabric per square metre: ");
        double cost = in.nextDouble();

        double subTotal = length * width * cost;
        double hstAmt = HST_RATE * subTotal;
        double finalTotal = subTotal + hstAmt;

        System.out.printf("%-12s $%7.2f\n", "Sub Total:", subTotal);
        System.out.printf("%-12s $%7.2f\n", "HST:", hstAmt);
        System.out.printf("%-12s $%7.2f\n", "Final Total:", finalTotal);
     }
}

How I planned my output: I figured that there would be 2 columns: one column with the string heading (e.g. "Sub Total:") and the 2nd column with the numeric value. For the string column, I counted 12 characters (the largest value is "Final Total:" which has 12 characters). Then I planned for a space after the string column, followed by a dollar sign ($). Then I allocated a total of 7 character columns for the numeric field (one of which is taken by the decimal point and two of which are taken by the digits after the decimal).

The String column would use the format specifier %-12s (left justify in 12 columns) and the numeric column would use the format specifier %7.2f So given that, and the space and $ between the 2 columns, I have the format string "%-12s $%7.2f\n".

5. Modify question 4: if the sub total is more than $100, apply a 10% discount to the order. Your output would appear as:

Cost of your vertical blinds:
Sub Total:   $ 120.00
Discount:    $  12.00
HST:         $  14.04
Final Total: $ 122.04

Solution 1:

import java.util.Scanner;

public class VerticalBlinds
{
     public static final double HST_RATE = .13;
     public static final double DISCOUNT = .1;  

     public static void main(String[] args)
     {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter length of window: ");
        double length = in.nextDouble();
        System.out.print("Enter width of window: ");
        double width = in.nextDouble();
        System.out.print("Enter cost of fabric per square metre: ");
        double cost = in.nextDouble();

        double subTotal = length * width * cost;
        if (subTotal > 100) {
            double discount = subTotal * DISCOUNT;
            subTotal -= discount;
            System.out.printf("%-12s $%7.2f\n%-12s $%7.2f\n", 
                "Sub Total:", subTotal, "Discount:", discount);
        } else {
            System.out.printf("%-12s $%7.2f\n", "Sub Total:", subTotal);
        }

        double hstAmt = HST_RATE * subTotal;
        double finalTotal = subTotal + hstAmt;
        
        System.out.printf("%-12s $%7.2f\n", "HST:", hstAmt);
        System.out.printf("%-12s $%7.2f\n", "Final Total:", finalTotal);
     }
}

Solution 2 (better one):

import java.util.Scanner;

public class VerticalBlinds
{
     public static final double HST_RATE = .13;
     public static final double DISCOUNT = .1;  

     public static void main(String[] args)
     {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter length of window: ");
        double length = in.nextDouble();
        System.out.print("Enter width of window: ");
        double width = in.nextDouble();
        System.out.print("Enter cost of fabric per square metre: ");
        double cost = in.nextDouble();

        double subTotal = length * width * cost;

        String discountOutput = "";
        if (subTotal > 100) {
            double discount = subTotal * DISCOUNT;
            subTotal -= discount;
            discountOutput = String.format("%-12s $%7.2f\n", 
                "Discount:", discount);
        } 

        double hstAmt = HST_RATE * subTotal;
        double finalTotal = subTotal + hstAmt;

        System.out.printf("%-12s $%7.2f\n%s", "Sub Total:", subTotal,
                discountOutput);
        System.out.printf("%-12s $%7.2f\n", "HST:", hstAmt);
        System.out.printf("%-12s $%7.2f\n", "Final Total:", finalTotal);
     }
}