ELEC 279 - Winter 2021
Introduction to Object-Oriented Programming
Quiz 3 Practice Problems - Week 11
I am changing things up a bit for the Quiz 3 practice problems. As I was creating the
quiz I realized that most of the content from the last few weeks was application based,
instead of knowledge based. Due to this, there are a greater number of written (long)
answer questions on the Quiz than in Quiz 1 and Quiz 2 and much fewer fact based
multiple choice questions. I did not feel that giving you all a bunch of the cut multiple
choice questions would adequately prepare you for the quiz.
Instead I have compiled for you all of the practice problems from weeks 7-10 that are
the most applicable for the quiz. I have reworded and adjusted some of them to
better reflect the phrasing you would see in the quiz, and be less like a lab,
as well as added a few additional ones. (What I have cut, and reworded/ added
should be very telling to what you will and won’t see on the Quiz) These will be a great
way to ensure that you have not only an understand the course concepts but can use
them. Written solutions are at the end of the document. For a verbal explanation of any
question you don’t understand the answer for see the respective practice problem videos
from that particular week. (I am also doing things this way as the number of people who
have viewed, let alone, done the practice problems is very low and the practice problems
are honestly the best way to prepare you for the quiz)
Page 1 of 10 – ELEC 279 - Lab 1 Week 2
Practice Problems
Week 7
- Define an interface called Measurable. Inside the interface define a public method,
measure, that takes in one String parameter and returns an int. - Define a class Table that implements the interface from the last question, and the
Comparable interface. - Define a compareTo method for a class that takes in Object type parameter, and is
used to compare an int type instance variable i, such that the object with the lower
value of i is before the other. - If an abstract class implements an interface, what methods must a concrete class
derived from the abstract class define? - If an interface B, is derived from another interface, A, a concrete class that implements
interface B must contain method definitions from which interface(s)? - True or false, the compiler will check to see if you have define a clone method in a
class that implements the Cloneable interface. Explain. - True or false, the compiler will check to see if you have correctly defined the compareTo
method if you create a class that implements the Compareable interface.
Explain. - True or false, a class can be derived from more than one abstract class.
- True or false, a class can implement more than one interface.
- True or false, interfaces contain instance variables.
- True or false, abstract classes can contain complete method definitions.
- True or false, methods in interfaces can be declared as private.
Page 2 of 10 – ELEC 279 - Lab 1 Week 2
Week 8 - To practice error handling, we first need some code that can cause exceptions/ errors.
Let’s define a method, pickANumber, that takes in a single int type parameter, and
returns a boolean value. This function will be used to compare the parameter with
an int type instance variable called myNumber which is between 1 and 10 inclusive.
If the numbers are the same the method returns True, if not it returns False. - Define a new exception called NumberTooBigException that extends Exception.
Create a constructor for the exception such that when the exception is caught it
can display ”The number INSERTNUMBERHERE is too big.” - Put a try block around your code in the pickANumberMethod, and create an if
statement to check if the value of the parameter is greater than 10. If the number
is greater than 10, throw a NumberTooBigException exception. - Catch your exception and display the message of the exception.
- Define a new exception called NumberWayTooBigException that extends NumberTooBigException.
- Add to the code in your pickANumberMethod code to check if the parameter is
greater than 50. If it is, throw a new NumberWayTooBigException exception.
Think critically about the order of your if-else statements. - Catch the NumberTooBigException. Think critically about the order of your catch
blocks. Inside the catch block for the NumberWayTooBigException output the
message of the exception, followed by ”Try a number way smaller.”. - Include code in your pickANumber method that will output to the user ”Do you want
to guess again?” after any messages from exceptions, whether or not an exception
does occur. - In your own words, describe the catch or declare rule.
- Given a class called Bird which has an inner class called Nest, where both the Bird
class and the Nest class have a method called hatch() that takes in no parameters,
do the following:
(a) Create a new object called myBird of type bird.
(b) Create a new object of type nest called myBirdNest.
(c) Run the hatch method with myBird.
(d) Run the hatch method with myBirdNest. - True or false, a non static inner class can have static instance variables and methods.
- True or false, a non static inner class can have a static inner class.
Page 3 of 10 – ELEC 279 - Lab 1 Week 2
Week 9 - Let’s say there is a class, PracticeProblem with instance variables; int numQuestions,
Date currDate, String author, Problem problem1. Assuming the Problem and Date
classes already have a clone method, define a clone method of the practice problem
class, avoiding privacy leaks, using the ”better” clone method. - Order the callback methods; onStart(), onResume(), and onCreate() in the order
they are called in an activity lifecycle. - During which callback method does the user become able to interact with the activity
in an Android application? - During which callback method an app release all resources from memory?
- Create an ArrayList with the identifier practice that contains 10 Date type objects.
- To this array list, change the first Date object in the list to an object with the
identifier newDate. - Get the element at index 5 and set it to the value of a Date variable with the
identifier gotDate. - Add to the end of the ArrayList an object with the identifier addedDate.
- Remove the element in the first index (index 0).
- Define a generic class GenericPractice that contains an instance variable of the type
parameter type called genericVariable. - Add a constructor to this class that takes in a parameter, genericVariable of the
type parameter, and sets the instance variable to that value. - Add an accessor for the genericVariable.
- Define a a new object called myGenericPractice of type GenericPractice, where the
parameter type is String. - Given the object, myObj, cast myObj to a GenericPractice type object with the
identifier myGenericObj and a String type parameter. - Define a new Generic class Generic whose type parameter can only be a class derived
from the Date class. - Define a new Generic class Generic34 which has two type parameters, one that can
only be a class that implements the Cloneable interface and one that can only be a
class that implements the Cloneable interface and is derived from the Date class - Define a static generic method that takes in a parameter, param1 of the type parameter,
and returns the parameter. (I know it makes no sense, just needed an easy
example)
Page 4 of 10 – ELEC 279 - Lab 1 Week 2 - Given a String, myString, call the method you just created (assuming it is within
the MyGeneric class) and give it myString as the parameter. - Define a new method, exampleMethod, that takes in a GenericPractice type object
(the generic class you defined earlier) and returns nothing. - Alter the method heading from the previous question such that the method will only
accept GenericPractice objects with a type parameter of a class that implements
the Cloneable interface. - Alter the method heading from the previous question such that the method will
only accept GenericPractice objects with a type parameter of a class that is used
to derive the Date class.
Week 10 - In your own words, give two reasons why the ”better” clone method is better than
the clone method that uses the copy constructor. - What is the differences between Sets and Lists
- Write a line of code to create an empty HashSet of strings.
- Do optional methods of interfaces need to be implemented in a class that implements
it? - Given a HashSet myHash, add all the elements of the set to an empty ArrayList of
strings you create with the identifier myList. - How are elements ordered in a HashSet?
- Create an empty hash set of strings with the capacity of 10.
- Add the string ”Hello” to the HashSet from the previous question.
- Remove all elements from the HashSet in the previous question that are not contained
in the HashSet otherHash.
Page 5 of 10 – ELEC 279 - Lab 1 Week 2
Solutions
Week 7 - My code:
- My code:
public inter fa ce Measurable {
public int measure ( S t ri n g dimen si on ) ;
} - My code:
public c l a s s Table implements Measurable , Comparable{}
- My code:
public int compareTo ( Ob jec t o the rOb j ){
i f ( i < o the rOb j . y )
return −1;
e l s e i f ( i == o therOb j . i )
return 0 ;
e l s e
return 1 ;
}
}- All the abstract methods from the abstract class, and any methods from the interface
not already defined in the abstract class.- All the methods from interfaces A and B.
- False. The Cloneable interface contains no method headings, so you do not need
to define a clone() method in the class (though you should unless you only have
primitive type instance variables).- False. The compiler only checks for correct syntax, not for if you have implemented
an interface as intended.- False.
- True.
- False.
- True.
- False.
Page 6 of 10 – ELEC 279 - Lab 1 Week 2
Week 8- My code
public boolean pickANumber ( int number ){
i f ( number==myNumber)
return True ;
e l s e
return F al s e ;
}- My code
public c l a s s NumberTooBigException extends Excep ti on {
public NumberTooBigException ( int number ){
super ( ”The number ”+ number+” i s t o o bi g . ” ) ;
}
}
3.- My code
public boolean pickANumber ( int number ){
try{
i f ( number>10)
throw(new NumberTooBigException ( number ) ) ;
i f ( number==myNumber)
return True ;
e l s e
return F al s e ;
}catch ( NumberTooBigException e ){
System . out . p r i n t l n ( e . ge tMessage ( ) ) ;
return F al s e ;
}
}- My code
public c l a s s NumberWayTooBigException extends NumberTooBigException{
public NumberWayTooBigException ( int number ){
super ( number ) ;
}
}
Page 7 of 10 – ELEC 279 - Lab 1 Week 2
6.- My code
public boolean pickANumber ( int number ){
try{
i f ( number>50)
throw(new NumberWayTooBigException ( number ) ) ;
e l s e i f ( number>10)
throw(new NumberTooBigException ( number ) ) ;
i f ( number==myNumber)
return True ;
e l s e
return F al s e ;
}catch ( NumberWayTooBigException e ){
System . out . p r i n t l n ( e . ge tMessage ( ) ) ;
System . out . p r i n t l n ( ”Try a number way sm all e r . ” ) ;
return F al s e ;
}catch ( NumberTooBigException e ){
System . out . p r i n t l n ( e . ge tMessage ( ) ) ;
return F al s e ;
}
}- My code
. . . . .
f i n a l l y {
System . out . p r i n t l n ( ”Do you want t o g u e s s a g ai n ? ” ) ;
}- In your own words.
- My Code:
Bird myBird = new Bird ( ) ;
Bird . Nest myBirdNest = myBird .new Nest ( ) ;
myBird . hatch ( ) ;
myBirdNest . hatch ( ) ;- False
- False
Page 8 of 10 – ELEC 279 - Lab 1 Week 2
Week 9- My code
public P r ac ticeP r oblem cl o n e ( P r ac ticeP r oblem o t h e r ) {
P r ac ticeP r oblem copy = ( P r ac ticeP r oblem ) super . cl o n e ( ) ;
copy . cu r rD a te = o t h e r . cu r rD a te . cl o n e ( ) ;
copy . problem1 = o t h e r . problem1 . cl o n e ( ) ;
return copy ;
}- onCreate(), onStart(), onResume()
- onResume()
- onDestroy()
- ArrayList
practice = new ArrayList (10); - practice.set(0, newDate);
- Date gotDate = practice.get(5)
- practice.add(addedDate);
- practice.remove(0);
10.
public c l a s s G e n e ri cP r a c ti c e{
private T g e n e r i c V a r i a b l e ;
public G e n e ri cP r a c ti c e (T g e n e r i c V a r i a b l e ){
th is . g e n e r i c V a r i a b l e = g e n e r i c V a r i a b l e ;
}
public T g e tG e n e ri c V a ri a bl e ( ) {
return g e n e r i c V a r i a b l e ;
}
}- GenericPractice
myGenericPractice; - GenericPractice
myGenericObj = (GenericPractice ) myObj; - public class Generic2
- public class Generic34< T extends Cloneable, M extends Date Cloneable>
- public static
T genericMethod(T param1) return param1; - MyGeneric.
genericMethod(myString); - public void exampleMethod(GenericPractice> param1);
- public void exampleMethod(GenericPractice extends Cloneable> param1);
Page 9 of 10 – ELEC 279 - Lab 1 Week 2- public void exampleMethod(GenericPractice extends Cloneable> param1);
- public void exampleMethod(GenericPractice super Date> param1);
Week 10- Watch Week 10 Video 0
- Lists allow for elements to occur more than once
- HashSet
newSet = new HashSet<>(); - My code
A r r ayLi s tmyList = new A r r ayLi s t <>();
myList . addAll (myHash ) ;- Based on a hash function that maps keys (indices) to values (elements).
- HashSet
newSet = new HashSet<>(10); - newSet.add(”Hello”);
- newSet.retainAll(otherHash)
Page 10 of 10 – ELEC 279 - Lab 1 Week 2你可能感兴趣的:(后端)
- My code: