Unformatted text preview:

COMP 110/401*Java Objects vs. Real-World ObjectsA Simple ClassInteractive Class Instantiation and Method InvocationAnatomy of a ClassProgram DevelopmentInteracting with ABMICalculatorFormal vs. Actual Parameters, Variables, Positional CorrespondenceErrorsCase MattersCase ConventionsStyle PrinciplesMain and Procedural Class Instantiation and Method Invocation*Packages*Starting ObjectEditor ProgrammaticallySummaryExercisesCOMP 110/401*Prasun Dewan1 2. ObjectsNow that we have a model of how the computer works, we can address the business-at-hand: how dowe program the computer. Using two simple, though realistic, examples, this chapter will explain someof the basic elements of a Java program. It will introduce the concept of style in programming andidentify some of the basic errors to guard against. It will also outline the process of running a program,that is, converting static program text into active code. After studying this chapter, you will be able towrite a program and interact with it.This chapter is meant for both Comp 110 and Comp 401 students. The names of sections that comp 110students can ignore have a * next to them.Java Objects vs. Real-World ObjectsRecall that one of the strengths of Java is that it allows a program to be composed of smaller structuresmuch as a script can be broken up into smaller units such as sections, paragraphs, and sentences, or abuilding can be broken up into rooms, doors, walls, windows, etc. The units of a building are physicalobjects, while the units of a script are abstract. Like the latter, the units of a program are also abstract. Infact, part of the challenge of programming will be to understand these abstractions. To makeprogramming more intuitive (and powerful), Java and other object-based programming languagesprovide abstractions, called objects, which are modeled after physical objects. Coding in Java consistsmainly2 of defining and interacting with these program objects.Since program objects are created by human beings, they are more like manufactured physical objects,such as cars and bicycles, rather than natural objects such as trees and rocks. We interact with a(manufactured) physical object by performing different kinds of operations on it. For instance, weaccelerate, brake, and steer a car (Figure 1 top). The set of operations we can perform on the car isdetermined by the factory that manufactured or defined it. Defining a new kind of car, thus, involvesconstructing a new factory for it.1  Copyright Prasun Dewan, 2010. 2 In a pure object-based programming language such as Smalltalk, programming consists exclusively of defining andinteracting with objects. As we shall see later, Java is not such a language, since it provides both object-based and traditional programming. Other books on Java start with traditional programming, whereas, here, we are starting with object-based programming.Similarly, we interact with a program object by performing different kinds of operations on it. Performingan operation on the program object is also called invoking or executing or calling the operation (Figure 1bottom). The operation itself is called a method. The methods that can be invoked on an object aredetermined by the class of the object, which corresponds to the factory that defines the blueprint of amanufactured physical object. Defining a new kind of computer object, then, involves creating ordefining or declaring a new class for it. An object is called an instance of its class. Just as a car can bemanufactured on demand by its factory, a computer object can be created on demand by instantiatingits class. The reason for choosing the term “class” for a computer factory is that it classifies the objectsmanufactured by it. Two objects of the same class are guaranteed to have the same behavior, much astwo cars produced by the same car factory are expected to work in the same way. Table 1 shows thecorrespondence between these Java concepts and real-world entities.Figure 1. Manufactured physical objects vs. program objectsTable 1. Java vs. real-worldJava Real-worldClass FactoryComputer Object Manufactured Physical ObjectMethod OperationInvoking/Executing a Method Performing an OperationInstance of a Class Manufactured by a FactoryDefining/Declaring a Class Constructing a FactoryInstantiating a Class Manufacturing an Objectexecute invoke callA Simple ClassTo make these concepts concrete, let us consider a simple class definition shown in Figure 2. The class isnamed ASquareCalculator and it defines a single method, square. This method is a (computer)function3, which is like a mathematics function in that it maps a set of values, called the domain, toanother set of values, called the range. The two occurrences of int here indicate that both the domainand range of the function is the set of integers. The linereturn x*x;indicates that an integer x in the domain is mapped to the square of x (that is, x multiplied with itself) inthe range. A domain value to be mapped is called a parameter of the function and the range value towhich it is mapped is called the result returned by the function. Figure 3 illustrates the nature of thesquare function. As Figure 3 shows, each of the domain or parameter values is mapped to its square inthe range or result values. The function defined by the class could have also been specified using a more familiar syntax used inmathematics, such assquare: I  I3 We will see the other kind of method, a procedure, in the next chapter.Figure 2. The class ASquareCalculatorFigure 3. The nature of the square functionsquare(x) = x2Why not use this familiar syntax to also write programs? It is difficult to follow this syntax literally whilewriting a program because it is designed for handwritten rather than typed text. For instance, in theabove example, it is easy to put the superscript, 2, by hand but not using a word processor. However,there are programming languages, called functional languages, which define syntaxes that are inspiredby the mathematics syntax. Unfortunately, Java cannot support such syntaxes because it is far morecomplex than functional languages, having features that conflict with a functional syntax.What we have done above is define the blueprint of square calculators. To actually manufacture a squarecalculator, we must instantiate the class ASquareCalculator. We can then ask the


View Full Document

UNC-Chapel Hill COMP 114 - Objects

Download Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?