首页 > > 详细

代写ELEC 279编程课程、Java,c/c++,Python程序代写代写Web开发|调试Matlab程序

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
1. Define an interface called Measurable. Inside the interface define a public method,
measure, that takes in one String parameter and returns an int.
2. Define a class Table that implements the interface from the last question, and the
Comparable interface.
3. 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.
4. If an abstract class implements an interface, what methods must a concrete class
derived from the abstract class define?
5. 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)?
6. 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.
7. 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.
8. True or false, a class can be derived from more than one abstract class.
9. True or false, a class can implement more than one interface.
10. True or false, interfaces contain instance variables.
11. True or false, abstract classes can contain complete method definitions.
12. True or false, methods in interfaces can be declared as private.
Page 2 of 10 – ELEC 279 - Lab 1 Week 2
Week 8
1. 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.
2. 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.”
3. 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.
4. Catch your exception and display the message of the exception.
5. Define a new exception called NumberWayTooBigException that extends NumberTooBigException.
6. 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.
7. 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.”.
8. 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.
9. In your own words, describe the catch or declare rule.
10. 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.
11. True or false, a non static inner class can have static instance variables and methods.
12. 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
1. 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.
2. Order the callback methods; onStart(), onResume(), and onCreate() in the order
they are called in an activity lifecycle.
3. During which callback method does the user become able to interact with the activity
in an Android application?
4. During which callback method an app release all resources from memory?
5. Create an ArrayList with the identifier practice that contains 10 Date type objects.
6. To this array list, change the first Date object in the list to an object with the
identifier newDate.
7. Get the element at index 5 and set it to the value of a Date variable with the
identifier gotDate.
8. Add to the end of the ArrayList an object with the identifier addedDate.
9. Remove the element in the first index (index 0).
10. Define a generic class GenericPractice that contains an instance variable of the type
parameter type called genericVariable.
11. Add a constructor to this class that takes in a parameter, genericVariable of the
type parameter, and sets the instance variable to that value.
12. Add an accessor for the genericVariable.
13. Define a a new object called myGenericPractice of type GenericPractice, where the
parameter type is String.
14. Given the object, myObj, cast myObj to a GenericPractice type object with the
identifier myGenericObj and a String type parameter.
15. Define a new Generic class Generic whose type parameter can only be a class derived
from the Date class.
16. 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
17. 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
18. Given a String, myString, call the method you just created (assuming it is within
the MyGeneric class) and give it myString as the parameter.
19. Define a new method, exampleMethod, that takes in a GenericPractice type object
(the generic class you defined earlier) and returns nothing.
20. 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.
21. 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
1. In your own words, give two reasons why the ”better” clone method is better than
the clone method that uses the copy constructor.
2. What is the differences between Sets and Lists
3. Write a line of code to create an empty HashSet of strings.
4. Do optional methods of interfaces need to be implemented in a class that implements
it?
5. Given a HashSet myHash, add all the elements of the set to an empty ArrayList of
strings you create with the identifier myList.
6. How are elements ordered in a HashSet?
7. Create an empty hash set of strings with the capacity of 10.
8. Add the string ”Hello” to the HashSet from the previous question.
9. 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
1. My code:
2. My code:
public inter fa ce Measurable {
public int measure ( S t ri n g dimen si on ) ;
}
3. My code:
public c l a s s Table implements Measurable , Comparable{}
4. 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 ;
}
}
5. All the abstract methods from the abstract class, and any methods from the interface
not already defined in the abstract class.
6. All the methods from interfaces A and B.
7. 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).
8. False. The compiler only checks for correct syntax, not for if you have implemented
an interface as intended.
9. False.
10. True.
11. False.
12. True.
13. False.
Page 6 of 10 – ELEC 279 - Lab 1 Week 2
Week 8
1. My code
public boolean pickANumber ( int number ){
i f ( number==myNumber)
return True ;
e l s e
return F al s e ;
}
2. 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.
4. 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 ;
}
}
5. 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.
7. 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 ;
}
}
8. 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 ? ” ) ;
}
9. In your own words.
10. My Code:
Bird myBird = new Bird ( ) ;
Bird . Nest myBirdNest = myBird .new Nest ( ) ;
myBird . hatch ( ) ;
myBirdNest . hatch ( ) ;
11. False
12. False
Page 8 of 10 – ELEC 279 - Lab 1 Week 2
Week 9
1. 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 ;
}
2. onCreate(), onStart(), onResume()
3. onResume()
4. onDestroy()
5. ArrayList practice = new ArrayList(10);
6. practice.set(0, newDate);
7. Date gotDate = practice.get(5)
8. practice.add(addedDate);
9. 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 ;
}
}
13. GenericPractice myGenericPractice;
14. GenericPractice myGenericObj = (GenericPractice) myObj;
15. public class Generic2
16. public class Generic34< T extends Cloneable, M extends Date Cloneable>
17. public static T genericMethod(T param1) return param1;
18. MyGeneric.genericMethod(myString);
19. public void exampleMethod(GenericPractice param1);
20. public void exampleMethod(GenericPractice param1);
Page 9 of 10 – ELEC 279 - Lab 1 Week 2
21. public void exampleMethod(GenericPractice param1);
22. public void exampleMethod(GenericPractice param1);
Week 10
1. Watch Week 10 Video 0
2. Lists allow for elements to occur more than once
3. HashSet newSet = new HashSet<>();
4. My code
A r r ayLi s t myList = new A r r ayLi s t <>();
myList . addAll (myHash ) ;
5. Based on a hash function that maps keys (indices) to values (elements).
6. HashSet newSet = new HashSet<>(10);
7. newSet.add(”Hello”);
8. newSet.retainAll(otherHash)
Page 10 of 10 – ELEC 279 - Lab 1 Week 2

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp2
热点文章
程序代写更多图片

联系我们 - QQ: 99515681 微信:codinghelp2
© 2014 www.7daixie.com
程序代写网!