1

So for an assignment in my programming class, we are supposed to make an ArrayList to store objects of a class created in a previous assignment. My only problem is that i'm not exactly sure how to make an add method for an object in java.

The class that we are making objects from is a class designed to give information about different cars. Here's the code for this class here:

public class NewCar
{
   String year;
   String make;
   String model;
   String _year;
   String _make;
   String _model;
   double sticker_price = 0;
   double discount = 0;
   double sales_tax = 0;
   int months = 0;
   double final_price = 0;


   public NewCar(String _year, String _make, String _model)
   {
      year = _year;
      make = _make;
      model = _model;
   }

   public double calcFinalPrice(double _sticker_price, double _discount, double _sales_tax)
   {      
      return final_price = ((_sticker_price - _discount) + _sales_tax *(_sticker_price - _discount));
   } 

   public double calcZeroPctMonPayt(int _months)
   {
      return (final_price / 6);
   }
}

Our professor expects us to use the calcFinalPrice method on each new object that we enter into the ArrayList as well, but I'm confused as to how to go about this in a singular method. The only ArrayList example we've done in class was a simple String type shopping list so i'm completely confused as to how to make a list of Objects.

Here's what I have so far, if someone could explain to me what's supposed to go into the add method to achieve the professors desired results I'd be super appreciative!

import java.util.*;
import java.util.ArrayList;

public class NewCarList
{
   private ArrayList<NewCar> carList = new ArrayList<NewCar>();

   public void add()
   {

   }
}
Abbey S
  • 111
  • 2
  • 15
  • 1
    `carList.add(new NewCar("","",""))`. You can adjust that to use it with a parameter passed to your method. – takendarkk Oct 23 '16 at 00:56
  • 1
    To elaborate, there's no need to make an `add` method, `ArrayList` already has one built-in. You can add a `NewCar` by doing what @takendarkk said – Shannon Oct 23 '16 at 00:59
  • You are on the right track, you have created `carList` correctly. Just call `carList.add(new Car("1990", "Volvo", "240"));` as the others describe. – Jameson Oct 23 '16 at 01:00
  • @Shannon I know there's one built in, but unfortunately for full credit the professor would like us to create one. Seems like they always want you to take the long way around on these assignments but I guess that's how you learn. – Abbey S Oct 23 '16 at 01:01
  • @takendarkk Thank you! But how exactly will I be able to use the calcFinalPrice method on each new object being created into the ArrayList? Sorry, I've only been programming for a few weeks so some of the simple things still escape me :/ – Abbey S Oct 23 '16 at 01:05
  • @Jameson I've done what you and the others have said, but when I view the values of the ArrayList the objects come up as "NewCar@1db9742" Instead of "1990 Volvo 240". Do you have any idea what could be causing this? – Abbey S Oct 23 '16 at 02:05
  • @AbbeyS You'll need to implement `toString()` in the `NewCar` class. you could do something simple like have it `return String.format("%s %s %s", year, make, model);` See: http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – Jameson Oct 23 '16 at 08:49

1 Answers1

1

If you're allowed to used the ArrayList's add method inside your add method, you could instantiate a NewCar object and assign it to a local variable. You can then call the calcFinalPrice method on the NewCar object and then add it to your ArrayList.

Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67
Ritzh
  • 61
  • 5