Monday 15 July 2013

if statement - Having trouble with a library book class java assignment -



if statement - Having trouble with a library book class java assignment -

i have assignment given pre-written driver programme date class , librarybook class, , have write classes pass bunch of checks in driver program. problem comes 1 of checks sees if charged returning book on time. should charged $0 reason programme says $15. here's method calculating fine:

public double getfineamount(date datereturned){ double fine; date duedate = this.getduedate(); if(datereturned.isonorbefore(duedate)){ fine = 0.00; } else{ if(this.isfiction){ fine = 1.2 * duedate.getdaysuntil(datereturned); if(fine > 15){ fine = 15.00; } } else{ fine = 1.5 * duedate.getdaysuntil(datereturned); if(fine > 20){ fine = 20.00; } } } homecoming fine; }

when date book borrowed feb. 10th, 2012 , due date march 2nd, 2012 reason isonorbefore method returns false. here isonorbefore method:

public boolean isonorbefore(date other){ boolean onorbefore; scanner sc = new scanner(other.tostring()).usedelimiter("-"); int otherday = sc.nextint(); int othermonth = sc.nextint(); int otheryear = sc.nextint(); if(otheryear >= this.year){ if(othermonth >= this.month){ if(otherday >= this.day){ onorbefore = true; } else{ onorbefore = false; } } else{ onorbefore = false; } } else{ onorbefore = false; } homecoming onorbefore; }

i believe problem caused jump year don't see what's causing error. here's code observe jump year in case helps

public boolean isleapyear(){ boolean leapyear; if(this.year % 100 == 0){ if(this.year % 400 == 0){ leapyear = true; } else{ leapyear = false; } } else{ if(this.year % 4 == 0){ leapyear = true; } else{ leapyear = false; } } homecoming leapyear; }

i can post more of code if needed.

how debug code? that's real question here, , that's how should answered. approaches debugging varied , should differ based on utilize case. in situation, i'd consider following:

system.out.println (..) statements both before , after suspect conditionals determine if if statements behaving planned , whether values going them expected. log files write out specified info @ key points in code file entire execution flow of programme can analyzed. @ log4j. debugger execution in ide eclipse.

hint: passing dates method expected?

note: can cut down length of code -- i.e. number of code blocks -- setting boolean onorbefore false , testing affirmative conditions set true.

java if-statement methods nested

No comments:

Post a Comment