• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

how to write a method to sum the prices of all cars in a list of cars.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi There,

First post so please take that into consideration.

OK so I am creating 2 classes 1 called Car and another called App with main method in it.

I have a few instance variables in Car

String make;
String model;
double price;

I have a constructor based on the above variables and getters and setters and a toString method.

In my App class in main I have instantiated an ArrayList<Car> and added 3 cars to it

My question is and it might be so simple and stupid but it is currently beyond my comprehension.
But I want to just sum the totalPrice of my 3 cars using a method in my Car class and to call it in my App class within main.

That is it.

I do not think there is any need for me to send code as it should be an understandable scenario to our Java gurus out there but if need be I will provide the code.



Regards,

Steve
 
Marshal
Posts: 72088
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us what you have got already, and tell us who you think you would add all the prices. If that doesn't give you the answer, try thisJava™ Tutorials section. Look at any pats with “List” in their names, and the section about aggregate operations.
 
Stephen Potter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for your reply,

OK so in my Car Class I have this



And in my App Class I have this



As I mentioned before, I contacted this green horn friendly forum for help, if I can get how this works then I can hopefully apply it to other calculations.

Regards,

SP
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We believe people remember things much better if they work them out for themselves than if somebody gives them a solution on a plate.
I have only had a 2‑second look at your code, but it looks all right so far. Did you find anything useful from those Java™ Tutorials? If you had to calculate that total without a computer, how would you do it? Your program will probably use the same technique.

I have noticed something: make all the fields private.
 
Bartender
Posts: 7804
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Car class only knows about a single Car, so you can't perform a sum there, so you'll have to do it in your App class. To loop through all the cars:
 
Stephen Potter
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey,

Many thanks for pointing me in the right direction.
I moved my method into the App class and then did some further reading on Streams and Lambda expressions and came up with a solution that was further refined by suggestions from IntelliJ my IDE.

I appreciate your help.

Regards,

SJP
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done Please show us the finished product.
 
Sheriff
Posts: 7947
548
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephen Potter wrote:// variables
String name;
String make;


What is the difference between the name and make in this context?

Please avoid writing such comments, which tell nothing extra for the reader of your code, but just clutter the code.

Why did you define those setName(), setMake() methods? Is car's make meant to change after the car is built (via constructor)? Remove all those and you'll see how your class will get more concise an simpler to cope with in your head, simply because it will have only what it has to have and nothing else.

In addition to Campbell's advice to make fields/variables private, make them final as well, that way you'll ensure they won't change their values over the time. That not only decreases errors, but also increases readability of the code, because knowing variable can't change its value, is much easier to read the code and concentrate on the things which may change their value.
 
Liutauras Vilda
Sheriff
Posts: 7947
548
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..and welcome to the Ranch
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would also like to see the source code if possible please.
 
Campbell Ritchie
Marshal
Posts: 72088
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Challenge:
We experienced people know that doubles are not suitable for exact calculations, and “exact calculations” includes anything to do with money. We also know that int arithmetic is suitable for exact calculations, but has a limited range because of the risk of overflow errors. We also know that the best datatype for calculating money is BigDecimal. The challenge is to change the price field to type BigDecimal and show us how you would do the arithmetic. It is easy enough to add ints with a Stream:- You can/should change the int to var.
...but you can't use sum() on a Stream<BigDecimal>. It is however possible to write that Stream code to add BigDecimals. Please show us how you totalled the prices, and see if you can work out how to total BigDecimals.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic