Programming Fundamentals in Java

Module [305] - Java Objects & Classes

This module will explore object oriented concepts (e.g. objects and class) which will retroactively explain some of the syntax you have been manipulating all along. As an added benefit, this material will help prepare your transition to COP2330 Object Oriented Programming for IT.

  • Textbook Reading Assignments
  • The following sections of the textbook are assigned for this module.
    • Chapter #9 – Objects & Classes
  • 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. As is usual for longer videos, here are links to the main parts;
  • 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 :)

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.

  • A01 - Rectangle
  • Video Link: YouTube
  • This exercise is #9.1 in the 11th & 10th editions of our text; was #7.1 in older editions of our text.
    Write a new class Rectangle featuring;
    • Two double data fields named width and height with default values of 1 for both
    • A constructor with no arguments which creates a default rectangle
    • A constructore with two arguments allowing us to create rectangle of a given width and height
    • A method getArea() which computes and returns the area of the rectangle
    • A method getPerimeter() which computes and returns the perimeter of the rectanble
    In addition, write a new class named TestingRectangle with a main method which;
    • creates a Rectangle object of width 4 and height 40
    • creates another Rectangle object of width 3.5 and height 35.9
    • displays the width, height, area and perimeter of both rectangles

IMPORTANT NOTE:

  • Some of the homework exercises below are marked as [optionals]; You do not have to complete these in order to get full participation credit for the HW assignment corresponding to this module.
  • More specifically, when the TAs select the exercise to upload, they will NOT select any of the optional ones. However, all non-optional exercises are fair game so you should complete them.

Please also note that the textbook exercises also ask you to draw UML diagrams for each of the classes below. Our goal, in this first programming course is to introduce you to the UML notation but we are not going to go much beyond that. As a result, you may draw these UML diagram on paper or using software, but you will not have to upload them as part of your homework.

This being said, post any questions about UML you might have in the module forum and the TAs and I will be glad to help you get a good grasp on why this notation is useful.

  • B01 - Stock [OPTIONAL]
  • This exercise is #9.2 in the 11th & 10th editions 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 11th & 10th editions 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 11th & 10th editions 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

Each of the following links will lead you to a separate tutor applet. The tutors take usually 30 minutes to work through.

  • Problet #1 - Classes - Access Concepts
  • URL Link: See Instructor Announcements
  • This Problet tutor will allow you to understand how to use Java methods and attributes inside of your own classes