Object Oriented Programming in Java

Module [M01] - Java Objects & Classes

This module will explore object oriented concepts (e.g. objects and class). If you covered these already in the pre-requisites, consider it a review. If not, this material will get you up to speed for this course.

  • Textbook Reading Assignments
  • The following sections of the textbook are assigned for this module.
    • Chapter #9 – Objects & Classes

Module Learning Outcomes

In this module, you will develop further your programming skills as follows;

Tracing
Ability to read and trace Java programs leveraging classes definitions and objects.
Implementing
Ability to write Java programs leveraging classes definitions and objects.
Testing
Ability to develop tests for Java programs leveraging classes definitions and objects.
Debugging
Ability to troubleshoot Java programs leveraging classes definitions and objects.
Designing
Ability to design Java programs leveraging classes definitions and objects.

As they are developed during the first offering of this course in Fall 2017, video notes will be posted here to supplement the reading assignments and help you prepare for your homework exercises.

Keep in mind that you should watch these videos only after you worked through the reading assignments. These videos are meant to help you improve your understanding of the assigned study material and focus on specific aspects that are known to be most difficult to some students. However, they assume that you have already worked through the reading assignments and posted questions on the forum to ensure you achieved at least a basic understanding of the material before to watch the videos.

  • Why OOP?
  • Video Link: YouTube
  • We briefly discuss why one would want to supplement the type of programming you learned in the pre-requisite to this course with object oriented techniques. We then jump into a code example to illustrate how we may use classes and objects in comparison to what we have been doing in the pre-requisite when we did not have access to these tools.
  • References vs. Objects
  • Video Link: YouTube
  • It is not uncommon for students to be confused by the difference between objects and their references. Does it not make sense to say that a variable contains an object? Why is the instructor always saying that the variable holds a reference to the object? Why does it matter? Watch this video to find out :)

Please note that, while we are going to be using the JGrasp IDE for most videos in this course, the following video uses a different development environment; Konqueror and command line Java compiler on a Linux server.

While the IDE will look differently and we will type commands to compiler and run our programs, the code itself is exactly as you would type it in JGrasp. So focus on how the problem at hand is solved and ignore the aesthetic details. The solution will work as-is in JGrasp.

  • Apprenticeship Exercise - Rectangle
  • Video Link: YouTube
  • This video uses the cognitive apprenticeship technique to show you how to leverage what you learned in the reading assignments & previous videos in order to work, from start to end, one of the practice exercises in our textbook. In this specific video, we are going to work on the Rectangle class that appears in exercise #9.1 in the 10th edition of our text and #7.1 in older editions.
  • B01 - Stock
  • This exercise is #9.2 in the latest edition of our text.
    Write a new class named Stock featuring;
    • A String data field named symbol which represents the stock symbol for this object
    • A String data field named name which holds the full name for this stock
    • A double data field named previousClosingPrice which stores the stock price for the previous day
    • A double data field named currentPrice which stores the current stock price
    • A constructor that creates a stock object, given a symbol and name
    • Accessor and Mutator methods for the fields previousClosingPrice, currentPrice
    • A method getChangePercent() which returns the percentage difference between previous closing price and current one
    In addition, write a new class named TestingStock with a main method which;
    • creates a Stock object with symbol ORCL, full name Oracle Corporation
    • sets the closing price of 34.5
    • sets the current price for this object to 30.35
    • displays the difference percentage
  • B02 - Account
  • This exercise is #9.7 in the latest edition of our text.
    Write a new class named Account featuring;
    • A private in data field named id with default value 0
    • A private double data field named balance
    • A private double data field named annualInterestRate with default value 0. We assume that all objects of the class Account will have the same interest rate
    • A private Date data field named dateCreated which stores the Date object representing when this account was first opened. The constructors will make sure to initialize this field with the current Date when the object is created
    • A constructor without arguments that creates a default account object
    • A constructor which creates an account with a given id and initial balance
    • Accessor and Mutator methods for the fields id, balance, annualInterestRate
    • Accessor methods for dateCreated
    • A method named withdraw which withdraw a specified amount form the account
    • A method named deposit which deposites a specified amount into the account
    • A method named getMonthlyInterestRAte() which computes and returns the monthly interest rate which is computed as the annualInterestRate / 12
    • A method named getMonthlyInterest() which computes and returns the monthly interests which is computed as balance * the monthly interest rate obtained by the previous method
    In addition, write a new class named TestingAccount with a main method which;
    • Creates a new account object with an id of 123, an initial balance $500, and an annual interest rate of 4.5%
    • Withdraw $200 from the above account, then display the new balance
    • Deposit $50 into the above account, then display the new balance
    • Display the monthly interests for the above account and the date it was opened
  • B03 - Fan
  • This exercise is #9.8 in the latest edition of our text.
    Write a new class named Fan featuring;
    • Three constants named SLOW, MEDIUM and FAST with respective values 1,2 and 3
    • A private int data field named speed with default value SLOW
    • A private boolean data field named on with default value false
    • A private double data field named radius with default value 5
    • A private string data field named color with default value "blue"
    • Accessor and Mutator methods for all four data fields
    • A constructor without arguments that creates a default Fan object
    • A method named toString which returns a string displaying the fan, including information about all of its fields
    In addition, write a new class named TestingFan with a main method which;
    • Create a new Fan object with default attributes, then display its description
    • Create a new Fan object with specific attributes, then display its description