CISC 3142
Programming Paradigms in C++
Lab #2
Preliminaries (Java)

How to Develop and Submit your Labs

Make sure you read the sections in the above on how to interpret CodeLab string comparison feedback

This lab is to be implemented in Java. It reinforces those concepts in Lecture 2 — Preliminaries. These topics were chosen because:

Lab 2.1 — Local and Member Variable (Class) Scope (2 lines)

Overview

This is a relatively simple assignment; it is basically a repetition of what was presented in class regarding using the same name for the constructor parameter and the instance variable it is being used to initialize.

The Detail

Given the following class outline:
class Name {
	Name(String first, String last) {
		…		// you are to supply this code
	}

	private String first, last;
}
supply the missing code of the constructor.

Lab 2.2 — Instance Variable and Superclass Initializing (14 lines)

Overview

The Details

Assuming the existence of the following classes
class Person {
	Person(Name name, int age) {…}
	public String toString() {return name + " (" + age + ")";}

	private Name name;
	private int age;
}
class Name {
	Name(String first, String last) {…}
	public String toString() {return first + " " + last;}

	private String first, last;
}
class Date {
	Date(int year, int month, int dom) {…}
	public String toString() {return month + "/" + dom + "/" + year;}

	private int year, month, dom;
}
Implement a subclass of Person named Employee with the following state/behavior:
class Employee extends Person {
	Employee(Name name, int age, int id, Date hireDate) { /* for you to implement */ }
	Employee(String first, String last, int age, int id, int year, int month, int dom) { /* for you to implement */ }
	public String toString() { /* for you to implement */ }

	int id;
	Date hireDate;
}
i.e.,

Lab 2.3 — A Rational Number Class in Java (69 lines)

Overview

A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school.

Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner:

Normalization of a Rational

A Rational Class

Write a class named Rational that provides basic support for rational numbers:
class Rational {
	Rational(int num, int denom) {…}
	Rational(int num) {…}
	Rational() {…}			// default constructor
	Rational(Rational r) {…}	// copy constructor

	Rational add(Rational r) {…}
	Rational sub(Rational r) {…}
	Rational mul(Rational r) {…}
	Rational div(Rational r) {…}

	Rational addInPlace(Rational r) {…}
	Rational subInPlace(Rational r) {…}
	Rational mulInPlace(Rational r) {…}
	Rational divInPlace(Rational r) {…}

	Rational negate() {…}
	Rational inverse() {…}

	int getNumerator() {…}
	int getDenominator() {…}

	int compareTo(Rational r) {…}
	boolean equals(Rational r) {…}

	public String toString() {…}

	private static int gcd(int a, int b) {…}

	private int num, denom;
}
i.e.,

Notes

This exercise addresses many of the topics covered in the 'Preliminaries' lectures

Code Used in this Lab