My Java is pretty rough. So far I've completed one class of Java all the way up to basic array functions. I've asked my Professor to explain it, but he talks with so much jargon. Could someone explain to me why this program spits out the "24" when I run it? O.o

I learned when changing any of the numbers, at least at the bottom (8,4,2), to '0', I get an error of "dividing by zero". But I couldn't figure out how that worked.

//#2 from Spring 2011 Test #2

public class T1b
    {
    protected int a;

    public T1b(int aIn)
        {
        a=aIn;
        }//Constructor

    public int f(int b, int c)
        {
        return a*b/c;
        }//f()
    }//class T1b



class D extends T1b
    {
    protected int third;

    public D(int a) 
        {
        super(a+1);
        third = a/3;
        }//Constructor

    public int g(int x)
        {
        return f(x, x/2);
        }//g()
    }//class D



class E extends D
    {
    public E(int z)
        {
        super(z);
        }//Constructor

    public int f(int b, int c, int d)
        {
        return super.f(b,c) +g(d);
        }//f()

    public static void main (String[]args)
        {
        E e = new E(5);
        System.out.println(e.f(8,4,2));
        }//main()
    }//class E

One way I work out the sequence of steps a program takes is to start at the main() method and number each statement in the order it is executed. A simple start:

    public static void main (String[]args)
        {
        E e = new E(5);                   // 1  
        System.out.println(e.f(8,4,2));
        }//main()


    public E(int z)                       // 2    
        {
        super(z);                         // 3
        }//Constructor

Another way is to add lots of println statements to display the results of all the computations as they are made in each method. This will require the the expressions be broken up into single steps so the results of each step can be printed.

commented: Thanks for the suggestion. It helps. +1

So I tried printing some of the statements but with no luck. This program was provided my professor, so I don't understand the purpose of it.

I guess let me start with this, what is e.f(8,4,2)? I don't understand how e.f works. I tried searching for "Java e function" but I ended up getting 'e' variable in math.

So far I understand we start at the main method. So I'm gonna go through this step by step like you just did with the //1 //2 //3. Thanks for that tip. :)

OK, I will simplify it into a mathematic equation for you (a little algebra but at the end it will become arithmatic).

Look at the first line in main() E e = new E(5);, the value saved in the class will be a constant which will be used in computation as a variable. The value of a will be 6 because you need to look back at line 26 which add the incoming value (5) by 1 before it calls back to line line 7 and store the value to a.

Now, let see what line 47 return super.f(b,c) +g(d); means. If you simplify it to a mathematic equation, it would be f(x) and g(x). The argument can be anything, you chould write it down as f(b,c) + g(d).

Then what is g(d) is expanded in line 32 to f(x, x/2) which is equal to f(d, d/2). Therefore, the new equation would be f(b,c) + f(d, d/2).

Now, the function is actually returning ab/c. In the first function, it would be expanded to f(b,c) ==> ab/c. The second function will be f(d, d/2) ==> ad/(d/2) ==> 2ad/d ==> 2a.

So the final equation would be ab/c + 2a. Now, pull out the a variable and you will get a((b/c) + 2).

So, the 3rd value going in has no effect because it will be canceled out at the end. You can test it by changing only the 3rd value and you will still see the same answer.

PS: You should know what a, b, c, and d variables are from the equation.

commented: Very simple. Thanks! +1

what is e.f(8,4,2)?

e is a reference to a class object. See E e = new E(5) where e is defined to be an instance of the class E
f is a method in the class referenced by e. See in the E class's definition public int f(int b, int c, int d)
8,4,2 are the three arguments passed to the f method

I definitely didn't understand how you knew to go back to line 7 for variabe a, with E e = new E(5).

What is happening at a=aIn;?

I think I completely see the rest.

What is happening at a=aIn;?

The value of the arg: aIn is assigned to the class variable: a

Thanks Norm and Tay. I got it. Pretty well understood. Any suggestions for easy practice problems to get it down?

ok you see at line 52 when you write :

 `E e = new E(5)`

As the class extends the class D, the constructor of the class D is called and
in fact it is

 `E e = new D(5)`;

Now class D also calls class Tb1 constructor by the instruction at line 26

`super(a+1)`

In fact it is super(5 + 1)
and at this moment
e.a is equal to 6.

now the execution continues at line 27 and
third is equal to 5/3.

Now the instruction is called

System.out.println(e.f(8,4,2));           at line 53 

and is in fact

 super.f(8,4) + g(2);     at line 47

f(8,4) returns 6* (8/4) = 12 as coded at line 14, as ais equal to 6.

`g(2)` calls `f(2,1)` which returns `6* (2/1) = 12 `

 So f(8,4) + g(2) = 24.

I hope you understood.thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.