Objectives

IntelliJ · Java Virtual Machine (JVM) · main method · Scanner · OO recap

Verifying IntelliJ Installation

The purpose of this step is to verify that your IntelliJ installation is working correctly.

Launch IntelliJ

Locate your IntelliJ application and start it.

If this is your first time starting IntelliJ, you will probably be presented with a series options i.e. you might be:

  • asked if you want to import settings...you don't need to import any settings.

  • presented with the User Licence Agreement...you will need to scroll to the bottom of this and accept it.

  • asked if you want to share anonymous data with Jetbrains (decide if you want to do this or not) and click the OK button.

  • asked to choose your default theme.

  • presented with a series of tools that can be installed. At the moment, we won’t be using them, so click on the button to Skip Remaining and Set Defaults.

IntelliJ shoud now startup.

Creating your first Java Program

Click Create New Project.

Something similar to this window should appear:

If your installation says that No SDK exists for the Project SDK (as ours does above), click on the New.. button and locate the version of Java you previously downloaded, for example:

Now that we have a valid Project SDK, click the Next button.

Make sure the Create project from template box is unchecked and click the Next button:

Enter the project name as HelloWorld and click Finish.

Close the Tip window when it appears.

You should now have an empty project:

Right click on the src folder and select New followed by Java Class.

When the dialog appears, enter Main as the class name. You now should have an empty class like this:

Add the following code to the empty Main class:

public static void main(String args[])
{
    System.out.println("Hello World!");
}

Our first project has now been created and it contains a Main.java file with Java code in it.

Running your first Java Program

This program, when run, will print Hello World! into the console window.

To run the program, click on the green triangle beside the main method. Then select Run ‘Main.main()’:

Hello World! should be printed to the console:

If you are not getting this result, you should flag this to your instructor who can help diagnose and resolve the issue.

ShopV1.0

  • This console based app will ask the user to enter details for one Product. The details of the product will be printed back to the user.

  • During this step, you may need to view this weeks lecture notes to remind you how to write classes.

  • In IntelliJ, create a new Java Project called ShopV1.0.

  • Within this project, create two new Classes; Product and Driver.

  • Your folder structure should look like this:

ShopV1.0 folder structure

Product Class

In the Product class, create four private instance fields:

  • productName of type String
  • productCode of type int
  • unitCost of type double
  • inCurrentProductLine of type boolean

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:

  • input of type Scanner (initialise this field using new Scanner(System.in))
  • product of type Product

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();
        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.

Write a main method that has the following code:

  public static void main(String[] args) {
        Driver c = new Driver();
        c.addProduct();
        c.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?

Exercise

This exercise will help you become more familiar with the Scanner class.

What are you being asked to do?

Write a single class app that uses Scanner to ask the user to type in the value of an angle: Input

If the angle is less than 90, print out acute angle, e.g.: Acute Angle Output

If it is exactly 90, print out right angle, e.g.: Right Angle Output

If it is more than 90 but less than 180, print out obtuse angle, e.g.:, Obtuse Angle Output

If it is exactly 180, print out straight angle, e.g.: Straight Angle Output

If it is otherwise, print out reflex angle, e.g.: Right Angle Output

How do I code that?

In IntelliJ, create a new Java Project called AngleProject.

Create a class called Angle.

Import the Scanner class; recall that this must be the first line in your file.

Add a main method to the class and in this method:

  • Create an object of the Scanner class.

  • Ask the user to enter an angle and store it (you can determine the variable type from the screen shots above).

  • Call the processAngle method (defined below) passing, as the parameter, the angle value that you just read in.

processAngle method

Create a private method, with a void return type and call it processAngle. It should accept one parameter of type int.

This method interrogates the parameter value and prints the type of angle it is to the screen (according to the screen shots above).

Run the App

Run the app; does all work as expected?

Challenge

This exercise will help you become more familiar with the Scanner class as well as working with a two class app.

This console based app will ask the user to enter their personal details. Their details will then be printed back to the console.

During this challenge, you may need to view this weeks lecture notes to remind you how to write classes. The ShopV1.0 solution will also come in handy here.

  • In IntelliJ, create a new Java Project called UserDetailsProject.

  • Within this project, create two new Classes; User and Driver.

User Class

In the User class, create four private instance fields:

  • private String name;

  • private String address;

  • private String dateOfBirth;

  • private double height;

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:

  • input of type Scanner (initialise this field using new Scanner(System.in))
  • user of type User

Write a method called addUser() that asks the user to input their details (name, address, date of birth, height). Use this data to create a new User object.

Write a method called printUser() that prints all the user details to the console. Hint: use the toString() method from the User class.

Write a main method that creates an object of Driver and invokes both addUser and printUser over the object.

Run the App

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

Sample Input

Sample Output