You are on page 1of 5

CIS112 Midterm

Name Kavita Kadam

Imagine you have created a world with an Aardvark who is running around eating Ants. se this scenario as you think a!out the "ollowing #uestions.

1. $rite a statement that will declare an integer instance varia!le named numAnts and set its initial value to %. And a statement that increments the varia!le !y one. &' (ts). private int numAnts = 0; numAnts++; 2. *here is an at+dge method to check i" the aardvark had gotten to one o" the edges. $hich method signature !elow returns a true,"alse answer i" the aardvark hits the edge and does not have a (arameter- &' (ts). a. (u!lic void at+dge & ) c. (u!lic void at+dge & !oolean . ) !. (u!lic !oolean at+dge & !oolean . ) d. public boolean atEdge ( )

/. a.

$hich i" statement will end the scenario i" the value o" numAnts is less than 0. &' (ts). !. i" &numAnts 7 0) 3 4reen"oot.Sto(& )5 6 d. if (numAnts < 4) { Greenfoot !top( ); "

i" &numAnts 1 2 0) 3 4reen"oot.Sto(& )5 6

c.

i" &numAnts 7 0) 3 4reen"oot.Sto(& )5 6

0. I want to (lace an Ant in the world at a random (lace. $hich line o" code does this- &' (ts). a add#b$ect(ne% Ant()& Greenfoot get'andom(umber(400)&Greenfoot get'andom(umber(400)); b. addObject(new Ant(), Greenfoot.getRandomNumber(),Greenfoot.getRandomNumber()); c. add8!9ect&new Ant&): 4reen"oot.get;andomNum!er&0%%))5 d. add8!9ect&new Ant&): 4reen"oot.get;andomNum!er&0%%): in Aardvark$orld)5

'. I" a class called Ant e.tends the Animal class: which class is called the su(er class- $hich is the Su! class. +.(lain the di""erence. &1% (ts). Ant class is subclass and Animal class is superclass !ubclass is a class t)at represents speciali*ation of superclass #n t)e ot)er )and superclass is a class %)ic) passes its be)aviour to its subclass A !ubclass in)erits all t)e met)ods from its superclass +)at means t)at it )as and can use all met)ods t)at its superclass defines

<. =or each o" the method signatures !elow: answer the "ollowing #uestions> &1% (ts). a. $hat is the method name!. ?oes the method return a value- I" yes: what is the ty(e o" the return valuec. @ow many (arameters does the method have (u!lic void look=orAnt&) a loo,-orAnt b no return value c no parameters (u!lic void walk &int distance) a %al, b no return value c one parameter named .distance/ of t0pe integer (u!lic !oolean "oundAnt& ) a foundAnt b boolean return value (true1false) c no parameters

sing one o" the methods: write a statement to call the method using the re"erence varia!le m0Anteater m0Anteater %al,(20);

A. +.(lain and de"ine the di""erence !etween methods that uses void in its descri(tion versus a return statement. Brovide an e.am(le "rom the !ook: class e.am(les or a we!site. Ce sure to e.(lain your e.am(le in detail. Ce sure to e.(lain your e.am(le in detail. &1'(ts). +)e return t0pe of met)od specifies %)at a met)od call %ill return +)e met)od %it) a void return t0pe does not return a value 3elo% are t)e e4amples of met)ods %it) void and one %it) an integer return t0pe 5 public void act ( ) { move ( ); turn (6); " public boolean at7orldEdge() { if(get8() < 90 :: get8() ; get7orld() get7idt)() < 90) return true; if(get=() < 90 :: get=() ; get7orld() get>eig)t() < 90) return true; else return false; " +)e above met)od act () )as a return t0pe void& %)ic) means t)e met)od does not return an0 value +)is met)od in turn calls ot)er met)ods& move() and turn() t)us causing t)e ob$ect to move and turn +)e met)od at7orldEdge() returns a boolean value (true or false) +)is met)od c)ec,s for t)e position of t)e current ob$ect& if t)e current ob$ect is in t)e %orld it returns +'?E or else returns -A@!E

D. In your own words: what is the (ur(ose o" a Constructor- Brovide an e.am(le "rom the !ooks: class e.am(les or a we!site. Ce sure to e.(lain your e.am(le in detail. &1' (ts). +)e Aonstructor is a met)od in t)e class %it) t)e same name as t)e class& %)ic) is automaticall0 e4ecuted %)enever an instance of t)is class is created Bt is used mainl0 to set up t)e ne% instance %it) t)e proper initial values for t)e starting state -or eg public Arab7orld( ) { add#b$ect (ne% Arab( )& 260& 200); " +)e above constructor for t)e class Arab7orld %ill add a ne% crab ob$ect to t)e %orld automaticall0 %)en t)e %orld is instantiated

E. In your own words: descri!e the di""erence !etween Instance Faria!les and Gocal Faria!les. $here and how are they used- Brovide an e.am(le "rom the !ook: class e.am(les or a we!site. Ce sure to e.(lain your e.am(le in detail. &1' (ts). +)e instance variable& as t)e name suggests is t)e variable of t)e instance of t)e class +)e value stored in t)is variable is al%a0s associated %it) its ob$ects onl0 and e4ists as long as t)e ob$ect e4ists +)e instance variable is declared t)e ,e0%ord private -or eg private GreenfootImage image1; +)e above e4ample s)o%s an instance variable named image2 %)ic) )olds an image for eac) ob$ect of t)e class +)e @ocal variables are defined %it)in t)e met)ods of a class +)ese variables are used for temporar0 storage %it)in t)e met)od and are not e4posed to ot)er met)ods and ob$ects +)e definition for t)ese variables does not reCuire public or private ,e0%ords -or eg public void pupAge() { int age = 0; age = age + D; !0stem out println(EFupp0 age is 5 E + age); " +)e local variable age is of t0pe integer and belongs to pupAge() met)od +)is variable can not be accessed from outside t)is class 1%. In your own words: descri!e the conce(t !ehind 8!9ect 8riented Brogramming. $hat are Classes: 8!9ects and Methods and how do they interact- &1' (ts). +)e #b$ect #riented programming is a uniCue met)odolog0 of programming %)ere t)e programming model is organi*ed b0 data and ob$ects instead of logic and actions +)is ma,es it muc) easier to program in real %orld terms -or e4ample& people t)eir address& p)one number&etc all can be different ob$ects +)ese ob$ects are defined as classes and can be related to eac) ot)er -or e4ample& a Aat class can be a c)ild of t)e Animal class +)e c)ild class in)erits all met)ods of t)e parent class +)us ma,ing it easier to program t)e logic +)ese classes can )ave data in form of variables and )ave met)ods for actions t)at can be performed on t)ese classes !ince a class defines onl0 t)e data it needs to be concerned %it)& %)en an instance of t)at class (an ob$ect) is run& t)e code %ill not be able to accidentall0 access ot)er program data +)e classes are reusable across applications