CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, or F if the statement is false. (a) T X F : If a child overrides its parent s method, it can still invoke the parent method with the super keyword, but if it does so, then it must invoke the parent method first. A parent class method can be invoked anywhere from the child method. It s only the constructor that has special requirements. (b) T X F : It is possible to create a concrete class in Java which does not support the tostring method. Every object inherits Object which has a tostring method which is always inherited. (c) X T F : The compiler will not compile a concrete class which implements an interface, but fails to implement all the methods in that interface. (d) X T F : A GUI contains code that manipulates a rectangular array of pixels, where each pixel is represented by three color values. (e) T X F : An abstract class is used to prevent a child class from overriding specific methods (such as a checkpassword method.) The final keyword prevents a child class from overriding a method. (f) X T F : Under the covers, the backing store for an ArrayList is an array of Object references. (g) X T F : In Java there is an expectation that if two objects are equal, they will have the same hashcode. (h) T X F : An interface defines the list of fields that must be provided by a class that implements that interface An interface defines the methods that must be implemented, not the fields. (i) X T F : An abstract class can implement an interface without implementing all the methods in the interface. The methods not yet implemented are considered abstract methods. (j) X T F : A child class cannot inherit the class methods of its parent. Class methods (or static methods) are not inherited. 2. (5 points) Given an array of strings such as String [] arr = { abc, acd,... ;, check all of the following which are valid ways to convert the array into an arraylist. X List<String> list = new ArrayList<>(); for(string str : arr) list. add(str); List<String> list = arr; X List<String> list = new ArrayList<>(); list.addall(arrays.aslist(arr)); List<String> list = arr.toarraylist(); X List<String> list = new ArrayList<>(Arrays.asList(arr)); String [] list = new ArrayList<>(Arrays.asList(arr)); Page 1 of 11
3. (5 points) In the java library, there is an interface called CharSequence, which defines the methods charat(int index), chars(), length(), subsequence(int start, int end), and tostring(). Note that the String class implements the CharSequence interface. Given the following Java line of code: CharSequence csq = Welcome to Java! ; Check all of the following which are valid invocations of methods: X csq.length() X csq.charat(3) csq.concat( Enjoy the ride! ) X csq.equals( Bienvenido a Java ) CharSequence.subSequence(0,3) Answer the following questions by filling in the blanks. 4. (10 points) Given the following Java lambda expressions and argument values, what result will the lambda expression produce? (a) x >x+3 with argument 3.0, (a) 6.0 (b) x > x x + 2 x + 1 with argument 3, (b) 16 (c) (x,y) >(x y) (x y) with arguments 9 and 2 (c) 49 (d) (x,y) >x.lst.compareto(y.lst)==0? x.lst.compareto(y.lst) : x. fst. compareto(y.fst) with arguments n1 and n2 where n1 and n2 are objects with public fst and lst fields with values: n1[ fst= Tom lst= Smith ], n2[fst= Tom lst= Smith ] (d) 0 (e) (x) > { if (0==x%2) return 0; if (0==x%3) return 3; return 1; with argument 21 (e) 3 Page 2 of 11
5. (10 points) Given the Java classes in Listing 1 on page 7, Listing 2 on page 7, Listing 3 on page 7, and Listing 4 on page 7, (a) If you execute until just before the first for loop in the Debate main method, complete the following graphical display of memory: (b) If you run java -cp test2.debate, what will get printed to the screen? Donald will build a wall. Hillary will pay your college tuition. Donald will make America great again. Hillary says we re stronger together! Page 3 of 11
6. (20 points) Given the class in Listing 5 on page 8, (a) What is the string produced by Frac f=new Frac(1,2); f.mult(new Frac(1,3)).toString()? Fraction [numerator=1 denominator=6] (b) Write the code for the add method of the Fraction class so that fraction addition is performed using a the formula: b + c d = ad+bc bd. public Fraction add ( Fraction that ) { long num = ( this. numerator that. denominator ) + ( that. numerator this. denominator ) ; long den = this. denominator that. denominator ; return new Fraction (num, den ) ; (c) Given two instances of class Fraction, Fraction frac1 and Fraction frac2, such that frac1. hashcode()==frac2.hashcode(), will frac1. equals( frac2) be true? Why or why not? Probably, but not required. hashcode maps the values of numerator and denominator to an integer, and it is possible that two unequal fractions map to the same hashcode. (d) Given two instances of class Fraction, Fraction frac1 and Fraction frac2, such that frac1.equals(frac2), will frac1==frac2 be true? Why or why not? Probably not. frac1 == frac2 implies frac1 and frac2 point to the same object, but equals allows two different objects with the same numerator and denominator to return true. (e) (5 points (bonus)) If you create a new Fraction(3,0), will the Fraction class work? If not, what is the first code that will break? When the creator invokes normalize, and it invokes gcd, gcd will get in an endless loop. Page 4 of 11
7. (10 points) Given the Java class in Listing 6 on page 8: (a) The constructor for the Square class invokes the getfont routine to get the value of the Font field associated with this square. How can the value for the font field be anything other than null, since this Square object has just been created? Since Square extends JButton, and the Square constructor does not invoke the JButton constructor explicitly with the super keyword, Java automatically invokes the JButton constructor with no arguments. The no-argument JButton constructor initializes the fields of JButton, including the font field. (b) The actionperformed method does not use the argument ActionEvent arg0. Why is it valid for the actionperformed method to ignore its argument? The only action registered to the Square object is a press of the button represented by this. Therefore, arg0 can ıonly be this. (c) Once the choose method is invoked on a specific Square object, can the actionperformed method ever get invoked? No. The choose method invokes setenabled(false) which prevents the button associated with this object from ever getting pressed again, and if the button cannot get pressed, the actionperformed method will not be invoked. (d) The actionperformed method returns immediately instead of allowing the computer to have a turn if the TicTacToe checkwinner method returns a true value. Can you speculate what the return value from checkwinner means? The TicTacToe checkwinner method must return a true value if someone has won the TicTacToe game (gotten three in a row.) If the human player won by putting an X in this square, there is no need for the computer to take a turn. Page 5 of 11
8. (10 points) For the following question, refer to the zoo package in Listing 7 on page 9, Listing 8 on page 9, Listing 10 on page 10, Listing 9 on page 9, and Listing 11 on page 10 (a) What would get printed to the screen if you run the main method from the Zoo class? I eat leaves and grass. I move with a lumbering walk. I just had one calf. I eat small animals. I move silently and sneakily. I just had two cubs. I eat mice. I move on my belly. I left 20 eggs to hatch. (b) Write a new sub-class of animal to put in a zoo. Your class should have a no-argument constructor. public c l a s s Monkey extends Animal { public Monkey ( ) { super ( bananas, by swinging on v i n e s, a cute baby monkey ) ; (c) Which line of code in the zoo package demonstrates the concept of polymorphism? Explain why that line shows polymorphism. The invocation of a.birth() in the main method of the Zoo class demonstrates polymorphism because a single instruction causes Java to invoke different methods from a single line of code. Note that a.eat() and a.move() invoke the same methods for each different kind of animal - the Animal.eat and the Animal.move methods - but have different field values. Thus, the invocations of a.eat and a.move are not strictly polymorphism. 9. (10 points) What will get printed by the main method in the Java code in Listing 12 on page 10? The third element is 19 List: 21 ->33 ->19 ->44 ->19 ->19 The third element is now 44 List: 19 ->19 ->44 ->19 ->33 ->21 10. (10 points) What output is produced by the main method in the Mapper class in Listing 13 on page 11: Big : [3.0, 3.0, 3.5, 4.5] Little: [1.5, 2.5, 3.0, 3.0] Page 6 of 11
Tear-off Pages Listing 1: Subconscious.java public class Subconscious { private S t r i n g name ; public Subconscious ( S t r i n g name) { this. name = name ; public S t r i n g getname ( ) { return name ; public void pr omi se ( ) { System. out. p r i n t l n (name + promises not to keep any promises. ) ; public void claim ( ) { System. out. p r i n t l n (name + w i l l r u l e the world! ) ; Listing 2: Red.java public class Red extends Subconscious { public Red( S t r i n g name) { super (name ) ; @Override public void pr omi se ( ) { System. out. p r i n t l n ( super. getname()+ w i l l b u i l d a wall. ) ; @Override public void claim ( ) { System. out. p r i n t l n ( super. getname()+ w i l l make America g r e a t again. ) ; Listing 3: Blue.java public class Blue extends Subconscious { public Blue ( S t r i n g name) { super (name ) ; @Override public void pr omi se ( ) { System. out. p r i n t l n ( super. getname()+ w i l l pay your c o l l e g e t u i t i o n. ) ; @Override public void claim ( ) { System. out. p r i n t l n ( super. getname()+ says we re s t r o n g e r t o g e t h e r! ) ; Listing 4: Debate.java public class Debate { public static void main ( S t r i n g [ ] args ) { Blue dem = new Blue ( H i l l a r y ) ; Red rep = new Red( Donald ) ; Subconscious [ ] p a r t i e s = { rep, dem ; for ( Subconscious party : p a r t i e s ) { party. promise ( ) ; for ( Subconscious party : p a r t i e s ) { party. claim ( ) ; Page 7 of 11
Tear-off Pages Listing 5: Fraction.java public class Fraction { private long numerator ; private long denominator ; public Fraction ( long numerator, long denominator ) { this. numerator = numerator ; this. denominator = denominator ; normalize ( ) ; public double todouble ( ) { return ( double ) numerator / denominator ; public void normalize ( ) { i f ( denominator <0) { denominator= denominator ; numerator= numerator ; long g = gcd (Math. abs ( numerator ), denominator ) ; i f ( g>1) { numerator /=g ; denominator /=g ; public Fraction add ( Fraction that ) { // Code not s u p p l i e d public Fraction mult ( Fraction that ) { return new Fraction ( this. numerator that. numerator, this. denominator that. denominator ) ; @Override public int hashcode ( ) { f i n a l int prime = 3 1 ; int r e s u l t = 1 ; r e s u l t = prime r e s u l t + ( int ) ( denominator ˆ ( denominator >>> 3 2 ) ) ; r e s u l t = prime r e s u l t + ( int ) ( numerator ˆ ( numerator >>> 3 2 ) ) ; return r e s u l t ; @Override public boolean e q u a l s ( Object obj ) { i f ( this == obj ) return true ; i f ( obj == null ) return f a l s e ; i f ( g e t C l a s s ( )!= obj. g e t C l a s s ( ) ) return f a l s e ; Fraction other = ( Fraction ) obj ; i f ( denominator!= other. denominator ) return f a l s e ; i f ( numerator!= other. numerator ) return f a l s e ; return true ; @Override public S t r i n g t o S t r i n g ( ) { return Fraction [ numerator= + numerator +, denominator= + denominator + ] ; static private long gcd ( long x, long y ) { i f ( x==y ) return x ; i f ( x<y ) return gcd ( y x, x ) ; return gcd ( x y, y ) ; Listing 6: Square.java public class Square extends JButton implements A c t i o n L i s t e n e r { Page 8 of 11
Tear-off Pages private static f i n a l long s erialversionuid = 1L ; int row ; int c o l ; TicTacToe game ; public Square ( TicTacToe game ) { this. game = game ; setfont ( getfont ( ). derivefont ( ( float ) 4 0. 0 ) ) ; s e t P r e f e r r e d S i z e (new Dimension ( 7 0, 7 0 ) ) ; setmargin (new I n s e t s ( 30, 30, 30, 30)); settext ( ) ; addactionlistener ( this ) ; public void choose ( S t r i n g val ) { settext ( val ) ; setenabled ( f a l s e ) ; @Override public void actionperformed ( ActionEvent arg0 ) { choose ( X ) ; i f ( game. checkwinner ( ) ) return ; game. computerturn ( ) ; game. checkwinner ( ) ; Listing 7: Animal.java package zoo ; public class Animal { S t r i n g food ; S t r i n g motion ; S t r i n g k i d s ; public Animal ( S t r i n g food, S t r i n g motion, S t r i n g k i d s ) { this. food = food ; this. motion = motion ; this. k i d s = k i d s ; S t r i n g eat ( ) { return I eat + food +. ; S t r i n g move ( ) { return I move + motion +. ; S t r i n g b i r t h ( ) { return I j u s t had + k i d s +. ; Listing 8: Tiger.java package zoo ; public class Tiger extends Animal{ public Tiger ( ) { super ( small animals, s i l e n t l y and s n e a k i l y, two cubs ) ; pacakge zoo ; Listing 9: Elephant.java Page 9 of 11
Tear-off Pages public c l a s s Elephant extends Animal { public Elephant ( ) { super ( l e a v e s and g r a s s, with a lumbering walk, one c a l f ) ; Listing 10: Snake.java package zoo ; public c l a s s Snake extends Animal { public Snake ( ) { super ( mice, on my b e l l y, 20 h a t c h l i n g s ) ; @Override S t r i n g b i r t h ( ) { return I l e f t 20 eggs to hatch. ; Listing 11: Zoo.java package zoo ; public class Zoo { ArrayList <Animal> animals ; public Zoo ( ) { this. animals = new ArrayList<Animal >(); public boolean add ( Animal e ) { return animals. add ( e ) ; public s t a t i c void main ( S t r i n g [ ] a r g s ) { Zoo zoo=new Zoo ( ) ; zoo. add (new Elephant ( ) ) ; zoo. add (new Tiger ( ) ) ; zoo. add (new Snake ( ) ) ; for ( Animal a : zoo. animals ) { System. out. p r i n t l n ( a. eat ( ) + + a. move ( ) + + a. b i r t h ( ) ) ; public class LinkedList { private int value ; private LinkedList next ; Listing 12: LinkedList.java public LinkedList ( int value ) { this. value = value ; public LinkedList add ( int value ) { i f ( next==null ) next=new LinkedList ( value ) ; else next. add ( value ) ; return this ; public int get ( int n ) { i f ( n==0) return value ; i f ( next==null ) throw new IllegalArgumentException ( I l l e g a l index ) ; return next. get (n 1); public LinkedList r e v e r s e ( ) { i f ( next==null ) return this ; return next. r e v e r s e ( ). add ( value ) ; public S t r i n g t o S t r i n g ( ) { S t r i n g r e s u l t= + value ; i f ( next!= null ) r e s u l t = r e s u l t + > + next. t o S t r i n g ( ) ; Page 10 of 11
Tear-off Pages return r e s u l t ; public s t a t i c void main ( S t r i n g [ ] a r g s ) { LinkedList l s t = new LinkedList ( 2 1 ) ; l s t. add ( 3 3 ) ; l s t. add ( 1 9 ) ; l s t. add ( 4 4 ) ; l s t. add ( 1 9 ) ; l s t. add ( 1 9 ) ; System. out. p r i n t l n ( The t h i r d element i s + l s t. get ( 2 ) ) ; System. out. p r i n t l n ( L i s t : + l s t ) ; l s t=l s t. r e v e r s e ( ) ; System. out. p r i n t l n ( The t h i r d element i s now + l s t. get ( 2 ) ) ; System. out. p r i n t l n ( L i s t : + l s t ) ; import java. u t i l. Arrays ; import java. u t i l. f u n c t i o n. Function ; Listing 13: Mapper.java public class Mapper { public double [ ] map( double [ ] rawdata, Function<Double, Double> f n ) { double [ ] r e s u l t=null ; i f ( rawdata!= null ) { r e s u l t = new double [ rawdata. l e n g t h ] ; for ( int i =0; i <rawdata. l e n g t h ; i ++) { r e s u l t [ i ]= fn. apply ( rawdata [ i ] ) ; return r e s u l t ; public s t a t i c void main ( S t r i n g [ ] a r g s ) { Mapper f o r a l l = new Mapper ( ) ; double [ ] data = { 1. 5, 2. 5, 3. 5, 4. 5 ; double [ ] big = f o r a l l. map( data, d >{ i f (d>3.0) return d ; else return 3. 0 ; ) ; double [ ] l i t t l e = f o r a l l. map( data, d >(d<=3.0)?d : 3. 0 ) ; System. out. p r i n t l n ( Big : + Arrays. t o S t r i n g ( big ) ) ; System. out. p r i n t l n ( L i t t l e : + Arrays. t o S t r i n g ( l i t t l e ) ) ; Question: 1 2 3 4 5 6 7 8 9 10 Total Points: 10 5 5 10 10 20 10 10 10 10 100 Bonus Points: 0 0 0 0 0 5 0 0 0 0 5 Page 11 of 11