ELEC 279 面向对象详细教程

【ELEC 279 面向对象详细教程】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
  13. 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.
  14. 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.”
  15. 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.
  16. Catch your exception and display the message of the exception.
  17. Define a new exception called NumberWayTooBigException that extends NumberTooBigException.
  18. 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.
  19. 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.”.
  20. 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.
  21. In your own words, describe the catch or declare rule.
  22. 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.
  23. True or false, a non static inner class can have static instance variables and methods.
  24. 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
  25. 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.
  26. Order the callback methods; onStart(), onResume(), and onCreate() in the order
    they are called in an activity lifecycle.
  27. During which callback method does the user become able to interact with the activity
    in an Android application?
  28. During which callback method an app release all resources from memory?
  29. Create an ArrayList with the identifier practice that contains 10 Date type objects.
  30. To this array list, change the first Date object in the list to an object with the
    identifier newDate.
  31. Get the element at index 5 and set it to the value of a Date variable with the
    identifier gotDate.
  32. Add to the end of the ArrayList an object with the identifier addedDate.
  33. Remove the element in the first index (index 0).
  34. Define a generic class GenericPractice that contains an instance variable of the type
    parameter type called genericVariable.
  35. Add a constructor to this class that takes in a parameter, genericVariable of the
    type parameter, and sets the instance variable to that value.
  36. Add an accessor for the genericVariable.
  37. Define a a new object called myGenericPractice of type GenericPractice, where the
    parameter type is String.
  38. Given the object, myObj, cast myObj to a GenericPractice type object with the
    identifier myGenericObj and a String type parameter.
  39. Define a new Generic class Generic whose type parameter can only be a class derived
    from the Date class.
  40. 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
  41. 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
  42. Given a String, myString, call the method you just created (assuming it is within
    the MyGeneric class) and give it myString as the parameter.
  43. Define a new method, exampleMethod, that takes in a GenericPractice type object
    (the generic class you defined earlier) and returns nothing.
  44. 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.
  45. 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
  46. In your own words, give two reasons why the ”better” clone method is better than
    the clone method that uses the copy constructor.
  47. What is the differences between Sets and Lists
  48. Write a line of code to create an empty HashSet of strings.
  49. Do optional methods of interfaces need to be implemented in a class that implements
    it?
  50. Given a HashSet myHash, add all the elements of the set to an empty ArrayList of
    strings you create with the identifier myList.
  51. How are elements ordered in a HashSet?
  52. Create an empty hash set of strings with the capacity of 10.
  53. Add the string ”Hello” to the HashSet from the previous question.
  54. 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
  55. My code:
  56. My code:
    public inter fa ce Measurable {
    public int measure ( S t ri n g dimen si on ) ;
    }
  57. My code:
    public c l a s s Table implements Measurable , Comparable{}
  58. 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 ;
    }
    }
  59. All the abstract methods from the abstract class, and any methods from the interface
    not already defined in the abstract class.
  60. All the methods from interfaces A and B.
  61. 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).
  62. False. The compiler only checks for correct syntax, not for if you have implemented
    an interface as intended.
  63. False.
  64. True.
  65. False.
  66. True.
  67. False.
    Page 6 of 10 – ELEC 279 - Lab 1 Week 2
    Week 8
  68. My code
    public boolean pickANumber ( int number ){
    i f ( number==myNumber)
    return True ;
    e l s e
    return F al s e ;
    }
  69. 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.
  70. 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 ;
    }
    }
  71. 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.
  72. 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 ;
    }
    }
  73. 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 ? ” ) ;
    }
  74. In your own words.
  75. My Code:
    Bird myBird = new Bird ( ) ;
    Bird . Nest myBirdNest = myBird .new Nest ( ) ;
    myBird . hatch ( ) ;
    myBirdNest . hatch ( ) ;
  76. False
  77. False
    Page 8 of 10 – ELEC 279 - Lab 1 Week 2
    Week 9
  78. 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 ;
    }
  79. onCreate(), onStart(), onResume()
  80. onResume()
  81. onDestroy()
  82. ArrayList practice = new ArrayList(10);
  83. practice.set(0, newDate);
  84. Date gotDate = practice.get(5)
  85. practice.add(addedDate);
  86. 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 ;
    }
    }
  87. GenericPractice myGenericPractice;
  88. GenericPractice myGenericObj = (GenericPractice) myObj;
  89. public class Generic2
  90. public class Generic34< T extends Cloneable, M extends Date Cloneable>
  91. public static T genericMethod(T param1) return param1;
  92. MyGeneric.genericMethod(myString);
  93. public void exampleMethod(GenericPractice param1);
  94. public void exampleMethod(GenericPractice param1);
    Page 9 of 10 – ELEC 279 - Lab 1 Week 2
  95. public void exampleMethod(GenericPractice param1);
  96. public void exampleMethod(GenericPractice param1);
    Week 10
  97. Watch Week 10 Video 0
  98. Lists allow for elements to occur more than once
  99. HashSet newSet = new HashSet<>();
  100. My code
    A r r ayLi s t myList = new A r r ayLi s t <>();
    myList . addAll (myHash ) ;
  101. Based on a hash function that maps keys (indices) to values (elements).
  102. HashSet newSet = new HashSet<>(10);
  103. newSet.add(”Hello”);
  104. newSet.retainAll(otherHash)
    Page 10 of 10 – ELEC 279 - Lab 1 Week 2
    • 推荐阅读