Question

In: Computer Science

Hi can someone fully explain how the output was reached? thanks public class Example { public...

Hi can someone fully explain how the output was reached?

thanks

public class Example
{
public static void main(String [] args)
{
int a = 0, b = 4, i, j;
  
for (i = 1; i<=3; i++)
{
for(j = 1; j < 3; ++j)
{
a = a + j;
b = b + a;
System.out.println (a + " " + b);
}
}
System.out.println ("ALL DONE");
}
}
  

output

1 5
3 8
4 12
6 18
7 25
9 34
ALL DONE

Solutions

Expert Solution

a=0,b=4
Loop: i<=3 means 1<=3 true
   loop: j=1 and j<3 means 1<3 true
       a=a+j means 0+1 so a=1
       b=b+a means b=4+1 = b=5
       so it will print 1 5
   loop: j<3 means 2<3 true
       a=a+j means 1+2 so a=3
       b=b+a means b=5+3 = b=8
       so it will print 3 8
   loop: j<3 means 3<3 false
a=3 and b=8
Loop: i<=3 means 2<=3 true
   loop: j=1 and j<3 means 1<3 true
       a=a+j means 3+1 so a=4
       b=b+a means b=8+1 = b=12
       so it will print 4 12
   loop: j<3 means 2<3 true
       a=a+j means 4+2 so a=6
       b=b+a means b=12+6 = b=18
       so it will print 6 18
   loop: j<3 means 3<3 false
a=6 and b=18
Loop: i<=3 means 3<=3 true
   loop: j=1 and j<3 means 1<3 true
       a=a+j means 6+1 so a=7
       b=b+a means b=18+7 = b=25
       so it will print 7 25
   loop: j<3 means 2<3 true
       a=a+j means 7+2 so a=9
       b=b+a means b=25+9 = b=34
       so it will print 9 34
   loop: j<3 means 3<3 false

      
      


Related Solutions

What output is produced by the following code?   Explain how it works. public class A {...
What output is produced by the following code?   Explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void main(String[]...
Hi all, Can someone please answer this question. Please, list all steps! thanks! Machines A and...
Hi all, Can someone please answer this question. Please, list all steps! thanks! Machines A and B are mutually exclusive and are expected to produce the following real cash flows:       Cash Flows ($ thousands) Machine C0 C1 C2 C3 A –102 +112 +123 B –122 +112 +123 +135       The real opportunity cost of capital is 12%.    a. Calculate the NPV of each machine. (Do not round intermediate calculations. Enter your answers in dollars not in thousands, e.g....
Hi Max. Thanks for sharing this great example! Excessive levels of debt can definitely be a...
Hi Max. Thanks for sharing this great example! Excessive levels of debt can definitely be a red flag. While the company may have legitimate reasons for selecting debt financing over equity financing, too much debt can paralyze the company as they may fall into violation of their debt covenants and have difficulty meeting their financial obligations. Class, how might the interest coverage ratio be telling in this type of situation?
Hi all, Can someone please answer this question. Please list steps! Thanks! You have the chance...
Hi all, Can someone please answer this question. Please list steps! Thanks! You have the chance to participate in a project that produces the following cash flows: Cash Flows ($) C0 C1 C2 3,900 5,100 –10,900 a. The internal rate of return is 14.13%. If the opportunity cost of capital is 14%, what is the NPV of the project? (A negative answer should be indicated by a minus sign. Do not round intermediate calculations. Round your answer to 2 decimal...
Hi all, Can someone please answer this question. Please list all steps! Thanks! Consider the following...
Hi all, Can someone please answer this question. Please list all steps! Thanks! Consider the following projects: Cash Flows ($) Project C0 C1 D –10,300 20,600 E –20,300 35,525 Assume that the projects are mutually exclusive and that the opportunity cost of capital is 9%. a. Calculate the profitability index for each project. (Do not round intermediate calculations. Round your answers to 2 decimal places.) Project Profitability Index D E b-1. Calculate the profitability-index using the incremental cash flows. (Do...
Hi all, Can someone please answer this question. Please list steps! Thanks! Consider the following cash...
Hi all, Can someone please answer this question. Please list steps! Thanks! Consider the following cash flows: Cash Flows ($) C0 C1 C2 −8,450 6,200 21,400 a. Calculate the net present value of the above project for discount rates of 0, 50, and 100%. (Do not round intermediate calculations. Round your answers to the nearest whole dollar amount.) NPV @ 0% $ NPV @ 50% $ NPV @100% $ b. What is the IRR of the project? (Do not round...
How can the classes be modified to satisfy the comments & example tester: public class InvalidIntegerException...
How can the classes be modified to satisfy the comments & example tester: public class InvalidIntegerException extends Exception{ // Write a class for an InvalidIntegerException here //Constructor that takes no arguments public InvalidIntegerException (){ super(); } //Constructor that takes a string message public InvalidIntegerException (String message){ super(message); } } import java.io.InputStream; import java.io.IOException; public class Parser { private InputStream in; public static final int CHAR_ZERO = (int) '0'; public Parser (InputStream in) { this.in = in; } // Complete the...
Hi all, Can someone please answer this question. Please, list all steps. Thanks! Mr. Art Deco...
Hi all, Can someone please answer this question. Please, list all steps. Thanks! Mr. Art Deco will be paid $150,000 one year hence. This is a nominal flow, which he discounts at a nominal discount rate of 8%. PV = $150,000 / (1 + .08) = $138,889 The inflation rate is 5%.    Calculate the PV of Mr. Deco’s payment using the equivalent real cash flow and real discount rate. (You should get exactly the same answer as he did.)...
How is net income overstated or understated determined? Can someone explain with an example please.
How is net income overstated or understated determined? Can someone explain with an example please.
Can someone explain how to manipulate the clock in verilog code? For example, I do not...
Can someone explain how to manipulate the clock in verilog code? For example, I do not understand how to go from 100MHz to 68027Hz.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT