— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet87/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   83   84   85   86   87   88   89   90   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

% java TestArrays
island = Fiji
island = Cozumel
island = Bermuda
island = Azores
exercise 
solutions
Exercise Solutions


you are here
4
primitives 
and 
references
69
besides 
42
 
class Triangle {
double area;
int height;
int length;
public static void main(String [] args) {
int x = 0;
Triangle [ ] ta = new Triangle[4];
while ( 
x < 4
) {
ta[x] = new Triangle();
ta[x]
.height = (x + 1) * 2;
ta[x]
.length = x + 4;
ta[x].setArea();
System.out.print(“triangle “+x+”, area”);
System.out.println(“ = “ + 
ta[x]
.area);
x = x + 1;
}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print(“y = “ + y);
System.out.println(“, t5 area = “+ t5.area);
}
void setArea() {
area
= (height * length) / 2;
}
}
class Triangle {
double area;
int height;
int length;
public static void main(String [] args) {
int x = 0;
Triangle [ ] ta = new Triangle[4];
while ( 
x < 4
) {
x < 4
x < 4
ta[x] = new Triangle();
ta[x]
.height = (x + 1) * 2;
ta[x]
.length = x + 4;
ta[x].setArea();
System.out.print(“triangle “+x+”, area”);
System.out.println(“ = “ + 
ta[x]
.area);
x = x + 1;
}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
System.out.print(“y = “ + y);
System.out.println(“, t5 area = “+ t5.area);
}
void setArea() {
area
= (height * length) / 2;
}
}
File Edit Window Help Bermuda
%java Triangle
triangle 0, area = 4.0
triangle 1, area = 10.0
triangle 2, area = 18.0
triangle 3, area = 
28.0
y = 
4, t5 area = 343
Reference Variables: 
HeapQuiz Objects:
id = 0
id = 1
id = 2
The case of the pilfered references
Tawny could see that Kent’s method had a serious 
fl aw. It’s true that he didn’t use as many reference 
variables as Bob, but there was no way to access any 
but the last of the Contact objects that his method cre-
ated. With each trip through the loop, he was assign-
ing a new object to the one reference variable, so the 
previously referenced object was abandoned on the 
heap – unreachable. Without access to nine of the ten 
objects created, Kent’s method was useless.
(The software was a huge success and the client gave Tawny and Bob an extra week 
in Hawaii. We’d like to tell you that by finishing this book you too will get stuff like that.)
hq[0]
hq[1]
hq[2]
hq[3]
hq[4]
Puzzle Solutions



4

methods 
use 
instance variables
this is a new chapter
71
State affects behavior,
behavior affects state

 
We know that objects 
have state and behavior, represented by instance variables and methods. But until now, we 
haven’t looked at how state and behavior are related. We already know that each instance of a 
class (each object of a particular type) can have its own unique values for its instance variables. 
Dog A can have a name “Fido” and a weight of 70 pounds. Dog B is “Killer” and weighs 9 pounds.
And if the Dog class has a method makeNoise(), well, don’t you think a 70-pound dog barks a 
bit deeper than the little 9-pounder? (Assuming that annoying yippy sound can be considered 
bark.) Fortunately, that’s the whole point of an object—it has behavior that acts on its state. In 
other words, methods use instance variable values. Like, “if dog is less than 14 pounds, make 
yippy sound, else...” or “increase weight by 5”. Let’s go change some state.
How Objects Behave
This oughta 
change her state!
Make it Stick



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   83   84   85   86   87   88   89   90   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish