Relevant Reading Material
Shift your thinking
In 5001 we had data and function that operate on this data. Typically we would
-
design our data and create examples (instances)
-
design functions that take this data as inputs and return outputs
Data and functions in Racket are 2 independent entities. The dependency between which data is manipulated by which function(s) is defined by the signatures of our functions.
With Object Orientation data and functions (called methods) are packaged together into a class. So a class has some data inside of it and some methods. These methods have access to these data. The dependency between which data is manipulated by which method is defined in 2 parts, the signatures of the methods and the location of the methods.
Examples are instance of classes (called objects) and these objects hold inside of them data and methods. We thus talk about the methods that an object understands (or has available). Computation is then composed by creating objects and asking them to evaluate their methods.
Values in Java
A value can be
-
Given as an argument.
-
Returned as a result of a method call.
-
Stored
In Racket we had Number
String
Symbol
Images
etc.
A value in Java is associated with a type. For now you can think of a type as a category that indicates the operations that the value understands.
Java has 2 kinds of types
-
Primitive types (start with a lower case letter)
-
e.g.,
int
,bool
,float
…
-
-
Reference types (start with a capital case letter)
-
e.g.,
Object
,LinkedList
,String
,Integer
…
-
From define-struct
to class
In Java, code is organized in Classes.
In Racket, we were able to create new values made up of a finite set of slots. The slots could then store other values.
(define-struct person (first second age))
;; A Person is (make-person String String PosInt)
;; INTERP: represents a person with their first, and second name
;; and their age
Informally, we can draw a parallel to Java’s classes.
public class Person { (1)
private String first; (2)
private String second;
private Integer age;
}
1 | We define a class using the keyword class . The keyword public is called a modifier and modifies the visibility of this class. Visibility here refers to the ability of code in other classes/files to "see" (use/refer to) this class |
2 | Slots from Racket are called fields in Java. In Java however we must specify the type and the name of the field. Informally the type maps to the Data Definition names that we used in Racket struct Data Definitions. The modifier private designates that this field is private to this class and can be accessed only from code that is within this class definition. |
Constructors and selectors
In Racket a define-struct
generated a set of functions that we used to create, access and test a value created from this structure.
;;;; CONSTRUCTOR
;;; make-person: String String PosInt -> Person
;;;; SELECTORS
;;; person-first : Person -> String
;;; person-second : Person -> String
;;; person-age : Person -> PosInt
;;;; Predicate
;;; person? : Any -> Boolean
In Java we need to code these methods. Java programmers also use different names for these methods here is the mapping
Racket | Java |
---|---|
Constructor |
Constructor |
Selector |
Getter |
Here is the updated Person
class with a constructor and getters
for each field. We will look at each new element in turn.
public class Person {
private String first;
private String second;
private Integer age;
/**
* Instantiates a new person given its first and second name and
* their age.
*
* @param first the first name of the person
* @param second the second name of the person
* @param age the age of the person
*/
Person(String first, String second, Integer age){
this.first = first;
this.second = second;
this.age = age;
}
/**
* Gets the person's first name.
*
* @return the first name of the person
*/
public String getFirst() {
return first;
}
/**
* Gets the person's second name.
*
* @return the second name of the person
*/
public String getSecond() {
return second;
}
/**
* Gets the person's age.
*
* @return the age of the person.
*/
public Integer getAge() {
return age;
}
}
Constructor
public class Person {
private String first;
private String second;
private Integer age;
/** (1)
* Instantiates a new person given its first and second name and
* their age.
*
* @param first the first name of the person (2)
* @param second the second name of the person
* @param age the age of the person
*/
Person(String first, String second, Integer age){ (3)
this.first = first; (4)
this.second = second;
this.age = age;
}
1 | A multiline comment in Java starts with /* and ends with */ . This is equivalent to Racket’s #| and |# . Java comments can be free from text but Java comments can also deal with HTML. The Javadoc tool can be used to read all comments in your source code and generate documentation. A single line comment in Java starts with // , equiavelent to Racket’s ; |
2 | Javadoc allows you to provide documentation for the arguments using the @param annotation, followed by the argument’s name and then a sentence describing what this argument is for. |
3 | This line starts the definition of the constructor method. Constructor methods are special methods. The name of a constructor method must be the same name as the class name, i.e., Person . Arguments are then provided between normal parenthesis separated by a comma. For each argument we need to specify its type a space and a name, i.e., String first . |
4 | The body of the constructor method is enclosed in curly parenthesis. While writing the body of the constructor method, if we want to refer to the fields of the class we must use the special keyword this . While inside the file of a class, if we want to access a field of the enclosing class we use this then a dot `. and then the name of the field. |
Getter
/**
* Gets the person's first name.
*
* @return the first name of the person (1)
*/
public String getFirst() { (2)
return first; (3)
}
1 | Javadoc provides the annotation @return that allows us to annotate and comment on the method’s return value. |
2 | For a normal method, the signature consits of a modifier public , the type of the value to be returned by the method String , the name of the method getFirst then the list of arguments enclosed in normal parenthesis. In this case there are no arguments. The modifier public here indicates that this method is available to be called from inside the class but also from other classes. |
3 | In the method body if we want to return a value back to the caller we must use the keyword return followed by the value to return or an expression that will be first evaluated to obtain a value and then return that result. |
Missing sections. |