1

I am a Java novice and I tried to get this code running but always seem to give me errors. I tried using sum and other functions too. What am I doing wrong?

Here is the question I am practicing-

Find the total sum of length of lines. A line is defined with 2 points A and B on the number line. For example if A = -3 and B = 10, the length of the line is 13. This is because the distance between -3 and 10 on the number line is 13 units (10 -(-3) = 13) Similarly, if A = 9 and B = 5, the length of line shall be 4 units as the distance between 9 and 5 on the number line is 4 units( 9 - 5 = 4)

Input - There will be 2 lines and each line will have number A & B integers separated by space.

Output - This should return the sum of length of 2 lines given as input by the user.

Sample Input:

5 9
-10 3

Expected Output:

17

Explanation: First line represents first line's coordinates i.e. A = 5, B = 9. Second line represents second line's coordinates i.e. A = -10, B = 3.

Length of first line = 4, length of second line is 13. Hence output is 17.

My code-

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Source{


    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] firstLineCoordinates = br.readLine().split(" ");
        int a1 = Integer.parseInt(firstLineCoordinates[0]);
        int b1 = Integer.parseInt(firstLineCoordinates[1]);

String[] secondLineCoordinates = br.readLine().split(" ");
        int a2 = Integer.parseInt(secondLineCoordinates[0]);
        int b2 = Integer.parseInt(secondLineCoordinates[1]);

        Line firstLine = new Line(a1, b1);
        Line secondLine = new Line(a2, b2);

        int totalSumOfLines = getTotalSumOfLines(firstLine, secondLine);
        System.out.println(totalSumOfLines);

        br.close();
    }

    private static int getTotalSumOfLines(Line firstLine, Line secondLine) {
        int sum = totalSumOfLines;
        return sum((b2 - a2) + (b1 - a1));
         // ERROR IN THIS METHOD
         
    }

    public static class Line {
        private int a;
        private int b;
public Line(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public int getA() {
            return a;
        }

        public int getB() {
            return b;
        }
    }
}

Compile time errors I am getting-

Source.java:29: error: cannot find symbol
        int sum = totalSumOfLines;
                  ^
  symbol:   variable totalSumOfLines
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                    ^
  symbol:   variable b2
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                         ^
  symbol:   variable a2
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                                ^
  symbol:   variable b1
  location: class Source
Source.java:30: error: cannot find symbol
        return sum((b2 - a2) + (b1 - a1));
                                 ^
symbol:   variable a1
  location: class Source
5 errors
Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
SciFu
  • 11
  • 2
  • 2
    None of the variables you defined in your `main()` are visible in the scope of your `getTotalSumOfLines()` method. See [What is 'scope' in Java?](https://stackoverflow.com/questions/38177140/what-is-scope-in-java) Either make the variables class-level or pass the variables to the method you need them in. – maloomeister Jul 19 '21 at 05:56
  • the issues are with the scope of the variables. – sittsering Jul 19 '21 at 06:04

2 Answers2

0

Try this

private static int getTotalSumOfLines(Line firstLine, Line secondLine) {
    int sum = 0;
    if(firstLine.getA() > firstLine.getB())
        sum = sum + (firstLine.getA() - firstLine.getB());
    else
        sum = sum + (firstLine.getB() - firstLine.getA());
    if(SecondLine.getA() > secondLine.getB())
        sum = sum + (SecondLine.getA() - secondLine.getB());
    else
        sum = sum + (secondLine.getB() - SecondLine.getA());
    return sum;
         
}
sittsering
  • 1,219
  • 2
  • 6
  • 13
0

private static int getTotalSumOfLines(Line firstLine, Line secondLine) {

     // take this to getTotalSumOfLines

    int sum1= Math.abs((firstLine.getA() - firstLine.getB()));
    int sum2= Math.abs((secondLine.getA() - secondLine.getB()));
    return sum1+sum2;

}