Friday, May 26, 2017

How To Add Fractions Java



initially, if i compile and run the tester, all of the methods are actually returning zeros. let's fix the amdahl speedup first. right now, it's returning 0. what if i just write it in sort of the way it is? now, if i run the tester


How To Add Fractions Java, again, i'm getting an actual of 2.875. when i expected 1.6. i think i'd better go back and fix it. right now, this is doing the divisions first, so 1 over s, and s over n.


and then the additions and subtractions in the middle. and there's no real sense of the fraction that we started with. this whole piece from s to n should stay together in the denominator, and the 1 minus s needs to stay together in its numerator. let's try this again. alright, looks like amdalh/s speed up is working, but we still need to do the cross ratio. i'll try doing this sort of naively again. i've written this


as it looks but flattened it onto one line. so, if i return this quantity and i compile i'm getting an error, unexpected type, required class found value. this error probably doesn't make a whole lot of sense to yet but this is a little hint of what's to come. a pair of parentheses like this immediately followed by some other quantity has another meaning. in particular, if you put a type in here instead of an expression like a minus c. what i need to indicate


is that i want to multiply, and i'll need to do it over here as well. and now if i try to run the tester again i'm definitely not getting right answers for the cross ratio. this is because when we have a bunch of operators with the same precedents travel will just start from the left and move to the right so instead of computing a minus c times b minus d over in parentheses b minus c times a minus d. this will do a minus c times b minus d divided by b minus c and multiply that entire quantity by a


minus d. what i need to do here, is group the denominator. i could also group the numerator, if i really wanted to but it wouldn't make much of a difference. if i compile again, and run the tester, i've now got two methods working right. now for the average. i might try retuning the sum of all of these, all divided by four. let's see how this works. if i run the tester, it looks like it works in one case, but not in the other. let's look at the second case. i'm going to go read inside of the tester.


the case that isn't working is when we try to take the average of 3, 4, 3, and 3. we should, in fact, expect 3.25, but it looks like we're losing the decimal. java is interpreting this as integer division, because 4 is an integer, and a, b, c and d are all declared as integers. there are a few ways i could fix this. i could write 4 as 4., or 4.0 and then it would get the right answer here. or i could actually change all of these ints to doubles, and then i wouldn't actually


need to specify that the 4 was a double. i can compile this and run the tester, and it still works. that's because if any of these variables are a double, this whole expression in parentheses is going to come out as a double, and a double divided by an int doesn't need to be done with integer division, that calls for regular division. this is a really easy thing to mix up. it looks right to us, but the computer's going to read it wrong and the compiler won't warn you. when we


ran this with all ints and no doubles we saw that it ended up being a run time error. this is a good example of why it's helpful to think about what you want your answer to be beforehand. calculate a couple examples and then write your code. good book on this quiz.


No comments:

Post a Comment

Up