Classes and objects in Java

Objective

This lab aims to have you review some basic Java concepts, including classes and objects. The lab will also allow you to (re-) familiarize yourself with the Eclipse software development environment. You will do this by answering some questions about a provided Java application, and by making a small modification to it.

Submission

Your lab must be submitted by email to This email address is being protected from spambots. You need JavaScript enabled to view it. not later than 14h30 on January 25th, 2017.

You must email a zip file of your lab report in PDF format and the source code for Part 2 before starting the next lab. In Microsoft Word, you can create a PDF file with the menu File> Save as ....

The report should include your answers to the questions below with a discussion of what you have learned, what discoveries you have made, what has been difficult in the lab, and so on. Be concise!

Introduction

An object is an intance of an abstract data type. In addition to storing the data (attributes), the object also has a set of operations (methods) that it can perform. An object is therefore a data structure with a part devoted to data and one to execution.

A Java program is a collection of objects that accomplish a certain task by exchanging messages between them. An object that transmits a message to another object is implemented as an object that calls the execution of a method from another object. The objects therefore have a set of messages (or methods) to which they respond, the set of messages to which an object responds is the interface for that object.

Each object has its own memory composed of other primitive objects or types. Each object has one or more types. All objects of a particular type can receive the same messages (because they all share the same interface).

The class of an object sets one of these types. All variables and methods are specified in the class definition. Thus a class indicates what kind of data and which methods form the interface for all the objects of a certain type. Once a class has been defined, objects can be instantiated or created from the class definition. An object is an instance of an abstract data type (a class).

An object can be considered as an instance of any of its superclasses, or the Java interfaces it implements, or interfaces implemented by these superclasses.

To answer questions in this lab, you can refer to any Java reference manual (or online search engine). However, be sure to clearly indicate your sources when referring to different books in your answers.

Preparation

Before you begin, have a quick look through the Eclipse survival guide.

Download and save the file wallball.zip. Do not extract the archive.

Launch Eclipse. Create a new Java Project and give it an appropriate name (for example, EEE320 Lab 1). In the New Java Project dialog box, you should choose to use the default JRE and to create separate folders for source and class files.

Select your project in the Package Explorer, right click and choose Import from the context menu. In the dialog box that appears, choose General > Archive File as the type. Browse to your saved copy of wallball.zip and import the file.

Open the project and the src folder by clicking on the expansion cross if necessary. The following three packages and eight Java source files should be listed inside the src folder:

wallballgame
  Ball.java
  Paddle.java
  WallBallEquipment.java
wallballgui
  WallballApp.java
  WallballFrame.java
  WallballView.java
wallballsupport
  Collidable.java
  Path.java

The program provides a very simple game called Wall Ball (it’s like Pong for one player). The main class is WallballApp. To run the game, select WallballApp.java and then click the run icon in the toolbar (the green circle with a white triangle inside of it). Play with the game and see how it works.

Part 1: Understand the Program

Answer the following questions. Feel free to refer to the Java 8 API Specification and The Java Tutorials where these would be helpful.

Java Classes, Objects and Object Creation

1. Provide an example of a class in the Wallball project.

2. Provide an example of an object in Wallball.

3. Provide an example of a constructor in Wallball.

4. In the class WallballView, the value BORDER is an attribute. Explain what this means. Is BORDER an object? Why or why not?

5. In the class WallballView, the value thePaddle is an attribute. Is thePaddle an object? Explain.

Object Creation and Initialization

We now look in more detail at object initialization and specifically at the initialization of the object references (the assignment of storage for actual objects and the binding of the reference to the new object). You can initialize a reference:

  • At the point the variable containing the reference is declared.
  • In the constructor of the containing object's class.
  • Right before you actually need to use the object. This can reduce overhead, if there are situations where the object doesn't need to be created.

6. In the class WallBallEquipment, provide an example of each kind of object initialization listed above.

7. What would be the effect of using Integer instead of int to specify the type of the attributes width and height in WallBallEquipment? What is the difference between an Integer and an int?

Classes and Interfaces

The package wallballgame contains three classes. Two of the classes, Ball and Paddle, extend the class WallBallEquipment. The class WallBallEquipment implements the interface Collidable from the wallballsupport package.

8. In your own words, explain the difference between a class and an interface in the Java programming language.

9. What does it mean for a class to extend another class?

10. What does it mean for a class to implement an interface?

11. The constructors for Ball and Paddle each contain a line of code similar to super(fgColor, bgColor, x, y); What does this line of code do and why is it necessary?

12. WallBallEquipment has two constructors. One contains the line of code this(fgColor, bgColor); What is the this keyword in Java, and how is it being used in this constructor?

Method Invocation

13. Explain, in your own words, how the following method from the class WallballView determines whether the Paddle has hit the Ball. Hint consider what objects are returned from each of the method calls in the if statement and trace the full flow of method calls.

private void checkHit(Collidable theBall, Collidable thePaddle) {
  if (thePaddle.getBoundingRectangle()
      .intersects(theBall.getBoundingRectangle())) {
    theBall.getPath().reverseHorizontal();
    } 
}

Program Design

14. The code in the WallBall project contains quite a few magic numbers (i.e., “unnamed numerical constants”). Explain in your own words what magic numbers are and why they should be avoided in your code.

15. Which of the principles discussed in the opening lecture of this course recommends the elimination of magic numbers in your code?

16. Research the Single Responsibility Principle and briefly summarize it in your own words.

17. Does the class WallballView conform to or violate the Single Responsibility Principle? Justify your opinion by making specific reference to parts of the code in WallballView that support your argument.

Part 2: Modify the Program

You may have noticed that the Paddle Size menu of the game does nothing. Modify the program to correct this problem. You can study the code that changes the color of the racket to find clues for the solution. The change will require adding about a dozen lines of code to three classes (depending on your solution).

When you get to change the size of the racket correctly. If the size change occurs near the border of the window, the racket may not be able to move. There is no need to address this issue.