Dave Drohan (SETU)

BACK NEXT

ShopV1.0

Creating Classes

Within the ShopV1.0 project, we will create two new classes, one called Product and the other called Driver.

To create a class called Product:

Empty Product Class Repeat this process to create a class called Driver.

Your folder structure should look like this:

src folder structure

Note: refer to the lecture materials for this week, if you can’t remember the exact format of the following fields/methods.

Product Class

In the Product class, create four private instance fields:

Write a constructor for this class that takes the four instance fields as parameters and updates the object state using these parameters. Note: no need for field validation yet.

Write a getter for each instance field.

Write a setter for each instance field. Note: no need for field validation yet.

Write a toString method that builds and returns a String comprising a user friendly representation of the object state.

Driver Class

In the Driver class, create two private instance fields:

Write a method called addProduct() that asks the user to input the details of the product (name, code, unit cost and whether or not it is in the current product line). Use this data to create a new Product object i.e.

System.out.print("Enter the Product Name:  ");
String productName = input.nextLine();
System.out.print("Enter the Product Code:  ");
int productCode = input.nextInt();
System.out.print("Enter the Unit Cost:  ");
double unitCost = input.nextDouble();

//Ask the user to type in either a Y or an N.  This is then
//converted to either a True or a False (i.e. a boolean value).
System.out.print("Is this product in your current line (y/n): ");
char currentProduct = input.next().charAt(0);
boolean inCurrentProductLine = false;
    if ((currentProduct == 'y') || (currentProduct == 'Y'))
            inCurrentProductLine = true;

product = new Product(productName, productCode, unitCost, inCurrentProductLine);

Write a method called printProduct() that prints all the product to the console. Hint: use the toString() method from the Product class to print a product.

Write a main method that has the following code:

public static void main(String[] args) {
    Driver driver = new Driver();
    driver.addProduct();
    driver.printProduct();
}

Run the App

Run the app; does all work as expected? Are you asked to enter the product details? Are all details you entered printed back to the console?

Crashing your Program

We are going to crash the program with an Exception. In a few weeks, we will look at the code required to handle and recover from this, but at the moment, we are just raising awareness of it.

Run your program again and start typing in the following details:

Your program will crash with a similar call stack dump:

call stack dump

Notice the exit code of the program is 1…it is usually 0 which means the process ended successfully.

The program crashed because we were expecting the user to type in an int for Product Code, however they typed in a String. We used a Scanner method nextInt() to read this input and as it was a String, the program couldn’t handle it, so it crashed.

Looking at the red text in the call stack dump, we start at the last line printed on the screen and work our way upwards/backwards.

NOTE: your line numbers will probably differ depending on how you’ve written your code.

So, we can see the last line printed was:

    at Driver.main(Driver.java:10)

This means that, in the Driver class (from my code), in the main method, on line 10, there was a method call made. If we look at our code, we can see that the method call was addProduct:

addProduct method call

The second last red line printed to the console was:

    at Driver.addProduct(Driver.java:20)

Again looking at our code, we can see that, within the addProduct method, line 20 was the read of the Product Code input from the user:

Scanner int read

Now looking at the remaining red text in the console, we can see that the other calls are made from the Scanner class itself in Java, with the top line stating the actual Exception, which is InputMismatchException:

InputMismatchException from Scanner

So, putting all this together, the call stack dump is telling us that:

We will look at how to stop this Exception from crashing our program in future lectures and labs.