Hello i need some help on what to add to the following if statement for the out put to read the value of sum times the actual number of sum:

public class ICMA42
{
   /* instance variables */
   private int alpha;
   private int bravo;
   private int sum;

   /**
    * Constructor for objects of class sum which initialises sum to 0.
    */
   public ICMA42()
   {
      this.alpha = 0;
      this.bravo = 0;
   }

   /**
    * Sets the value of alpha to the receiver to the value of the argument aAlpha.
    */
   public void setAlpha (int aAlpha)
   {
      this.alpha = aAlpha;
      this.sum = alpha + bravo;
   }

   /**
    * Sets the value of alpha to the receiver to the value of the argument aAlpha.
    */
   public void setBravo (int aBravo)
   {
      this.bravo = aBravo;
      this.sum = alpha + bravo;
   }

   /**
    * find the sum of the two int type arguments and store the result in a local variable called sum 
    */
   public void sumArgs()
   {
     if (sum > 10)
     {
      System.out.println("Sum is " + this.sum);
     }
     if ((sum >= 1) && (sum <= 10))
     {
      System.out.println("Sum is " + this.sum);
     }
   }
}

Edited 1 Year Ago by erictheonly1

I am confused, do you have 2 variables called sum? One decleared globally and one that is a parameter of a method? Maybe show the entire code or give more explanation

Okay, i just saw what James replied to your previous post, is this what you want:

If sum is 5 it will print

Sum is 5
Sum is 5
Sum is 5
Sum is 5
Sum is 5

If this is what you want, use loop as he said on your other thread, for loop will do it

Edited 1 Year Ago by Slavi

Yes, thank you for your reply. Just struggling to figure out where exactly this would go in the code.

Ok, so i've added this just need to figure out how to make it print out the number of sum:

for(int sum=1; sum<11; sum++)
     {
      System.out.println("Sum is " + this.sum);
     }

Nope, let's take a look at it

For(int i = 0; i<AnyVariableOfYourChoice; i++)
basically says, for the integer i which is 0, as long as i is less than AnyVariableOfYourChoice, increment i by 1 after the code inside the loop has executed

So in your case it would be

for(int i=0; i<sum; i++)
     {
      System.out.println("Sum is " + sum);
     }

so you create a variable i and set it to 0, lets say your sum is 5. It checks the condition is i<sum? yes it is, so the code inside the loop executes, where it prints the sum and at the end after all the code inside the loop has been executed, it increases the value of i. Then for the next iteration i is 1 and it checks again and so on ... At the point when i is 5, the loop won't execute, however it will have executed 5 times already since i starts at 0

Thank you for your help, one last bit, how do i make this stop after sum = 10?

Edited 1 Year Ago by erictheonly1

There's no need for an extra if
Have another look at Slavi's post. If it's still not clear come back here, because there's something you haven't got right yet.

This question has already been answered. Start a new discussion instead.