What am I doing wrong (java class)
1Trying to do finish a past assignment, running into a logic error.
The credit card number (CC#) must be 16 digits in length
The CC# can be in either form: ####-####-####-#### or ################
The Expiration Date (Exp) must be in the form: MM/YY (Example: 12/15)
Notify user of correct entry form for CC# and Exp
Name, CC#, and Exp must be entered as Strings
Use a Subclass, VerifyCard() to validate the CC# with the following private method conditions:
o Condition 1: The first digit must be a 4
o Condition 2: The fourth digit must be one greater than the fifth digit
o Condition 3: The product of the first, fifth, and ninth digits must be 24
o Condition 4: The sum of all digits must be evenly divisible by 4
o Condition 5: The sum of the four digits in the expiration date must be less than the product of the last two digits of the card number
o Condition 6: The sum of the first four digits must be one less than the sum of the last four digits
o Condition 7: If you treat the first two digits as a two-digit number, and the 15th and 16th digits as a two digit number, their sum must be 100 (Example: 4643262531465454 -> 46 + 54 = 100)
- 9 comments, 46 replies
- Comment
Hey, @Thumperchick - could you take my name and the assignment name, and the package name off the two code samples? I have a unique name and don’t know that I want my entire meh posting history easily accessible to potential employers.
@Pantheist how about I delete them and you repost? I couldn’t pull out that specific info without the formatting getting wonky.
@Thumperchick works, thank you for replying so quickly
@Pantheist no problem, {easily recognizable name.}
goes to google…
@Thumperchick Haha it’s colorful enough without meh
@Pantheist I still know your name now
@jbartus I don’t think it would stop you from hiring me, so I don’t mind
@Pantheist I work in a relevant field you never know. Then again I hang out here too so you’re probably right.
@jbartus Haha yeh, my hope was that you’d forgive my meh’antics seeing as you’re a regular here.
@Pantheist nevar!
@Pantheist You need to get better at naming methods. A random programmer should be able to look at the method names without the contents and tell what it does. No one could tell what “fourVFive” or “oneFiveNine” is supposed to do without reading the content of the method.
If you ever get some free time outside of class, I recommend reading Clean Code.
@JazzyJosh don’t worry- I renamed em later. Was just very tired when I was doing this and trying to make sure my methods worked before cleaning up (this morning).
@Pantheist See, I’d say that is exactly backwards. If you write clean code to begin with, you spend less time debugging and figuring out shit as you go along.
Plus, and I speak from experience here, you can submit the beautiful code and get a passing grade, even if the shit never works quite right.
@baqui63 As far as coding goes, I can see the argument. As far as grades go, time invested aside, I have a 4.0 so my method seems to work.
@baqui63
@Panthiest.
Grades. Fucking grades. I remember those.
@f00l I’m ready to forget them. Taking 18 credits a semester year round, working full time, and making time for my wife and my drinking habit. It’s a lot.
@Pantheist
When might you be done? It’s -to a large degree - an endurance thing I think, assuming you fit the material in the first place.
@f00l If all goes according to plan, may 2018
@Pantheist
Ouch. Pain. Financial pain. Undergrad?
@f00l yes * 4.
@JazzyJosh When I was coding, our guideline was code should be easy enough that any programmer should be able to pick up the code in the middle of the night and be able to understand it. Since I was on-call for nighttime issues, I appreciated the sentiment.
P.S. that didn’t apply to assembler, that just didn’t make sense morning, noon or night (I can’t believe I used to read that crap.)
@mikibell +1. Clear naming conventions for sub, functions, arrays, and lots and lots of comments. And don’t forget to update your comments if you have to change an array or function call. Make it functional, but also make it elegant!
@mikibell
The reading of assembler code is an art I’m happy to have lost. Not so bad to lose the writing skills either.
@f00l I used to excel at reading exception outputs…now it is all octal to me. I actually did enjoy my coding days, but I am getting too old to recover from being on-call at night. That is a young person’s game. my husband was in programming too. When the phone would ring, each would pray it was the other person’s job. I still remember his first question was always “have you cleared the brc’s”. I have no clue what a brc is!
Without knowing too much about Java:
int[] ccnA = stringToInt(ccn);
Does this break ccnA down and put each number of the string as an array item? Otherwise, your other functions called within CCVerify won’t work. I would assume stringToInt would return 1 value, and not an array.
@lichme Yup- tested that I was getting arrays correctly before I started on the tests
Oh God, first I gotta decipher borderline cryptic word problems, then turn ‘em into code?!? What’s it doin’ when ya run it? “Working”, but not returning the correct answer I take it?
I think I see at least one spot that may be causing the issue. It’s in the bit for condition 5. These two bits here:
int checkCC = 0;
and
checkCC *= ccn[i];
Assuming my competency is still what it once was (no promises), the values getting fed into the loop are getting multiplied by 0, which results in an answer of 0, which is no bueno for the check you’re trying to make. Thinking through it, setting the initial value of checkCC to 1 should fix it I believe.
@nogoodwithnames Yeah, that seems to fix it.
@nogoodwithnames That’s all there was to it. Good spot, appreciate the help!
Here’s the first thing I want to suggest after glancing at the code. In your method comments instead of just writing
@return test
write something more like@return 0 if first digit is '4', 1 otherwise
. Especially since the methods return the opposite of what I’d expect. I’d expect 1 if the test is true and 0 if not.@SSteve Gotcha, will do. I just made it 0 for pass and 1 for fail so I could have any non-0 count as a fail. Made sense to me since if you ever wanted to add more tests all you would have to do is add the condition and not change anything else.
@Pantheist You can chain together as many Boolean functions as you want with &&. You get the added benefit that most languages will stop evaluating when the first function returns false (called short-circuit evaluation). That way you can put the computationally cheap functions first and if any of them return false, the expensive functions don’t have to get run.
@SSteve nice I’ll have to try that out. What I ended up doing to get it to stop as soon as it failed a condition was use nested if statements
Making the change that nogoodwithnames pointed out should fix your problem. Here are other issues I noticed. These may be because it’s a work in progress, so ignore where needed.
If you don’t enter anything for the expiration date, your program crashes. Always check for bad input. Especially in school assignments.
When you answer ‘Y’ to “Would you like to calculate another final grade?” (which looks like it was copied from an earlier assignment and not changed) the program doesn’t allow the user to enter the card holder name and skips straight to entering the credit card number.
You haven’t written a method for condition 6
You aren’t testing for valid expiration date format
A suggestion: for methods that return a yes/no value, return a boolean, not an int. Have them return true if the test passes. I’d only return an int if the method has multiple failure modes in which case zero indicates success and non-zero indicates the reason for failure. I would make
ccnA
andexpA
class variables, take all the testing out of the constructor, and write apublic boolean ccIsValid()
method that runs all the tests in a big and statement.@SSteve Yeah, most of that was just because I was testing it before I cleaned it up, but I do really like the suggestion to use boolean. I also skipped condition 6 by mistake… At least it’s an easy one. Thanks again for the help
@SSteve @pantheist this whole post had me cackling in the general direction of the QA cubes next to mine.
Hey Pantheist… they show you how to use javaScript code here:
https://santatracker.google.com/codeboogie.html
my first attempt:
pointLeft();
pointRight();
Good luck… I remember those days of debugging…
@mikibell he’s programming in Java, Javascript is a distinct language despite similarities.
@jbartus I know… I also know that those are what we called subroutines, think they are called functions and realllllllly not code… but hey, it made me laugh!!!
@mikibell There’s a maze game like this I played once. It was fun.
My “fuck with the grader” instinct might have forced me to implement this with regex. (Yes, I have sadomasochistic tendencies, especially when it comes to puzzles.)
See this, this and this. If those don’t make enough sense to you, see this, this and this, respectively.
My love for regex may stem from using SNOBOL (actually, SPITBOL) when I was a young computer geek. Or I may just be insane. Or both.
@baqui63
Doesn’t the first condition imply the second condition to some extent?
@baqui63 Haven’t read the links yet, but funny you say that- I got points off for not using regex in the first condition
@f00l In my experience, OLD computer geek strongly implies the second condition. Some young ones escape before it happens.
@compunaut hey, I resemble both those conditions!!
@compunaut
Why are old coders the insane ones? Do they slowly become insane? Does one have to be insane from the beginning in order to survival be to become an old coder?
How do the younger ones escape?
@f00l I escaped by recognition of early onset symptoms. So I ran away from coding before the insanity firmly set. Only later, to be infected from other sources.
It’s my firm assertion that kids and coding are the two major causes of adult onset insanity…
@ruouttaurmind
@f00l is to blame that
You were infected by in-
Sanity. Enjoy!
You guys frequently amaze me (once again), but I have to ask, is meh actually a legitimate resource for information, and how do you list it in the references?
@2many2no Why not? It isn’t a history class where information can just be made up. You can judge it by looking, compiling, and running. No reason not to list meh the same way I list any lifted code- just put it in a comment with a link
@Pantheist
Good history comes from primary historical sources whenever possible.
I.e. People writing on the scene at the time, or close to both, who possess no political agenda.
Of course you gotta find sources. This is a problem sometimes.
@f00l I gotcha. I was just saying why I might not trust meh as a source for history, but for programming advice it’s fine.
@Pantheist
Meh is on the internet.
Primary source, obviously.