Вы находитесь на странице: 1из 5

AP Computer Science A Mr.

Lee
Summer AssignmentsJava mrlee280@gmail.com
Assignment 1. You can use a free, online IDE (Integrated Development Environment at
!ttp"##$$$.compileonline.com#compile%&ava%online.p!p to $rite and test 'ava programs.

(ote t!e t!ree lines of import statements t!at I added. Include t!em in ever) program )ou $rite
in t!e assignments t!at follo$.
Alternative 1: *o to !ttp"##+lue&.org#inde,.!tml, and scroll do$n for -lue' installer. .e $ill use
-lue' to $rite 'ava programs in t!e sc!ool computer la+, )ou s!ould use it at !ome. You also
need a development /it, so get t!e com+ined installer if )ou !ave not done t!is +efore.

.arnings" 'D0 is (12 t!e 'ava
plug3in )ou ma) !ave installed
for )our +ro$sers. 4gain, if
)ou5ve not programmed +efore,
)ou s!ould get t!e com+ined
installer.
If )ou5ve never done !eav)
programming +efore, -lue' ma)
+e indecip!era+le. .ait till t!e
fall, and use 6ompile1nline for
no$.


Alternative 2: 4fter installing t!e 1racle 'D0, )ou can also do$nload and install Dr'ava from
!ttp"##$$$.dr&ava.org# instead. It5s simpler to use t!an -lue'.
Assignment 2. Enter t!e follo$ing statement into t!e main met!od (see a+ove, and clic/
78un9. 6ongratulations, )ou !ave $ritten )our ver) first program:
System.out.println( "Hello World" );
You s!ould see 7;ello .orld9 in t!e printout in t!e 78esult9 +o, on t!e rig!t.

2!e ;ello.orld class contains one met!od. 2!e name of t!e met!od is main. 4ll &ava
applications must !ave a met!od named main.
class ;ello.orld
{
public static void main (String[] args)
{
// write your code here
System.out.println( "Hello orld" )!
"
}

6!ange t!e contents of t!e main met!od to t!is"
System.out.print( "A" );
System.out.print( "B" );
System.out.println( "C" );
System.out.print( "D" );

.!at is t!e difference +et$een <)stem.out.print and <)stem.out.println=
(a <)stem.out.println prints somet!ing and puts a line +rea/ after printing.
<)stem.out.print &ust prints and does (12 put in a line +rea/.
(+ 2!e second one is t$o letters longer t!an t!e first.
If )ou pic/ed a, congratulations 3 t!at5s t!e correct ans$er. If )ou pic/ed b, $ell tec!nicall) it5s
correct +ut it5s not t!e +est c!oice. 2r) again.
2!is is t!e main met!od. 2!e contents of t!e main met!od are +et$een t!e curl) +races > ?.
Variables, Data Types, an Assignment Statements
4 varia+le is a named storage space in memor). 2!in/ of a varia+le li/e a +o, t!at !olds a value.
;ere are some rules for naming a varia+le"
<tart $it! a letter. You can also start a varia+le $it! an underscore c!aracter (% or a
dollar sign (@ +ut no+od) does t!at. Don5t )ou +e that guy or gal eit!er.
4fter t!e letter )ou ma) !ave more letters, digits, or t!e underscore c!aracter.
(o spaces in a varia+le name.
2!ere are a+out A0 reserved $ords (a./.a. /e)$ords t!at cannot +e used. Bor e,ample,
)ou cannot !ave a varia+le named public or class.
'ava is case3sensitive so +e careful. 2!e varia+le x is different from t!e varia+le X.
In 'ava, +efore )ou can use a varia+le )ou must declare $!at data t)pe t!e varia+le is. 2!e data
t)pe determines $!at /ind of data a varia+le can contain. ;ere are t$o e,amples"
int year;
double interestrate;
2!e /e)$ord int indicates t!at year stores onl) integers. 2!e /e)$ord double indicates t!at
interest_rate stores decimals. (otice t!at eac! line ends in a semicolon. C
4nd !o$ do $e assign values to t!ese varia+les= .it! assignment statements, of course.
year!"#$%;
2!e a+ove statement assigns t!e value of 20DA to t!e varia+le )ear. 2!e eEuals sign is t!e
assignment operator. In an assignment statement t!e e,pression on t!e rig!t (of t!e F is
al$a)s evaluated first and t!en assigned to t!e varia+le on t!e left.
E,ample D.
6ode E,planation
int age, next;
age = 16;
next = age + 1;

System.out.println( age );
System.out.println( next );
The variables age and next are each declared as an int.
A value of 16 is assigned to age.
First the expression on the right is evaluated. Umm. 16 +
1 is 17! Then the 17 is assigned to next.
The value of age (16) is printed.
The value of next (17) is printed.
E,ample 2.
6ode E,planation
double percent, cost;

percent = 0.2;
cost = 11.0;
cost = cost percent*cost;


System.out.println( cost );
The variables percent and cost are each declared as a
double.
A value of 0.1 is assigned to percent.
A value of 100.0 is assigned to cost.
First the expression on the right is evaluated 11.0
0.2*11.0 which has a value of 8.8. Then the 8.8 is
assigned to cost replacing the old value of 11.0.
The value of cost (8.8) is printed.
Assignment !. 8eplace t!e class and main met!od $it! t!e follo$ing program. It s!ould
calculate and displa) t!e perimeter and area of a given siGed rectangle. 6op) t!e +elo$ code
and complete t!e t$o missing lines. 2!ese s!ould +e assignment statements and t!e
e,pressions on t!e rig!t s!ould include t!e length and width varia+les.

6ompile and run )our program. 1+viousl) t!e perimeter s!ould +e H0 and t!e area HC. 6!ange
t!e lengt! and $idt!, recompile, and run again. 2!e perimeter and area s!ould +e correct for
t!e ne$ lengt! and $idt! values t!at )ou c!ose.
Assignment ". .rite a program using t!e follo$ing guidelines.
<tart t!e main met!od $it! a comment t!at !as )our name (do not include t!e student
num+er. ;int" 2)pe t$o for$ard slas!es, ##, at t!e +eginning of t!e line.
(ame t!e class BirstIrogram
Include a main met!od
Jse <)stem.out.println to print )our name.
6reate t$o int varia+les K one for )our age and one for )our !eig!t in inc!es. 4ssign t!em
appropriate values.
6reate a varia+le of t)pe dou+le. .!en )ou assign it a value, t!e e,pression on t!e rig!t
must include t!e int varia+le t!at descri+es )our !eig!t in inc!es. Jse 2.AL in t!e
conversion.
Binis! $it! t!ree <)stem.out.println statements t!at print )our age and !eig!ts in t!e
same format as s!o$n in i'ava.
4l$a)s run an) programs )ou $rite or c!ange to ma/e sure it $or/s correctl).
Things You Should Know About ints and doubles.
You ma) declare and assign a value to a varia+le in t!e same statement.
int num ! $&;
double ' ! (#&.)"*+;
Literals (t!e num+ers )ou enter !ave data t)pes. DL and 3A00 are ints. 0.00L and 3CC.D
are dou+les.
You ma) assign an int to a dou+le +ut )ou cannot assign a dou+le to an int.
double , ! $%; -- o.
int . ! ).#; -- not o.. ).# is a double
If an e,pression contains onl) dou+les t!en t!e value of t!e e,pression is a dou+le. Bor
e,ample" since DL.0 # 2.0 involve t$o dou+les t!e result is M.0, not M.
If an e,pression contains onl) ints t!en t!e value of t!e e,pression is an int. Bor e,ample"
since HM # D0 involve t$o ints t!e result must +e an int. 'ava al$a)s rounds to$ard Gero
so t!e result is H.
If an e,pression contains dou+les and ints t!en t!e value of t!e e,pression is a dou+le.
Bor e,ample" since HM # D0.0 involve an int and a dou+le t!e result is H.M.
Assignment #. 8eset t!e 6ompile1nline editor and use <)stem.out.println(N to output eac!
calculation +elo$. .!at do )ou e,pect to +e output (printed= .!at is actuall) printed= Do )ou
understand $!) t!e output is $!at it is= If not, email me.

System.out.println ( (/ - $# );
System.out.println ( (/ - $#.# );
8emem+er to empt) t!e main met!od +et$een eac! program run.
int ' ! (;
System.out.println ( ' );
' ! ' 0 +;
System.out.println ( ' );
' ! ' 0 +;
System.out.println ( ' );

double , ! + 0 $.*;
System.out.println ( , );

double cat ! ( %.# 0 $.# ) - ".# 0 $.#;
System.out.println ( cat );

double do1dare ! & - %;
System.out.println ( do1dare );

int y ! ).#
System.out.println ( y );
If )ou don5t understand $!) it displa)s $!at it displa)s, email me.
Assignment $. .rite a program t!at calculates t!e average of t!ree num+ers. Declare four
varia+lesN t!ree to !old t!e t!ree num+ers and one varia+le to store t!e average. .rite an
e,pression t!at finds t!e average of t!e t!ree num+ers and assign t!e result to t!e average
varia+le. Displa) t!e average.
6ompile and run )our program. 6!ange t!e t!ree num+ers and verif) t!at t!e average is still
calculated correctl).

The Math Class and Exponents.
'ava does not !ave an e,ponent operator. 2o raise somet!ing to a po$er )ou must use a
met!od of t!e Mat! class. ;ere are some e,amples"

dou+le , F Mat!.po$( 2, H N ## assigns a value of 2H or 8 to ,
dou+le G F Mat!.po$( ,, H.A N ## assigns a value of ,H.A or a+out DLL8.DALC88 to G

2!e Mat! class contains man) useful mat! related met!ods. It is special (naturall) +ecause )ou
never create a Mat! o+&ect. 4lso notice t!at t!e po$ met!od al$a)s returns a dou+le.
<earc! online for e,amples on !o$ to use t!e Mat! class. Ma/e sure )ou include t!ose t!ree
import statements (see first page of t!is document or )our program $on5t $or/.

Вам также может понравиться