The Artima Developer Community
Sponsored Link

Java Answers Forum
hey i know u guys dont like hwk help but i jst need some help getingstarted

1 reply on 1 page. Most recent reply: Nov 7, 2002 10:32 PM by twisty

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Cshah

Posts: 16
Nickname: tkc204
Registered: Oct, 2002

hey i know u guys dont like hwk help but i jst need some help getingstarted Posted: Nov 7, 2002 9:13 PM
Reply to this message Reply
Advertisement
// 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();

// first method

sd.m_inputs.m_first = 5;
sd.m_inputs.m_second = 7;
sd.calculateA();
System.out.println( "A: sum = " + sd.m_results.m_first );
System.out.println( "A: diff = " + sd.m_results.m_second );

// second method

Pair inputsB = new Pair();
inputsB.m_first = 9;
inputsB.m_second = 12;
Pair resultsB = sd.calculateB( inputsB );
System.out.println( "B: sum = " + resultsB.m_first );
System.out.println( "B: diff = " + resultsB.m_second );

// third method

Pair resultsC = new Pair();
sd.calculateC( 13, 17, resultsC );
System.out.println( "C: sum = " + resultsC.m_first );
System.out.println( "C: diff = " + resultsC.m_second );
}

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

// Put your code here.

}
}

thanks alot just give me hints please


twisty

Posts: 22
Nickname: twisty
Registered: Nov, 2002

Re: hey i know u guys dont like hwk help but i jst need some help getingstarted Posted: Nov 7, 2002 10:32 PM
Reply to this message Reply
I'll do the first two for you, as once you understand that, you will find the remaining method to be fairly easy to create.
private void calculateA()
{
	m_results.m_first = m_inputs.m_first + m_inputs.m_second;
	m_results.m_second = m_inputs.m_first - m_inputs.m_second;
}
 
private Pair calculateB( Pair inputs )
{
	Pair pairResult = new Pair();
	
	pairResult.m_first = inputs.m_first + inputs.m_second;
	pairResult.m_second = inputs.m_first - inputs.m_second;
	
	return pairResult;
}

If your having any problem with the remaining question, feel free to ask.

Flat View: This topic has 1 reply on 1 page
Topic: text files Previous Topic   Next Topic Topic: Apache-Tomcat mod_jk  servlet mapping issue

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use