[HD Pg 265, Sec. 9.4.0 - exercises]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 265, Sec. 9.4.0 - exercises]

Post by OCAJO1 »

The following is the code encompassing the entire 8 exercises of chapter 9,

Code: Select all

Package Chp9Ex;
import java.util.*;

interface Drivable {
     
    public void drive();

    default void start(){
        
        System.out.println("Start the vehicle.");
    }
}

interface VehicleHelper {
    
    static void register(Vehicle v){
        
        System.out.println("Vehicle's VIN number: "+v.getVIN()+".");
    }
}

abstract class Vehicle {
    
    String vin;
    
    abstract public void getMakeAndModel();
    
    abstract public String getFeatures( String featureName);
    
    final String getVIN(){
        return vin;
    }
    
}

class Car extends Vehicle implements Drivable, VehicleHelper {
    
    public Car(String sc){
          
        this.vin = sc;
        VehicleHelper.register(this);   
        
         //pass value of reference this, since can't instantiate the class within
         //its constructor causing stackOverflow due to recursion.
         
    }
    
    @Override
    public void drive () { }
    
    
    @Override
    public String getFeatures (String featureName) {
        
        String result = "N.A";
       
        ArrayList<String> fn = new ArrayList<>();
        
        fn.add("Height");
        fn.add("width");
        fn.add("length");
        fn.add("power");
        fn.add("boot capacity");
        
        for (String item : fn){
            if (featureName.equals(item)){
                result = "Feature '"+item+"' is supported.";
                break;
            }    
        }   
        
        return result;
    }
    
    @Override
    public void getMakeAndModel(){ }
    
    @Override
    public void start(){
        
        System.out.println("Start the car.\n");
    }
   
}

class Truck extends Vehicle implements Drivable, VehicleHelper {
    
    public Truck(String st){
          
        this.vin = st;
        VehicleHelper.register(this);    
    }
    
    @Override
    public void drive () { }
    
    @Override
    public String getFeatures (String featureName) {
        
        String result = "N.A";
       
        ArrayList<String> fn = new ArrayList<>();
        
        fn.add("Height");
        fn.add("width");
        fn.add("length");
        fn.add("power");
        fn.add("boot capacity");
        
        for (String item : fn){
            if (featureName.equals(item)){
                result = "Feature '"+item+"' is supported.";  
                break;
            }    
        }   
        
        return result;
    }
    
    @Override
    public void getMakeAndModel(){ }
    
    @Override
    public void start(){
        
        System.out.println("Start the Truck.\n");
    }
  
}

class ToyCar extends Car {
       
    public ToyCar(){
        super("");
    }
       
}

public class CompareAutos {
    

    /**
     * @param args the command line arguments
     */
     
    public static void main(String[] args) {
        
        String setCVin = "vinC111"; 
        Car c = new Car(setCVin); 
        System.out.println("Car's vin number: "+c.getVIN());
        VehicleHelper.register(c);  //calling register from here does not satisfy question 8
        System.out.println(c.getFeatures("Wings"));
        c.start();
        
        String setTVin = "vinT222";
        Truck t = new Truck(setTVin);
        System.out.println("Truck's vin number: "+t.getVIN());
        VehicleHelper.register(t); //calling register from here does not satisfy question 8
        System.out.println(t.getFeatures("power"));
        t.start();

    }
    
}
I appreciate taking a look at my code to see if it does follow the requirements of section 9.4 exercise and is not just bunch of interfaces and classes - in particular:

1. Does the code stratify second half of question 3 - Furthermore, you don't want any subclass to change the behavior of the getVIN method. Where and how will you code the getVIN method?

2. Does the code satisfy questions 5 - Ensure that every vehicle is created with a VIN.?

3. In order to make the code work, I had to change the parameter in VehicleHelper Interfaces' method "register" in question 8, from type Vehicle to Type String. So I have a feeling something is not right, but I can't put my finger on it!

(This is question 8. - Create an interface named VehicleHelper with a static method register(Vehicle v) that prints the VIN of the vehicle. Ensure that VehicleHelper's register method is invoked whenever an instance of a vehicle is created.)

Thank you
Last edited by OCAJO1 on Sun Mar 17, 2019 6:53 pm, edited 31 times in total.

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by admin »

1. Well, why do you want to make vin static? Do all vehicles have the same vin? "Not letting any one change the behavior" screams "final" not "static"!
2. No.
3. The requirement says that the register method's parameter type is Vehicle. If you change it to String, then that is clearly the thing that is not right.

Your code and questions indicate that you are not clear about the meaning and purpose of static and final. It is not a complicated exercise. As I said before, you should seriously consider discussing this with your instructor.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by OCAJO1 »

Ok since the original code bummed out so bad, I reviewed the chapter again, and updated the code in yesterday's blog.

1. Did use of constructors satisfy exercise 5 and second half of exercise 8? If not, I'll appreciate a hint.
2. I'm still trying to figure out how to pass the vin number, a String, from getVIN() to register(Vehicle v) so it can be printed! Any Possible hints?

Thanks

p.s. I added a test case for question 7 as well.
7. You have a list of features such as height, width, length, power, and boot capacity, on which you want to compare any two vehicles. New feature names will be added to this list in future. Create a getFeature(String featureName) method such that it will return "N.A" for any feature that is not supported by a particular vehicle.

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by admin »

First, why do you have a Vehicle vv; field in Vehicle class? Why are your constructors calling getVIN();

1. No, exercise 5 is not satisfied by your code. Your statement Car c = new Car(); shows that a Car is being created without a vin. So, why do you think you have satisfied the requirement of exercise 5, which says, "Ensure that every vehicle is created with a VIN."?
You have to pass the VIN as an argument to the constructor.

2. Why do you need to pass the vin number, a String, from getVIN() to register(Vehicle v). Can you not just call System.out.println(v.getVin()); in the register method?
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by OCAJO1 »

Well I'm learning more doing the exercises than reading the chapter over and over. I guess that would be the difference between the theoretical Java programming vs. applied Java programming :)

(As for declaring vv as Vehicle in the Vehicle class, it was just a place holder so the program would compile, till I could figure out question 8.)

1. So now that I know what does "Ensure" mean in this context, calling getVIN() from the constructors was just silly.

2. Interesting that you should mention utilizing the print statement. I tried that from the get go. But variable v being null and causing NullPointerException, is what started the whole path to question about how to pass a string to a Vehicle type, in order to avoid NullPointerException.


Question:

1. I've updated the program in the first blog again, but I still can't get my mind around why v parameter is of type Vehicle in 1st part of question 8?

2. As for the 2nd part of 8, is calling register(Vehicle v) from the constructors, the correct way to ensure that VehicleHelper's register method is invoked whenever an instance of a vehicle is created? Even if it is, calling methods from constructors doesn't look like the best practice. Is there another way to ensure invocation?

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by admin »

> public Car () { } // needed for question 6.
This is should not be there because it will negate exercise 5.

1. register method takes a Vehicle as an argument because that's the requirement. As of now, the register method prints only the vin but it could do something else with the passed Vehicle.

2. Yes. There is usually done using dependency injection frameworks such as Spring.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by OCAJO1 »

>So in such combination requirements such as questions 5 and 6, the no argument super in ToyCar class should become super(""), passing a null argument to the single argument constructor in Car class. Is that correct?

1. Then given such requirement, it still leaves me with how to satisfy 1st part of question 8 without causing NullPointerException caused by v.getVIN, since I have no vehicle type to pass to the register method! What step am I not seeing?

2. So for now calling register(Vehicle v) from the constructors is the way to go.

Thanks

p.s. Can you please recommend a good source for dependency injection frameworks in java, that has deeper explanation and possible examples, than Oracle Docs?

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by admin »

Yes, in ToyCar's no args constructor you could pass a dummy value for vin in the call to super: super("ToyCarVIN");

1. To invoke VehicleHelper's register, you just need to pass "this" from Vehicle's constructor i.e. Vehicle(String vin){
this.vin = vin;
VehicleHelper.register(this);
}

There shouldn't be a NPE anywhere.

2. Yes.

3. For dependency injection, check out Spring. It has good documentation and there are tons of books and articles on it.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by OCAJO1 »

Can't believe it - after running around looking at all kinds of things, I missed checking out passing value of reference this, since (unlike in the CompareAuto's main()) couldn't instantiate a class in its own constructor causing StackOverflow due to recursion :cry:

Well, learned a biggy today :thumbup:

Side question:

I added the following to the Truck's constructor and it complied and the register method printed the Truck's vin (as expected). But why would compiler/JVM allow such a thing?

//Car f = new Car(sc);
//VehicleHelper.register(f);

Thanks

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by admin »

Why do you think the compiler should not allow it?
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by OCAJO1 »

I guess is just extending the practice of not using the constructors like other methods. But I guess it is left to the code designers to use them responsibly as it were . Come to think of it, not allowing it all together may interfere with such things as dependency injection too.

p.s. Since the code in the original blog entry, is the entire 9.4 exercise, I wonder if it should be kept or deleted?

Online
admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 265, Sec. 9.4.0 - exercises]

Post by admin »

That's fine. We can leave it there. Whoever wants the easy way out will always find one :)
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 49 guests