Page 1 of 2 12 LastLast
Results 1 to 20 of 27
Like Tree1Likes

Thread: parking code

  1. #1
    disel is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default parking code

    hi guys I am new to java and my java teacher gave me an exercise to write a code about a parking and I am facing difficult time solving it can you help me please?
    the exercise is:
    a class vehicle is described by the instance variables; matriculation number, mark , owner's name and place number. only the variable 'owner's name' is private and the others are public.
    a- implement the class 'Vehicle' and its constructor.
    b- add an access method to the variable ' owner's name'
    c-add a method toString() to return a string containing all the information of the vehicle.
    now let's implement a class 'car' and a class ' van' derived from the class 'vehicle' . the class 'car' has an additional instance variable denoting the color of the car and a static variable denoting the number of cars. the class 'van' has an additional instance variable denoting the length of the van and a static variable denoting the number of vans.
    d- implement the two classes 'car' and 'van' with their constructors.
    e- add to each class the method to String().
    now let's implement a class 'parking' to manage the entry and exit of vehicles from the parking.
    at the entrance of each vehicle (car or van) a place number is assigned to this vehicle. the class 'parking' is described by a table of type 'vector' and a variable size that represents the maximum capacity of the parking.
    f- ad to the class 'parking' a method enter (vehicle v , int place) permitting to enter a vehicle v in the parking and assign to it a place. this method adds the vehicle to the table 'vector' if there is still available space.
    g- add to the class 'parking' a method 'exit' (vehicle v) to exit the vehicle v from the parking, this operation deletes the vehicle from the table ' vector'.
    h - add to the class 'parking' a method search(vehicle v) allowing returning the place of the vehicle in the parking. if the vehicle does not exists the methods returns -1.
    i- to test your program add to the class 'parking the method main that does the following actions:
    -create an object parking
    -initialize its size to 100
    -create two car objects with references v1 an v2
    -create two car objects with references van1 an van2
    -enter the four vehicles to the parking
    -exit van van1 from the parking
    - check if the van2 is still in the parking. if the vans exists, display the information.

    please guys this homework has 30 % of my final grade , your help is highly appreciated.

  2. #2
    disel is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: parking code

    this is what I wrote so far and everything is working well:

    Java Code:
    public class Parking 
    { public static void main(String[] args) 
      { Vehicle v1 = new Vehicle(123,"GMC","Jean",1);
        System.out.println(v1);
        Car car1 = new Car(123,"GMC","Jean",1,"White",5);
        System.out.println(car1);
      }
    }
    
    
    public class Vehicle extends Parking
    { public int matriculation_number;
      public String mark;
      private String owner_name;
      public int place_number;
      
      Vehicle(int matriculation_number, String mark,String owner_name, int place_number)
      { this.matriculation_number = matriculation_number;
        this.mark = mark;
        this.owner_name = owner_name;
        this.place_number = place_number;
      }
      
      public String getname()
      {return(this.owner_name);
      }
      
      public String toString()
      {return ("Matriculation Number : "+this.matriculation_number+" Mark : "+this.mark+" Owner's Name : "+this.owner_name+" Place Number :"+this.place_number);
      }
    }
    
    
    
    public class Car extends Vehicle
    { String color;
      static int number_of_cars;
      
      Car(int matriculation_number,String mark, String owner_name,int place_number,String color, int number_of_cars)
      { super(matriculation_number,mark,owner_name,place_number);
        this.color = color;
        this.place_number = place_number;
      }
    
      public String toString()
      { return(super.toString()+ " Color : " + this.color+" Number of Cars : " + Car.number_of_cars);
      }
    }
    i am facing problems with the methods 'exit' and 'enter' and the 'search' I do not know how to write these methods ... and how to check if a specific car is in th eparking ...
    Last edited by disel; 05-14-2014 at 12:23 AM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: parking code

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    disel is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: parking code

    done

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: parking code

    Pick one of the methods and describe what the code needs to do to implement it.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: parking code

    Hi,

    I just looked at the description what is required and at your implementation.

    For me it seems that you ignored all the points that should be done to the Parking class.
    - Vector to store Vehicles
    - Variable that tells the maximum size
    And then you have to do the enter method. The enter method takes a vehicle and a slot number so you could check if the slot is empty. If not, you throw en exception. If it is empty you put the car in there.
    I think that is straight forward once you added the first 2 steps required.

    And your main method needs to change of course. You first need an instance of Parking so that you can work with it to store cars.

    Konrad

  7. #7
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: parking code

    create an array list that holds vehicles... Make an int that keeps track of the max capacity and an int that keeps track of how many cars there are already in the lot... First thing you do when adding the car is making sure that you have space (capacity < carCount)... then just add the car to your array and increase car count... when searching for the car you create a for loop in your method that searches for what ever value you want to use (int i = 0; i < parkingLot.size(); i++) (if (parkingLot.get(i).getName().equals(name))... then just print it out and for the exit method you do the same thing to search for the car... remove it from your lot (parkingLot.remove(i)) and decrease your carCount...

    Hope this helped, good luck!

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,417
    Rep Power
    5

    Default Re: parking code

    When you Car class extends Vehicle, that makes sense because a car is a vehicle. But you have Vehicle class extending Parking. That says a vehicle is a parking. Does that make sense to you? I don't think you need to extend Parking at all. It looks like it is just the main class (i.e. driver - no pun intended) of your program.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    disel is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: parking code

    thank you guys for your response , but can you explain more? please if u can just write the "enter" method so I can take an idea about how to write other methods ...
    thank you in advance.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: parking code

    Can you make a list of the steps the enter() method is supposed to do?
    That will give you something to work from when writing the code.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    disel is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: parking code

    method enter (vehicle v , int place) permits to enter a vehicle v in the parking and assign to it a place. this method adds the vehicle to the table 'vector' if there is still available space.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: parking code

    Can you make a list of the steps? You've posted a description, not the steps the code needs to do.
    Then start at the first step and work on that.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: parking code

    I would start with this vector. How can you write a method that doing something with it when it does not exist.

  14. #14
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: parking code

    Quote Originally Posted by disel View Post
    thank you guys for your response , but can you explain more? please if u can just write the "enter" method so I can take an idea about how to write other methods ...
    thank you in advance.
    Java Code:
    public class Parking{
    	private Vehicle[] parkingLot;
    	public Parking(int capacity){
    		parkingLot = new Vehicle[capacity];
    	}
    	public void enter(Vehicle x, int y){
    		int i = y - 1;
    		if (parkingLot[i] == null){
    			parkingLot[i] = x;
    			System.out.println("There in now a car in spot " + y);
    		}
    		else {
    			System.out.println("There is alredy a car in spot " + y);
    		}
    	}
    }
    The reason why I use the integer i is because it's kinda weird putting a car in position 0... You don't have to do that... With the Parking object we take in how many spaces there are in you parking and create a new parkingLot array with capacity as it's length... For the method we take in a vehicle and what position you want it in... We check if the space is free and if it is we put the car there otherwise we just tell the user that the space is taken... I hope this helps you and gives you an idea of what to do for the other methods... Good luck!

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    926
    Rep Power
    2

    Default Re: parking code

    Quote Originally Posted by mbschultz97 View Post
    I hope this helps you and gives you an idea of what to do for the other methods... Good luck!
    You bet this helps the OP! Although your help is appreciated, there is a fine line between helping and spoonfeeding the answer. There is a big chance that the people you "help" with this hand this in as their own code, getting great marks, and in the end, they still don't understand it. This obvisouly is exercise code, and should be treated as such.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  16. #16
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: parking code

    Quote Originally Posted by SurfMan View Post
    You bet this helps the OP! Although your help is appreciated, there is a fine line between helping and spoonfeeding the answer. There is a big chance that the people you "help" with this hand this in as their own code, getting great marks, and in the end, they still don't understand it. This obvisouly is exercise code, and should be treated as such.
    So you're saying I did't really help? I just grabbed the food and shoved it down their throats? Also I learned mostly by looking at other peoples code and seeing how they did it.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: parking code

    @mbschultz97
    We're not a homework service. Please don't post code that can be copied and pasted into their code.
    Showing techniques is good.
    Writing full code for copying is not good.

    Also the code has NO comments that help an OP understand what the code is doing. For example what is this for:
    Java Code:
    int i = y - 1;
    Also the variable names are poor. What is in y? Is it the requestedSlot?
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    mbschultz97 is offline Super OP Noob
    Join Date
    May 2014
    Location
    Virginia
    Posts
    65
    Rep Power
    0

    Default Re: parking code

    Quote Originally Posted by Norm View Post
    @mbschultz97
    We're not a homework service. Please don't post code that can be copied and pasted into their code.
    Showing techniques is good.
    Writing full code for copying is not good.

    Also the code has NO comments that help an OP understand what the code is doing. For example what is this for:
    Java Code:
    int i = y - 1;
    Also the variable names are poor. What is in y? Is it the requestedSlot?
    I explained why I did int i = y -1 in the first sentence of my explanation

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,417
    Rep Power
    5

    Default Re: parking code

    What is and is not spoon feeding can be a matter of interpretation. We all have probably done it from time to time. If someone is having a difficult time parsing something and you provide them a regex to do it or explicit code snippet on how to do it, that is spoon feeding in my opinion. If you suggest they use a regex or look at some of the string methods, that is helping them help themselves.

    On the other hand, if they have tried to do it themselves and are not succeeding, you can be more specific. Also, if someone asks questions which may not be related to an assignment, like == vs equals or how some feature in the JDK is used, then I usually try and show them (sometimes before and sometimes after they have tried figure it out for themselves). It can be very subjective. I don't always make the correct choice.

    And be cautious when learning from others code. You could end up emulating bad technique(s). I like to look at the JDK API source myself. And I believe someone recently said the Apache site enforces pretty good design techniques.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: parking code

    in the first sentence of my explanation
    It would be better if the program's doc were in the code so when the OP copies it, the doc is there.
    When the doc and the code are in two places, that requires the reader to jump back and forth between the two places
    and there is a possibility for missing connections.

    It's better to comment the code.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Parking Ticket Simulator
    By Anthony5347 in forum New To Java
    Replies: 0
    Last Post: 12-07-2013, 06:10 AM
  2. Solving the parking lot fees problem with java
    By 511prs in forum New To Java
    Replies: 3
    Last Post: 09-13-2012, 08:56 AM
  3. a parking garage program
    By jihad in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •