Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
ICOM 4015
Advanced Programming Laboratory
Chapter 2
Introduction to Java:
Variables, Data Types and Objects
University of Puerto Rico
Electrical and Computer Engineering Department
by Juan E. Sur´ıs
1 Introduction
In this chapter we will explore variables, data types and objects. Our goal for today’s exercise
is to get acquainted with Java’s native data types and how their behavior is dependent on
the amount of memory used to store them. We will explore what objects are, how to create
them and how we interact with them. Finally, we will learn how to create arrays and that
Java treats arrays as objects.
Before you begin, create a new project named Lab 2. You will be creating several main
classes, all in this project.
2 Variables
In Java, every value has a type. For example the number 13 is of type int, while 13.0 is
of type float. We often want to store values so that we can use them at a later time.
A variable is a storage location in the computer’s memory where we can remember these
values. A variable has a type, name and content.
2.1 Native data types
When we declare a variable in Java, we must always define the type of variable it is. A
variable’s data type determines the values it may contain as well as the operations that may
be performed on it. The primitive data values Java defines are:
• byte - is an 8-bit signed integer. Can hold values from −(27 − 1) to 27.
• short - is a 16-bit signed integer. Can hold values from −(215 − 1) to 215.
• int - is a 32-bit signed integer. Can hold values from −(231 − 1) to 231.
• long - is a 64-bit signed integer. Can hold values from −(263 − 1) to 263.
• float - is a single precision 32-bit floating point number.
• double - is a double precision 64-bit floating point number.
• boolean - holds one of two values, true or false.
• char - is a single 16-bit Unicode character.
Notice that we have 4 integer types and 2 floating point types, differentiated by the
number of bits used to represent them. Care must be taken when we assign values to these
types of variables, as we may get unexpected results that are very hard to diagnose.
1
Figure 1: Example 1
Exercise 1: Create a main class called Lab2Ex1, type the code in Figure 1 and
execute. You must add the following line at the top of Lab2Ex1.java (more on this later):
import java.math.BigDecimal;
Care must also be taken when we we perform operations of a one type expecting a result
of a different type.
Figure 2: Example 2
Exercise 2: Create a main class called Lab2Ex2, type the code in Figure 2 and
execute.
2.1.1 Data type conversion
Sometimes we wish (or need) to convert from one data type to another. This may happen
when dealing with variables of the same type but of different size. We can safely assign a
value from a smaller sized variable to a bigger sized variable. However, the reverse is not safe.
Exercise 3: Create a main class called Lab2Ex3, type the code in Figure 3 and
execute.
2.2 Object data types
Objects are entities that are more complex than the simple data types we have just seen.
In fact, objects can take just about any form and be contain a large amount of data. We
can also use variables to hold objects. Actually, variables themselves do not hold the object
2
Figure 3: Example 3
themselves, but a reference to the object. A reference is the address of the memory location
where the object is stored.
Figure 4: Example 4
Exercise 4: Create a main class called Lab2Ex4, type the code in Figure 4 and
execute.
Notice that the variable s3 was declared and assigned the value of another reference.
Does this mean that the object was copied? In the next example we will get the answer.
2.2.1 Object Construction
The String objects we saw in the Example 4 is a special kind of object, in the sense that
it was created for us by Java. All we did was provide a string using quotation marks. The
String object was created for us. Typically, we need to explicitly create objects ourselves,
as Java knows how to create very few types of objects.
There are two elements to creating an object. First, we need the keyword new. This
keywork tells Java that we wish to create a new object. The keyword is followed by a spe-
cial method defined by every class called the Constructor. The constructor must be named
exactly as the class. A class may define more than one constructor (more on this later in
the semester), as long as they all take different number and/or type of arguments. Perform
the following search on google.com to find the Javadoc for the class Rectangle: “Rectangle
3
site:java.sun.com”
Figure 5: Example 5
Exercise 5: Create a main class called Lab2Ex5, type the code in Figure 5 and
execute.
Figure 6: Debugging example 5
Notice in Example 5 there is a line that is highlighted. Place a breakpoint on that line
and start debugging. Once you switch to Debug Perspective, explore the objects as in Figure
6. Take a look at the values and you can confirm that they are equal. Furthermore, notice
that they both have the same id. This tells us that they are the same object.
Exercise 6: Add a breakpoint to Lab2Ex5.java at the first println line. Start
debugging and explore the objects as in Figure 6. Do the objects have the same id?
2.3 Arrays
An Array is a container object (yes, I said object) that holds a fixed amount of values of a
given type. The size of the array, which we will call n, is specified at the time of creation and
may never be changed. Each value, called an element, is stored in its own unique location,
4
where each location is accessed by a corresponding index. Indexes are integers that take
values from 0 to n− 1.
As with objects, we need to construct the array object before we use it. To construct an
array, we also use the keyword new, but in a different way. Instead of calling a constructor
method, we specify the size of the array.
As with variables, the elements of the arrays will be references if the array type is a class.
At the time we create the array, the elements references will be created, but not the objects.
Figure 7: Example 7
Exercise 7: Create a main class called Lab2Ex7, type the code in Figure 5 and
execute.
3 Independent Exercises
Complete the following exercises:
1. Exercise P2.1 Write an AreaTester program that constructs a Rectangle object
and then computes and prints its area. Use the getWidth and getHeight methods.
Also print the expected answer. (20 pts.)
2. Exercise P2.4 The intersection method computes the intersection of two rectan-
gles. Write a program IntersectionPrinter that constructs two rectangle objects,
prints them, and then prints the rectangle object that describes the intersection. Then
the program should print the result of the intersection method when the rectangles
do not overlap. (20 pts.)
3. Exercise P2.5 In Java library, a color is specified by its red, green and blue com-
ponents between 0 and 255. Write a program BrighterDemo that constructs a Color
object with red, green, and blue values of 50, 100, and 150. Then apply the brighter
method and print the red, green and blue values of the resulting color. (20 pts.)
5