posted 2 years ago

It appears to me that it's testing various features of the EnhancedUNumber class to see if it works according to somebody's expectations.

posted 2 years ago

It also is a poorly written test sequence. Each test should be independent. In the first set, if test1resultA fails its test, the other two tests are skipped.

Suppose the other tests would all pass - that would give great insight on where to look in the code to see where the bug is.

Suppose the other tests would all pass - that would give great insight on where to look in the code to see where the bug is.

posted 2 years ago

Who wrote this code? (I hope you don't say "My instructor.")

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Nestor Coronado

Greenhorn

Posts: 13

posted 2 years ago

Yup! It was my instructor who wrote it. Though I think he intentionally makes errors to see if we can correct them. In any case, here's the assignment for that code:

***You have been asked to create a method for EnahncedUNumber objects that is very similar to what

standard “toString()” methods do, but in this case, your method accepts a integer parameter that is to

guide the formatting process. The parameter species the preferred size of the resulting output string.

***The “toString(int)” you are to create is designed to produce “human friendly” representations of floating

point values. Values with a characteristic (e.g. powers of 10) less than -3 or greater than 6, scientific

notation should be used. Otherwise, use normal decimal format. The single parameter integer parameter is

used to communicate a preferred size for the result. There are two rules about this size value:

***1. The smallest parameter value the method will accept is 8. Any value less than 8 will be ignored

and the value 8 will be used in its place. This is shown below in Figure 1

“0.12341234 × 101” would be displayed as “1.2341234”, nine characters in length if size did not matter.

Methods calls of “toString(5)” and “toString(8)” should produce the same output.

That output should be “1.234123” with one digit truncated to fit into a space of eight characters.

Figure 1

***2. The output may be larger than the preferred size value to ensure that a meaningful output is

provided. For example, UNumbers can have characteristics far larger than those of double

values in Java. It is possible to represent floating-point values with a characteristic of a thousand

or a million. If one were to represent such a value, the number of characters for even a single

digit of significance can easily exceed 8 as shown below in Figure 2. In this figure, if even one

character would be eliminated, the result would be not just wrong, but very wrong. So, the routine

is allowed to grow the size as required to represent the value with precision reduced.

“-0.4 × 10-3856”expressed in the standard “E” format would be “-4.E-3857”, nine characters in length. It is

not possible to express this value with fewer characters.

Figure 2

Values converted to strings by this method should strive to honor the specified size parameter when

possible. The following are other rules the method should honor:

1. Positive values are displayed without the use of a plus sign while negative values are always

displayed with a minus sign.

2. All values will display a decimal point so there is no confusion about whether or not the value is

floating point or integer.

3. Even though the underlying normalized UNumber values assume the decimal point is to the left of

the most significant digit, all scientific-notation output produced by this method must be shown

with the most significant digit of the value to the left of the decimal point.

4. When values in scientific notation are produce, the characteristic value (i.e. the value after the “E”

must be displayed with a either a “+” or a “-”. In the case where the characteristic value to be

displayed is zero, use “+0” and not “-0”.

5. Values with a displayed characteristic less than -3 and greater than 6 must be displayed in

scientific notation.

6. The operation of the method must not alter the value of the EnhancedUNumber

I have the sample method, and the sample code given. I'm just having trouble putting it together. Basically, I think the reasoning goes like this:

So I have the sample code with the toString() method mentioned above. I could change that code by duplicating the toString(), but changing it so that it meets the requirements above. Then conditional logic would determine what method gets the value passed into it?

Would someone be able to -hint- me in the right direction? I really want to learn this stuff, but without a person in front of me I get lost.

***You have been asked to create a method for EnahncedUNumber objects that is very similar to what

standard “toString()” methods do, but in this case, your method accepts a integer parameter that is to

guide the formatting process. The parameter species the preferred size of the resulting output string.

***The “toString(int)” you are to create is designed to produce “human friendly” representations of floating

point values. Values with a characteristic (e.g. powers of 10) less than -3 or greater than 6, scientific

notation should be used. Otherwise, use normal decimal format. The single parameter integer parameter is

used to communicate a preferred size for the result. There are two rules about this size value:

***1. The smallest parameter value the method will accept is 8. Any value less than 8 will be ignored

and the value 8 will be used in its place. This is shown below in Figure 1

“0.12341234 × 101” would be displayed as “1.2341234”, nine characters in length if size did not matter.

Methods calls of “toString(5)” and “toString(8)” should produce the same output.

That output should be “1.234123” with one digit truncated to fit into a space of eight characters.

Figure 1

***2. The output may be larger than the preferred size value to ensure that a meaningful output is

provided. For example, UNumbers can have characteristics far larger than those of double

values in Java. It is possible to represent floating-point values with a characteristic of a thousand

or a million. If one were to represent such a value, the number of characters for even a single

digit of significance can easily exceed 8 as shown below in Figure 2. In this figure, if even one

character would be eliminated, the result would be not just wrong, but very wrong. So, the routine

is allowed to grow the size as required to represent the value with precision reduced.

“-0.4 × 10-3856”expressed in the standard “E” format would be “-4.E-3857”, nine characters in length. It is

not possible to express this value with fewer characters.

Figure 2

Values converted to strings by this method should strive to honor the specified size parameter when

possible. The following are other rules the method should honor:

1. Positive values are displayed without the use of a plus sign while negative values are always

displayed with a minus sign.

2. All values will display a decimal point so there is no confusion about whether or not the value is

floating point or integer.

3. Even though the underlying normalized UNumber values assume the decimal point is to the left of

the most significant digit, all scientific-notation output produced by this method must be shown

with the most significant digit of the value to the left of the decimal point.

4. When values in scientific notation are produce, the characteristic value (i.e. the value after the “E”

must be displayed with a either a “+” or a “-”. In the case where the characteristic value to be

displayed is zero, use “+0” and not “-0”.

5. Values with a displayed characteristic less than -3 and greater than 6 must be displayed in

scientific notation.

6. The operation of the method must not alter the value of the EnhancedUNumber

I have the sample method, and the sample code given. I'm just having trouble putting it together. Basically, I think the reasoning goes like this:

So I have the sample code with the toString() method mentioned above. I could change that code by duplicating the toString(), but changing it so that it meets the requirements above. Then conditional logic would determine what method gets the value passed into it?

Would someone be able to -hint- me in the right direction? I really want to learn this stuff, but without a person in front of me I get lost.

Campbell Ritchie

Marshal

Posts: 58345

178

posted 2 years ago

Start by going through those instructions and writing a table of how many digits you are supposed to show depending on how many digits there are in the number. Remember you can work out the number of places before the decimal point by working out the logarithm of a number.

Once you have got that straight, I suggest you find out about the Formatter class, the String#format method and the formatting sections in the Java™ Tutorials: 1 2. Those are all closely related to one another. I think you can use one of them in the toString(int) method.

Once you have got that straight, I suggest you find out about the Formatter class, the String#format method and the formatting sections in the Java™ Tutorials: 1 2. Those are all closely related to one another. I think you can use one of them in the toString(int) method.

Don't get me started about those stupid light bulbs. |