Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Java Swing
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.
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.
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.setvModel("Regius Ace 167");
        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());
        System.out.println("Van 01 Hire Availability :" + oVan1.hireVan());
    Van oVan2 = new Van();
    oVan2.setvModel("Hiace 113");
        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());
        System.out.println("Van 02 Hire Availability :" + oVan2.hireVan());
    Van oVan3 =  new Van();
    oVan3.setvModel("Hiace KDH110");
        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());
        System.out.println("Van 03 Hire Availability :" + oVan3.hireVan());
    Van oVan4 = new Van();
    oVan4.setvModel("Caravan 2010");
        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());
        System.out.println("Van 04 Hire Availability :" + oVan4.hireVan());
    Van oVan5 = new Van();
    oVan5.setvModel("Delica L200");
        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());
        System.out.println("Van 05 Hire Availability :" + oVan5.hireVan());
    //This hireVan() method is used to find out the vans that can be hired! 
    public String hireVan(){
      String hStatus;  
           hStatus = "Hired !";   
            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
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
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
public class Van extends Vehicle{
    public int intPassengers;
    public boolean bolFullAC;
    public int intWheels;
    public Van(){
    public Van(String strRegNumber, String strMake, String strModel, boolean bolHired, int intVHeight, int 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
Posted 11-Dec-12 6:17am
Edited 17-Dec-12 9:11am
Sergey Alexandrovich Kryukov at 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?
Sergey Alexandrovich Kryukov at 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.
Chiranthaka Sampath at 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 at 11-Dec-12 22:16pm
You don't show the collection of cars... OK, did you consider using LINQ?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need a class above the Vehicles - some VehicleFacade:
public calls VehicleFacade {
Vector<vehicle> vehicles = new Vector<vehicles>();
  public VehicleFacade(){ /* doany(); */}
  public void addVehicle(Vehicle 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:
public class VehicleTest{
  private final VehicleFacade facade;
  public VehicleTest(){
    facade = new VehicleFacade();
  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 Wink | ;) )
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).
TorstenH. at 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(){
and one constructor full values:
public van(String strRegNo, String strVBRand, /*add other values*/){
this(); // call 0 args constr. always first
// rest of the fun
Chiranthaka Sampath at 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. at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Simple Solution:
Your main method, which is the entry point for your app, needs to move out of the class van:
public class VehicleManagement(){
  public VehicleManagement(){
  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:
// 0 args constr. for basic init
public van(){
//and one constructor with full set of values:
public van(String strRegNo, String strVBRand, /*add other values*/){
  this(); // call 0 args constr. always first
  // rest of the fun

Back to VehicleManagement:
To store the Vehicles you'll need a place like a List[^]:
  private final List<vehicle> oList = new ArrayList<vehicle>();
  private void ignition(){
    oList.add(new Van("REgNo1", "GM", /* more values*/);
OK, now all Vans and other Vehicles are stored in that List. We can now ask for some information:
  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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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:

 *  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();
  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:
    // 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; // 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[^] @ 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.
Chiranthaka Sampath at 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
TorstenH. at 18-Dec-12 11:48am
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 at 19-Dec-12 4:13am
When I developing the class VehicleManagement() i got the following error.
no suitable method found for add(List)
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 cannot be converted to Vehicle by method invocation conversion)
TorstenH. at 19-Dec-12 6:43am 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 at 19-Dec-12 8:40am
Certainly pal! I will need your help in future. Thanks any way!
Chiranthaka Sampath at 19-Dec-12 8:45am
Thanks pal however I am appreciating your help!
TorstenH. at 19-Dec-12 12:51pm
PLease mark the solution as "accept solution", so others can find it too when they have a similar problem.
Chiranthaka Sampath at 19-Dec-12 3:05am
When I developing the class VehicleManagement() i got the following error.
no suitable method found for add(List)
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 cannot be converted to Vehicle by method invocation conversion)

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897

Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100