Chapter 5.3 – Programming with Objects | Introduction to Programming Using Java

Chapter 5.3 – Programming with Objects | Introduction to Programming Using Java

 

5.3 Programming with Objects

 

There are several ways in which object-oriented concepts can be applied to the process of designing and writing programs. The broadest of these is object-oriented analysis and design which applies an object-oriented methodology to the earliest stages of program development, during which the overall design of a program is created.

Here, the idea is to identify things in the problem domain that can be modeled as objects. On another level, object-oriented programming encourages programmers to produce generalized software components that can be used in a wide variety of programming projects.

Of course, for the most part, you will experience “generalized software components” by using the standard classes that come along with Java. We begin this section by looking at some built-in classes that are used for creating objects. At the end of the section, we will get back to generalities.

 

Chapter 5.3 - Programming with Objects | Introduction to Programming Using Java

 

5.3.1 Some Built-in Classes

 

Although the focus of object-oriented programming is generally on the design and implementation of new classes, it’s important not to forget that the designers of Java have already provided a large number of reusable classes. Some of these classes are meant to be extended to produce new classes, while others can be used directly to create useful objects. A true mastery of Java requires familiarity with a large number of built-in classes—something that takes a lot of time and experience to develop.

In the next chapter, we will begin the study of Java’s GUI classes, and you will encounter other built-in classes throughout the remainder of this book. But let’s take a moment to look at a few built-in classes that you might find useful.

A string can be built up from smaller pieces using the + operator, but this is not very efficient. If str is a String and ch is a character, then executing the command “str = str + ch;” involves creating a whole new string that is a copy of str, with the value of ch appended onto the end.

Copying the string takes some time. Building up a long string letter by letter would require a surprising amount of processing. The class StringBuffer makes it possible to be efficient about building up a long string from a number of smaller pieces. To do this, you must make an object belonging to the StringBuffer class. For example:

 

Chapter 5.3 - Programming with Objects | Introduction to Programming Using Java

 

(This statement both declares the variable buffer and initializes it to refer to a newly created StringBuffer object. Combining declaration with initialization was covered in Subsection 4.7.1 and works for objects just as it does for primitive types.)

Like a String, a StringBuffer contains a sequence of characters. However, it is possible to add new characters onto the end of a StringBuffer without making a copy of the data that it already contains. If x is a value of any type and buffer is the variable defined above, then the command buffer.append(x) will add x, converted into a string representation, onto the end of the data that was already in the buffer. This command actually modifies the buffer, rather than making a copy, and that can be done efficiently.

A long string can be built up in a StringBuffer using a sequence of append() commands. When the string is complete, the function buffer.toString() will return a copy of the string in the buffer as an ordinary value of type String. The StringBuffer class is in the standard package java.lang, so you can use its simple name without importing it.

A number of useful classes are collected in the package java.util. For example, this package contains classes for working with collections of objects. We will study these collection classes in Chapter 10. Another class in this package, java.util.Date, is used to represent times. When a Date object is constructed without parameters, the result represents the current date and time, so an easy way to display this information is:

 

Chapter 5.3 - Programming with Objects | Introduction to Programming Using Java

 

Of course, to use the Date class in this way, you must make it available by importing it with one of the statements “import java.util.Date;” or “import java.util.*;” at the beginning of your program. (See Subsection 4.5.3 for a discussion of packages and import.)

I will also mention the class java.util.Random. An object belonging to this class is a source of random numbers (or, more precisely pseudorandom numbers). The standard function Math.random() uses one of these objects behind the scenes to generate its random numbers. An object of type Random can generate random integers, as well as random real numbers. If randGen is created with the command:

Random randGen = new Random(); and if N is a positive integer, then randGen.nextInt(N) generates a random integer in the range from 0 to N-1. For example, this makes it a little easier to roll a pair of dice. Instead of saying “die1 = (int)(6*Math.random())+1;”, one can say “die1 = randGen.nextInt(6)+1;”. (Since you also have to import the class java.util.Random and create the Random object, you might not agree that it is actually easier.) An object of type Random can also be used to generate so-called Gaussian distributed random real numbers.

The main point here, again, is that many problems have already been solved, and the solutions are available in Java’s standard classes. If you are faced with a task that looks like it should be fairly common, it might be worth looking through a Java reference to see whether someone has already written a class that you can use.

 

programming with objects

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5 – Objects and Classes | Introduction to Programming Using Java

Chapter 5.1.1 – Objects, Classes and Instances | Introduction to Programming Using Java

Chapter 5.1.2 – Fundamentals of Objects | Introduction to Programming Using Java

Chapter 5.1.3 – Getters and Setters | Introduction to Programming Using Java

Chapter 5.2 – Constructors and Object Initialization | Introduction to Programming Using Java

Chapter 5.2.2 – Constructors | Introduction to Programming Using Java

1 thought on “Chapter 5.3 – Programming with Objects | Introduction to Programming Using Java”

Leave a Comment