Question

In: Computer Science

The questions in this assessment use the following. class R { ... } class A extends...

The questions in this assessment use the following.

      class R { ... }
      class A extends R { ... }
      abstract class B extends R { ... }
      final class C extends R { ...}
      class D extends A { ... }
      class E extends B { ... }
      class F extends B { ... }
      // none of the classes implement a toString() method

[0] Draw a class hierarchy for the classes defined above.

[1] No or Yes:  class R extends Object

[2] class G extends C   does not compile. Why?

[3] class G extends E, F   does not compile. Why?

[4] B doh = new B();   does not compile. Why?

[5] System.out.println(new R());   prints: R@6bc7c054   Why?

[6] No or Yes:  class D can have subclasses (children).

[7] If  public String toString() { return "Arizona"; }  
    is added to class A, then System.out.println(new D()); 
    prints what?
[8] Assume the following is added to class C.

    public String toString() { 
       int azAgeIn2018 = 2018 - 1912;
       return "Arizona is " + azAgeIn2018 + " years young!";
    }

    What does System.out.println(new C()); print?

[9] System.out.println(new R().hashCode());  prints: 1808253012.
    No  public int hashCode()  method is implemented in class R.
    Briefly explain why this compiles and runs?

[10] If  public int hashCode() { return 48; }  is added to 
     class R, then what does the following statement print?

     System.out.println("Arizona is state# " + new R().hashCode());

Solutions

Expert Solution

Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

[0] Draw a class hierarchy for the classes defined above.

[1] No or Yes:  class R extends Object

Answer: Yes, every classes created in Java extends Object class by default.

[2] class G extends C   does not compile. Why?

Answer: Because C class is marked as final, which means class C cannot be used to inherit any other classes (C cannot be the super class of any class).

[3] class G extends E, F   does not compile. Why?

Answer: Java does not support multiple inheritance, so it is not possible to extend a class from two classes directly, though it is possible using interfaces, but with limited features.

[4] B doh = new B();   does not compile. Why?

Answer: Because B is an abstract class, it cannot be used to create an object (cannot be instantiated), it can only be used to inherit other classes from them.

[5] System.out.println(new R());   prints: R@6bc7c054   Why?

Answer: Because R does not override toString() method, so it just calls the Object class’ toString() method which denotes the class name and hashcode to identify an object.

[6] No or Yes:  class D can have subclasses (children).

Answer: Yes, class D can have subclasses since it is not final.

[7] If  public String toString() { return "Arizona"; }  
    is added to class A, then System.out.println(new D()); 
    prints what?

Answer: it will print ‘Arizona’ since the class D invokes super class R’s toString method by default.

[8] Assume the following is added to class C.
 
    public String toString() { 
       int azAgeIn2018 = 2018 - 1912;
       return "Arizona is " + azAgeIn2018 + " years young!";
    }
 
    What does System.out.println(new C()); print?

Answer: It will print ‘Arizona is 106 years young!’

[9] System.out.println(new R().hashCode());  prints: 1808253012.
    No  public int hashCode()  method is implemented in class R.
    Briefly explain why this compiles and runs?

Answer: As I mentioned earlier, all classes in Java inherits Object class. Object class has the method hashCode implemented by default. So if a class does not override hashCode() method, calling hashCode() method on the object of that class will invoke the super class’ (Object) hashCode method which returns the above value (note: depends on the system and architecture, this value can vary)

[10] If  public int hashCode() { return 48; }  is added to 
     class R, then what does the following statement print?
 
     System.out.println("Arizona is state# " + new R().hashCode());

Answer: It will print ‘Arizona is state# 48’ since it will override the hashCode() method of Object class to return 48.


Related Solutions

Use the R script to answer the following questions: (write down your answers in the R...
Use the R script to answer the following questions: (write down your answers in the R script with ##) (1). Import FarmSize.csv to Rstudio. Use the correct function to build a linear regression model predicting the average size of a farm by the number of farms; Give the model a name (e.g. FarmSize_Model). Call the model name to inspect the intercept and slope of the regression model. Verify the answers in your manual calculation. (2). Use the correct function to...
Use the R script to answer the following questions: (write down your answers in the R...
Use the R script to answer the following questions: (write down your answers in the R script with ##) (1). Import FarmSize.csv to Rstudio. Use the correct function to build a linear regression model predicting the average size of a farm by the number of farms; Give the model a name (e.g. FarmSize_Model). Call the model name to inspect the intercept and slope of the regression model. Verify the answers in your manual calculation. (2). Use the correct function to...
You coded the following class: public class N extends String, Integer { } When you compile,...
You coded the following class: public class N extends String, Integer { } When you compile, you get the following message: N.java:1: ‘{‘ expected public class N extends String, Integer                                                ^ 1 error Explain what the problem is and how to fix it.
Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class...
Using your Barrel class and Temperature class create a VacuumBarrel class that extends the Barrel class and incorporates a Temperature as the private data of the VacuumBarrel. Provide constructors, add(VacuumBarrel), subtract(VacuumBarrel), divide(int), equals(VacuumBarrel), greaterThan(VacuumBarrel) (that has one parameter and returns true if the this is greater than the parameter), toKelvin(), toFahrenheit(), toCelsius(), and toString() methods. Use a similar demo to the one you used with the Temperature class. This demo will put instances of VacuumBarrel in three arrays and manipulates...
Write a class that extends the LeggedMammal class from the previous laboratory exercise.
C++ code on Visual Studio Code:Write a class that extends the LeggedMammal class from the previous laboratory exercise. The class will represent a Dog. Consider the breed, size and is registered. Initialize all properties of the parent class in the new constructor. This time, promote the use of accessors and mutators for the new properties. Instantiate a Dog object in the main function and be able to set the values of the properties of the Dog object using the mutators....
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. 1. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint:...
Use R to complete the following questions. You should include your R code, output and plots...
Use R to complete the following questions. You should include your R code, output and plots in your answer. Two methods of generating a standard normal random variable are: a. Take the sum of 5 uniform (0,1) random numbers and scale to have mean 0 and standard deviation 1. (Use the properties of the uniform distribution to determine the required transformation). b. Generate a standard uniform and then apply inverse cdf function to obtain a normal random variate (Hint: use...
JAVA -The next three questions use the following class: class Identification { private int idNum;   ...
JAVA -The next three questions use the following class: class Identification { private int idNum;    public Identification() { this(0); }    public Identification(int startingIdNum) { idNum = startingIdNum; }    public int getIdNum() { return idNum; }    public void setIdNum(int idNum) { this.idNum = idNum; } } Here is one program using the above class: public class Main {    public static void main(String[] args) {        Identification i1 = new Identification();        Identification i2 =...
Use the definition of incidence taught in class, and answer the following questions. Be sure to...
Use the definition of incidence taught in class, and answer the following questions. Be sure to show your work. 1.      Spring fever! Epidemiologists and teachers alike have noticed the cyclic occurrence of the dreaded spring fever. Students afflicted with this disease exhibit certain listlessness in class. And they seem to stare out the windows with remarkable tenacity. Why, it’s almost as if the trees growing outside are more interesting than doing algebra. (Gasp!) At Metropolitan High School, teachers have been...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT