Results 1 to 5 of 5
  1. #1
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default Help me understant it

    hello i would to ask you on one thing... if you can explain it to me or give me some link at some thoery of it because i cant find it nowhere

    public class Passenger
    {
    private PublicTransport vehicle = null;


    public boolean getOn(PublicTransport vehicle){
    if (!(this.isTravelling())) {
    this.vehicle=vehicle;
    return true;
    } else {
    return false;
    }
    }
    public boolean getOff(){
    if (this.isTravelling()) {
    this.vehicle=null;
    return true;
    } else {
    return false;
    }
    }

    public boolean isTravelling(){
    if (this.vehicle!=null) {
    return true;
    }else{
    return false;
    }
    }

    public String toString(){
    return (this.isTravelling()?"Travelling towards "+vehicle.directsTo():"I'm not travelling");
    }
    }

    whats that? private PublicTransport vehicle = null; it is some pointer or what? so in the getOn method the passenger will remember on which vehicle is he travelling on or what?
    this is the rest of the code


    public interface PublicTransport
    {
    /**
    * Returns the target end station of the public transport vehicle.
    *
    * @return target station
    */
    String directsTo();

    /**
    * Inverts the direction of transport line (e.g.when the vehicle arrives
    * the end station).
    */
    void changeDirection();
    }

    public class Tram implements PublicTransport {

    private String from;

    private String to;


    public Tram(String from, String to){
    this.to=to;
    this.from=from;

    }

    public void changeDirection(){
    String help=this.from;
    this.from=this.to;
    this.to=help;
    }

    public String directsTo(){
    return this.to;
    }
    }

    thank you for your help...

  2. #2
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by fleg View Post
    whats that? private PublicTransport vehicle = null; it is some pointer or what? so in the getOn method the passenger will remember on which vehicle is he travelling on or what?
    this is the rest of the code
    For the first part: You have a class Passenger with methods getOn, getOff, isTravelling, toString. It has a private variable vehicle.

    "private PublicTransport vehicle = null;" ---> This is a private variable of type PublicTransport with the name vehicle which has a value of null.

    The method getOn takes a parameter of type PublicTransport and returns a boolean. When you instantiate a Passenger variable (example: Passenger Bill = new Passenger(); ) and use the method get on such as: someBoolean = Bill.getOn(someVaribleOfTypePublicTransportation) the method getOn checks if passenger is travelling by calling the isTravelling method if passenger is not travelling then it sets the value of vehicle to someVaribleOfTypePublicTransportation that was passed into the getOn method and returns true otherwise if passenger is travelling it just returns false. If we instantiate another Passenger variable like: Passenger Betty = new Passenger(); now Betty will have it's own vehicle variable of type PublicTransportation.


    I googled "java passing parameters" for this link: Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Also, here a few Java tutorial links that might help you:
    http://download.oracle.com/javase/tu...ava/index.html
    http://www.tutorialspoint.com/java/java_quick_guide.htm
    http://www.javabeginner.com/

    I hope this helps! :)
    Chris
    Last edited by tashimoto; 10-25-2010 at 09:40 PM. Reason: added OP quote

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by fleg View Post
    hello i would to ask you on one thing... if you can explain it to me or give me some link at some thoery of it because i cant find it nowhere
    On what you've stuck? If you could ask your question more specifically, then we can comment on it more clearly.

  4. #4
    fleg is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    okey thanks but i have one more question to this topic... I should add a Class Driver which implements Driveable and Driver will have a constructor which gives him some vehicle with which he will be able to manipulate...and driver has methods like accelerate and slow down and etc... so i have made the variable Tram someTransport cause i thought that thanks to it, Driver will be able to manipulate with the methods and atributes in Tram class but there is some problem which i dont understand :/
    if someone can help me with this i will be thankfull

    // after editing so i fixed it and it goes as it should go but i would like to ask you if you cant find any mistakes in it or how can i improve my code? i would be very thankfull
    Java Code:
    public class Tram implements Driveable, PublicTransport   {
    
        private String from;
        public static int MAX_SPEED = 50;
        private String direction;
        
        private int maxSpeed;
        private int speed;
        /**
         * Constructor without setting the maximum speed, so is used only the static variable.
         
         */
        
        public Tram(String from, String direction){
            this.direction=direction;
            this.from=from;
            this.maxSpeed = MAX_SPEED;
            speed=maxSpeed;
        }
    
          /**
         * Constructor with setting the maximum speed as we want
         
         */ 
        public Tram(String from,String direction,int maxSpeed){
            this.direction=direction;
            this.from=from;
                if (speed<0){
                        this.maxSpeed=MAX_SPEED;
                        speed=maxSpeed;
                    }else{
                        this.maxSpeed=maxSpeed;
                        speed=maxSpeed;
                    }
        }
       
        public void changeDirection(){
            String s=this.from;
            this.from=this.direction;
            this.direction=s;
        }
        
        public String directsTo(){
            return this.direction;
        }
    
        
         /**
         * Method returns speed after accelerating.
         */ 
        public int accelerate(){
            speed++;
            return speed;
        }
    
        public int getCurrentSpeed(){
            return speed;
        }
    
        public int getMaxSpeed(){
            return maxSpeed;
        }
        
        public int slowDown(){
            this.speed=this.speed - 1;
            if (this.speed<0){
                speed=0;
            } 
            return speed;
        }
        
        public String toString(){
            return "Tram : " +speed + "km/h";
        }
    }
    Java Code:
    public interface Driveable 
    {
    
        /**
         * It makes the vehicle accelerating, i.e. it increases the speed with 1km/h.
         * The vehicle cannot exceed its maximal speed.
         * 
         * @return current speed (after the acceleration)
         */
        int accelerate();
        
       
        /**
         * It returns current speed (in km/h)
         * 
         * @return current speed
         */
        int getCurrentSpeed();    
        
        /**
         * It returns maximal speed of the vehicle
         * 
         * @return maximal sped
         */
        int getMaxSpeed();
        
        /**
         * It makes the vehicle slowing down with 1 km/h. 
         * The minimal speed is 0 km/h.
         * 
         * @return current speed (after slowing down)
         */
        int slowDown();
        
        /**
         * Returns text description of the driveable.
         * 
         * @return text description of the driveable
         */
        String toString();
    }
    Java Code:
    public class Driver implements Driveable
    {
        String someVehicle;
        private Tram someTransport;
        public Driver(Tram someTransport)
        {
           this.someTransport=someTransport;
        }
    
        public void straightaway(){
            while (someTransport.getCurrentSpeed()<someTransport.getMaxSpeed()){
                someTransport.accelerate();
            }
                
        }
        
        public void carefully(){
            int help = someTransport.getCurrentSpeed()/2;
            while (someTransport.getCurrentSpeed() !=help){
                someTransport.slowDown();
        }
    }
        public void stop(){
            while (someTransport.getCurrentSpeed() !=0){
                someTransport.slowDown();
        }
    }
        
        public int accelerate(){
            
            return someTransport.getCurrentSpeed();
        }
    
         public int getCurrentSpeed(){
            return someTransport.getCurrentSpeed();
        }
        
        public int getMaxSpeed(){
            return someTransport.getMaxSpeed();
        }
        
         public int slowDown(){
            someTransport.slowDown();
         
            return someTransport.getCurrentSpeed();
        }
        
        public String toString(){
            return "I really dont know what to write here";
        }
    }
    Java Code:
    public class Demo
    {
        public static void main(String[] args){
            Tram tram1=new Tram("Bystrc-Ecerova","Reckovice", 100);
            Driver driver1 = new Driver (tram1);
            driver1.straightaway();         
            System.out.println(tram1.toString());    //should write Tram : 100 KM/H
            driver1.carefully();
            System.out.println(tram1.toString());    //should write Tram : 50 KM/H
            driver1.carefully();
            System.out.println(tram1.toString());    //should write Tram : 25 KM/H
            driver1.straightaway();
            System.out.println(tram1.toString());    //should write Tram : 100 KM/H
            driver1.stop();
            System.out.println(tram1.toString());    //should write Tram : 0 KM/H
        }
            
    }
    Last edited by Eranga; 10-27-2010 at 09:08 AM. Reason: i fixed it and it works now - code tags added

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    If you've find the solution your self, it's much much better.

    Anyway, please use code tags when you are posting code segments here in the forum. Unformated codes are really hard to read. And also if you've solve the problem please mark the thread solved from tools menu.

Posting Permissions

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