On this page:
1 Gradescope Accounts
2 Introduction
3 Intelli  J IDE
Learn to set up your workspace.
Intelli  J Setup
The Project Template
3.1 The First Project
Ready to run
Interface? Implements?
Making examples
Simple Data Definitions
Data Definitions with Containment
Data Definitions for Unions of Data
Self-Referential Data
8.13

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.

Related files:
  tester.jar     javalib.jar     Shapes.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 set up your workspace.🔗
IntelliJ Setup🔗
The Project Template🔗
3.1 The First Project🔗
Ready to run🔗
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.

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.