Results 1 to 13 of 13
Thread: Comparing values problems
- 11-13-2010, 04:06 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
Comparing values problems
Hello everybody,
I'm trying to write a program that calculates the year salary of a person in two variants A and B for a period of 25 years.
The program should display for each year the raise and the total sum of the money. And in the end the program should display a comparing sentence,
saying which variant is better A or B and from which year.
This is the program code that i've done so far, but sth is wrong with the sentence cuz it shows always the last year as a reference....please help.....
Java Code:public class Lohnerhohung { public static void main(String[] args){ double kapitalA=1200,kapitalB=1200; double [] sumA = new double[100]; double []sumB= new double[100]; double varianteB = 0; int i=0; double jahreA,sumA_1 = 0,sumB_1=0; System.out.println("Year | Year A | Sum A | Year B | Sum B"); do{ kapitalA = (kapitalA * 0.100) * 11; jahreA=kapitalA+100; kapitalB=kapitalB*Math.pow((1+0.1/11), 11); varianteB+=kapitalB; sumA_1+=jahreA; sumA[i]=sumA_1; sumB_1+=varianteB; sumB[i]=sumB_1; System.out.print("\n"+i); System.out.print("\t"+jahreA); System.out.print("\t\t"+sumA[i]); System.out.print("\t\t"+varianteB); System.out.print("\t\t"+sumB[i]); i++; } while(i<25); if(sumA[i]>kapitalB){ System.out.println("A is greater from year"+i);} else if (sumB[i]>sumA[i]){ System.out.println("B is greater from year"+i);} } }
- 11-13-2010, 04:41 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
After the while loop i equals 25 and both sumA[i] and sumB[i] are zero... that's why
I'm new to Java but I like to help where ever I can. :)
- 11-14-2010, 02:00 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Keep track of the summation separately for validation. Or else you've traverse the same again to find the total.
- 11-14-2010, 10:36 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
Thank you guys, i appreciate your words....I have tried a lot to make things better, but still having problems to find a way to publish that sentence in the end....here is the changed code
The problem now is that it gets the highest number of all of them, but i want the first max number. Meaning when one of the sums gets higher, then it should print that number when the sum got higher, and the year when it got higher..Java Code:public class Lohnerhohung { public static void main(String[] args){ double kapitalA=1200,kapitalB=1200; double [] sumA = new double[100]; double [] sumB = new double[100]; double varianteB = 0; int i=1; double jahreA,sumA_1 = 0,sumB_1=0; double max = 0; System.out.println("Year | Year A | Sum A | Year B | Sum B"); for(i=0;i<25;i++){ kapitalA = (kapitalA * 0.100) * 11; jahreA=kapitalA+100; kapitalB=kapitalB*Math.pow((1+0.1/11), 11); varianteB+=kapitalB; sumA_1+=jahreA; sumA[i]=sumA_1; sumB_1+=varianteB; sumB[i]=sumB_1; System.out.print("\n"+i); System.out.print("\t"+jahreA); System.out.print("\t\t"+sumA[i]); System.out.print("\t\t"+varianteB); System.out.print("\t\t"+sumB[i]);} max = Double.NEGATIVE_INFINITY; for(i=1;i< 25;i++){ if(sumB[i]>sumA[i]) { max = sumB[i]; } } System.out.println("\n"+max+"\t"+i); } }
example
In this case i want the program to publish just the sumB and year 1....but i cant...with the code that i mentioned i can just get the maximum one....Java Code:Year | Year A | Sum A | Year B | Sum B 0 1420.0 1420.0 1325.6556809295562 1325.6556809295562 1 1552.0 2972.0 2790.1248345802273 4115.780515509783 2 1697.2000000000003 4669.200000000001 4407.94304548282 8523.723560992603
please help...this is driving me crazy.......:(
- 11-14-2010, 02:43 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You've terminate the process at i = 25. At that time in both arrays the value of element 26 (or the index 25) holds the value of zero. So none of the criteria is not met, and nothing is print to the console.
Could you explain a bit what you really want to do? What's the logic behind to print those statements.
- 11-14-2010, 04:45 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
The logic of this is that the program should print a statement which shows which alternative is better and from which year.
In my case the program is calculating the salary in two ways so the program should print the a statement that says
alternative A or B (comparing the sumA and sumB) is better from year ?.
- 11-15-2010, 01:47 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
But the thing is sumA and sumB holds zero in your case. Did you try to debug the code?
- 11-15-2010, 09:18 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
holds zero.....i don't understand...
yeah i also debugged the program...it's ok....
just the statement is not.......
- 11-15-2010, 09:43 AM #9
U have to put comments above each line of code for understanding your logic.
As per the above code ecev max should also be an array.and i u are printing which will be always 25 .Ramya:cool:
- 11-15-2010, 05:43 PM #10
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
guys....if i knew how to do it,
i wouldn't ask for help..........so please if you have a real answer for it, tell it to me....
i cannot explain more then this....i think i have made all the my questions clear...
- 11-15-2010, 08:57 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Okay, I'll give it a try then...
You want to find an answer to the question: in what year does the sum of variant B exceed the sum of variant A. Right?
What you need to do is check sumA and sumB inside the while statement and remember i when sum B exceeds sum A. After the loop you can print your sentence.
Something like:
This assumes that variant A starts as the 'better' option which is the case in your code. If you want to try other variants and you don't know which one of the to starts as the better option you should first check which one starts as the best one like this:Java Code:int j = -1; do{ kapitalA = (kapitalA * 0.100) * 11; jahreA=kapitalA+100; sumA_1+=jahreA; sumA[i]=sumA_1; kapitalB=kapitalB*Math.pow((1+0.1/11), 11); varianteB+=kapitalB; sumB_1+=varianteB; sumB[i]=sumB_1; if (sumB[i] > sumA[i] && j == -1) { j = i; } System.out.printf("%2d",i); System.out.printf("\t%10.2f",jahreA); System.out.printf("\t\t%10.2f",sumA[i]); System.out.printf("\t\t%10.2f",varianteB); System.out.printf("\t\t%10.2f\n",sumB[i]); i++; } while(i<25); System.out.println("B is greater from year "+j); }
To print the sentence addJava Code:if (i == 0) { // in the first year determine best starting position bestStartingPosition = sumA[i] > sumB[i] ? 'A' : 'B'; } switch (bestStartingPosition) { case 'A': // if A has the best start, check whether B exceeds A this year if (sumB[i] > sumA[i] && j == -1) { j = i; } break; case 'B': // if B has the best start, check whether A exceeds B this year if (sumA[i] > sumB[i] && j == -1) { j = i; } }
after the while loopJava Code:if(bestStartingPosition == 'A' && j != -1) { System.out.println("B is greater from year "+j); } if(bestStartingPosition == 'B' && j != -1) { System.out.println("A is greater from year "+j); }
I hope this helps to get your problem solved.
ErikLast edited by venerik; 11-15-2010 at 09:00 PM. Reason: added printing the sentence
I'm new to Java but I like to help where ever I can. :)
- 11-16-2010, 07:11 PM #12
Member
- Join Date
- Nov 2010
- Posts
- 6
- Rep Power
- 0
Thank u so much....this is what i was trying to do all day....
i just forgot to put that variable to save i.....
Thanks again
- 11-16-2010, 09:15 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Similar Threads
-
Problems with values that come from the JSlider
By peterhabe in forum AWT / SwingReplies: 6Last Post: 08-14-2012, 09:30 PM -
Problems with hashMap, has values in it but can't find them?
By mainy in forum New To JavaReplies: 5Last Post: 07-28-2009, 10:22 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks