System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə20/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   16   17   18   19   20   21   22   23   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

 Chapter 3 Self Test
1.
Write a program that reads characters from the keyboard until a period is received.
Have the program count the number of spaces. Report the total at the end of the
program.
2.
Show the general form of the if­else­if ladder.
3.
Given
to what if does the last else associate?
4.
Show the for statement for a loop that counts from 1000 to 0 by –2.
5.
Is the following fragment valid?
6.
Explain what break does. Be sure to explain both of its forms.
7.
In the following fragment, after the break statement executes, what is displayed?


8.
What does the following fragment print?
9.
The iteration expression in a for loop need not always alter the loop control variable
by a fixed amount. Instead, the loop control variable can change in any arbitrary way.
Using this concept, write a program that uses a for loop to generate and display the
progression 1, 2, 4, 8, 16, 32, and so on.
10.
The ASCII lowercase letters are separated from the uppercase letters by 32. Thus, to
convert a lowercase letter to uppercase, subtract 32 from it. Use this information to
write a program that reads characters from the keyboard. Have it convert all lowercase
letters to uppercase, and all uppercase letters to lowercase, displaying the result. Make
no changes to any other character. Have the program stop when the user enters a
period. At the end, have the program display the number of case changes that have
taken place.
11.
What is an infinite loop?
12.
When using break with a label, must the label be on a block that contains the
break?


Chapter 4
Introducing Classes, Objects, and Methods
Key Skills & Concepts

Know the fundamentals of the class

Understand how objects are created

Understand how reference variables are assigned

Create methods, return values, and use parameters

Use the return keyword
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out


B

Return a value from a method

Add parameters to a method

Utilize constructors

Create parameterized constructors

Understand new

Understand garbage collection

Use the this keyword
efore you can go much further in your study of Java, you need to learn about the class.
The class is the essence of Java. It is the foundation upon which the entire Java
language is built because the class defines the nature of an object. As such, the class
forms the basis for object­oriented programming in Java. Within a class are defined
data and code that acts upon that data. The code is contained in methods. Because
classes, objects, and methods are fundamental to Java, they are introduced in this
chapter. Having a basic understanding of these features will allow you to write more
sophisticated programs and better understand certain key Java elements described in
the following chapter.
CLASS FUNDAMENTALS
Since all Java program activity occurs within a class, we have been using classes since
the start of this book. Of course, only extremely simple classes have been used, and we
have not taken advantage of the majority of their features. As you will see, classes are
substantially more powerful than the limited ones presented so far.
Let’s begin by reviewing the basics. A class is a template that defines the form of an
object. It specifies both the data and the code that will operate on that data. Java uses a
class specification to construct objects. Objects are instances of a class. Thus, a class is
essentially a set of plans that specify how to build an object. It is important to be clear
on one issue: a class is a logical abstraction. It is not until an object of that class has
been created that a physical representation of that class exists in memory.
One other point: Recall that the methods and variables that constitute a class are called

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   16   17   18   19   20   21   22   23   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə