Share Pastebin
Guest
Public paste!

Jon

By: a guest | Mar 21st, 2010 | Syntax: Java | Size: 4.26 KB | Hits: 61 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1.  
  2. ******************************************************
  3. Error:
  4. MyCarList.java:19: cannot find symbol
  5. symbol  : constructor MyCar(int)
  6. location: class MyCar
  7.                 mycars = new MyCar(maxSize);
  8.  
  9.  
  10. MyCarList Class
  11. **********************************************************
  12.  
  13. import java.util.Comparator;
  14. import java.util.*;
  15.  
  16. /**
  17.  * Class to represent a collection of cars
  18.  * @Jon Reid
  19.  * @version 2 - 9/3/10
  20.  */
  21.  
  22. public class MyCarList implements CarList{
  23.  
  24.         //Define some class vars
  25.         private Car[] mycars;
  26.         private int currentSize;
  27.         private final int maxsize = 100; // assign max array size here, easy to edit later
  28.        
  29.         public MyCarList(int maxSize){
  30.                 currentSize = 0;
  31.                 mycars = new Car(maxSize);
  32.         }
  33.        
  34.         public boolean add(Car car){
  35.                 //check if the array is full
  36.                 if(currentSize == mycars.length){
  37.                    //we need to expand the array's length to cope
  38.                    expand();
  39.                         return false;
  40.                 }
  41.                 else if(currentSize < mycars.length){
  42.                         mycars[currentSize] = car;
  43.                         currentSize ++;
  44.                         return true;
  45.                 }
  46.                 //safe to add the element and increase currentSize by one
  47.                 mycars[currentSize++] = car;
  48.                 return false;
  49.         }
  50.                
  51.         public boolean remove(Car car){
  52.                 boolean result = false;
  53.                 for(int i = 0; i < mycars.length; i++){
  54.                                         if (mycars[i] == car){
  55.                                                 mycars[i] = null;
  56.                                                 currentSize--;
  57.                                                 result = true;
  58.                                         }      
  59.                         }
  60.                 return result;
  61.         }
  62.        
  63.         public Car find(String regno){
  64.                 for(int i = 0; i < mycars.length; i++){
  65.                         if(mycars[i].getRegNo().equals(regno))
  66.                                 return mycars[i];      
  67.                 }                      
  68.         return null;
  69.         }
  70.        
  71.         public int sortByRegNo(){
  72.                 Arrays.sort(mycars, new CompRegNo());
  73.         }      
  74.        
  75.         public int sortByYear(){
  76.                 Arrays.sort(mycars, new CompYear());
  77.         }      
  78.        
  79.         public int sortByPrice(){
  80.                 Arrays.sort(mycars, new CompPrice());
  81.         }
  82.        
  83.         public int sortByModel(){
  84.                 Arrays.sort(mycars, new CompModel());
  85.         }              
  86.        
  87.         public void clear(){
  88.                 mycars= new Car[maxsize];
  89.                 currentSize = 0;
  90.         }
  91.        
  92.         public int size(){
  93.                 return currentSize;
  94.         }
  95.        
  96. //      public void java.util.Iterator<Car> iterator(){
  97.        
  98.         public String toString(){
  99.                 String contents = ""; //create empty string
  100.                 for ( int i = 0; i < currentSize; i++){
  101.                         contents += "\n\nCar: " + i + "\n" + mycars[i].toString() + "\n";
  102.                 }      
  103.                 return contents;
  104.         }
  105.        
  106.         public void expand(){
  107.                 //declare a tempArray to hold the current array
  108.                 Car[] tempArray = mycars;
  109.                 int newSize = mycars.length+5;
  110.                 //create a new array that is one element larger
  111.                 mycars = new Car[newSize];
  112.                 //copy the contents of tempArray into the new store (larger array)
  113.                 //(note the condition statement 'maxSize-1')
  114.                 for(int i = 0; i < tempArray.length; i++){
  115.                         mycars[i] = tempArray[i];
  116.                 }
  117.         }
  118.  
  119.         private void swap(int i, int j){
  120.                 Car temp = mycars[i];
  121.                 mycars[i] = mycars[j];
  122.                 mycars[j] = temp;
  123.         }
  124.        
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. MyCar Class
  136. **********************************************************
  137.  
  138. import java.util.Comparator;
  139. /**
  140.  * Class to represent cars
  141.  * @Jon Reid
  142.  * @(v1 & 9/3/10)
  143.  */
  144. public class MyCar implements Car{
  145.         //object values
  146.         String model;
  147.         String regNo;
  148.         int year;
  149.         int price;     
  150.        
  151.         //constructor
  152.         public MyCar(String theModel, String theRegNo, int theYear, int thePrice){
  153.                 model = theModel;
  154.                 regNo = theRegNo;
  155.                 year  = theYear;
  156.                 price = thePrice;
  157.         }
  158.        
  159.         /**
  160.                 not used
  161.         */
  162.         public int compareTo(Car a){
  163.                 return 1;
  164.         }
  165.        
  166.          /**
  167.      * Get the car's registration number
  168.      * @return  the cars model
  169.      */
  170.         public String getModel(){
  171.                 return model;
  172.    }
  173.  
  174.          /**
  175.      * Get the car's registration number
  176.      * @return  the cars registration number
  177.      */  
  178.         public String getRegNo(){
  179.                 return regNo;
  180.    }
  181.  
  182.  
  183.    public int getYear(){
  184.                 return year;
  185.    }
  186.  
  187.  
  188.         public int getPrice(){
  189.                 return price;
  190.   }
  191.  
  192.  
  193.         public void setPrice(int newPrice){
  194.                 price = newPrice;
  195.         }
  196.  
  197.  
  198.         public boolean match(String thisModel, int thisYear, int thisPrice){
  199.                 if(thisModel == model && thisYear == year && thisPrice == price){
  200.                         return true;
  201.                 }
  202.                 else{
  203.                         return false;
  204.                 }
  205.    }
  206.  
  207.   public int getCompCount(){
  208.         return 1;
  209.   }
  210.    
  211.          
  212.   public String toString(){
  213.         String carString = ("Price: " + price +
  214.                                                  " Year  : " + year +
  215.                                                  " Reg Number : " + regNo +
  216.                                                 " Registration : " + model);
  217.         return carString;
  218.   }
  219. }