• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to use two different numbers for the same calculation?

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

For my elevator assignment, I need to keep the old average wait time and average travel time, and compare them with the new average wait time and average travel time (by changing the time it takes for the elevator to ascend or descend from 2 seconds to 1 second. The variable is floorTime). However, I am not sure how I could do this without replacing the old numbers that are passed to the class that sets the wait time and the travel time.

How could I have my program calculate two sets of different numbers without having to create more methods just for the new values? (Using old values, calculate, then save average time. Old values changes to new, calculates, then saves as new average time. All using the same methods)

Here's the code that sends the values to the Building class:


Building class that initializes the elevators with the values above:



I tried to create another object to pass in the new values, but that would replace the old one. The only other way that I can think of doing this is to have separate calculation for old values and new values, which would double the amount of code lines.

Any input would be greatly appreciated!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeong Ryu wrote:I tried to create another object to pass in the new values, but that would replace the old one.


You didn't show us the rest of your Building class, but I guess your member variables NUM_FLOORS and NUM_ELEVATORS are static. That means that there's only one copy of those variables, which is shared by all instances of class Building. Make the variables non-static (and change their names, to make them conform standard Java naming conventions).

See: Understanding Instance and Class Members in Oracle's Java Tutorials.
 
reply
    Bookmark Topic Watch Topic
  • New Topic