Advertisement
apez1

Term 2: Lesson 4 - Coding Activity

Dec 16th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. package project;
  2.  
  3. public class Vehicle {
  4.  
  5.     private int location;
  6.        
  7.     public Vehicle() {
  8.        
  9.     location = 0 ;
  10. }
  11.    
  12.     public Vehicle(int loc) {
  13.        
  14.         if(loc >= -20 && loc <= 20) {
  15.             location = loc;
  16.         }
  17.         else {
  18.             location = 0;      
  19.         }      
  20.     }
  21.    
  22.     public void forward() {
  23.                
  24.         if(location < 20) {
  25.            
  26.             location ++;
  27.  
  28.         }
  29.        
  30.    
  31.     }
  32.    
  33.     public void backward() {
  34.        
  35.         if(location > -20) {
  36.            
  37.             location-- ;
  38.            
  39.            
  40.         }
  41.        
  42.        
  43.     }
  44.    
  45.     public int getLocation() {
  46.        
  47.         return location;   
  48.            
  49.     }
  50.  
  51.     public String toString() {
  52.        
  53.         int spot = location + 20 ;
  54.         String finished = "";
  55.        
  56.         for(int i = 0; i != spot; i++) {
  57.            
  58.             finished += " " ;
  59.            
  60.         }
  61.        
  62.         finished += "@" ;
  63.        
  64.        
  65.    
  66.     return finished ;
  67.    
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement