// This program calculates the sum and difference of // pairs of numbers, but it does it in a number of // different ways to demonstrate the various techniques // of passing data into and out of functions. // // Your job is to fill in the bodies of the various // functions, doing so in a way that is consistent // with the comments in each function. /*Take the SumDiff.java program and fill in the missing pieces.
* Wherever there is a "// Put your code here." comment, add the code that is needed to make the function work. * Do not change any existing code; you should only add new code. * The total amount of new code you need to write should end up being about 10 lines. * The correct output from the program is:
A: sum = 12 A: diff = -2 B: sum = 21 B: diff = -3 C: sum = 30 C: diff = -4
*/ public class SumDiff { // The Pair class is just used to bundle two numbers. // (When a class is only used inside another class, the // definitions can be nested like this.)
private static class Pair { public int m_first; public int m_second; }
public Pair m_inputs; public Pair m_results;
public static void main( String[] args ) { SumDiff sd = new SumDiff(); sd.m_inputs = new Pair(); sd.m_results = new Pair();
private void calculateA() { sd.m_inputs = sd.m_inputs.m_first + sd.m_inputs.m_second; // transmit data in implicitly using instance variable m_inputs // transmit data out implicitly using instance variable m_results
// Put your code here.
}
private Pair calculateB( Pair inputs ) { // transmit data in explicitly using call-by-reference // transmit data out explicitly using a return value
// Put your code here.
}
private void calculateC( int a, int b, Pair results ) { // transmit data in explicitly using call-by-value // transmit data out explicitly using call-by-reference