Page 1 of 3 123 LastLast
Results 1 to 20 of 48
  1. #1
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default New RentalCar project

    Hi everyone, it looks like this week all colleges are going over arrays. I also have a project to do. The project includes three files a Car class, a RentalCarCompany, and a tester. I completed the Car class but I am having problems with the RentalCarCompany class. I am having difficulties coming up with two fields:

    Please feel free to critize, point out mistakes, and pitch in. I read my chapters in arrays, I read the tutorial, and I still can't think of a way to create this too.

    Any help will be appreciated.
    Last edited by camaro01; 03-07-2011 at 05:12 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I think it may help if you show us the car class as well, however your getters seem odd to me, mind explaining your thought process on them? A few of them return a variable you didn't declare. Also you didn't give the door count variables a value. You also declared a lot at the bottom, those are instance variables too and it is a bit confusing to put them at the bottom.

    Also, are you aware that the car list never seems to get filled?

    Also, I am still checking your code and I will probably think of more thoughts and questions for you, what is the goal? Is this to simulate a filled car lot? Do you mean to have a filled list of cars you keep track of?
    Last edited by sunde887; 03-05-2011 at 11:12 PM.

  3. #3
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I don't mind sharing what I have on the Car.class and the car list is supposed to be filled from the tester (I think) according to the instructions. The tester class will build 5 rental cars and from there I should be able to print out a report with each renter's name, car number, car name, type, doors, rate, and the number of days rented.
    Last edited by camaro01; 03-07-2011 at 05:13 AM.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Not so bad, definitely helped to see that. One cool tip with setters is the use of the this keyword, which can be used in constructors as well.

    Java Code:
    public class C{
      int x;
    public void setter(int x){
      this.x = x;
    }
    }
    constructers can use this too. With this you don't have to create new variable names.

    In the rental class just create a getter for getting the array list, then perform operations on the array, if you want to count all cars with 2 doors you will loop through the array and add each car object that has 2 doors, each getter you created can be used in a similar fashion.

    Java Code:
    class RentalCarDealer{
      // make the arrays and such
      public int get2DoorCount(){
        int count;
        for(Car c : carList){
          if(c.getDoorCount == 2){
            count++;
          }
        }
        return count;
      }
    }
    This is an example. I am typing this on an iPhone so I apologize for not explaining better.
    Last edited by sunde887; 03-05-2011 at 11:42 PM.

  5. #5
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    Thanks for the help. I have to count both 2-door and 4-door cars. At the end, the report will print out the number of 2 door cars and 4 door cars that were rented. If I use the if statement in the 2-door class with count, do I have to use a different variable if I use the loop in the 4-door? but since I have to return both 2 and 4 door numbers should I assign them different names?

    Like this?
    Last edited by camaro01; 03-07-2011 at 05:13 AM.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can use the same variable name, due to scoping the variable is only around until the end of the brackets. When you can this method you would use it to set some int value. When is your assignment due?

    Quick example
    Java Code:
    public int method1(){
      int j = 0;
      return j;
    }
    public int method2(){
      int j = 10;
      return j;
    }
    ...
    Assume these are in the same class, you can have as many nearly identical methods with just different names and it works fine.

    If you want I will be helping you tonight but, but I'm using a phone, come Monday I'll be ableto use my laptop where i can explain in much greater detail. I can also answer any question you have.

  7. #7
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    My concern was that if I am using the same:
    Java Code:
    return count;
    in each method, would it be returned to the wrong field. And yes, they are in the same class.

    Anyway, my project is due tomorrow Sunday night at 11PM CST. I am in CST and I would really appreciate your help. Just let me know what time is best for you and I be right here.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, I can access a computer to give you more help at midnight cst, bit for now I'll do what I can with my phone. It won't matter because the method I showed you is not a setter. The int count is created in that method and dies after that method. A setter usually returns void, this returns an int, if you want though you can quickly change this to a setter

    Java Code:
    public void 2DCount(){
      //assuming there is an instance variable named 2Door
      for(Car c : carList){
        if(c.getDoorCount() == 2){
          2Door++; 
        }
      }
    }
    this method actually changes the instance variable instead. Let me know if this is clear to you. Try making these in your actual class, one for 2 doors, one for 4, and post the full rental code, make sure to use correct method names and variables.

  9. #9
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I can't use it as a setter because of the requirements in the project. For the rental class have the following:

    The UML class diagram for the RentalCarCompany class looks like this:

    You know that deviating from the assignment will cost me grade. I am sure there are many ways to do this and probably easier ones but you know how school is.
    PS: I tried to copy the UML box from the word doc but it did not show up.
    Last edited by camaro01; 03-07-2011 at 05:13 AM.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How does it want you to count the items with 2 doors? Does it give you a method declaration? This isn't really a setter, it just changes the value of the 2door count variable which is why i used the term setter, post every method declaration you want this class to have and I'll continue to help you as much as I can.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As another example of a method you supplied, to find the total sales you know that a cars rental price is the rate times the number of days, so, you want to loop trough the array and using the getters calculate the cars rental money than add it to the total.

    Loop through cars
    For each car calculate rate times days
    Add that calculation to the total rental amout

  12. #12
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    for the total sales I should use the following:


    I just need to combine those two.
    Last edited by camaro01; 03-07-2011 at 05:14 AM.

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, so, first things first, what are you looping through? A list of cars, do cars have methods for getting days and rental price? If they do can you think of some way to calculate the rent earned by that item? If you can you just need to do that and then add it to the total retail variable.

  14. #14
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    It is easier to put it in paper: I have to go through a list of cars(array) and to get the rental price I multiply rate * days for each car, then I add all those amounts, and that will give me the total rental sales. I guess I am missing how to get the total rental price of each car individually.

    Would it be something like


    If I create this method, how would I pass the entire array through it? Is it making more sense now?
    Last edited by camaro01; 03-07-2011 at 05:14 AM.

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try putting a method in the car class, this method simply returns a double representing the rental price generated by price times days, then you can loop through the cars array and calculate the price and add it to the total but you also have a method to calculate individual cars prices. I hope this is making some sense to you.
    Last edited by sunde887; 03-06-2011 at 02:12 AM.

  16. #16
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    I have been thinking about this and I just realized that I don't think I said this before but I still have to write another class to test the car class and the rentalcarcompany. So,
    this method will work:

    And then I just have to create the loop in the test program so I can run each car through it and get the total amount of sales using this loop.

    Does this makes more sense?
    Last edited by camaro01; 03-07-2011 at 05:14 AM.

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This code won't work correct, when you get to the test class you will see and I will be able to give a more detailed explanation tomorrow around 3pm. For now though let's step back and evaluate. This method should return a double representing a dollar amount. The list is filled with car objects, so you need a way to determine how much it costs to rent one skgle car. Since we are working on a single car right now, let's modify the car class. Wouldn't it be helpful if the car class had a method to compute te amount to rent it?

    In car create a method with the following declaration
    Java Code:
    public double getRentalCost()
    Your code is close but you need to do three things for this method,
    1. Declare a double with any name
    2. Set the double to the value of days times rate
    3. Return the double

    Once you created this method you can now create A car object and call the method on the object.

    Then you can loop through the list of cars and apply this method to each object. Write the car class method only and show me completed code. Your last post is actually mostly correct but the syntax used is wrong. After you finish this tell me what method you want help with next.

  18. #18
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    Very well, I added the new method to return the total. Here is the car class modified.


    Since we took a step back and went back to the car class, I need to use the toString but I will make another post with an example.
    Last edited by camaro01; 03-07-2011 at 05:15 AM.

  19. #19
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    The toString method should yield something like this:
    Last edited by camaro01; 03-07-2011 at 05:15 AM.

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The first thing I'd like to discuss is the toString method, all classes inherit a default toString from the object super class, the default value which creates a string which is of he form ClassName@af126b, so if you ever see this you know that it has something to do with toString. The second thing I'd like to touch on is that after providing an overridden tostring you can simply do
    Java Code:
    System.out.println(myObj);
    this will automatically call the objects tostring method.

    Now that I have explained some toString basics let's get into the actual implementation of it for your class.

    The toString method should return a string, when you deal with a method that returns type you can think of the method as that type. So since it returns a string, you can assign it to a string reference, if it returns an int you can assign it to an int and the same for any other type.

    Now tostring is generally an easy to implement and the best way for me to show you how to do it is to give you a code snippet to compile and run, so copy and paste the following and compile and run it so you can see an example similar to what you want for your class

    Java Code:
    public class MyClass{
      String s;
      int j;
      
      public String toString(){
        return "this is the classes string: " + s + "\n" +
                 "this is the classes int: " + j;
      }
      public static void main(String[] args){
        MyClass mc = new MyClass();
        mc.s = "hello";
        mc.j = 10;
        System.out.println(mc);
      }
    }
    Run that then apply this logic for your tostring method, i intentionally used a new line character since your method will use it.

    Finally, add a main method to your car class for your testing purposes so you can create an object, print it and call methods on it, especially the new one you just defined. Your rent method is off a bit you need to declare the double you are gonna return it before you set it and return it.
    Last edited by sunde887; 03-06-2011 at 04:53 AM.

Page 1 of 3 123 LastLast

Similar Threads

  1. CS project
    By seanfmglobal in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 04:53 AM
  2. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 11:15 AM
  3. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 09:12 PM
  4. Help With Project!!!
    By jackhammer in forum New To Java
    Replies: 5
    Last Post: 12-04-2008, 06:10 AM

Posting Permissions

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