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

    Default

    Man, this is coming along a lot easier. I added the following toString to the Car class:

    This would be the block of information on each car. Can I have the toString method more than once in the same class?
    Last edited by camaro01; 03-07-2011 at 04:36 AM.

  2. #22
    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 are gonna put a toString in the rental class as well and that will handle the otter part, we will get to that soon enough

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

    Default

    I thought about it. I also try to add the main method to the Car class and that didn't work. So I try to build a test class and I couldn't pass the values to the car in the car class. Any advice on how to implement the main method in the car class?

  4. #24
    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 is my last post for the night but I will begin posting again tomorrow at 7 to help you out. I will also have a computer at 3 so I can help you a lot quicker and, hopefully, more efficiently. I don't know your understanding of main so I will explain it and provide you some code to test and run. Using main is a great way to test, for example, you can see if your method is working how you expected it to.

    All main methods must be public, and static, and void. They take a String[] as well.
    Java Code:
    public static void main(String[] args)
    This method does everything, it is the "driver" of everything else. It is static since its the first thing the jvm puts on the stack. The main method calls new methods onto the stack to perform operations. In main you do everything, it's the entry point to your code. In a gui you create the gui window in the main method. Once the main method is launched you can do many things.

    For example you could create some cars.

    here is a working example car class with a main method for you
    Java Code:
    public class Car{
      String name;
      double price;
      public Car(){
        name = "A Basic car";
        price = 1256.97;
      }
      public Car(String name, double price){
        this.name = name;
        this.price = price;
      }
      public String toString(){
        return "The car is a " + name + ", and it costs $" + price;
      }
      public static void main(String[] args){
        Car one = new Car();
        Car two = new Car("Mercedes", 100000.65);
        System.out.println(one);
        System.out.println(two);
        ArrayList<Car> carListAL = new ArrayList<Car>();
        carListAL.add(one);
        carListAL.add(two);
        for(Car c : carListAL){
          System.out.println(c);
        }
        Car[] carArray = { one, two };
        for(int i = 0; i < carArray.length; i++){
          System.out.println(carArray[i]);
        }
      }
    }
    This will create some objects, print them, then store them in an array list and print them all, then store them in an array and print them. It shows the use of regular for loops(Array loop), for:each loops(AL loop).

    Now onto a slightly different topic, in your method to calculate the price, you should change it a bit. Don't declare public instance variables, this is for security, you don't want outside classes to change the variables incorrectly. You also shouldn't declare it necessarily as an instance variable unless it is expected for the class. You should probably do this instead(unless you want to have an instance variable(which you might)

    Java Code:
    public double getPrice(){
      double price;
      price = days * rate;
      return price;
    }
    This way will require slightly less code in the total calculation. For instance, if it sets an instance variable when you loop through you will have to apply the method to each car in the list, then use the getter to add the instance variables to a total sum.

    If you want to use an instance variable change it to private and create a getter and setter for it.

    Most of the methods only need you to use getters. Test the main method for your car class and make sure your toString is working as you would like it, then we can move back to the rental class.

    Start by creating the avg days and avg rate methods, they are possibly the easiest methods.

    Since the Cars are being stored in an array in the rental class you will be looping a lot so brush up on looping skills if necessary. Try to implement the avg days, avg rate, and total rental fees yourself, and post the code(hint: they all use loops).
    Last edited by sunde887; 03-06-2011 at 05:59 AM.

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

    Default

    Good morning. I copied the code you provided and it didn't work. I looked at it and I realized that I needed to import the ArrayList so I did and it worked. Looking at it, I realized that I when I created the new object I was missing the parenthesis. I added the parenthesis and my test class worked. So I have half of the test class working now and I was able to use the toString and print half the list.

    Let me show you what I have in the test class.

    What you think?

    Now I just need to work on the RentalCarCompany class.
    Last edited by camaro01; 03-07-2011 at 04:36 AM.

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

    Default

    Looks good, don't forget in the final test class you will create a rentalcarcompany object instead of the array of cars.

    I want you to try tour best to get avg days and rate in the rental class defined, for now I'm not gonna give you any tips. If you need spend some time thinking about it and then post up the code for those two methods. You should be able to do this, if you struggle just keep thinking about it.
    Last edited by sunde887; 03-06-2011 at 04:50 PM.

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

    Default

    I have been working on getting this and so far this is what I have:

    1) to get the average days rented I need to add the total numbers of days all cars were rented for and divide that by the number of cars.

    I implemented the following method:

    Java Code:
    	public double getAveDays()
    	{
    		double aveDays;	
    		aveDays = (totalDays / rentCnt) * 1.0; //Any number multiply by 1 produces the same number
    		return aveDays;
    	}
    I was having problems because I was trying to divide two ints and get a double as result so I decided to multiply the result of the division by 1.0 and that fixed the problem and now I have a "double" result.

    Once I did, I realized that I needed to implement two more methods. One to get the number of days the cars are rented and another to get the numbers of cars that were rented.

    I started working with the numbers of cars since there are only 5 cars and I created the following method:
    Java Code:
    public int rentCnt()
    	{
    	for (int i = 0; i < theCars.length; i++)
    	rentCnt = rentCnt + 1;
    	return rentCnt;
    	}
    The problem here is that I am just counting the length of the array and not the values inside the array.

    Most of the problems in the book with arrays have only one value inside. In this problem I have more than one value in the array. I don't know or I can't find how to select one value from the inside of the array.

    Example,
    I created the following cars
    Java Code:
    Car mycar1 = new Car("Richard", 200, "BMW", "Sport", 2, 69.99, 10);
    Car mycar2 = new Car("Michael", 200, "Ford", "Sedan", 4, 29.99, 5);
    Car mycar3 = new Car("Nadia", 200, "Mercedes Benz", "Sedan", 4, 99.99, 7);
    Each car has 7 values so how can I just pick one value form the array, loop throught, and add it up?

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

    Default

    When you loop through the array, what is in the spot if it is empty? You can test for this and only increase car count when this condition is not true.

    As for your second question, i am not entirely sire what you are asking, you can access an array element with the array syntax.
    Java Code:
    int[] iar = new int[5];
    iar[0]
    ...
    iar[4]
    allows you to access the elements. Since in your case each item is a car you can apply all the car class methods to get information of each object.

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

    Default

    Let me ask you this and this will clarify my misunderstanding on arrays.

    How would you create an array that will hold the following information:

    account name (String)
    account number (int)
    account balance(double)
    account city where it was opened (String)

    And if you wanted to sum the balances in all the accounts in the array, how would you go about it?

  10. #30
    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 would create a class that holds all that information for you.

    Java Code:
    class BankAccount{
      //instance variables
      private double balance;
    
      //constructors
      //getters
      public double getBalance(){
        return balance;
      }
      //setters
      public void setBalanace(double balance){
        this.balanace = balance;
      }
      public static void main(String[] args){
        BankAccount[] bArray = {
          new BankAccount(),
          new BankAccount(), //I didnt use a constructor so I am using the default and will 
          new BankAccount(), //use a loop to initialize the values
        };
        for(int i = 0; i < bArray.length; i++){
          bArray[i].setBalance(i); //since each item in the array is a bank account you can 
                                               //call class methods on each item in the array
        }
        double sum = 0; //this is going to be the sum, notice it is declared outside the
         //loop so changes can be made and kept.
         for(int i = 0; i < bArray.length; i++){
           sum += bArray[i].getBalance();
         }
         System.out.println(sum);
       }
    }
    You will be doing something similar with your array. Remember, an array of car objects can have car methods called on each item in the array.

    Java Code:
    Car[] cars = new Car[5];
    for(int i = 0; i < cars.length; i++){
      cars[i].someClassMethod();  // all car class methods can be called here.
    }
    The main thing to take away from this assignment is how to use a class in another class(one class is the class) the next class is like a holder for your class.

    In this case the rental class holds some number of cars and can perform calculations on the entire list. In general this classes methods are designed to loop through its instance variable and produce some result. The better you design the car class, the easier the rental class will be. (Your car class is designed well.)
    Last edited by sunde887; 03-06-2011 at 08:57 PM.

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

    Default

    I just realized what you said and for some reason I am not able to access the car class. I have been doing double work and I didn't realize.

    For example:

    I was trying to get the totalrate in the rentalClass by doing this:

    Java Code:
    	public double getTotalRate()
    	{
    		double totalRate;
    		totalRate = days * rate;
    		return totalRate;
    	}

    And I was having problems with days and rate as they were not being recognize so I have to create them there to as instance fields.

    Then, I just realized that I have rentalCost in the Car class:

    Java Code:
      		public double getRentalCost()
    		{
    			rentalCost = (rate * days);
    			return rentalCost;
    		}
    these two are giving me the same results.

    Why can I just call the rentalCost in the Rental Class?

    Shouldn't the rent class have access to all the methods in the car class?

  12. #32
    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 directly, and you don't want any other class to have direct access. That is why you make your instance variables private. You then use the getters to extract the variables in another class.

    In the car class you access them directly so
    Java Code:
    public double getRentalCost(){
      double rent;
      rent = rate * days;
      return rent;
    }
    however if you wanted to access them from in the rental class you will need to use a getter method on a car object.

    Here is why I made you design a method in the car class which should return a double(it should look similar to what I have above, it should not set any instance variable in the car class)

    Then to get the total you can repeatedly apply this method to each and every car in the array. Ill give you this one.

    Java Code:
    public getTotalRate(){
      double total = 0; //you want to have something to add the car values to
      //don't forget, to find the total you want to find the rate for EACH car in the array
      for(int i = 0; i < cars.length; i++){
        //I am assuming that the instance variable for the array of cars is named cars
        //if its something else you will need to use the correct name in the loop
        total += cars[i].getRentalCost(); //this calls the method for each car in the array
      }
      return total;
    }
    I am aware I excessively commented this but if you post it in with correct names you should have a working code. Explain to me why.


    I need to try and help you understand something first. You have a class called car, and in this class you defined a bunch of methods to make calculations, get instance variables, and set variables. Then you create an object of type car
    Java Code:
    Car one = new Car();
    The array is filled with car objects so when you say
    Java Code:
    cars[1]
    you are pointing to the item at index 1 in the array, which is a car, and you can treat it like any other car.

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

    Default

    Thanks for the explanation above. Now I understand the how array things and the different variables inside each element.

    I got that part working, or at least not giving problems so I am moving on to the next part of the project. I need to count the number of cars so I created this method:

    Java Code:
    	public int rentCnt()
    	{
    
    		int rental = 0; 		 					
    		
    		for(int rental : theCars)
    		sum = sum + rental;
    		return rental;
    
                  }
    but when I try to compile it gives me these errors:


    Java Code:
    RentalCarCompany.java:53: rental is already defined in rentCnt()
    		for(int rental : theCars)
    		        ^
    RentalCarCompany.java:53: incompatible types
    found   : Car
    required: int
    		for(int rental : theCars)
    		                 ^
    RentalCarCompany.java:54: cannot find symbol
    symbol  : variable sum
    location: class RentalCarCompany
    		sum = sum + rental;
    		^
    RentalCarCompany.java:54: cannot find symbol
    symbol  : variable sum
    location: class RentalCarCompany
    		sum = sum + rental;
    		      ^
    RentalCarCompany.java:82: cannot find symbol
    symbol  : variable total
    location: class RentalCarCompany
    			total += theCars[i].getRentalCost(); 	
    			^
    5 errors
    Any suggestions on why I am getting these errors. Also, the total can't be found. I probably have to declare it.

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

    Default

    declare total at the beginning of the method, and at the end return the method, then you can print that method and it will print the total price. Remember, if it returns a double, it can be treated as if the method is a double.

    Another problem, your for:each loop is wrong, what is the cars list made of? ints? or cars?
    try
    Java Code:
    for(Cars c : carList)
    Would you mind posting all the code you currently have? and how long do you have before you have to turn this in?

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

    Default

    I don't mind posting the code.
    Last edited by camaro01; 03-07-2011 at 04:37 AM.

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

    Default

    Here is the Rental class:
    Last edited by camaro01; 03-07-2011 at 04:37 AM.

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

    Default

    I was also thinking that I can create a loop to count the 2-doors and the 4-door cars and keep track of them. It seems that would be easier.

    I put it in paper and it looks like this:

    I am not sure how I would pass the array through this.
    Last edited by camaro01; 03-07-2011 at 04:38 AM.

  18. #38
    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, you still have some problems but you are close, what would you like me to go over next?

    Each car has a number for door count the get2DoorCount method you made returns the price, just loop through the array, if it has 2 doors add to that, if it has 4 add to that.

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

    Default

    Thanks, let's go down the list.

    The first error I get is this:
    Java Code:
    RentalCarCompany.java:19: <identifier> expected
    	 theCars = new [20];
    	              ^
    1 error
    I don't know how to fix. I have tried the the way the book has it but it keeps giving the same error.

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

    Default

    arrays need a type

    Java Code:
    int[] intArr = new int[20];

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. CS project
    By seanfmglobal in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 03:53 AM
  2. Adding a project to an existing project
    By Seijuro in forum NetBeans
    Replies: 4
    Last Post: 08-08-2010, 10:15 AM
  3. open existing project project ..
    By itaipee in forum Eclipse
    Replies: 1
    Last Post: 12-28-2008, 08:12 PM
  4. Help With Project!!!
    By jackhammer in forum New To Java
    Replies: 5
    Last Post: 12-04-2008, 05: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
  •