CS 1063  Week 2:  An Introduction to Objects and Classes  

Use and Construct Objects

Objectives

Assignments


Activity 1  More println examples

Part 1:  Load an existing program Part 2:  Add the following statements to the HelloWorld class Part 3:  Escape Sequences:  Add the highlighted lines to your HelloWorld class and run it.

Activity 2  Use the println method to draw text pictures

Setup: Write a program that displays your name inside a box on the console screen, like this:
+----------+
| Bylander |
+----------+
Add code to print this staircase:
    +---+
    |   |
+---+---+
|   |   |
+---+---+

Activity 3 Lab 1  The FaceMain class

The Rectangle Class in the java.awt package

A public interface is a list of methods you can use.  The implementation or code is hidden.  The public interface for the Rectangle class is given below.  Some methods are missing.
Constructor: Methods:

Activity 4: Constructing Rectangle Objects

Setup:

Constructing Objects

Objects are entities in your program that you manipulate by invoking methods.  To construct any object, you do the following:

  1. Use the new operator.
  2. Give the name of the class.
  3. Supply construction parameters(if any) inside parentheses.

Construct a Rectangle object with top left corner at (5,10), width 20, and height 30.  Add the following line to your main and run the program.  You should see java.awt.Rectangle[x=5,y=10,width=20,height=30]

System.out.println( new Rectangle(5, 10, 20, 30) );
System.out.println();  // Prints a blank line

Variable Names

Usually you want to do something more to an object than just create it, print it and forget it.  To remember an object, you need to hold it in an object variable.  A variable is an item of information in memory whose location is identified by a symbolic name.  You can choose any variable names you like, provided you follow a few simple rules.

An object variable is a container that stores the location of an object.  All object variables must be initialized before you access them.  Construct a Rectangle object named room whose top left corner is at (10,20), width 30 and height 40.  Add the following code to your program and run.

// Declare a variable named room
Rectangle room;
// Initialize room
room = new Rectangle(10,20,30,40);
// Print room and a double space
System.out.println("room = " + room + "\n");

This is a picture of what is happening in memory after the assignment statement is executed.  Locate the object reference and the object.

     Object      Object 
    Variable    Reference         Object

   +---------+               +----------------+
   |         |               |   Rectangle    |
   | room = ---------------> |                |
   |         |               |       x = 10   |
   +---------+               |       y = 20   |
                             |   width = 30   |
                             |  height = 40   |
                             |                |
                             +----------------+

Vocabulary

Test all of the other methods listed in the public interface.


Use WebCT to take your first Quiz

All 10 quizzes will be available on Thursday, Friday, and Saturday of each week.  (Always check your calendar.)  That means you will have 3 days to complete the quiz.  The first quiz will be taken together in class but from then on the student is responsible for taking the quiz on their own.

WebCt can be entered using the following link: http://webct.utsa.edu