edhesive Term 2 Unit 5Week 4 definitions Questions 012 refer

edhesive Term 2 Unit 5-Week 4 definitions: Questions 0-12 refer to the following class publie elasa Battery private boolean fullyCharg charge private String type: publie Battery (int ch, string ty) charge 100) if (charge charged true fully type ty otherwise public boolean Battery is fully charged, false true if the //implementation not shown //other methods not shown M/Battery public class Inventory inventory public Inventory (ArrayLiat

Solution

Page 1 Question

10) To add a method that can count how many Battery object in the ArrayList inventory are type AA, which of the following is true
Answer- The method should be implemented in Inventory
Explanation- since we have an variable inventory in Inventory class. since we have the variable we can iterate on the ArrayList. and count the number of Battery which as the type AA.


11) Which accessor method could be implemented in inventory?
Answer- a) getInventory() //returns the ArrayList of all of the Batteries in the Inventory
Explanation- getInventory should be in the Inventory class since the variable is declared in Inventory class

Page 2 Questions

7. What is the output of the following code.
  
   A a = new A(7);
Answer- c) onetwo
Explanation- the above line will invoke the parameterized constructor, since we are passing a parameter.
           public A(int x){
               this(); // This line will call the default constructor (constructor without parameters) which will print the word one.
               System.out.print(\"two\");//This will print two in the console.
       }
      

8. Which of the following could correctly replace /* Missing Code */ in the default constructor so that value is set to a possible die roll? (assume that the lowest possible value is 1)

Answer- e) value = (int) (Math.random()*numSides) + 1;
Explanation - To generate a random number we have to use Math.random() function.
                   (int)(Math.random() * ((Max - Min) + 1)) + Min , where Min is the min value (here min is 1) and Max is the max value(here max 6)
                  
           Here we have to cast the value to int, since the Math.random gives us a double data type.


9. The keyword to call the constructor and build an object in memory is

Answer- a) new
Explanation - The new key word is used to create new Object in java. The new is a identifier.

// Note: Please feel free to ask any question/doubts. God bless you!!

 edhesive Term 2 Unit 5-Week 4 definitions: Questions 0-12 refer to the following class publie elasa Battery private boolean fullyCharg charge private String ty

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site