Program opened below. You can scroll through the algorithm, Java code and output.

Sum of Two Angles Using Object Passing and Returning

Download .java

Question

Add two angles expressed in degrees and minutes using object passing and object returning.

Step 1: Start the program and define a class with degree and minute data members. Step 2: Define a constructor to initialise an angle object. Step 3: Define a method that accepts another angle object as its argument. Step 4: Add the minute values of the current object and the argument object. Step 5: Divide the total minutes by 60 to calculate the carried degrees. Step 6: Keep the remainder as the final minute value. Step 7: Add both degree values and the carried degrees. Step 8: Create and return a new object containing the resulting angle. Step 9: In main(), accept the degrees and minutes of the first angle. Step 10: Create the first angle object using the constructor. Step 11: Accept the second angle and create its object. Step 12: Call the addition method and store the returned object. Step 13: Display the resulting degrees and minutes on separate lines, then stop.
Java
import java.util.*;
class Angle
{
    int degree, minute;
    Angle(int d, int m) { degree = d;
        minute = m;
    }
    Angle add(Angle b)
    {
        int m = minute + b.minute;
        int d = degree + b.degree + m / 60;
        m = m % 60;
        return new Angle(d, m);
    }
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("First angle (degrees minutes): ");
        Angle a = new Angle(sc.nextInt(), sc.nextInt());
        System.out.print("Second angle (degrees minutes): ");
        Angle b = new Angle(sc.nextInt(), sc.nextInt());
        Angle c = a.add(b);
        System.out.println("Sum = " + c.degree + " degrees " + c.minute + " minutes");
    }
}
Input: 12 45; 8 30 Output: Sum = 21 degrees 15 minutes
← Previous Program 47 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.