Page 186 - Introduction to Programming with Java: A Problem Solving Approach
P. 186

                152 Chapter 5 Using Pre-Built Methods 5.1 Introduction
In Chapters 3 and 4, we focused on basic Java programming language constructs—variables, assignments, operators, if statements, loops, and so on. We also introduced a more advanced programming technique— calling a method. Method calls provide a lot of “bang for your buck.” In other words, they do a lot and require very little work on your part. For example, you get great benefit for little effort when you call the print and println methods for output, the next, nextLine, nextInt, and nextDouble methods for input, and the charAt, length, equals, and equalsIgnoreCase methods for string manipula- tion. In this chapter, we want to expose you to other methods that are already written, already tested, and are readily accessible to all Java programmers.
While this chapter raises your awareness of valuable already-written methods, it also gives you a better feeling for what methods can do in general. And learning what methods can do is an important first step in learning about object-oriented programming (OOP). We describe OOP in all its glory in the next chapter, but for now, here’s a pared-down explanation: OOP is the idea that programs should be organized into objects. An object is a set of related data plus a set of behaviors. For example, a string is an object: A string’s “set of related data” is its characters, and its “set of behaviors” is its methods (the length method, the charAt method, etc.). Each object is an instance of a class. For example, a single string object, “hello,” is an instance of the String class. This chapter serves as a transition from Java basics in Chapters 3 and 4 to OOP in the remainder of the book. We carry out this transition by showing you how to use pre-built OOP code without having to implement it yourself. More specifically, in this chapter, you learn how to use methods, and in the next chapter, you’ll learn how to write your own classes and the methods that go inside those classes.
 There are two basic types of methods, instance methods and class methods, and we provide exam- Apago PDF Enhancer
ples of both in this chapter. Instance methods are methods that are associated with a particular instance of a class. For example, to call the String class’s length method, you have to associate it with a par- ticular string. So in the example below, note how the firstName string is associated with the length method:
firstNameSize = firstName.length();
The firstName string is an example of a calling object. As the name implies, a calling object is an object that calls a method. Whenever you call an instance method, you have to prefix the method name with a call- ing object and then a dot.
Class methods are methods that are associated with an entire class, not with a particular instance of a class. For example, there’s a Math class that contains many class methods. Its methods are associated with math in general, not with a particular instance of math (a particular instance of math doesn’t even make sense). To call a class method, you prefix the method name with the name of the class that defines it. For ex- ample, the Math class contains a round method that returns the rounded version of a given value. To call the round method, you prefix it with Math like this:
paymentInDollars = Math.round(calculatedEarnings);
We start the chapter with an overview of the API library, which is Sun’s collection of pre-built classes. We then examine the Math class, which provides methods for mathematical calculations. We next turn our at- tention to the wrapper classes, which encapsulate (wrap up) primitive data types. We then expand on our previous discussion of the String class by providing additional string methods. After that, we describe the printf method, which provides formatted output functionality. We then discuss the Random class, which provides methods for generating random numbers. We end the chapter with an optional GUI track section. In it, we discuss methods provided by the Graphics class and describe how to call graphics methods from within a Java applet. Very cool stuff!

























































































   184   185   186   187   188