• No results found

3 length + 23 size = 2

N/A
N/A
Protected

Academic year: 2021

Share "3 length + 23 size = 2"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

This  pledged  exam  is  closed  textbook,  open  class  web  site,  and  two  pieces  of  paper  with  notes.  You  may  not   access  any  other  code  or  websites  including  your  own.  Write  your  email  id  and  name  on  every  page  of  the   test.  

The  only  window  to  be  open  on  your  computer  is  a  single  browser.  

Whenever  you  are  writing  a  message  method  as  your  answer  to  an  exercise,  there  is  a  grading  preference   for  the  use  of  the  this  variable  and  inspectors  and  mutators  when  appropriate.  

Pledge:

Warm  up  

1. Write   a   complete   program  Shoe.java   that   prompts   its   user   for   the   length   of   the   user’s   foot   in  

centimeters.   The   program   reports   the   person’s   metric   shoe   size.   Metric   shoe   size   can   be                                 approximated  as  follows:  

size

=

3

×

length

+

23

2

⎢

⎣

⎢

⎥

⎦

⎥

,   where   €

⎣ ⎦

 are  the  mathematical  symbols  for  casting  a  decimal  value  to  an  integer.  The  following  

demonstrates  a  sample  run.  

Enter foot length (centimeters): 29.5 Metric shoe size: 39

Your  answer  

import java.util.*; public class Shoe {

public static void main( String[] args ) {  

(2)

2. Suppose  x  and  y  are  initialized  object  variables  of  the  same  type  (e.g.,  they  are  both  Color  variables).     Complete  the  initialization  of  boolean  variable  b  so  that  it  indicates  whether  x  and  y  reference  the   same  object.  

boolean b = ______________________________________________________________ ;

 

3. Suppose  x  and  y  are  initialized  object  variables  of  the  same  type  (e.g.,  they  are  both  Color  variables).     Complete   the   initialization   of  boolean   variable  b   so   that   it   indicates   whether  x   and  y   reference   objects  with  like  attributes.  

boolean b = ______________________________________________________________ ;

Adding  functionality  to  existing  classes  

4. Define  a  void  message  method  absolute()  that  is  to  be  part  of  the  class  Calculator.  The  method   updates  the  total  of  its  calculator  to  its  absolute  value.  The  following  code  segment  demonstrates  its   usage.    

Calculator c = new Calculator();

c.add( -7 ); // running total is now -7

c.absolute(); // running total is now 7

Your  answer  

public void absolute() {

5. Define   a  void   message   method  square()   that   is   to   be   part   of   the   class  Calculator.   The   method   updates   the   total   of   its   calculator   to   be   the   square   of   its   current   value.   The   following   code   segment   demonstrates  its  usage.    

Calculator c = new Calculator();

c.add( 7 ); // running total is now 7

(3)

6. Define  a  Rational  message  method  invert()  that  is  to  be  part  of  the  class  Rational.  The  method   returns   a   new  Rational   that   is   a   flipping   of   its   rational;   i.e.,   its   numerator   becomes   the   new   Rational’s  denominator  and  its  denominator  becomes  the  new  Rational’s  numerator.  The  following   code  segment  demonstrates  its  usage.    

Rational a = new Rational( 3, 5 ); // a is 3 / 5

Rational b = a.invert(); // b is 5 / 3

Your  answer  

public Rational invert() {

7. Define  a  void  message  method  reduce()  that  is  to  be  part  of  the  class  Rational.  The  method  takes   one  int  parameter  v.    If  v  is  a  factor  of  both  its  numerator  and  the  denominator,  then  the  method   divides   both   of   them   by  v.     If   instead  v   is   not   a   common   factor,   then   no   changes   are   made   to   its   numerator  and  denominator.  The  following  code  segment  demonstrates  its  usage.    

Rational a = new Rational( 6, 9 ); // a is 6 / 9

a.reduce( 7 ); // a is 6 / 9 as 7 is not a common factor

a.reduce( 3 ); // a is 2 / 3 as 3 is a common factor

Your  answer  

(4)

8. Define  a  void  message  method  swap()  that  is  to  be  part  of  the  class  Rational.  The  method  takes  one   Rational   parameter  r.     The   method   swaps   the   numerator   and   denominator   of   its   rational   with   Rational  r.    The  following  code  segment  demonstrates  its  usage.    

Rational a = new Rational( 6, 9 ); // a is 6 / 9

Rational b = new Rational( 3, 4 ); // b is 3 / 4

a.swap( b ); // a is 3 / 4 and b is 6 / 9

Your  answer  

public void swap( Rational r ) {

9. Define  a  boolean  message  method  isOlderThan()  that  is  to  be  part  of  the  class  Song.  The  method   takes  one  Song  parameter  s  and  returns  whether  its  song  has  an  earlier  recording  year  than  Song s.   The  following  code  segment  demonstrates  its  usage.    

Song s1 = new Song( n1, p1, y1 ); Song s2 = new Song( n2, p2, y2 );

boolean b = s1.isOlder( s2 ); // is s1 recorded earlier than s2

Your  answer  

(5)

10. Define  a  boolean  message  method  atMoreThan()  that  is  to  be  part  of  the  class  Movie.  The  method   takes  one  Movie  parameter  m  and  returns  whether  its  movie  is  playing  at  more  theaters  than  Movie m.     The  following  code  segment  demonstrates  its  usage.    

Movie m1 = new Movie( "Water for Elephants" ); Movie m2 = new Movie( "Thor" );

...

boolean b = m1.atMoreThan( m2 ); // is m1 at more theaters than m2

Your  answer  

public boolean atMoreThan( Movie m ) {

Library  methods  

11. Write  an  int  library  method  factorial()  that  takes  one  int  parameter  n.  The  loop-­‐based  method   returns  n!;  i.e.,    1  *  2  *  3  *  …  *  n.  The  following  code  segment  demonstrates  its  usage.  

int v = factorial( 5 ); // v is 120; i.e., 1 * 2 * 3 * 4 * 5

Your  answer  

public static int factorial( int n ) {                    

(6)

12. Write  an  int  library  method  choose()  that  takes  two  int  parameter  n  and  k.  The  method  returns   n!  /  (k!  *  (n  -  k)!  )  

The  following  code  segment  demonstrates  its  usage.   int a = choose( 2, 2 ); // is 1

int b = choose( 3, 2 ); // is 3

int c = choose( 4, 2 ); // is 6

Your  answer  

public static int choose( int n, int k ) {

13. Write  a  recursive  int  library  method  gcd()  that  takes  two  int  parameter  x  and  y  and  returns  their   greatest  common  divisor.  

• If  y  is  zero,  the  greatest  common  divisor  of  x  and  y  equals  x.  

• If  y  is  not  zero,  the  greatest  common  divisor  of  x  and  y  equals  the  greatest  common  divisor  of  y   and  r,  where  r  is  the  remainder  of  x  divided  by  y.  

The  following  code  segment  demonstrates  its  usage.   int n = gcd( 72, 54 ); // is 18

Your  answer  

(7)

14. Write  a  recursive  int  library  method  choose()  that  takes  two  int  parameter  n  and  k  and  returns   the  number  of    ways  subsets  of  size  k  can  be  made  from  a  set  of  size  n.  

• If  k  is  0,  then  the  number  of  ways  of  making  the  subsets  is  1.  

• Otherwise,  if  n  is  0,  then  the  number  of  ways  of  making  the  subsets  is  0;  

• Otherwise,  the  number  of  ways  of  making  the  subsets  is  the  sum  of  two  quantities:   o The  number  of  ways  subsets  of  size  k  –  1  can  be  made  from  a  set  of  size  n  –  1;   o The  number  of  ways  subsets  of  size  k  can  be  made  from  a  set  of  size  n  –  1.   The  following  code  segment  demonstrates  its  usage.  

int a = choose( 2, 2 ); // is 1

int b = choose( 3, 2 ); // is 3

int c = choose( 4, 2 ); // is 6

Your  answer  

public static int choose( int n, int k ) {

Library  methods  for  array  manipulation  

15. Write  an  int  library  method  product()  that  takes  one  int[]  parameter  x.  The  method  returns  the   product  of  the  element  values.  The  following  code  segment  demonstrates  its  usage.  

int[] a = { 3, 1, 4, 1, 5, 9 };

int p = product( a ); // p is 540; i.e., 3 * 1 * 4 * 1 * 5 * 9

Your  answer  

(8)

16. Write   a  boolean   library   method  gotIt()   that   takes   a   single  int[]   parameter  x   and   one  int   parameter  v.  The  method  returns  whether  any  of  the  elements  in  x  are  equal  to  v.  The  following  code   segment  demonstrates  its  usage.  

int[] a = { 3, 1, 4, 1, 5, 9 };

boolean b1 = gotIt( a, 4 ); // b1 is true, because x has a 4

boolean b2 = gotIt( a, 8 ); // b2 is false, because x does not have a 8

Your  answer  

public static boolean gotIt( int[] x, int v ) {

17. Write   a  boolean   library   method  isIncreasing()   that   takes   a   single  int[]   parameter  x.   The   method  returns  whether  the  element  values  for  array  x  are  in  strictly  increasing  order.  

int[] a = { 3, 1, 4 }; int[] b = { 1, 3, 3, 4 } int[] c = { 1, 3, 4, 5, 9 };

boolean b1 = isIncreasing( a ); // is false as 3 >= 1

boolean b2 = isIncreasing( b ); // is false as 3 >= 3

boolean b3 = isIncreasing( c ); // is true as 1 < 3 < 4 < 5 < 9

Your  answer  

(9)

18. Write  a  int[]  library  method  glue()  that  takes  a  two  int[]  parameters  x  and  y  (the  arrays  can   have  different  numbers  of  elements).  The  method  returns  a  new  int[]  array  whose  length  is  the  sum   of   the   lengths   of   arrays  x   and  y.   The   initial   elements   in   the   new   array   are   to   come   from  x   and   the   remaining  elements  are  to  come  from  y.  Suggestion:  use  two  while  loops.  

int[] a = { 3, 1, 4 }; int[] b = { 1, 5, 9, 2 };

int[] c = glue( a, b ); // c is 3, 1, 4, 1, 5, 9, 2

Your  answer  

public static int[] glue( int[] x, int[] y ) {

19. Write  an  int  library  method  count()  that  takes  one  int[][]  parameter  x  and  one  int  parameter   v.     The   method   returns   the   number   of   elements   in   the   two-­‐dimensional   array   that   equal  v.   The   following  code  segment  demonstrates  its  usage.  

int[][] a = { { 3, 1, 4, 1 }, { 5, 9, 2, 6 } }; int n1 = count( x, 1 ); // n1 becomes 2

int n2 = count( x, 12 ); // n2 becomes 0

Your  answer  

(10)

Class  Pixel  implementation  –  see  handout  for  specification  

 

22. public static boolean legalLevel( int n ) {

23. public Pixel() {

24. public Pixel( int r, int g, int b ) {

(11)

26. public void setRed( int r ) {

27. public void setGreen( int g ) {

28. public void setBlue( int b ) {

29. public void setRGB( int r, int g, int b ) {

(12)

31. public int getGreen() {

32. public int getBlue() {

33. public Color toColor() {

(13)

36. public boolean sameGreen( Pixel p ) {

37. public boolean sameBlue( Pixel p ) {

38. public boolean same( Pixel p ) {

(14)

40. public String toString() {

20. True  or  false  

• True                    False   I  am  part  of  the  class  picture.      

• True                    False   Since  the  last  test  I  have  attended  every  class  or  had  an  excused  absence.       • True                    False   I  did  the  Collab  survey  for  this  class.  

• True                    False   I  did  the  exit  survey  for  this  class.  

Page 1 ________ / 12 Page 8 ________ / 8 Page 2 ________ / 16 Page 9 ________ / 8 Page 3 ________ / 8 Page 10 ________ / 12 Page 4 ________ / 8 Page 11 ________ / 15 Page 5 ________ / 8 Page 12 ________ / 15 Page 6 ________ / 8 Page 13 ________ / 12

References

Related documents

An examination of trends by race/ethnicity indicate that Asian/Native American, Hispanic, and White students in charter schools all show greater percentages of proficient or

This section outlines the method to find the best allocation of n distinguishable processors to m dis- tinguishable blocks so as to minimize the execution time.. Therefore,

• Storage node - node that runs Account, Container, and Object services • ring - a set of mappings of OpenStack Object Storage data to physical devices To increase reliability, you

The Department of Health, Physical Education, Recreation and Dance offers a Master of Science in Education in Health and Physical Education and a Master of Science in

We are now using the second part of our test database (see Figure 4 ) ; the boxpoints table which contains 3000 customer points, and the box table with 51 dierent sized bounding

ter mean to the prototypes computed from the true labels of all the samples. Similar to the semi-supervised scenario, we use a PN trained in the episodic mode as the feature

Although total labor earnings increase with the unskilled unions’ bargaining power, we can say nothing when the increase in production is due to stronger skilled unions, since

prediction of onsets of major depression and generalized anxiety, Arch Gen Psychiatry, 60: 8, 789-796. Fair society, healthy lives: strategic review of health inequalities in