— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet47/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   43   44   45   46   47   48   49   50   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

you are here
4
dive In 
A Quick Dip
25
File Edit Window Help Poet
% java Shuffle1
a-b c-d
class Shuffle1 {
public static void main(String [] args) {
int x = 3;
while (x > 0) {
if (x > 2) {
System.out.print(“a”);
}
x = x - 1;
System.out.print(“-”);
if (x == 2) {
System.out.print(“b c”);
}
if (x == 1) {
System.out.print(“d”);
x = x - 1;
}
}
}
}
class Exercise1b {
public static void main(String [] args) {
int x = 1;
while ( x < 10 ) {
x = x + 1;
if ( x > 3) {
System.out.println(“big x”);
}
}
}
This will compile and run (no output), but
}
without a line added to the program, it 
would run forever in an infinite ‘while’ loop!
class Foo {
public static void main(String [] args) {
int x = 5;
while ( x > 1 ) {
x = x - 1;
if ( x < 3) {
System.out.println(“small x”);
}
}
This file won’t compile without a
}
class declaration, and don’t forget
}
the matching curly brace !
class Exercise1b {
public static void main(String [] args) {
int x = 5;
while ( x > 1 ) {
x = x - 1;
if ( x < 3) {
System.out.println(“small x”);
}
}
}
}
A
B
C
Code Magnets:
Exercise Solutions
The ‘while’ loop code must be in-
side a method. It can’t just be 
hanging out inside the class.


26
 
chapter 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
J A V A
R
R
A
Y
S
B A N C H
S
T
A
T
I
C
M
A
I
N
Y T E O U T P R I N T
W
I
L
N T
S R I G
D E C L A R E
C O M M A N D
J
V
M
T
H
O
I C
P
U
B
L
I
V
A
R
A
B
E
C
O
M
I
L
R
V
O
I
D
F
L
O
A
O P
class PoolPuzzleOne {
public static void main(String [] args) {
int x = 0;
while ( 
X < 4
) {
System.out.print(“a”);
if ( x < 1 ) {
System.out.print(“ “);
}
System.out.print(“n”);
if ( 
X > 1
) {
System.out.print(“ oyster”);
x = x + 2;
}
if ( x == 1 ) {
System.out.print(“noys”);
}
if ( 
X < 1
) {
System.out.print(“oise”);
}
System.out.println(““);
X = X + 1;
}
}
}
File Edit Window Help Cheat
%java PoolPuzzleOne
a noise
annoys
an oyster
class Test {
public static void main(String [] args) {
int x = 0;
int y = 0; 
while ( x < 5 ) {
System.out.print(x + ““ + y +“ “);
x = x + 1;
}
}
}
y = x - y;
y = y + x;
y = y + 2;
if( y > 4 ) {
y = y - 1;
}
x = x + 1;
y = y + x;
if ( y < 5 ) {
x = x + 1;
if ( y < 3 ) {
x = x - 1;
}
}
y = y + 2;
22 46
11 34 59
02 14 26 38
02 14 36 48
00 11 21 32 42
11 21 32 42 53
00 11 23 36 410
02 14 25 36 47
Candidates: 
 
 
 
Possible output:
puzzle 
answers


2

classes 
and
 objects
this is a new chapter
27
I was told there would be objects. 
 
In chapter 1, we put all of our code in the 
main() method. That’s not exactly object-oriented. In fact, that’s not object-oriented at all. Well, 
we did use a few objects, like the String arrays for the Phrase-O-Matic, but we didn’t actually 
develop any of our own object types. So now we’ve got to leave that procedural world behind, 
get the heck out of main(), and start making some objects of our own. We’ll look at what makes 
object-oriented (OO) development in Java so much fun. We’ll look at the difference between 
class and an object. We’ll look at how objects can give you a better life (at least the program-
ming part of your life. Not much we can do about your fashion sense). Warning: once you get 
to Objectville, you might never go back. Send us a postcard.
A Trip to Objectville
We’re going to 
Objectville! We’re 
leaving this dusty ol’ 
procedural town for good.
I’ll send you a postcard.
Make it Stick


once upon a time in 
Objectville
28
 
chapter 2
the spec
nce upon a time in a software shop, two 
programmers were given the same spec and told to 
“build it”. The Really Annoying Project Manager 
forced the two coders to compete, 
by promising that whoever delivers 
first gets one of those cool Aeron™ 
chairs all the Silicon Valley guys have. 
Larry, the procedural programmer, and 
Brad, the OO guy, both knew this would 
be a piece of cake. 
Larry, sitting in his cube, thought to 
himself, “What are the things this program 
has to do? What procedures do we need?”. 
And he answered himself , “rotate and 
playSound.” So off he went to build the 
procedures. After all, what is a program if not 
a pile of procedures?
Brad, meanwhile, kicked back at the cafe 
and thought to himself, “What are the things 
in this program... who are the key players?” He 
first thought of The Shapes. Of course, there 
were other objects he thought of like the User, the Sound, 
and the Clicking event. But he already had a library of code 
for those pieces, so he focused on building Shapes. Read 
on to see how Brad and Larry built their programs, and 
for the answer to your burning question, “So, who got the 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   43   44   45   46   47   48   49   50   ...   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