473,411 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

another class difining question

I would like to write a program for calculating the distance of an from initial distance x0 and to a distance vot
distance = x0 + v0t

Here is my "intended" code
Expand|Select|Wrap|Line Numbers
  1. class distance:
  2.     def __init__(self, iniX, iniV):
  3.         self._x0= iniX
  4.         self._v0= iniV
  5.  
  6.     def setTime(self,t):
  7.         self._t0=t
  8.  
  9.     def result(self):
  10.         return self._x0 + self._v0*t
  11.  
  12. #the result I would like to have
  13. d = distance(5,10)
  14. d.setTime(5)
  15. #d = 5 + 10*5 = 55
  16. >>>55
  17. d.setTime(5)
  18. #d=d(5) [the first distance set by t=5] + vo*5[the new distance set by another  t=5]=105
  19. >>>105
  20.  
How can I write the right code?
Oct 7 '07 #1
3 1207
bartonc
6,596 Expert 4TB
I would like to write a program for calculating the distance of an from initial distance x0 and to a distance vot
distance = x0 + v0t

Here is my "intended" code
Expand|Select|Wrap|Line Numbers
  1. class distance:
  2.     def __init__(self, iniX, iniV):
  3.         self._x0= iniX
  4.         self._v0= iniV
  5.  
  6.     def setTime(self,t):
  7.         self._t0=t
  8.  
  9.     def result(self):
  10.         return self._x0 + self._v0*t
  11.  
  12. #the result I would like to have
  13. d = distance(5,10)
  14. d.setTime(5)
  15. #d = 5 + 10*5 = 20
  16. >>>20
  17. d.setTime(5)
  18. #d=d(5) [the first distance set by t=5] + d(5)[the new distance set by another  t=5]=40
  19. >>>40
  20.  
How can I write the right code?
Here's where you initially when wrong: self._v0*t t exists in the set function only. You save a copy for other functions to use (self._t0), so use that.
Expand|Select|Wrap|Line Numbers
  1. class distance:
  2.     def __init__(self, iniX, iniV):
  3.         self._x0= iniX
  4.         self._v0= iniV
  5.         self._t0 = 1
  6.  
  7.     def __str__(self):
  8.         return "%f" %self.result()
  9.  
  10.     def setTime(self,t):
  11.         self._t0 = t
  12.  
  13.     def result(self):
  14.         return self._x0 + (self._v0 * self._t0)
  15.  
  16.  
  17. d = distance(5, 10)
  18. print d
  19. d.setTime(5)
  20. print d
Oct 7 '07 #2
Since 5 + (10 * 5) will never equal 20, I can't be sure about your math, but here's where you initially when wrong: self._v0*t t exists in the set function only. You save a copy for other functions to use (self._t0), so use that.
Expand|Select|Wrap|Line Numbers
  1. class distance:
  2.     def __init__(self, iniX, iniV):
  3.         self._x0= iniX
  4.         self._v0= iniV
  5.         self._t0 = 1
  6.  
  7.     def __str__(self):
  8.         return "%f" %self.result()
  9.  
  10.     def setTime(self,t):
  11.         self._t0 = t
  12.  
  13.     def result(self):
  14.         return self._x0 + (self._v0 * self._t0)
  15.  
  16.  
  17. d = distance(5, 10)
  18. print d
  19. d.setTime(5)
  20. print d
Thank you for helping me.

In your code,

Expand|Select|Wrap|Line Numbers
  1.     def __str__(self):
  2.         return "%f" %self.result()
the result(), we haven't defined yet. when I try , the distance can't be updated; still 55, if enter setTime(5) several times.

It should be 105 if I enter setTime(5) 2 times, 155 if enter setTime(5) 3 times. In other words, it continues add v0*t into the previous distance
Oct 7 '07 #3
bartonc
6,596 Expert 4TB
Thank you for helping me.
Expand|Select|Wrap|Line Numbers
  1. class distance:
  2.     def __init__(self, iniX, iniV):
  3.         self._x0= iniX
  4.         self._v0= iniV
  5.  
  6.     def __str__(self):
  7.         return "%f" %self.result()
  8.  
  9.     def setTime(self, t):
  10.         self._x0 += self._v0 * t
  11.  
  12.     def result(self):
  13.         return self._x0
  14.  
  15.  
  16. d = distance(5, 10)
  17. print d
  18. d.setTime(5)
  19. print d
  20. d.setTime(5)
  21. print d
5.000000
55.000000
105.000000

If you use better (more descriptive) names for your variable, then it will be easier for those of us that are reading your code to be able to help. Remember, all this is your creation and none of us as been thinking along these same lines.
Oct 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Murat Tasan | last post by:
so here is another general question about java... why can't you declare an abstract static method. i can envision the case (indeed i have experienced the case) where one would want an...
5
by: Stewart Midwinter | last post by:
I've got an app that creates an object in its main class (it also creates a GUI). My problem is that I need to pass this object, a list, to a dialog that is implemented as a second class. I want...
7
by: jesse | last post by:
In java, one constructor can call another constructor through this(...) for instance class foo { public: foo(int k) { this(k,false)}; foo(int k, boolean m){...}; }
19
by: CMAR | last post by:
I have the following markup. The problem is that the browser, e.g., IE6, inserts several lines of blank space between the <div> and the following table. Is there a way to minimize that vertical...
6
by: SearedIce | last post by:
Consider the following simplified hypothetical code: #include <iostream.h> class rabbit { public: rabbit() {x = 3; y = 3; /*code here to set field to 1*/} void runtocage(); int x;
5
by: cppsks | last post by:
The following seems to work for g++. Is it legal though? I was thinking that ALL non-const statics have to be initialized in the source files somewhere. #include <iostream> namespace nvps {...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
2
by: Nik | last post by:
Hi, I'm trying (and failing) to learn MC++ and I get the feeling that when I ask this question, the proverbial record will scratch to a halt and everyone will stare at me as though I'm totally...
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.