Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: how I can solve this program ?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how I can solve this program ?

    hi all

    Can anyone understand me how i can solve this program ?

    I understand its idea and I solved the first class "Vehicle" But stopped at the other classes

    ( class "Vehicle" is under the question )


    The aim of the project is to write two applications: one for Car Rent Company, the other one is for Bus Company. The class Vehicle is going to be used as a super class for the two companies i.e. The Car.java and Bus.java are going to implement the Vehicle. Each one of them is going to reuse the Vehicle differently according to the company needs.
    Write a program that contains the following classes:
    1) Create a class called Vehicle that contains fields for the vehicle's licensePlateNumber, maker and modelName. Provide both a default constructor and 3 argument constructor. Provide accessor (get) and mutator (set) methods for the fields.

    2) Create a subclass of Vehicle called Car that contains a field for the car availability in the Garage (to show it is avilable to use or it is already rented).

    3) Create a subclass of Vehicle called Bus that contains fields for the number of seats and number of avilable seats in the Bus.

    4) Write two main applications. First one creates a menu for the Car Company and declares an array of 3 cars; the second application creates a menu for the Bus Company and declares an array of 3 Buses. An example for the array declarations:

    Car [] mycar = {
    New Car("123AAA","Toyota","Corolla"),
    New Car("1432BBB","Honda","Stream","") , new Car("121BCA","Toyota","Avalon")
    };
    Bus [] mybus = {
    new Bus("787FFF","Toyota"," HIACE "),
    new Bus("909GGB","Ford","Flex",""), new Bus("888YKL","ryanbus","AECMerlin") };


    5) Bothe menu should allow its users to add and delete a new car or a new bus. The Car Company menu should give an option to rent a car from the Garage. This is done by changing the value of the car availability. On the other hand, the Bus Company will have an option in the menu to reserve a seat based on the number of seats that are avilable on the bus.


    Welcome to Car Company
    Main Menu

    1. Add a new car.
    2. Delete a car.
    3. Rent a car
    4. Exit.


    Welcome to Bus Company
    Main Menu

    1. Add a new bus.
    2. Delete a bus.
    3. Reserve a seat.
    4. Exit.


     Hints1: to rent a car (option3 in the menu), you should display all the cars in the array, the user will select one then you check if this car is avilable in the garage. If it is avilable then set its availability value to indicate that this car is used now.

     Hints2: to reserve a seat (option3 in bus menu), you should display all the buses in the array, then the user will select one then you check if there are enough seats still exit. Then you subtract the number of avilable seats by1.

    class "Vehicle" :

    public class Vehicle {
     
    private int	licensePlateNumber ;
    private String maker;
    private String modelName;
     
     
     public Vehicle(int licensePlateNumber,String maker,String modelName) 
     	{
     	this.licensePlateNumber=licensePlateNumber;
     	this.maker=maker;
     	this.modelName=modelName;
        }
     
        public Vehicle() 
        {
        }
     
        public void setLicensePlateNumber(int LPN)
        {
        licensePlateNumber=LPN;	
        }     	
         public void setMaker(String m)
        {
        maker=m;	
        }
        public void setModelName(String MN)
        {
        modelName=MN;
        }
     
     
         public int getLicensePlateNumber()
        {
        return licensePlateNumber;	
        }     	
         public String getMaker()
        {
        return maker;	
        }    
        public String getModelName()
        {
        return modelName;
        }  
     
     
     
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: how I can solve this program ?

    New Car("123AAA","Toyota","Corolla"),
    New Car("1432BBB","Honda","Stream","") , new Car("121BCA","Toyota","Avalon")

    It appears there's a comma after Stream.

    Also, new isn't capitalized.

    public class Car extends Vehicle
    {

    // new methods and methods that are overridden or overloaded.

    }

    public class Bus extends Vehicle
    {

    // new methods and methods that are overridden or overloaded.
    }

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how I can solve this program ?

    ya i Know that the class car and class bus are extends the super class Vehicle

    but the methods are my problem and how i can use them in the mains of each class "car , bus "
    Last edited by Noni; January 11th, 2011 at 02:54 PM.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: how I can solve this program ?

    Not quite sure on this one, but try

    public Car(int licensePlateNumber,String maker,String modelName, boolean availability)
    {
    super(licensePlateNumber, maker, modelName); // calls constructor of Vehicle class
    this.availability = availability;
    setAvailability(availability);
    }
     
    public void setAvailability(boolean availability)
    {
    this.availability = availability;
    }
     
    public boolean getAvailability()
    {
    return availability;
    }

     
    public Bus(int licensePlateNumber,String maker,String modelName, int numberOfSeats, int numberOfAvailableSeats)
    {
    super(licensePlateNumber, maker, modelName);
    this.numberOfSeats = numberOfSeats;
    this.numberOfAvailableSeats = numberOfAvailableSeats;
    setNumberOfSeats(numberOfSeats);
    setNumberOfAvailableSeats(numberOfAvailableSeats);
     
    }
     
    public void setNumberOfSeats(int numberOfSeats)
    {
    this.numberOfSeats = numberOfSeats;
    }
     
    public int getNumberOfSeats()
    {
    return numberOfSeats;
    }
     
    public void setNumberOfAvailableSeats(int numberOfAvailableSeats)
    {
    this.numberOfAvailableSeats = numberOfAvailableSeats;
    }
     
    public int getNumberOfAvailableSeats()
    {
    return numberOfAvailableSeats;
    }

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: how I can solve this program ?

    As for how to use them in the "main", you can simply use the objects in your arrays.

    However, it would be better if your constructor for Vehicle called the three set methods of that class and used the parameters passed to the 3 arg constructor to set the values. This, with the new code I gave, will set the values so you don't get a Null Pointer Exception, or shouldn't, and also, when you call the Vehicle constructor, should call the superclass' constructor and which calls the three set methods for you. You don't need to rewrite the three set methods of your Vehicle class unless you plan to change what they do for your subclasses.

    What you mean by "adding" or "deleting" a car or bus I'm not quite sure. You can cancel a ticket or a bus seat so that it becomes available, but you cannot, say, delete the Car or Bus at index 2. Your delete, I'm assuming, for Car, in your main class, should call the setAvailable(true) for the Car at that index in the array. Your delete for Bus, I'm assuming, should
    setAvailableSeats(getAvailableSeats() + 1) for the Bus at that index in the array.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how I can solve this program ?

    Javapenguin, please don't just spoonfeed code like that (I thought you were going to cease from doing this: Spoonfeeding), especially when you need to preface the code with a comment like "Not quite sure on this one"
    Last edited by copeg; January 11th, 2011 at 03:39 PM.

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how I can solve this program ?

    add function : the user enter new car or bus " enter the 3 varible " its licensePlateNumber ,its maker and its modelName" for example "123AAA","Toyota","Corolla" and I add this new car user entered it to array of car

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how I can solve this program ?

    option1 in car menu "add a car,bus"..



    the user enter new car or bus " enter the 3 variable " its licensePlateNumber ,its maker and its modelName" for example "123AAA","Toyota","Corolla" and you add this new car user entered it to array of car in last indx
    .................

    option2 in car menu "delete car , bus".



    only delete the last indx of array
    ..................

    option3 in car menu "rent a car"..



    First of all, display all the cars in the array "in all its details" to the user

    the display out put:

    1- the first car ("123AAA","Toyota","Corolla")

    2- the second car("1432BBB","Honda","Stream","") ,

    3- the third car("121BCA","Toyota","Avalon")



    then he will select one " the number he selected denote the index of array"

    then check if this car is avilable in the garage. If it is avilable then set its availability value to indicate that this car is used now.



    ..." this mean check if this car is avilable in the garage..by giving value for the variable availability "availability=0 if it is avilable" and display "the car is available" , if it is not available you chang the availability value to 1 and display "the car is rented and not available" after that you return the value of availability to 0"


    this rent idea is the same in the option 3 in bus menu "Reserve a seat"

    this is what my teacher said it to us

    I made three classes " car , bus and the super class "Vehicle" is up"

    class car

    public class Car extends Vehicle {
     
    int availability=0;
     
    public Car(int licensePlateNumber,String maker,String modelName,int availability)
    {
    super(licensePlateNumber,maker,modelName); // calls constructor of Vehicle class
    this.availability =availability;
    }
     
     
    public int getAvailability()
    {
    return availability;
    }

    class bus

    public class Bus extends Vehicle {
     
    	int numberOfSeats ;
    	int numberOfAvailableSeats ;
     
    public Bus(int licensePlateNumber,String maker,String modelName,int numberOfSeats,int numberOfAvailableSeats)
    {
    super(licensePlateNumber, maker, modelName);
    this.numberOfSeats = numberOfSeats;
    this.numberOfAvailableSeats = numberOfAvailableSeats;
     
    }
     
    public int getNumberOfSeats()
    {
    return numberOfSeats;
    }
     
    public void setNumberOfAvailableSeats(int numberOfAvailableSeats)
    {
    this.numberOfAvailableSeats = numberOfAvailableSeats;
    }
     
    public int getNumberOfAvailableSeats()
    {
    return numberOfAvailableSeats;
    }
     
    }

    so how I can make the main of car and bus class
    Last edited by Noni; January 19th, 2011 at 05:36 PM.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: how I can solve this program ?

    That's simple.

    For instance, to create one Bus object and one Car object in the main method, you
    public static void main(String[] args)
    {
     
     
     
    Car car = new Car(12345, "Luke Skywalker", "Model K", 1);
     
    Bus bus = new Bus(23456, "Elvis Presley", "Megabus 2012", 200, 35);
    // a.k.a for the array, buses[0] = new Bus(bus parameters);
     
    }

    As for details, consider having a toString() method for Car and Bus that returns the values. Use the get methods, not the variables themelves in the Car and Bus classes, to do that.

    Use a for loop to print out the stuff by calling the toString() of each Car object in the array.

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: how I can solve this program ?

    int choice = 0;
     
    while (choice !=4)
    {
     
    System.out.println("Welcome to Car Company");
    System.out.println("Main Menu");
    System.out.println("");
    System.out.println("1. Add a new car.");
    System.out.println("2. Delete a car.");
    System.out.println("3. Rent a car");
    System.out.println("4. Exit.");
     
    choice = console.nextInt();
    int carChoice = 0;
    if (choice ==1)
    {
    System.out.println("Enter a car to choose between 1 and 3.");
    carChoice = console.nextInt();
     
    if (carChoice ==1)
    {
    if (cars[carChoice -1].getAvailability() == true)
    {
    // output
    cars[carChoice -1].setAvailability(false);
    }
    }
    }
    }

    You get the picture.

Similar Threads

  1. Solve Them Please
    By omath in forum Java Theory & Questions
    Replies: 1
    Last Post: December 25th, 2010, 04:26 PM
  2. solve it plz
    By tillu in forum Java Theory & Questions
    Replies: 4
    Last Post: December 17th, 2010, 01:45 PM
  3. Plz solve the problem
    By rasheedmgs in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 14th, 2010, 11:59 AM
  4. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 08:28 PM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM