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 -
class distance:
-
def __init__(self, iniX, iniV):
-
self._x0= iniX
-
self._v0= iniV
-
-
def setTime(self,t):
-
self._t0=t
-
-
def result(self):
-
return self._x0 + self._v0*t
-
-
#the result I would like to have
-
d = distance(5,10)
-
d.setTime(5)
-
#d = 5 + 10*5 = 55
-
>>>55
-
d.setTime(5)
-
#d=d(5) [the first distance set by t=5] + vo*5[the new distance set by another t=5]=105
-
>>>105
-
How can I write the right code?
3 1207
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 -
class distance:
-
def __init__(self, iniX, iniV):
-
self._x0= iniX
-
self._v0= iniV
-
-
def setTime(self,t):
-
self._t0=t
-
-
def result(self):
-
return self._x0 + self._v0*t
-
-
#the result I would like to have
-
d = distance(5,10)
-
d.setTime(5)
-
#d = 5 + 10*5 = 20
-
>>>20
-
d.setTime(5)
-
#d=d(5) [the first distance set by t=5] + d(5)[the new distance set by another t=5]=40
-
>>>40
-
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. -
class distance:
-
def __init__(self, iniX, iniV):
-
self._x0= iniX
-
self._v0= iniV
-
self._t0 = 1
-
-
def __str__(self):
-
return "%f" %self.result()
-
-
def setTime(self,t):
-
self._t0 = t
-
-
def result(self):
-
return self._x0 + (self._v0 * self._t0)
-
-
-
d = distance(5, 10)
-
print d
-
d.setTime(5)
-
print d
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. -
class distance:
-
def __init__(self, iniX, iniV):
-
self._x0= iniX
-
self._v0= iniV
-
self._t0 = 1
-
-
def __str__(self):
-
return "%f" %self.result()
-
-
def setTime(self,t):
-
self._t0 = t
-
-
def result(self):
-
return self._x0 + (self._v0 * self._t0)
-
-
-
d = distance(5, 10)
-
print d
-
d.setTime(5)
-
print d
Thank you for helping me.
In your code, -
def __str__(self):
-
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
Thank you for helping me.
-
class distance:
-
def __init__(self, iniX, iniV):
-
self._x0= iniX
-
self._v0= iniV
-
-
def __str__(self):
-
return "%f" %self.result()
-
-
def setTime(self, t):
-
self._x0 += self._v0 * t
-
-
def result(self):
-
return self._x0
-
-
-
d = distance(5, 10)
-
print d
-
d.setTime(5)
-
print d
-
d.setTime(5)
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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){...};
}
|
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...
|
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;
|
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
{...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |