Lab 4 2020: I Can Relate to This: Relations.

Download this File (includes instructions below)

Introduction

Relations are a very general mathematical concept. A relation maps together multiple values. Every coherent set of values is called a tuple. For example, here we consider some values related to students data. The tuple domain is String*int*int, representing the name of a specific student, the student id and its mark for a course. To encode tuples in Java, we define a class StudentData, with fields and constructor initializing the fields.

public StudentData(String name, int id, int mark) {
  this.name = name;
  this.id = id;
  this.mark = mark;
}

Not all string*int*int tuples are valid elements of our relation. To be included in our relation there are constraints:

//  Constraints for Part 1:
public String name;// not null, not empty string
public int id;// bigger than zero
public int mark;// a number between 0 and 100

In mathematics, a relation is just "composed" by tuples, but in our java encoding we have to choose a way to store our tuples together. We will use ArrayList to store our tuples. Then, to manipulate our relation, we will have many method that accept ArrayList and return another ArrayList, with different content. For example, consider the following code:

public static ArrayList< StudentData>
 selectIncorrectData(ArrayList< StudentData> data) {
  ArrayList< StudentData> result = new ArrayList< StudentData>();
  for (StudentData tuple : data) {
    if (!isCorrectTuple(tuple)) {
      result.add(tuple);
    }
  }
  return result;
}

Can you understand the code above?

  1. What is the role of the local variable result?
  2. Why is a new ArrayList() created?
  3. What is the role of the "for"
  4. What do you think about the "if"? is it a good idea to delegate the condition to another method?
  5. Try to memorize this pattern: "create result - fill result - return result".

Part 0

Check the system is working, this should print some data. Run this code using the IDE of your choice, for example BlueJ. The method asTable turns ArrayList into a nice printable string. Do not worry about its implementation. To complete this lab you have to follow the instructions below, uncommenting the relevant chunks of code(the ones not starting with ##).

public static void main(String[] arg) {
  System.out.println("##PRINT THE DATA##");
  System.out.println(asTable(data1()));
  ...

Part 1 (Core, 20%)

Method selectIncorrectData calls isCorrectTuple, but its code is not complete. Complete the code of isCorrectTuple (directly after the main method). After you complete this task, the next line of code should print 5 entries, for: "", Wrong Rogers, Silly Mark, Negative Sasha and Max POWER. See near the class fields for the exact constraints.

System.out.println("##Part1: Incorrect data##");
System.out.println(asTable(selectIncorrectData(data1())));

Part 2 (Core, 20%)

To get the correct data, you could imagine to take all the data and just remove the incorrect one, as in:

ArrayList< StudentData> correctData = data1();
correctData.removeAll(selectIncorrectData(data1()));

However, it would be nicer to also be able to select correct data directly without accumulating and removing it as above. To complete part two, complete the method selectCorrectData. After you complete this task, the next lines of code should print 8 tuples:

System.out.println("##Part2: Correct data##");
ArrayList< StudentData> correctData = selectCorrectData(data1());
System.out.println(asTable(correctData));

Part 3 (Completion, 20%)

Selections:one typical operation on relations is selection. One obvious selection that can be done is selecting the students that passed and the students that failed the course. To complete part 3, complete the methods selectPass and selectFail. While you complete part 3, think about similarity and difference of selectPass and selectFail with respect to selectIncorrectData and selectCorrectData.

System.out.println("##Part3: Select Pass##");
System.out.println(asTable(selectPass(correctData)));
System.out.println("##Part3: Select Fail##");
System.out.println(asTable(selectFail(correctData)));

Part 4 (Completion, 20%)

Note how the id unequivocally identify a specific tuple. Out of this intuition, we can create selectId, a function returning a specific StudentData tuple out of our relation. We say that our relation is a functional relation with respect to id. To complete part 4, complete the method selectId.

System.out.println("##Part4: Select Individual students##");
System.out.println(selectId(1042, correctData).name);// prints Douglas Adams
System.out.println(selectId(1043, correctData).name);// prints Marco Rossi

Part 5 (Challenge, 20%)

We want now to select the student(s) with maximum mark. This is not simple, since we first need to find the highest mark and then select the students with such mark.

System.out.println("##Part5: Select Highest Mark Students##");
System.out.println(asTable(selectHighest(correctData)));
Topic attachments
I Attachment Action Size Date Who Comment
StudentData.javajava StudentData.java manage 9 K 18 Sep 2017 - 14:53 Main.lukefaho