John 3:16
Campbell Ritchie wrote:I like the use of the static imports.
Campbell Ritchie wrote:You should know better than to ask for code review when I am awake. Mwaahaahaahaah!
Actually there is a lot of good code there. These changes could make it even better:
1: Don’t use tabs for indentation; use spaces. Reasoning here. 2: Far too much code in the main method. Most of that code ought to be moved into the rest of the class, and it looks as if it can be divided into about 3 separate methods, if not more. Lots of small methods = good, one big method = bad. 3: I like the use of the static imports. 4: No constructors for any of the classes. I agree with what they say here that you ought to write a constructor in every class. 5: The KeyboardInput class looks to me like a utility class. It should have all static members, a private constructor, and the Scanner object a private static field. If you search this forum you will find suggestions I have written earlier about utility classes. 6: Never use float arithmetic unless you have a very good reason. Use doubles throughout. 7: Your method to solve quadratic equations lloks as if it could be a static method in a utility class, too. Or you would pass the cooefficients to the constructor and use them as fields in an object. 8: That method will return NaN if there is no real solution. It is worth learning the strange behaviours of NaN.
John 3:16
Jordan D. Williams wrote:
Campbell Ritchie wrote:You should know better than to ask for code review when I am awake. Mwaahaahaahaah!
Actually there is a lot of good code there. These changes could make it even better:
1: Don’t use tabs for indentation; use spaces. Reasoning here. 2: Far too much code in the main method. Most of that code ought to be moved into the rest of the class, and it looks as if it can be divided into about 3 separate methods, if not more. Lots of small methods = good, one big method = bad. 3: I like the use of the static imports. 4: No constructors for any of the classes. I agree with what they say here that you ought to write a constructor in every class. 5: The KeyboardInput class looks to me like a utility class. It should have all static members, a private constructor, and the Scanner object a private static field. If you search this forum you will find suggestions I have written earlier about utility classes. 6: Never use float arithmetic unless you have a very good reason. Use doubles throughout. 7: Your method to solve quadratic equations lloks as if it could be a static method in a utility class, too. Or you would pass the cooefficients to the constructor and use them as fields in an object. 8: That method will return NaN if there is no real solution. It is worth learning the strange behaviours of NaN.
Thank you kid sir! That will take a LOT to process!! I am not sure about half of what you said, but I will search the forums and try to figure it out. I will post the revised code when I am done.
The only reason why I imported java.lang.System.* was so that I don't have to type System.out.print... I realize it doesn't really save that much time/typing, but you know... I was trying to use everything I know.
One more thing... In the current book that I am reading they explain that every time you make a String Java creates a new object and you can't change the contents of that object. So every time I type System.out.println("Some string") it create a new object, right? And if that's right... Would it be better to connect some of the strings in another method with StringBuilder maybe?
Thanks for all the pointers, by the way!
~Bill
Bill Johnston wrote:
For a second application after "Hello World" I am impressed - good job. I too would probably prefer not to have the static import of System but I am not adamant about it.
John 3:16
~Bill
Jordan D. Williams wrote:
I am still struggling to understand how everything fits together in OOP.
Soon...
James Boswell wrote:I would implicitly declare the four parameters a, b, c and d for the method quadraticEquationSolver Makes the interface clearer IMO. Also, return a list of floats instead of an array as the quadratic equation can produce 0, 1 or 2 results.
Your actual coding style is very clear and well commented.
Jayesh A Lalwani wrote:Then change your main method to code to the Equation interface. Your code in the main method will reduce quite a bit.
Bill Johnston wrote: ... Don't forget to to have a "default" in your switch statement; even though it looks like you'll never need it here, if you change the code in the other class that mandates 1 or 2, you will...
Bill Johnston wrote: ... You could put that "entry" data in a loop (probably a for loop) as well, something like:
Campbell Ritchie wrote:
You should know better than to ask for code review when I am awake. Mwaahaahaahaah!
Actually there is a lot of good code there. These changes could make it even better:
1: Don’t use tabs for indentation; use spaces. Reasoning here.
2: Far too much code in the main method. Most of that code ought to be moved into the rest of the class, and it looks as if it can be divided into about 3 separate methods, if not more. Lots of small methods = good, one big method = bad.
3: I like the use of the static imports.
4: No constructors for any of the classes. I agree with what they say here that you ought to write a constructor in every class.
5: The KeyboardInput class looks to me like a utility class. It should have all static members, a private constructor, and the Scanner object a private static field. If you search this forum you will find suggestions I have written earlier about utility classes.
6: Never use float arithmetic unless you have a very good reason. Use doubles throughout.
7: Your method to solve quadratic equations lloks as if it could be a static method in a utility class, too. Or you would pass the cooefficients to the constructor and use them as fields in an object.
8: That method will return NaN if there is no real solution. It is worth learning the strange behaviours of NaN.
John 3:16
Dennis Deems wrote:
I do not. It is jarring to see out.println when you are used to see System.out.println. System is a member of the package java.lang so there is no need for an import statement at all.Campbell Ritchie wrote:I like the use of the static imports.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
John 3:16
John 3:16
Jordan D. Williams wrote:Is that truly disorganized, or it's normal and it just seems unorganized to me because I am still thinking in terms of procedural languages?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
-1 * (b) == -b.
Winston Gutkowski wrote:Mat.pow(b, 2) == b * b (and the latter is probably a lot quicker).
Winston Gutkowski wrote:Why not have a, b, c and d as parameters? You don't appear to do anything with your coeffs array except set it to internal fields with those names anyway, and giving them explicit names will help you to document them (I'm still not quite sure what d is supposed to be).
John 3:16
Never believe anything he tells you. Any code he wrote will be total rubbish.Jordan D. Williams wrote: . . . This class is not my doing. I ripped off Campbell Ritchie . . .
Jordan D. Williams wrote:Besides, QuadraticEquation.solve() implements the Equation interface and SimpleEquation.solve() also implements the Equation interface.
Would I be able to take the coeffs as parameters and still implement the method so it can be consistent when I add more classes that solve other equation types?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
John 3:16
Junilu Lacar wrote:One more nit I just found:
Look at the EquationSelector class. What happens to it if you add new types of equations? Say you wanted to add Exponential and Cubic equations to your portfolio of equations. This is going to require you to change EquationSelector, right? Is that change really necessary? Should EquationSelection have to change if you add or remove Equation types? Probably not. I smell a need to refactor. But I would wait until I actually had to add a new Equation type. I would have tests that provide a safety net to facilitate refactoring. And I would do TDD on this.
Here's how it would go down:
1. Someone (maybe myself) asks me to add Exponential and
Cubic equations to the list of supported equations
2. I develop Exponential equation and its unit tests via TDD
3. I integrate Exponential equation and test the program end to end.
4. I find that I need to change EquationSelector to get it to give Exponential
equation as a choice.
5. I refactor so that I won't have to change EquationSelector when I add
(or remove) new Equation types.
6. I develop Cubic equation and its unit tests via TDD
7. I integrate Cubic equation and test the program end to end.
8. I'm done.![]()
John 3:16
Jordan D. Williams wrote:from what I understand, with your suggested implementation, I would have to have a getCoefficients() method in each equation type class and gather the coefficients through that method, rather than the GetCoeffs utility class.
Jordan D. Williams wrote:Unit testing... I have no idea how to do it? Are there any resources that can point me to? (Besides the one you showed in your post? I did not get anything on that page) That is something that is completely new to me. I mean.. I know that you "test" your code before you release the application, but as far as "how" to write and implement code that does that, I have no idea.
John 3:16
Junilu Lacar[b wrote:Exercise 1. Assigning values and using the for-loop (10 points)[/b]
Instructions: Given the JUnit test below, write the code that would make it pass. Use what you learned about assigning values and the for-loop in your solution.
Challenge 1 - Extra credit (2 points)
Modify AccumulatorTest so that instead of using a hard-coded value for the expected result, you will use an established formula to double check the brute-force calculations made by your Accumulator.
Challenge 2 - Extra credit (4 points)
Add a test to AccumulatorTest that will require you to implement an overloaded version of total() that takes an array of integers as its parameter. The result should be the sum of all the integers in the array. Your solution should include the code in Accumulator that will make the test pass.
John 3:16
John 3:16
Jordan D. Williams wrote:So here is my second try. This time with Eclipse and even tested with JUnit so I know it all works
. The only question now is if this is what you had in mind as a solution.
Junilu Lacar wrote:
Yes, absolutely. Now I hope you have been test-driven infected. It's a great way to program. I'm confident that you're ready for the material in "Growing Object-Oriented Software, Guided by Tests" now.![]()
John 3:16
Junilu Lacar wrote:
Inappropriate Coupling - TMI (too much intimacy)
When a class knows too much about other class(es), it creates unnecessary coupling. Why would an Equation care about how its coefficients are obtained? Your implementations of Equation know too much about this detail -- they are coupled too tightly with GetCoeff. This is where my niggle about private constructors without factory methods comes into play. The thing that clued me in to this smell was that the method name getValues in the Equation interface did not logically match the method's return type. It felt to me like the method wanted to be named something like buildEquation or createEquation with a variable length list of coefficients appropriate for the type of equation passed into it.
John 3:16
John 3:16
Jordan D. Williams wrote:Do both EquationTest and QuadraticEquationTest run simultaneously? In other words QuadraticEquationTest extends EquationTest, but the entirety of EquationTest also gets executed when you run the QuadraticEquationTest?
That question probably stems from my little understanding of inheritance. When you instantiate a class that extends a parent class, I guess the parent class also gets instantiated?
Basically you have this:
(elided code)
creates a QuadraticEquation object and runs the test on THAT type of object?
Junilu Lacar wrote:
It's like when you were born, you took on some of your father's characteristics but that didn't create another "instance" of your father in you. In a sense, you "are" your father but at the same time you are "your father's child". Maybe it's better to say "You are of your father." Another analogy: you know how sometimes people will say "You laugh just like your father." or "You do this just like your father." It's kind of like that with object inheritance. Whatever the parent is programmed to do, so shall the child do it as well. Only as the programmer, you can chose to make the child either do something exactly like it's parent, in which case you'd just call/invoke the inherited method or you can make the child do that thing slightly differently than its parent, in which case you'd override the method and program some new logic.
John 3:16
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |