Results 1 to 13 of 13
  1. #1
    mari.beka is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default 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);}
        }
    
        }

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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. :)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Keep track of the summation separately for validation. Or else you've traverse the same again to find the total.

  4. #4
    mari.beka is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    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
    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);
             } 
        }
    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..


    example
    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
    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....
    please help...this is driving me crazy.......:(

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  6. #6
    mari.beka is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    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 ?.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    mari.beka is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    holds zero.....i don't understand...
    yeah i also debugged the program...it's ok....
    just the statement is not.......

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    830
    Rep Power
    6

    Default

    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:

  10. #10
    mari.beka is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    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. #11
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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:

    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);
        }
    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:
    		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;
    			}
    		}
    To print the sentence add

    Java 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);
        }
    after the while loop
    I hope this helps to get your problem solved.

    Erik
    Last 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. :)

  12. #12
    mari.beka is offline Member
    Join Date
    Nov 2010
    Posts
    6
    Rep Power
    0

    Default

    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

  13. #13
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    You are welcome.
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. Problems with values that come from the JSlider
    By peterhabe in forum AWT / Swing
    Replies: 6
    Last Post: 08-14-2012, 09:30 PM
  2. Replies: 5
    Last Post: 07-28-2009, 10:22 PM
  3. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  4. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •