Advertisement
StefanTobler

TERM 2 Lesson 4

Jan 6th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. public class Vehicle
  2. {
  3.   private int location;
  4.  
  5.   public Vehicle()
  6.   {
  7.     location = 0;
  8.   }
  9.  
  10.   public Vehicle(int loc)
  11.   {
  12.     if (loc >= -20 && loc <= 20)
  13.       location = loc;
  14.     else if (loc < -20)
  15.       location = -20;
  16.     else
  17.       location = 20;    
  18.   }
  19.  
  20.   void forward()
  21.   {
  22.     if (location < 20)
  23.       location++;
  24.   }
  25.  
  26.   void backward()
  27.   {
  28.     if (location > -20)
  29.       location--;
  30.   }
  31.  
  32.   int getLocation()
  33.   {
  34.     return location;
  35.   }
  36.  
  37.   public String toString()
  38.   {
  39.     for (int i = -20; i < location; i++)
  40.       System.out.print(" ");
  41.     return "@";
  42.   }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement