• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to format additional test cases with scanner?  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening!

I want to add some more test cases to the code below, but I'm not quite sure how to format those additional test cases. Can anyone help?

Class 1-

Class 2-



Thank you very much for the help!
 
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can shorten a lot of those methods by getting rid of the if‑elses. Look at the old Sun Style Guide.
 
Saloon Keeper
Posts: 5157
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted HERE.

Jeremiah, please don't create new topics for the same issue. You have not incorporated the suggestions in your other post here so people may be duplicating effort.
 
Carey Brown
Saloon Keeper
Posts: 5157
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah wrote:

You have two problems with this code, one is that you are missing a brace, which you've already found. The other is that, being a utility class with only static methods and no member variables, a toString() method no longer has any meaning. There are no members to print. If you want something printed you'd do it in the method that calls decode(). You could slip a print into decode() but I wouldn't suggest it, that would violate the singleness of purpose that decode() has which is to decode a letter.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I attempted to ask this question several times across several of my posts, but each time it was passed over in favor of helping me fix other issues. If I'm remembering correctly, I've asked a separate three times, which I can find if needed. While I do appreciate the help with my other issues, this is a issue and I felt that maybe posting it separately would get the help I needed. The title of my other thread was also "Final line of code not returning proper values," and I felt that maybe asking this question separately may help others trying to find a solution to a similar issue.

I will not duplicate my questions in the future, but I would appreciate it if you close this thread, and not the thread on which I was actively receiving help with a separate issue, so I can continue working.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't realize that you were replying on this forum! I'll try that!
 
Carey Brown
Saloon Keeper
Posts: 5157
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using the Character class methods toUpperCase() and toLowerCase(). I'm surprised you are not also using isUpperCase() and isLowerCase() and isDigit().
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! I'm working on your suggestions now, but is there any way that you can answer my original question for this thread? It pertains to both the code used in my original thread and this one; I'm just confused on how to format the additional test cases with the imported scanner-

Jeremiah Waters wrote:
I want to add some more test cases to the code below, but I'm not quite sure how to format those additional test cases. Can anyone help?

 
Carey Brown
Saloon Keeper
Posts: 5157
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremian wrote:Digits 0-9 become A-J

This requirement could be performed as a simple mathematical expression using character constants like 'A' and '0'. Remember that characters have numeric values.
 
Carey Brown
Saloon Keeper
Posts: 5157
54
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:I want to add some more test cases to the code below, but I'm not quite sure how to format those additional test cases. Can anyone help?

I'm not quite sure what you're asking for. If you use test=Decoder.decode(letter) it seems like that would handle all of your cases. Am I missing something?
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion. I'm trying to get it to where it'll ask for me to enter several letters (sort of an "enter letter" then output response, "enter letter" then output that response, on and on). I didn't explain that as clearly as I could've, sorry.
 
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[SOAPBOX]
This is why I get frustrated with schools not keeping up to date with modern development practices being used in the industry. If I were teaching, I'd want to introduce unit tests and unit testing to students. That way, it would lead them to object-oriented programming sooner, which I don't think is that far of a stretch from what they're doing with all procedural static methods. It's just one less thing they will need to unlearn later. The other problem is that they never seem to progress to a point where they actually need to unlearn that stuff. They seem to be finishing their Java courses still writing freaking static methods all the time. And writing big blocks of code, too. Sigh.

Ok, I'm getting down now.
[/SOAPBOX]

I would just write some JUnit tests:
 
Junilu Lacar
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't buy any argument along the lines of "Unit tests are too complicated for students." I have to call BS on that. Martin Odersky's Scala course on Coursera.org uses unit tests from Day 1 and as far as I recall, there was no requirement for students to already know SBT or testing framework (ScalaTest?) in order for them to use those unit tests and verify their assignments.

If anything, I would start students off with a pre-written set of tests that they need to provide implementations for. Once they get the tests to all pass, they should be confident that their code works. They don't need to understand the full workings of the unit tests. They just need some basic instructions on how to run the tests.

I found this to be a pretty effective way to introduce students to tests even if they had never seen a test before.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I would just write some JUnit tests:



Thank you for this suggestion! I may try this approach to my code after I complete it in its current form. However, I'm still looking for assistance with my original question:

Jeremiah Waters wrote:I want to add some more test cases to the code below, but I'm not quite sure how to format those additional test cases. I'm trying to get it to where it'll ask for me to enter several letters (sort of an "enter letter" then output response, "enter letter" then output that response, on and on). Can anyone help?

 
Junilu Lacar
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:Sorry for the confusion. I'm trying to get it to where it'll ask for me to enter several letters (sort of an "enter letter" then output response, "enter letter" then output that response, on and on). I didn't explain that as clearly as I could've, sorry.


If you don't want to mess with unit tests like those that I showed above, you will need to create a loop so that those statements that you're executing one time will be executed multiple times by the loop. The problem with your approach is that you're doing it manually. That is slooooow. I'm a lazy programmer and if I can do it in a more automated fashion, I would spend time writing code to do all the testing for me, even if I'm not using an automated unit testing framework like JUnit.
 
Jeremiah Waters
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help! I was misreading an error and resolved the issue! I will take everyone's input to heart when reworking my code to be more efficient; thank you for your time!!

For anyone having a similar issue, here is my final code:


 
Junilu Lacar
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeremiah Waters wrote:Thank you all for your help! I was misreading an error and resolved the issue! I will take everyone's input to heart when reworking my code to be more efficient; thank you for your time!!


Rather than make it more efficient, rework the code so that it's clearer and smaller. There is a lot of redundant code in your final version and there are many things you can do to make it clearer and cleaner.

Just as an example, consider this equivalent version of your isUpper() method:

This code works exactly the same as your code, except it's one line and it's much clearer because it uses char literals instead of the equivalent numeric values. You can do similar things for your isLower() and isNumber() methods.
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . This code works exactly the same as your code . . .

Well, nearly exactly.
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eaasrlier today, i wrote:Well, nearly exactly.

. . . and I was mistaken; sorry Junilu. It is exactly the same. I read your code wrongly.
 
Junilu Lacar
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Eaasrlier today, i wrote:Well, nearly exactly.

. . . and I was mistaken; sorry Junilu. It is exactly the same. I read your code wrongly.


No problem. I can see how my code tripped you up.  I was trying to make it reflect the kind of shortcut notation we tried to use when we (my classmates and I) were learning to program:

That expression layers on visual positional clues on top of the relational operators in an attempt to make the intent clearer. Of course, it doesn't work, but this does:

so I will often use that arrangement rather than this:

I suppose removing the formulaic nature of the expression and making it purely intent-ful would help. Something like this perhaps:
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . .

That reminds me of what we used to write when I did O level maths: 10 ≤ x ≤ 20. I know no programming language where that sort of thing will compile; the nearest I know is. . . which is Forth, but its semantics is totally different.

. . .. . .

It all depends on whether you look at the code and see it or hear it.

If A is less than the char and the char is less than Z

versus

If the char is greater than A and the char is less than Z

I know I should say, “less than or equal”.

. . . making it purely intent-ful would help. . . .

That modularises the code, so it is better in procedural coding, or OO, or functional.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!