You are on page 1of 6

CmSc150 Fundamentals of Computing I Homework 06 due 02/17 !

5 pm S"#$%I"&

1. Programming exercise
Download the project HW06-Errors.zip. It contains two classes Account and Transactions. The Transactions class has several s nta! and se"antic errors. #our tas$ is to correct the". When co"pilin% the class ou will %et an error "essa%e &or each s nta! error. 'or each error "essa%e do the &ollowin%( ). Write down the error "essa%e *. Write down the reason &or the "essa%e +. Write down the necessar corrections ,. -orrect the pro%ra" and co"pile a%ain. When there are no "ore error "essa%es. run the pro%ra" and e!a"ine the results. E!a"ine the code and &ind out the lo%ical /se"antic0 errors. Write down the se"antic errors and e!plain the can 1e corrected. -orrect the pro%ra". 2ip the &older. chan%e the e!tension to .zi and send it 1ac$. Errors: ). a"ount 3 scan.ne!tDou1le/04 -annot &ind s "1ol 5scan6 7canner o1ject not de&ined i"port java.util.7canner4 7canner scan 3 new 7canner/7 ste".in04 *. int 1al 3 Account.deposit /a"ount04 8on-static "ethod deposit/dou1le0 cannot 1e re&erenced &ro" a static conte!t int 1al 3 acc).deposit /a"ount04 +. int 1al 3 acc).deposit /a"ount04 possi1le loss o& precision dou1le 1al 3 acc).deposit /a"ount04 ,. Account.withdraw /acc)04 withdraw/dou1le0 in Account cannot 1e applied to Account Account.withdraw /a"ount04 9. Account.withdraw /a"ount04 8on-static "ethod deposit/dou1le0 cannot 1e re&erenced &ro" a static conte!t acc).deposit /a"ount04 :o%ical error( a&ter withdrawal the 1alance is not recorded -orrection( 1al 3 acc).withdraw /a"ount04

2. Questions and answers


True/False (Write T or F immediately after the number of the question)
). ;ava "ethods can return onl pri"itive t pes /int. dou1le. &loat. char. 1oolean. etc0 . . Answer( False. E!planation( ;ava "ethods can also return /re&erences to0 o1jects. such as a 7trin%. *. 'or"al para"eters are those that appear in the "ethod call and actual para"eters are those that appear in the "ethod header Answer( False. E!planation( The <uestion has the two de&initions reversed. 'or"al para"eters are those that appear in the "ethod header. actual para"eters are the para"eters in the "ethod call /those 1ein% passed to the "ethod0. +. All ;ava classes "ust contain a "ain "ethod which is the &irst "ethod e!ecuted when the ;ava class is called upon. Answer: False. E!planation( =nl the driver program re<uires a "ain "ethod. The driver pro%ra" is the one that is &irst e!ecuted in an ;ava pro%ra" /e!cept &or Applets0. 1ut it "a call upon other classes as needed. and these other classes do not need "ain "ethods ,. ;ava "ethods can return "ore than one ite" Answer( False. E!planation( All ;ava "ethods return a sin%le ite". whether it is a pri"itive data t pe an o1ject. or void. The reserved word continue is used to e!it the re"ainder o& a loop and test the condition a%ain. 9. The &ollowin% "ethod header de&inition will result in a s nta! error( pu1lic void a>ethod/ 04 Answer: True. E!planation( The reason &or the s nta! error is 1ecause it ends with a 546 s "1ol. It instead needs to 1e &ollowed 1 ? @ with 0 or "ore instructions inside o& the 1rac$ets. An a1stract "ethod will end with a 546 1ut this header does not de&ine an a1stract "ethod. 6. A "ethod de&ined in a class can access the classA instance data without needin% to pass the" as para"eters or declare the" as local varia1les Answer( True. E!planation( The instance data are %lo1all availa1le to all o& the classA "ethods and there&ore the "ethods do not need to receive the" as para"eters or declare the" locall . I& varia1les o& the sa"e na"e as instance data were declared locall inside a "ethod then the instance data would 1e 5hidden6 in that "ethod 1ecause the re&erences would 1e to the local varia1les.
*

B. De&inin% &or"al para"eters re<uires includin% each para"eters t pe Answer: True. E!planation( In order &or the co"piler to chec$ to see i& a "ethod call is correct. the co"piler needs to $now the t pes &or the para"eters 1ein% passed. There&ore. all &or"al para"eters /those de&ined in the "ethod header0 "ust include their t pe. This is one ele"ent that "a$es ;ava a Strongly Typed lan%ua%e. C. While "ultiple o1jects o& the sa"e class can e!ist. in a %iven pro%ra" there can 1e onl one version o& each class Answer( True. E!planation( A class is an a1straction. that is. it e!ists as a de&inition. 1ut not as a ph sical instance. Dh sical instances are created when an o1ject is instantiated usin% new. There&ore. there can 1e "an o1jects o& t pe 7trin%. 1ut onl one 7trin% class.

'ultiple C(oice )use oldface or underline to answer*


). The 1ehavior o& an o1ject is de&ined 1 the o1jectAs a0 10 c0 d) e0 instance data constructor visi1ilit "odi&iers methods all o& the a1ove

Answer( d. E!planation( The "ethods dictate how the o1ject reacts when it is passed "essa%es. Each "essa%e is i"ple"ented as a "ethod. and the "ethod is the code that e!ecutes when the "essa%e is passed. The constructor is one o& these "ethods 1ut all o& the "ethods co"1ine dictate the 1ehavior. The visi1ilit "odi&iers do i"pact the o1jectAs per&or"ance indirectl . *. The relationship 1etween a class and an o1ject is 1est descri1ed as a. classes are instances o& o1jects b. objects are instances of classes c. o1jects and classes are the sa"e thin% d. classes are pro%ra"s while o1jects are varia1les e. o1jects are the instance data o& classes Answer( 1. -lasses are de&initions o& pro%ra" entities that represent classes o& thin%sEentities in the world. -lass de&initions include instance data and "ethods. To use a class. it is instantiated. These instances are $nown as o1jects. 7o. o1jects are instances o& classes. Dro%ra" code directl interacts with o1jects. not classes.

+. Which o& the &ollowin% reserved words in ;ava is used to create an instance o& a classF a. 1. c. d. e. class pu1lic pu1lic or private. either could 1e used i"port new

Answer( e. E!planation( The reserved word 5new6 is used to instantiate an o1ject. that is. to create an instance o& a class. The state"ent new is &ollowed 1 the na"e o& the class. This calls the classA constructor. E!a"ple( -ar ! 3 new -ar/ 04 will create a new instance o& a -ar and set the varia1le ! to it.
,. I& a "ethod does not have a return state"ent. then

a. b. c. d. e.

it will produce a s nta! error when co"piled it must be a oid method it can not 1e called &ro" outside the class that de&ined the "ethod it "ust 1e de&ined to 1e a pu1lic "ethod it "ust 1e an int. dou1le. &loat or 7trin% "ethod Answer( 1. E!planation( All "ethods are i"plied to return so"ethin% and there&ore there "ust 1e a return state"ent. However. i& the pro%ra""er wishes to write a "ethod that does not return an thin%. and there&ore does not need a return state"ent. then it "ust 1e a void "ethod /a "ethod whose header has 5void6 as its return t pe0.

9. A classA constructor usuall de&ines a. 1. c. d. e. how an object is initiali!ed how an o1ject is inter&aced the nu"1er o& instance data in the class the nu"1er o& "ethods in the class i& the instance data are accessi1le outside o& the o1ject directl Answer( a. E!planation( The constructor should 1e used to 5construct6 the o1ject. that is. to set up the initial values o& the instance data. This is not essential. 1ut is t picall done. The inter&ace o& an o1ject is dictated 1 the visi1ilit "odi&iers used on the instance data and "ethods. 6. Instance data &or a ;ava class a. 1. c. d. e. are li"ited to pri"itive t pes /e.%.. int. &loat. char0 are li"ited to 7trin%s are li"ited to o1jects/e.%.. 7trin%s. classes de&ined 1 other pro%ra""ers0 "a 1e pri"itive t pes or o1jects. 1ut o1jects "ust 1e de&ined to 1e private ma" be #rimiti e t"#es or objects Answer( e. E!planation( The instance data are the entities that "a$e up the class and "a 1e an t pe availa1le whether pri"itive or o1ject. and "a 1e

pu1lic or private. G usin% o1jects as instance data. it per"its the class to 1e 1uilt upon other classes. This relationship where a class has instance data that are other classes is $nown as a has-a relationship. B. -onsider a "ethod de&ined with the header( pu1lic void &oo/int a. int 10. &ollowin% "ethod calls is le%alF a. b. c. d. e. &oo/0. 0.)04 foo$% & 1' 2 ( ))* &oo/004 &oo/ 04 &oo/) H *. + I 0.)04 Answer( 1. E!planation( The onl le%al "ethod call is one that passes two int para"eters. In the case o& answer 1. 0 E ) is an int division /e<ual to 00 and * I + is an int "ultiplication. 7o this is le%al. The answers &or a and e contain two para"eters. 1ut the second o& each is a dou1le. The answers &or c and d have the wron% nu"1er o& para"eters. C. -onsider a "ethod de&ined with the header( pu1lic void dou1le&oo/dou1le !0. Which o& the &ollowin% "ethod calls is le%alF a. 1. c. d. e. dou1le&oo/004 dou1le&oo/0.99904 dou1le&oo/0.) H 0.*04 dou1le&oo/0.). 0.*04 all of the abo e are legal exce#t for d Answer( e. E!planation( In the case o& a. the value 0 /an int0 is widened to a dou1le. In the case o& c. the addition is per&or"ed ieldin% 0.+ and then dou1le&oo is called. The para"eter list in d is ille%al since it contains two dou1le para"eters instead o& ). J. The value o& the ;ava e!pression )0 - 25 a. 6 1. 9 c. + d. B e. none o& the a1ove
% 4 is:

Which o& the

)0. I& the int varia1les int1 and int2 contain the values 9 and 5, respectively,
then the value a. 1. c. d. e. of the expression 0.C 0 ).C 1.% ) (double)(int ! int2) is:

Answer( d. E!planation( 'irst the inte%er division is per&or"ed. 1ecause the e!pression is in parentheses. The result is ). Then the t pe castin% is applied and the result is real ). i.e. ).0

)). I& the int varia1les int1 and int2 contain the values 9 and 5, respectively,
then the value a. 1. c. d. e. of the expression 0.C 0.9 1., ).0 ) (double)(int ) ! int2 is:

Answer( c. E!planation( 'irst the t pe castin% operation is applied to the value in int1 /note that the contents o& int) is not chan%ed0 . As a result the e!pression now involves a dou1le value. there&ore the value in int* is also trans&or"ed to dou1le. and onl then the division is per&or"ed. )*. I& the int varia1les int1 and int2 contain the values
then the value a. b. c. d. e. of the expression 0.C %.).C *.0 ) 0 and 5, respectively, int2 ! (double)(int ) is:

Answer( 1. E!planation( 'irst the t pe castin% operation is applied to the value in int1 /note that the contents o& int) is not chan%ed0 . As a result the e!pression now involves a dou1le value. there&ore the value in int* /however not chan%in% the contents o& int*0 is also trans&or"ed to dou1le. and onl then the division is per&or"ed.

How to turn in the assi%n"ent( The pro%ra"( 2ip the project and send it as attached &ile 1 e-"ail. 'ollow the na"in% conventions. The <uestions( write our answers into this docu"ent. write our na"e. then save and send as attached .doc docu"ent 1 e-"ail

You might also like