Course Info

#Programming Fundamentals 1

This is an introductory Programming module and assumes no prior knowledge of programming.

In this module, we will introduce you to the Java programming language through the Processing Development Environment (PDE) and then IntelliJ.

First, we will work through non-complex problems that will introduce you to the basic constructs of programming languages i.e. Sequence, Selection and Loops. You will also learn to use variables, different data types, manipulate the data, logical operators and methods. This will be done using processing.org

Then, using IntelliJ, we will progress to more complex problems that will briefly introduce you to object-oriented programming and data structures. You will do a deeper dive into both of these areas in the semester 2 module, Programming Fundamentals 2.

ShopV2.0 - Primitive Arrays

This console based app will ask the user how many products they would like to enter (this will be the capacity of the Primitive Array). When prompted, they will input the details for each of the products. When finished the data entry, the product details will be printed on the console.

During this step, you may need to view this weeks lecture notes to remind you how to use Arrays.

In IntelliJ, create a new Java Project called ShopV2.0.

Copy the Product and Driver classes from ShopV1.0 and paste them into ShopV2.0. Note: solution to ShopV1.0 is here.

Create a new class in the ShopV2.0 project called Store.

Your folder structure should look like this:

ShopV2.0 folder structure

Product Class

There are no changes required in this class.

Store Class

In the Store class, create two private instance fields:

  • products as an array of Products

  • total as an int representing the number of products currently in the array and also the next available index number in the array. Initialise it to 0 at declaration time.

Store Constructor

Write a constructor for this class that takes one parameter (numberItems). This parameter will be used to set the size of the array of Products.

Store - isFull

Write a private method called isFull that takes no parameters. This method should return true if the value of the total field is equal to the length of the products array, false otherwise.

Store - isEmpty

Write a private method called isEmpty that takes no parameters. This method should return true if the value of the total field is zero, false otherwise.

Store - add

Write an add method that takes one parameter (an Object of type Product) and returns a boolean value.

This method tests the boolean value returned from the isFull() method. If isFull() returns:

  • true then the add method should return a value of false i.e. the array is full, so we could not add a product.

  • false, then add the product object that was passed as a parameter to the products array using the total field as the index location. Increment the total field by 1. Return true, indicating that the product was successfully added to the array.

Store - listProducts

Write a listProducts method that takes no parameter and returns a String value. This method tests the boolean value returned from the isEmpty() method. If isEmpty() returns :

  • true i.e. no products have been added, return the text “No products”.

  • false, use a for loop to cycle over the elements in the array, building a String of all products. Return this string.

Driver Class

We will refactor this class to remove the Product instance field and replace it with an object of type Store. We will also cater for entering and printing multiple products. To do this:

  • Delete the Product instance field and include a Store instance field instead.

  • Create a new method called processOrder that takes no parameters and has a void return type. In this method, type in the following code:

    private void processOrder(){
        //find out from the user how many products they would like to order
        System.out.print("How many Products would you like to have in your Store?  ");
        int numberProducts = input.nextInt();

        store = new Store(numberProducts);

        //ask the user for the details of the products and add them to the order
        for (int i = 0; i < numberProducts; i++){
            addProduct();
        }
    }
  • In your existing addProduct() method, change your code so that it looks like this(first and last lines were changed):
    private void addProduct(){
        input.nextLine();  //dummy read of String to clear the buffer - bug in Scanner class.

        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;

        boolean isAdded = store.add(new Product(productName, productCode, unitCost, inCurrentProductLine));
        if (isAdded){
            System.out.println("Product Added Successfully");
        }
        else{
            System.out.println("No Product Added");
        }
    }
  • Note: there is a bug in Scanner. When you read an int and then do a String read, Scanner ignores the String read. For this reason, you need to put in a “dummy” read of a String (i.e. input.nextLine() as the first line of code in the method).

  • In your printProduct() method, change the line of code to be:

    System.out.println(store.listProducts());
  • In the main method, replace this line of code:
   driver.addProduct();

with:

   driver.processOrder();

Run the App

Run the app; does all work as expected?

  • Are you asked how many products you would like to enter?
  • Can you enter details for each product?
  • When finished the data entry, are the product details printed to the console?