Write a class called DataPoint. A datapoint should have the following two properties: - name - The name should always be exactly 5 characters long. If a name that is too short is given, the name should be padded with additional 'X' characters to make it exactly 5 characters long. If a name is given that is too long the extra characters should be removed. Thus is a user supply 'AB' as the name, it should be padded to become 'ABXXX' whilst if a user gives 'ABCDEFG' as a name, it should be truncated (shortened) to 'ABCDE' - value - The value should always be between 0 and 100. You may assume a numeric value will be supplied. if the value that is given is less than 0, the value should be set to exactly 0 and a message should be printed stating 'The value must be greater than 0'. If a value of more than 100 is given, the value should be set to exactly 100 and a message should be printed stating 'The value must be less than 100' In addition to changing the properties given the above constraints, the user of the class should be able to read both of the properties described. In addition to the above properties, the class should have the following methods: increment() - when this procedure is called the value property should be increased by 5. However, if this would raise the value to more than 100, it should be set to be exactly 100 instead. Note: This method will only change the value, no additional information will be printed. decrement() - when this procedure is called the value property should be decreased by 5. However, if this lowers the value to less than 0, it should be set to be exactly 0 instead. Note: This method will only change the value, no additional information will be printed.
Write a class called DataPoint. A datapoint should have the following two properties:
- name - The name should always be exactly 5 characters long. If a name that is too short is given, the name should be padded with additional 'X' characters to make it exactly 5 characters long. If a name is given that is too long the extra characters should be removed. Thus is a user supply 'AB' as the name, it should be padded to become 'ABXXX' whilst if a user gives 'ABCDEFG' as a name, it should be truncated (shortened) to 'ABCDE'
- value - The value should always be between 0 and 100. You may assume a numeric value will be supplied. if the value that is given is less than 0, the value should be set to exactly 0 and a message should be printed stating 'The value must be greater than 0'. If a value of more than 100 is given, the value should be set to exactly 100 and a message should be printed stating 'The value must be less than 100'
In addition to changing the properties given the above constraints, the user of the class should be able to read both of the properties described.
In addition to the above properties, the class should have the following methods:
increment() - when this procedure is called the value property should be increased by 5. However, if this would raise the value to more than 100, it should be set to be exactly 100 instead. Note: This method will only change the value, no additional information will be printed.
decrement() - when this procedure is called the value property should be decreased by 5. However, if this lowers the value to less than 0, it should be set to be exactly 0 instead. Note: This method will only change the value, no additional information will be printed.
For example:
Test | Result |
---|---|
x = DataPoint('ABCDE', 96) print(x.name) print(x.value) | ABCDE 96 |
x = DataPoint('ABCDE', 96) print(x.name) print(x.value) x.decrement() print(x.value) | ABCDE 96 91 |
x = DataPoint('ABCDE', -1) | The value must be greater than 0 |
x = DataPoint('ABCDE', 80) print(x.value) x.value = 110 | 80 The value must be less than 100 |
x = DataPoint('ABC', 23) print(x.name) print(x.value) | ABCXX 23 |
x = DataPoint('ABCDEFGHIJKLMNO', 83) print(x.name) print(x.value) | ABCDE 83 |
x = DataPoint('ABCDE', 91) print(x.name) print(x.value) x.increment() print(x.value) x.increment() print(x.value) x.increment() print(x.value) | ABCDE 91 96 100 100 |
Step by step
Solved in 2 steps