Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
1.) I have created an abstract class as the following. All the methods that belongs to the class is also added as at the following.
Java
public abstract class Vehicles {
    
    public String vRegNo;
    public String vModel;
    public String vBrand;
    public boolean hired; // This hired attribute is used to store the hire           availability of the vehicle.
    public int vLength;
    public int vHeight;
    public byte vWheels;

    public String getvRegNo() {
        return vRegNo;
    }

    public void setvRegNo(String vRegNo) {
        this.vRegNo = vRegNo;
    }

    public String getvModel() {
        return vModel;
    }

    public void setvModel(String vModel) {
        this.vModel = vModel;
    }

    public String getvBrand() {
        return vBrand;
    }

    public void setvBrand(String vBrand) {
        this.vBrand = vBrand;
    }

    public boolean isHired() {
        return hired;
    }

    public void setHired(boolean hired) {
        this.hired = hired;
    }

    public int getvLength() {
        return vLength;
    }

    public void setvLength(int vLength) {
        this.vLength = vLength;
    }

    public int getvHeight() {
        return vHeight;
    }

    public void setvHeight(int vHeight) {
        this.vHeight = vHeight;
    }

    public byte getvWheels() {
        return vWheels;
    }

    public void setvWheels(byte vWheels) {
        this.vWheels = vWheels;
    }
    
    
}



2.) To use that class I have created another sub class as van at the below.

Java
public class Van extends Vehicles {
    
    public int vLoadCapacity;

    public int getvLoadCapacity() {
        return vLoadCapacity;
    }

    public void setvLoadCapacity(int vLoadCapacity) {
        this.vLoadCapacity = vLoadCapacity;
    }
    
    
    
    
    
    public static void main(String[] args) {
        
    Van oVan1 = new Van();
    oVan1.setvRegNo("PA-6589");
    oVan1.setvBrand("Toyota");
    oVan1.setvModel("Regius Ace 167");
    oVan1.setvLoadCapacity(16);
    oVan1.setvHeight(6);
    oVan1.setvLength(14);
    oVan1.setHired(true);
    
        System.out.println("Van 01 Vehicle Registration No :" + oVan1.getvRegNo());
        System.out.println("Van 01 Brand :" + oVan1.getvBrand());
        System.out.println("Van 01 Model :" + oVan1.getvModel());
        oVan1.getvLoadCapacity();
        oVan1.getvHeight();
        oVan1.getvLength();
        System.out.println("Van 01 Hire Availability :" + oVan1.hireVan());
        System.out.println("");
       
    Van oVan2 = new Van();
    oVan2.setvRegNo("PB-6139");
    oVan2.setvBrand("Toyota");
    oVan2.setvModel("Hiace 113");
    oVan2.setvLoadCapacity(12);
    oVan2.setvHeight(6);
    oVan2.setvLength(13);
    oVan2.setHired(true);
    
    
        System.out.println("Van 02 Vehicle Registration No :" + oVan2.getvRegNo());
        System.out.println("Van 02 Brand :" + oVan2.getvBrand());
        System.out.println("Van 02 Model :" + oVan2.getvModel());
        oVan2.getvLoadCapacity();
        oVan2.getvHeight();
        oVan2.getvLength();
        System.out.println("Van 02 Hire Availability :" + oVan2.hireVan());
        System.out.println("");

    
    Van oVan3 =  new Van();
    oVan3.setvRegNo("NA-9999");
    oVan3.setvBrand("Toyota");
    oVan3.setvModel("Hiace KDH110");
    oVan3.setvLoadCapacity(18);
    oVan3.setvHeight(7);
    oVan3.setvLength(18);
    oVan3.setHired(false);
    
        System.out.println("Van 03 Vehicle Registration No :" + oVan3.getvRegNo());
        System.out.println("Van 03 Brand :" + oVan3.getvBrand());
        System.out.println("Van 03 Model :" + oVan3.getvModel());
        oVan3.getvLoadCapacity();
        oVan3.getvHeight();
        oVan3.getvLength();
        System.out.println("Van 03 Hire Availability :" + oVan3.hireVan());
        System.out.println("");

    
    Van oVan4 = new Van();
    oVan4.setvRegNo("NB-0005");
    oVan4.setvBrand("Nissan");
    oVan4.setvModel("Caravan 2010");
    oVan4.setvLoadCapacity(17);
    oVan4.setvHeight(7);
    oVan4.setvLength(15);
    oVan4.setHired(false);
    
        System.out.println("Van 04 Vehicle Registration No :" + oVan4.getvRegNo());
        System.out.println("Van 04 Brand :" + oVan4.getvBrand());
        System.out.println("Van 04 Model :" + oVan4.getvModel());
        oVan4.getvLoadCapacity();
        oVan4.getvHeight();
        oVan4.getvLength();
        System.out.println("Van 04 Hire Availability :" + oVan4.hireVan());
        System.out.println("");

    
    Van oVan5 = new Van();
    oVan5.setvRegNo("NP-2314");
    oVan5.setvBrand("Mitsubishi");
    oVan5.setvModel("Delica L200");
    oVan5.setvLoadCapacity(10);
    oVan5.setvHeight(7);
    oVan5.setvLength(10);
    oVan5.setHired(true);
    oVan5.hireVan();
    
        System.out.println("Van 05 Vehicle Registration No :" + oVan5.getvRegNo());
        System.out.println("Van 05 Brand :" + oVan5.getvBrand());
        System.out.println("Van 05 Model :" + oVan5.getvModel());
        oVan5.getvLoadCapacity();
        oVan5.getvHeight();
        oVan5.getvLength();
        System.out.println("Van 05 Hire Availability :" + oVan5.hireVan());
        System.out.println("");

        
     
        
    }
    
    //This hireVan() method is used to find out the vans that can be hired! 
    public String hireVan(){
      String hStatus;  
        if(hired==true){
           hStatus = "Hired !";   
        }else{
            hStatus ="Not Hired !";
        }
        
        return hStatus;
        
    }
    
 
}


But my problem is now how am I able to calculate the no of vans that can be hired totally. I need to create a method in side the
Java
public class van extends vehicles 
.

Is there any solution with static methods ? Please help me!

Thank You!

####################################################################
on 12-17-2012 questioner addeed:

The solutions are rather difficult to understand. However I have created the whole classes as the below and created 2 constructors as you have guided me.

Class 01
Java
public abstract class Vehicle {
    
    private String strRegNumber;
    private boolean bolHired;
    private String strMake;
    private String strModel;
    private int intVLength;
    private int intVHeight;
}
//After this setters & getters have set correctly.

Class 02
Java
public class Van extends Vehicle{
    
    public int intPassengers;
    public boolean bolFullAC;
    public int intWheels;
    
    public Van(){
        
        this.setStrRegNumber("");
        this.setStrMake("");
        this.setStrModel("");
        this.setBolHired(false);
        this.setIntVHeight(0);
        this.setIntVLength(0);
        
    }
    
    public Van(String strRegNumber, String strMake, String strModel, boolean bolHired, int intVHeight, int intVLength){
        
        this();
        this.setStrRegNumber(strRegNumber);
        this.setStrMake(strMake);
        this.setStrModel(strModel);
        this.setBolHired(bolHired);
        this.setIntVHeight(intVHeight);
        this.setIntVLength(intVLength);
    }
           
}



But my question is pal then how am I able to add a new van with relevant values to the attributes using a method and calculate the no of vans that available to hire. And also pal if you can give me some advice on a good book to refer with practical scenarios and examples please try to find me one! I really appreciate it.

Thank You

Chiransj
Posted
Updated 17-Dec-12 8:11am
v2
Comments
Sergey Alexandrovich Kryukov 11-Dec-12 11:51am    
Not quite a clear question. Why it's a problem to count them? The purpose of abstract class and the derived class is not clear. Formally, this is OOP, but not OOP design. You don't have any virtual, no overrides, so why? You need to explain your logic: what are the abstractions here, what's the purpose, etc.
And please understand that static should be used only in rare special cases. Such members are shared by the whole process, so it's usually unsafe (bad for threading, for example). Do you really understand what a static and instance (non-static) members do?
--SA
Sergey Alexandrovich Kryukov 11-Dec-12 11:52am    
Also, for asking questions, always make the code sample shorter. You need only a couple of car properties and a couple of car instance in main, just to demonstrate the concept.
--SA
Chiranthaka Sampath 11-Dec-12 21:23pm    
Then pal could you read my problem and give me a solution using a method to calculate the total no of vans that can be hired using a method.
Sergey Alexandrovich Kryukov 11-Dec-12 22:16pm    
You don't show the collection of cars... OK, did you consider using LINQ?
--SA

You need a class above the Vehicles - some VehicleFacade:

Java
public calls VehicleFacade {

Vector<vehicle> vehicles = new Vector<vehicles>();

  public VehicleFacade(){ /* doany(); */}

  public void addVehicle(Vehicle vehicle){
    if(this.validateVehicle(vehicle)){
      vehicles.add(vehicle);
    }
  }

  private boolean validateVehicle(Vehicle vehicle){
    //validate vehicle

    // additional for special versions
    if(vehicle instanceof Van){
      return false;// in case vehicle is not valid 
    }
    return true; // all ok
  }

  // additional methods for deleting vehicles, counting vehicles, selecting vehicles, validation

  public Vector<vehicle> getVehicles(){
    return vehicles;
  }

}


This you can hold, work with and ask in your Main-Class:

Java
public class VehicleTest{
   
  private final VehicleFacade facade;
  
  public VehicleTest(){
    facade = new VehicleFacade();
    
    this.initTest();
  }

  private void initTest(){
    // code of your former main goes here - with small changes:
    Van van = new Van();
    facade.addVehicle(van); // vehicle van is stored in facade

    // more fun

  
    System.out.println("Vehicles available for hiring: " + facade.getNumberOfAvailableVehicles()); 
    // you'll figure the method   "getNumberOfAvailableVehicles()" out...
  }
  
  public static void main(String[] args) {
    VehicleTest test = new VehicleTest(); // break out of static main function
  }

}


(I hope the code is valid, i wrote it in the answer box and did not validate it. Simple malfunctions should be easy to fix by you ;) )

This is one of the GoF[^], called the Facade Pattern[^]. It's no big deal at all, more a structure giving idea on one class (often also an Interface) to gather all the objects and store them in one destination.

I can recommend to read as much about the GoF as possible, this is what makes you a developer and not only a programmer.

Have fun.

EDIT: I hope you figure how one breaks the static main function - I step over to the constructor and do the rest after the constructor (which should by definition not execute code, so I pushed the test into the function initTest() and let the fun happen there).
 
Share this answer
 
v3
Comments
TorstenH. 11-Dec-12 15:25pm    
you should also create a constructor for van with a full blown set of values to create one right away.
Also, creating a van by calling new Van() currently leaves all values empty - so you would get a null if asking the facade for special values.
so please create a 0 args constructor:

public van(){
this.setvRegNo("");
this.setvBrand("");
}

and one constructor full values:

public van(String strRegNo, String strVBRand, /*add other values*/){
this(); // call 0 args constr. always first
this.setvRegNo(strRegNo);
this.setvBrand(strVBrand);
// rest of the fun
}
Chiranthaka Sampath 11-Dec-12 21:22pm    
Thanks for your advice for the design patterns but pal my requirement is to calculate the total no of vans that can be hired using a method! Please try to understand my situation.
TorstenH. 12-Dec-12 1:00am    
It's finals time. I know.

I'll add a simpler solution.
EDIT: Check Solution 2 for a simple one.
Simple Solution:

Your main method, which is the entry point for your app, needs to move out of the class van:
Java
public class VehicleManagement(){

  public VehicleManagement(){
    this.ignition();
  }

  private void ignition(){
    // code of former main goes here
  }

  public static void main(String[] args) {
     VehicleManagement oManagement = new VehicleManagement(); // break out of static main function
  }
}


This leaves the Van as what it is - a Vehilce, not more.
Please follow my advice and add some constructors to your Van, as this is common sense:

Java
// 0 args constr. for basic init
public van(){
  this.setvRegNo("");
  this.setvBrand("");
}

//and one constructor with full set of values:
public van(String strRegNo, String strVBRand, /*add other values*/){
  this(); // call 0 args constr. always first
  this.setvRegNo(strRegNo);
  this.setvBrand(strVBrand);
  // rest of the fun
}



Back to VehicleManagement:

To store the Vehicles you'll need a place like a List[^]:
Java
private final List<vehicle> oList = new ArrayList<vehicle>();
private void ignition(){
  oList.add(new Van("REgNo1", "GM", /* more values*/);
}</vehicle></vehicle>


OK, now all Vans and other Vehicles are stored in that List. We can now ask for some information:
Java
private int getNumberOfAvailableVehicles(){
  int iReturn = 0;
  for (Vehicle oVehicle: oList){ // loop through list
    try{ if(!oVehicle.getHired()) iReturn++; } // count
    catch(Exception oException){ return -1;} // something went wrong
  }
  return iReturn;
}


Finally some refactoring:
- Why are all your variables starting with a "v" ? The idea is good, you should use a Type related prefix: "str" for String, "i" for integer. That's called Hungarian Notiation[^]. It's a bit out of style, but still makes sense in big/complicated apps.
- length and height of the Vehicle should be double values, not int. That makes more sense. (not float, I know that teachers like float, but double is the one to go for!).

have fun.
 
Share this answer
 
OK, it's Christmas time - so here we go with the complete solution:

1. Take your current classes "Vehicle" and "Van" - those are looking quite fine now.
the new constructors are good (beside of that you missed the "int intWheels" to be set in "Van", but that does not harm the app until it is requested)

2. Delete the main function in "Van"
"Van" will be an object we are working with, but it will not execute code.

3. add another class beside of "Vehicle" and "Van"
The class "VehicleManagement" will now be the center of our fun:


Java
/**
 *  This will be your Center class.
 *  "Vehicle" and "Van" are just objects that you are working with. 
 *  The fun is in here! 
 */
public class VehicleManagement(){
  // List to hold the Vehicles (Vehicle is the abstract "mother". The List will hold any object that extends "Vehicle")
  ArrayList<vehicle> oVehicles = new ArrayList<vehicles>();

  // constructor - not much more to do than to pass on the ball.
  public VehicleManagement(){
    // most would use "init()". But that is often taken and as one most times does not want to 
    // override methods, I decided a long time ago to go with ignition();
    this.ignition(); 
  }
 
  private void ignition(){
    // Both "jobs" where outsourced into their own functions. Those functions return the result of their work.

    // load List with Vehicles
    // ArrayList can "swallow" a complete List of Objects - so let's just throw them in there:
    oVehicles.addAll(createVehicles());

    // now let's count the hired vehicles.
    // the function "getHiredVehicles()" will count for us and return the result:
    System.out.println("Number of hired Vehicles: " + getHiredVehicles());

   
  }

  private List<vehicles> createVehicles(){
    ArrayList<vehicles> oList = new ArrayList<vehicles>(); // this List only exists  locally in this method!!

    oList.add(new Van("PA-1000", "1968", "Chevy", false, 6, 20)); // some van, not hired
    oList.add(new Van("PA-1001", "1969", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1002", "1970", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1003", "1971", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1004", "1972", "Chevy", false, 6, 20)); // ...
    oList.add(new Van("PA-1005", "1973", "Chevy", true,  6, 20)); // some van HIRED!
    oList.add(new Van("PA-1006", "1974", "Chevy", true,  6, 20)); // ...
    oList.add(new Van("PA-1007", "1975", "Chevy", true,  6, 20)); // ...
    oList.add(new Van("PA-1008", "1976", "Chevy", true,  6, 20)); // ...
    
    // ok, enough vans to test the code. Let's return the answer:
    return oList; 
  }

  private int getHiredVehicles(){
    // cause all beings beside of humans count from 0: 
    int iCountHired = 0;

    // to write the following if-statement like that is common.
    // easy to see what is expected for the if statement to work.

    // foreach-loop rushing through the List of Vehicles
    for(Vehicle oVehicle: oVehicles){ // check every Vehicle...
      if(true == oVehicle.getHired()){ // ...if it is hired...
        iCountHired++; // ...if so, then count...
      }
    } // done! 
    return iCountHired; // ...now return the result!
  }
  

  // It is common to place the main at the last position.
  // it's the static entry point for the JVM to run the application
  // one breaks out of the main as fast as possible
  // there is NEVER functional code in the main!

  public static void main(String[] args) {
     VehicleManagement oManagement = new VehicleManagement(); // break out of static main function

     // "new VehicleManagement();" would be enough in this example, 
     // cause there is nothing to do with a variable "oManagement"
  }
}


You should understand this basics, as they are common used throughout programming.

Good books do not need to be expensive.
This: Learning Java (Java Series) from o'reilly[^] @ amazon.com would do the job just fine.
It's not up to date - and does not need to.
As you can see within the ratings it is well written and covers all topics for a beginning Java developer.
Get a new copy and do not just place it on the book shelf (every one owns such programming books that are hardly read...)

Books from o'Reilly are always worth a recommendation.
 
Share this answer
 
v3
Comments
Chiranthaka Sampath 18-Dec-12 9:11am    
So good so far. OK then I want to create a method to display the vans that can be hired! So how am I able to do that? Please try to help me on this matter also as the previous ones!

Thank You
Chiransj
TorstenH. 18-Dec-12 11:48am    
no.
If you can't figure how to do that - try this

Seriously, this is just basic stuff. I've given you far more than what is good for you.
You should understand my above code (that I commented 100% more than usually!), and be able to work with it.

You will get into serious trouble if that is already to complicated.
Chiranthaka Sampath 19-Dec-12 4:13am    
When I developing the class VehicleManagement() i got the following error.

no suitable method found for add(List<vehicle>)
method ArrayList.add(int,Vehicle) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(Vehicle) is not applicable
(actual argument List<vehicle> cannot be converted to Vehicle by method invocation conversion)
----
TorstenH. 19-Dec-12 6:43am    
...so you browsed through the code-asssist and found out that it should be ArrayList.addAll(List).

I changed it, check out the method ignition()
Chiranthaka Sampath 19-Dec-12 8:40am    
Certainly pal! I will need your help in future. Thanks any way!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900