Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: I need extensive help with my java assignment

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need extensive help with my java assignment

    I have a java assignmet due tommorow and it has two classes and a driver class, now I've finished the first class, but for the second class and the drive class I have no idea what i'm doing and im a java noob can someone give me extensive help on the reainging assignment so I can finish it by 2:00 pm tommorw here is wha each should contain and my code
    the second class and code:
    write a class CarCollection that implements a collection of rental cars owned by a car rental company. This class must have exactly two instance variables:

    a one-dimensional array of Cars that stores a collection of rental cars,
    an int for the number of rental cars in the array (needed because the array may not be full).
    This class must provide exactly seven public methods:

    A default constructor that initializes the instance variables by creating an array of Cars with 15 cells and setting the number of rental cars to zero
    public void addCar(String vin, int odometer): this method creates a Car object representing a new rental car with VIN vin and initial odometer reading odometer, and then inserts this new rental car into the array (more precisely, into the unoccupied array cell with the smallest index). However, if the array is already full before insertion, then no new rental car should be inserted into the array.
    public String rentCar(String vin): this method allows a customer to rent a car with VIN vin; however, if that car has been rented out or does not exist, then this method will find out a car that is currently available for rent and has the smallest odometer reading among all such cars and will rent this car out. This method returns the VIN of the car that is finally rented out, but returns the string "NONE" if all cars in the collection are unavailable for rent.
    public void driveCar(String vin, int distance): this method simulates driving a rental car with VIN vin for distance miles, but does nothing if that car is currently not being rented out or does not exist.
    public double returnCar(String vin): this method allows a renter to return a rental car with VIN vin and returns the dollar amount s/he needs to pay for the rent. However, if that car is in fact already in stock or does not exist, then an amount of zero dollars should be returned instead. Assume that the car rental company charges a car renter $0.07 per mile of driving distance.
    public boolean hasRentedCar(): this method returns true if there is at least one rental car that has been rented out and not yet returned, false otherwise.
    The toString() method returns a string representation of the entire car collection. The return string should consist of multiple lines, each of which is the string representation of a single rental car (i.e., the return string of the toString() method of a Car object). The return string should list the cars in the same order that they are stored in the array.

    public class CarCollection
    {
     
      private Car[] Cars; 
      private int numcars;
     
      public CarCollection()
      {
        Cars = new Car[15];
        numcars = 0;
     
      }
      public void addCar(String vin, int odometer)
      {
     
        Car rental = new Car(vin, odometer);
     
            Cars[numcars] =  rental;
                numcars++;
     
      }
     
    public String rentCar(String vin)
      {
     
      int odo = Cars[0].getOdometer();
      for( int i = 0; i < numcars; i++)
          if( vin.equals(Cars[i].getVin())|| Cars[i].rent()== false){
            if(Cars[i].getOdometer() < odo)
                  Cars[i].rent();
               return Cars[i].getVin();
        }
     
      if( Cars[i].rent() == false){
      return "NONE";
      }
      Cars[i].rent();
      return vin;      
     
    }
    public void driveCar(String vin, int distance)
    {
     
      for(int p = 0; p < numcars; p++)
        if (Cars[p].rent()!= true)
        vin = Cars[p].getVin();
       vin.drive(distance);
    }
    public double returnCar(String vin)
    {
     
      double CHARGE = 0.07;
     
        for( int a= 0; 0 < numcars; a++)
        if(vin.equals(Cars[a].getVin() || Cars[a].rent()== true)
             return 0.00;
           else
             vin.returnRent();
             return vin.drive(distance) * CHARGE;
     
           }
     
    public boolean hasRentedCar()
        {
     
      for( int f = 0; f < numcars; f++)
        if(Cars[i].rent() == false)
         return true;
          else
            return false;
     
        }
       public String toString()
       {
         String as;
         for (int t = 0; t < numcars; t++)
         as +=  Cars[t];
     
         return as;
       }
       }
    the driver
    Finally, write a driver class RentCars that allows the car rental company to manage its car rentals. The main method in this class should take two command-line arguments: the path of an input file, followed by the path of an output file. The input file contains the VINs and odometer readings of at most 15 rental cars owned by the company. The driver program should first instantiate a CarCollection object, and then read in the information of every rental car from the input file and add the car to the CarCollection object (the rental cars should be added in the same order that they appear in the input file). Next, it should run into a loop. At each iteration of the loop, it should prompt the user to enter one of the following five options:

    The first user option a allows the company staff to add a rental car to its collection. If the user specifies this option, then the program should prompt the user to enter the VIN and initial odometer reading of a new rental car and then insert the car to the collection by invoking the addCar() method of the CarCollection class.
    The second user option r allows a customer to rent a car from the company. If the user specifies this option, then the program should prompt the user to enter the VIN of the rental car s/he desires to rent and then rent that car by invoking the rentCar() method of the CarCollection class (note that another car may be selected instead if the desired car is unavailable for rent).
    The third user option d allows a car renter's driving distance to be recorded. If the user specifies this option, then the program should prompt the user to enter the VIN of the rental car being driven and the distance traveled in miles and then simulate the driving by invoking the driveCar() method of the CarCollection class.
    The fourth user option t allows a car renter to return a car to the company. If the user specifies this option, then the program should prompt the user to enter the VIN of the rental car s/he wants to return, invoke the returnCar() method of the CarCollection class, and finally print the dollar amount that s/he should pay for the rent.
    The fifth user option q terminates this program. If the user specifies this option, then the program should invoke the toString() method of the CarCollection class, write its return string to the output file, and then terminate. However, if there is at least one car that has been rented out but not yet returned, then nothing should be written to the output file and the program should continue to run.
    code(not finished)
    import java.util.Scanner;

    public class RentCars
    {
      public static void main( String [] args)
      {
     
     CarCollection begin = new CarCollection();
     
     char response;
     
        response = prompt();
        while (response != 'q' && response != 'Q') {
          test(response);
          response = prompt();
        }
     
        System.exit(0);
      }
     
      //-----------------------------------------------------------------
      //  Prompts the user to enter a flag specifying which method of
      //  the Rational class to test.
      //-----------------------------------------------------------------
      public static char prompt()
      {
        String response;
     
        response = ("Enter an option (a, r, d, t, q): ");
     
        return response.charAt(0);
      }
     
    public static void test(char option)
      {
        switch(option) {
          case 'a':
          case 'A': addCar();
                    break;
          case 'r':
          case 'R': CarRental();
                    break;
          case 'd':
          case 'D': RenterDistance();
                    break;
          case 't':
          case 'T': Return();
                    break;
          default : System.out.println("Invalid Response - Try Again");
        }
     
    }
    public static void addCar()
    {
      String  vin;    
      int odo;
     
     
     
      System.out.print("Enter a vin number: ");
      vin = keyboard.nextLine();
      System.out.print("Enter a intial odometer reading: ");
      odo = keyboard.nextInt();
     
      begin.addCar(vin, odo);
    }
    public static void CarRental()
    {
       String  vin; 
      System.out.print("Enter a vin number: ");
      vin = keyboard.nextLine();
    Last edited by MoshMoakes; December 12th, 2009 at 07:57 PM.


Similar Threads

  1. Replies: 1
    Last Post: May 8th, 2009, 08:55 AM
  2. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM
  3. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM
  4. [SOLVED] Problem with Grading calculator in java program
    By Peetah05 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: March 28th, 2009, 04:25 PM