Recitation 1: Introduction to IntelliJ and Simple Data Definitions
Goals: The goals of this lab are to get familiar with our work environment: the IntelliJ IDE, the handin-server submission process, the basics of running a program in Java, and program testing framework.
The second part of the lab will focus on practicing data definitions and examples in Java.
1 Gradescope Accounts
You will use Gradescope to submit your homework. You should have received an invitation at the beginning of the semester to join; accept and join our class, you should be able to see the first assignment listed there.
2 Introduction
We start with designing data - designing classes of data that are connected to each other in a systematic way, showing that the Design recipe for Data Definitions can be used virually without change in a completely different language than we have used in the first part.
The programs we provide give you examples of the progressively more complex data (class) definitions, and illustrate the design of methods for these class hierarchies.
3 IntelliJ IDE
IntelliJ is an integrated (program) development environment used by many professional Java programmers (as well as programmers using other programming languages) developed by JetBrains. It’s one of the best out there, and they have designed it to maximize developer productivity by providing robust features that support various programming languages and frameworks, especially Java and Kotlin.
The environment provides an editor, allows you to organize your work into several files that together comprise a project, and has a compiler so you can run your programs.
Several projects can exist in one workspace. You can probably keep all the work until the end of the semester in one workspace, with one project for each programming problem or a recitation problem.
There are several steps in getting started:
Learn to install IntelliJ Ultimate linked to your SHU account.
Learn to set up your workspace and launch an IntelliJ project.
Learn to manage your files and save your work.
Learn how to edit your Java programs and run them, using our tester library.
Learn to set up your workspace.
Start by identifying where you are going to store all of your files for the course. Navigate to this directory. If you do not have one, create a folder to store all of your files. Anywhere to save your files is fine as long as the location path is remembered. A generic place would be somewhere such as your Desktop or your Documents directory. That would look something like this default on Windows.
C:\Users\(YOURUSERNAME)\Documents\Intro2ProgramDesign
Create a new workspace folder in your directory where you will keep all your Java files. A directory example would look like the following.
C:\Users\(YOURUSERNAME)\Documents\Intro2ProgramDesign\IntelliJWorkSpace
Note that C: is the drive that Windows boots to by default, and your drive may be identified by something else if your system is configured differently. The (YOURUSERNAME) would be your unique username that you are signed in as. Anywhere to save your workspace is fine as long as you can remember the location path.
Next, set up another folder inside of this same directory where you will keep all your Java library files. A continued example of this corresponding to the last example would look something like
C:\Users\(YOURUSERNAME)\Documents\Intro2ProgramDesign\IntelliJJars
We will refer to these two folders as IntelliJWorkspace and IntelliJJars. Make sure the two folders IntelliJWorkspace and IntelliJJars are subfolders of the same folder.
Apply for your free student version of IntelliJ Ultimate
After downloading the IntelliJ Ultimate installation application, begin the IntelliJ installation.
Check the box that asks if you want to update the PATH Variable (Restart Needed) for bin files to be able to be accessible from the kernel and click OK.
IntelliJ Setup
Right click the following two libraries and select "Save link as..." Save them to your IntelliJJars folder.
The libraries you will need are:
You will also need to download the latest JRE
Select Windows > x64 installer Download and install this.
Adjust the IntelliJ settings.
The Code style section explains how to configure IntelliJ to only use spaces instead of tabs, and how to set the editor to show you the line numbers for all lines in the code. Do that now.
The Project Template
Open IntelliJ and navigate to the directory when you are storing all your files.
If you have a fresh installation of IntelliJ click New Project in the upper right corner. If you had IntelliJ previously, in the File menu, select New then Java Project. In the window that appears, be sure to set this project’s location to be inside IntelliJWorkspace folder that we have created previously. Name the project 2123-template. The Language to be selected is Java. Select JDK, make sure you select the version of OpenJDK you installed above. Finally, select Create, as the other defaults should be fine. We will re-use this template for many future projects
Make the libraries available to the project template.
Go to the File menu and select Project Structure. Under the Project Settings section, click Project, and ensure that the SDK is that version of OpenJDK you installed. Under Language Level, choose 11 - local variable syntax for lambda parameters. Now, navigate to Libraries. To the right of the Project Settings column, click on the plus sign ’+’ for New Project Library and select Java. Navigate to the directory where the JAR files javalib.jar & tester.jar are located on your local file system. Select the JAR files that you want to add and click OK. Click Apply and then OK to close the dialog. This will make these JAR files known to IntelliJ, allowing you to use the classes and methods defined in these libraries!
Template Run Configuration
Go to the Run menu and select Edit Configurations. Click Add new run configuration, and select Application. Change the name to Template and click Store as project file. Under Build and run, click the drop-down menu and make sure that Java version from the OpenJDK you downloaded is selected. In the next box over, write tester.Main. (If this causes you to need to re-enter your configuration name, do so.) Click Apply and then OK to close the dialog.
Now, you can select File then choose New Project Setup then Save Project as Template. We are now finished with this one.
3.1 The First Project
We are now going to set up your first real project. Now that you have all that prepared, this is what it will look like for you to code a Java file for the forseeable future.
Once again, in the File menu, select New then Java Project. In the window that appears, you should now be able to scroll down to the bottom and select 2123-template. Name the project Shapes.
Add the Shapes.java file to your project.
Download the file Shapes.java to a temporary directory or the desktop.
Place that Shapes.java file in the blue src directory on the right-hand side, the project pane. You can drag and drop it into src within IntelliJ, or you can use File Explorer to move it under IntelliJWorkspace Shapes src. It does not matter. IntelliJ just has to be able to read a Shapes.java file inside of your project file.
After completion of these steps, Shapes.java should be included inside of your Recitation1 > src.
View the file Shapes.java.
In the Projects pane, under Shapes, there should be a blue src, and if you click on the dropdown arrow, it will reveal Shapes.java.
Double click on Shapes.java. The file should open in the main pane of IntelliJ.
You can now edit it in the usual way. Notice that the Outline pane lists all classes defined in this file as well as all fields and methods. It is almost as if someone was building our templates for us.
Ready to run
In the Run menu tab select Edit Configurations....
In the left column, beneath Application, you should see Template, the name of the configuration we began to set up.
Click on the Program Arguments entry box. In the Program Arguments text field enter ExamplesShapes which will be the name of your examples class for testing.
Later, when you define your own program, you will use the class name of your Examples... class instead of ExamplesShapes.
At the bottom of the Run/Debug Configurations window select Apply then OK.
To run this project, make sure Shapes.java is shown in the main pane (and is currently selected), then hit the green circle with the white triangle on the top left side of the main menu.
Interface? Implements?
As we fondly remember from CS1114, one of our favorite types of data is union data: enumerations, itemizations, etc. You’ll learn more about this in lecture on Wednesday, but to define union data in Java, we define that union as an interface and say each branch of the data implements the interface. Read the code in your shapes file: you’ll notice the IShape interface is defined at the top, and that each class implements it. In the examples section, the examples are given the type of the interface, and are created with the constructor of the specific class. Follow suit when defining your own examples.
Making examples
Add examples of shapes found in the following image (ignore the colors):
Simple Data Definitions
Problem 1
Here is a data definition in DrRacket:
;; to represent a person ;; A Person is (make-person String Number String) (define-struct person [name age gender]) (define tim (make-person "Tim" 23 "Male")) (define kate (make-person "Kate" 22 "Female")) (define rebecca (make-person "Rebecca" 31 "Non-binary"))
Draw the class diagram that represents this data.
Define the class Person that implements this data definition and the class ExamplesPerson that contains the examples defined above. You should do this in a new Person.java file. Right click the default package under Lab1 and select New > Class. Name it Person.
Run your program to make sure it works.
Data Definitions with Containment
Problem 2
Modify your data definitions so that for each person we also record the person’s address. For each person’s address we only record the city and the state; each of these should be its own field. Create an Address class to contain the address information, then modify the Person data definition to include an Address.
Draw the class diagram for this data definition
Define Java classes that represent this data definition.
Tim lives in Boston, MA, Kate lives in Warwick, RI, and Rebecca lives in Nashua, NH.
Make examples of these data and add two more people.
Data Definitions for Unions of Data
Problem 3
A deli menu includes soups, salads, and sandwiches. Every item has a name and a price (in cents - so we have whole numbers only).
For each soup and salad we note whether it is vegetarian or not.
Salads also specify the name of any dressing being used.
For a sandwich we note the kind of bread, and two fillings (e.g peanut butter and jelly; or ham and cheese). Assume that every sandwich will have two fillings, and ignore extras (mayo, mustard, tomatoes, lettuce, etc.)
Define classes to represent each of these kinds of menu items. Think carefully about what type each field of each class should be. Do you need to define any interfaces? Construct at least two examples each of soups, salads, and sandwiches.
Self-Referential Data
The HtDP book includes the data definition for Ancestor Trees:
;; An Ancestor Tree (AT) is one of ;; -- 'unknown ;; -- (make-tree Person AT AT) ;; A Person is defined as above
Convert this data definition into Java classes and interfaces. What options do you have for how to translate this into Java? Make examples of ancestor trees that in at least one branch cover at least three generations.