-
first java online class, need help
public class Numbers
{
public static void main(String[] args)
{
int valueOne = 50, valueTwo = 25;
int sum;
int difference;
sum = calculateSum(valueOne - valueTwo);
difference = calculateDifference(valueOne - valueTwo);
}
public static int calculateSum(int sum)
{
sum = valueOne + valueTwo;
sum = calculateSum(valueOne - valueTwo);
System.out.println("Sum of values" + sum);
}
public static int calculateDifference(int difference)
{
difference = valueOne - valueTwo;
System.out.println("Difference of values" + difference);
}
}
/*trying to write simple program to create class named Numbers whose main()
method holds two interger variables. Assigning values to variables. Create
two additional methods, sum() and difference(), that compute the sum of and
difference between the values of the two variables, respectively. Each method
should perform the computation and display the results. In turn, call each
of the two methods from main(),passing the values of the two interger variables.
I know this is simple; but it is not working for me and not sure where I
am going wrong, please help*/
-
Re: first java online class, need help
mp:
Alrighty....you are close, but are missing a few things. Let's break this
problem down into simple parts. I am actually going to start at the end and
work backward. We need two methods, one that computes the difference, and
the other that computes a sum. Each method should take the two values as
parameters, and display results. I'll show you the method for difference,
you can do the other method for sum:
public static void calculateDifference(int val1, int val2) {
int diff = val1 - val2;
System.out.println("The difference is: " + diff);
}
Next, you need a main method, which assigns values and calls the difference
and sum methods. Your main method is nearly right, you just need to rework
the lines that call these methods. I'll show you the call to the difference
method, you can do the other call for sum:
calculateDifference(valueOne, valueTwo);
There is no need to try to calculate difference or sum in the main
method....your difference and sum methods already do that for you....you
just need to make sure you pass both values to those methods as parameters.
Finally, you ought to change your email address to use a domain different
than 127.0.0.1 --- that is the local loopback address, and I can't mail you
this answer, because my mailserver will simply return the message to the
localhost.
Hope that helps,
BradO
--
----------
Brad O'Hearne
brado@neurofire.com
DevX Java Section Leader
"mp" <java.@127.0.0.1> wrote in message news:3d87bfff$1@10.1.10.29...
>
> public class Numbers
> {
> public static void main(String[] args)
> {
> int valueOne = 50, valueTwo = 25;
> int sum;
> int difference;
> sum = calculateSum(valueOne - valueTwo);
> difference = calculateDifference(valueOne - valueTwo);
> }
> public static int calculateSum(int sum)
> {
> sum = valueOne + valueTwo;
> sum = calculateSum(valueOne - valueTwo);
> System.out.println("Sum of values" + sum);
> }
> public static int calculateDifference(int difference)
> {
> difference = valueOne - valueTwo;
> System.out.println("Difference of values" + difference);
> }
> }
>
>
> /*trying to write simple program to create class named Numbers whose
main()
> method holds two interger variables. Assigning values to variables.
Create
> two additional methods, sum() and difference(), that compute the sum of
and
> difference between the values of the two variables, respectively. Each
method
> should perform the computation and display the results. In turn, call
each
> of the two methods from main(),passing the values of the two interger
variables.
> I know this is simple; but it is not working for me and not sure where I
> am going wrong, please help*/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks