8.13
contents← prevupnext →
Recitation 1: Introduction to IntelliJ and Simple Data Definitions🔗
Goals: The goals of this lab are to get familiar with our workenvironment: the IntelliJ IDE, the handin-server submission process, the basicsof 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.
Related files:
tester.jar javalib.jar Shapes.java
1Gradescope Accounts🔗
You will useGradescope to submit yourhomework. You should have received an invitation at the beginning ofthe semester to join; accept and join our class, you should be able tosee the first assignment listed there.
2Introduction🔗
We start with designing data - designing classes of data that areconnected to each other in a systematic way, showing that theDesign recipe for Data Definitions can be used virually withoutchange in a completely different language than we have used in the first part.
The programs we provide give you examples of the progressivelymore complex data (class) definitions, and illustratethe design of methods for these class hierarchies.
3IntelliJ IDE🔗
IntelliJ is an integrated (program)development environment used by many professional Java programmers (as well as programmersusing other programming languages) developed by JetBrains. It’s one of the best out there, and theyhave designed it to maximize developer productivity by providing robust features thatsupport various programming languages and frameworks, especially Java and Kotlin.
The environment provides an editor, allows you to organize your work into severalfiles 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 theend of the semester in one workspace, with one project for each programming problemor 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 yourfiles. 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 allyour Java files. A directory example would look like the following.
See Also601.229 (F24): Assignment 1: Big IntegersProject #2 - Extendible Hash Index - CMU 15-445/645 :: Intro to Database Systems (Spring 2024)Lab 5: Dplyr, Pipes, and MoreLab 4: Purrr and a Bit of DplyrC:\Users\(YOURUSERNAME)\Documents\Intro2ProgramDesign\IntelliJWorkSpace
Note that C: is the drive that Windows boots to by default, and your drive may be identifiedby something else if your system is configured differently. The (YOURUSERNAME) would be yourunique username that you are signed in as. Anywhere to save your workspace is fine as long asyou can remember the location path.
Next, set up another folder inside of this same directory where you will keepall your Java library files. A continued example of this corresponding to the last examplewould 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 subfoldersof 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 yourIntelliJJars folder.
The libraries you will need are:
Related files:
tester.jar javalib.jarYou 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 ofIntelliJ 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 beinside IntelliJWorkspace folder that we have created previously. Name the project 2123-template. The Languageto be selected is Java. Select JDK, make sure you select the version of OpenJDK you installed above. Finally, select Create, as the otherdefaults 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 Libraryand select Java. Navigate to the directory where the JAR files javalib.jar & tester.jarare 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 theclasses 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.1The First Project🔗
We are now going to set up your first real project. Now that youhave all that prepared, this is what it will look like for you tocode a Java file for the forseeable future.
Once again, in the File menu, select New thenJava Project. In the window that appears, you should now beable to scroll down to the bottom and select 2123-template.Name the project Shapes.
Add the Shapes.java file to your project.
Related files:
Shapes.javaDownload 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. IntelliJjust 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 classesdefined in this file as well as all fields and methods. It is almost as if someone wasbuilding 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 fieldenter 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 shownin the main pane (and is currently selected), then hit the green circle with the white triangleon 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 datain Java, we define that union as an interface and say each branch of the dataimplements the interface. Read the code in your shapes file: you’ll notice the IShape interfaceis defined at the top, and that each class implements it. In the examples section, the examplesare 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:
;;torepresentaperson ;;APersonis(make-personStringNumberString) (define-structperson[nameagegender]) (definetim(make-person"Tim"23"Male")) (definekate(make-person"Kate"22"Female")) (definerebecca(make-person"Rebecca"31"Non-binary"))
Draw the class diagram that represents this data.
Define the class Person that implements this datadefinition and the class ExamplesPerson that contains theexamples defined above. You should do this in a new Person.java file.Right click the default package under Lab1 and select New > Class. Name itPerson.
Run your program to make sure it works.
Data Definitions with Containment🔗
Problem 2
Modify your data definitions so that for each person we alsorecord the person’s address. For each person’s address we onlyrecord the city and the state; each of these should be its own field.Create an Address class to contain the address information, thenmodify 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 typeeach 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:
;;AnAncestorTree(AT)isoneof ;;--'unknown ;;--(make-treePersonATAT) ;;APersonisdefinedasabove
Convert this data definition into Java classes and interfaces. What options doyou have for how to translate this into Java? Make examples of ancestor treesthat in at least one branch cover at least three generations.
contents← prevupnext →