This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Having trouble with "min = max = nums[0];"

 
Ranch Hand
Posts: 44
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went through testMaxWithAllPositiveNumbers with all
of the steps. I then added



All tests passsed

Now I will try:
 
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ray Gilbert wrote:Oops
I meant:


That shows that you understand what you're doing in the test code.  
 
Ray Gilbert
Ranch Hand
Posts: 44
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got:

Exception in thread "main" java.lang.RuntimeException: Test failed: expected 99 but got 2147483647 instead

I will go through the same process with testMaxWithSomeNegativeNumbers() and testMaxWithAllNegativeNumbers()
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To summarize, in this exercise you learned:

1. How to use tests to specify the behavior you want to develop
2. How to incrementally add functionality based on a failing test
3. How to use tests to verify functionality that you just implemented
4. How to separate "concerns" like testing, finding min, finding max
5. How to systematically attack a problem
6. How to use tests to triangulate a bug

Feel free to add to that list if I'm missing anything.

The next thing to do, since you've gone through this exercise with guidance, is to do it all over again without guidance. Start another project in Eclipse and try to walk through the process again. But rather than memorize, think about the intent of each move you make. For example, starting with the seed code I gave, you can think to yourself (or talk out loud to yourself -- who cares if you look like a senile old man) "Ok, I need to write a test that says how I want the min() method to behave. Let's start with some positive numbers only as its input." then write the assertion that will say that.

Repetition of the process, especially the thinking and decision making parts, is how you ingrain the process and the principles in your brain and create useful "muscle memory" that eventually turns into a learned skill.

Good luck and keep asking questions here when you feel you're stuck.
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One other thing I forgot to mention: if you look at your test code, you'll see examples of how you might use the min() and max() methods in your MinMax.main() method. That's another thing that tests should give you: examples of how to use the production code's API.

With the examples the tests give you, it's easier now to translate this pseudo code into actual Java code:

create an array of 10 integers
loop 10 times:
  prompt user for an integer
  set the next element of the array to that integer
find the minimum value in the array and display it
find the maximum value in the array and display it

The first line is easily translated to

and the last two lines are:
 
Ray Gilbert
Ranch Hand
Posts: 44
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Junilu. I went from looking at your code and feeling like I've never heard of Java to basically understanding this in a day. Amazing.
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ray Gilbert wrote:Thank you Junilu. I went from looking at your code and feeling like I've never heard of Java to basically understanding this in a day. Amazing.


Glad to help. I gave you a cow for your effort and stick-to-it-iveness.  
 
Junilu Lacar
Marshal
Posts: 14399
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before I forget, a few more advantages that writing a failing test before you write any production code gives you:

1. You're not locked in to any particular solution up front. This opens up many more opportunities to change the implementation later on, if you find one that's better. The tests should still pass though, with little or no change at all.

2. Writing tests first forces you into more testable designs. Code that is testable is usually better designed than code that was written without testability in mind. Think about it: if you write a test that fails first, the code you write that will make the test pass has to be testable, otherwise, how can you make the test code work and then pass at all? If you try it the other way around, just write code and keep tweaking it until it works, see how easy it will be to write a test for that code. More often than not you'll find it very difficult.

To illustrate point #1, change your min() and max() implementations to the following (don't change anything else except adding imports to make the code compile):


Make sure you're compiling with at least Java 8. If you run your tests again, they should still pass without needing to make any changes to them. This way, you can tell that your tests are only expressing the intent of the production code, not its implementation. This is one of the indications of well-designed code.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!