Menu
Question Index
...

What will be the output of the following program?

class TrackSteps {
    public static void main(String[] args)
    {
        int[][] positions = {{3, 5}, {7, 8}, {1, 3}, {6, 0}, {2, 7}};
        for (int i = 0; i < positions.length - 1; i++)
        {
            int xStep = positions[i][0] > positions[i + 1][0] ? -1 : 1;
            int yStep = positions[i][1] > positions[i + 1][1] ? -1 : 1;
            int xChange = positions[i][0] - positions[i + 1][0];
            int yChange = positions[i][1] - positions[i + 1][1];
            if (Math.abs(xChange) > Math.abs(yChange))
            {
                System.out.print(Math.abs(yChange) + "[" + xStep + "," + yStep + "]" + "+" + (Math.abs(xChange) - Math.abs(yChange)) + "H ");
            }
            else
            {
                System.out.print(Math.abs(xChange) + "[" + xStep + "," + yStep + "]" + "+" + (Math.abs(yChange) - Math.abs(xChange)) + "V ");
            }
        }
    }
}


3[-1,-1]+1V 5[1,1]+1V 3[-1,1]+2V 4[1,-1]+3H
3[1,-1]+1H 5[-1,1]+1H 3[1,1]+2H 4[-1,-1]+3V
3[1,1]+1H 5[-1,-1]+1H 3[1,-1]+2H 4[-1,1]+3V
Some other output not shown here
Compilation Error or Run-time Error

Doubts

Problems

Topic: Java Math Class - java.lang.Math

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App