6 Replies - 13776 Views - Last Post: 19 March 2009 - 05:07 PM Rate Topic: -----

#1 XxYippyxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 09

Sum of Squares

Post icon  Posted 17 March 2009 - 08:24 PM

Hello! i don't know why, but I am having one heck of a time figuring this particular assignment out! I have spent a ridiculous amount of time on it w/o any gains! First I will post the assignment then the code I have written henceforth. I am not quite getting how to go from one method to another exactly or compute the sum of squares in the second method. It makes no sense to me on why I should in the first place. Why not just sum the squares and not pass it on? Yes, I made the mistake of taking this class online so my interactions with a professor are limited.

Lab #4 - Writing and Executing Methods Contained in the Same Program

Write an application that contains two static methods: main and sumSquares. The sumSquares method takes five integer arguments and returns one integer value. The method first squares each of the five values passed to it. It then adds together each of the squared results. The sum of the squared values if then returned.

Objectives:

Upon completion of this lab the student will be able to:

Describe and understand the structure and format of methods used in the Java programming language.
Write a method in a Java application program.
Invoke or call (cause the method to execute) a method.
Understand what arguments (synonymous with parameters) are as identified in a method header.
Pass values to a method when calling the method.
Return a value from a method.

Step by step instructions:

1. Write a Java application called ExMethods. In this program called ExMethods, write a method called main which enters five values for five integer variables. You can use a Scanner object to input each of the five values (one value for each of the five variables). Use a println statement to prompt the user to enter each of the five values.
2. After entering values for these five variables (still in method main) call a method named sumSquares, passing it the five integer values which were entered.
3. Write the method sumSquares. This method should have five integer values for arguments. This method should find the sum of the squares of the five integer values and return this sum. For example, if the five values passed to the method are 4, 5, 2, 1 and 0, then the sumSquares method would first calculate the sum of the squares equal to 4 * 4 + 5 * 5 + 2 * 2 + 1 * 1 + 0 * 0. The method sumSquares would then return the value of this calculated sum.
4. In method main the value returned by the sumSquares method should be displayed using the println statement.



import java.util.Scanner;



public class ExMethods
{
	public static void main(String []args)

	{
		Scanner keyboard = new Scanner(System.in);

		int i = 0;
		int num1 = 0;
		int num2 = 0;
		int num3 = 0;
		int num4 = 0;
		int num5 = 0;
		int sum;

	  System.out.println("Enter a number:");
	  num1 = keyboard.nextInt();



	  System.out.println("Please enter a second number:");
	 num2 = keyboard.nextInt();

	  System.out.println("Please enter a third number:");
	 num3 = keyboard.nextInt();

	  System.out.println("Please enter a forth number:");
	 num4 = keyboard.nextInt();

	  System.out.println("Please enter a fifth number:");

	 num5 = keyboard.nextInt();

	 sum = sumOfSquares(num1,num2,num3,num4,num5);
	 System.out.println("The sum of squares equals:" + sum);



 }
	 public static int sumOfSquares( int num ) {
	 return (int) Math.pow(num * (num + 1) / 2, 2);
	 }


	}
System.out.println("The sum of the squares is: " + sum);
	 }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Sum of Squares

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Sum of Squares

Posted 17 March 2009 - 08:28 PM

You need to complete the sumOfSquares method. It needs to take 5 parameters and return the...well...the sum of the squares of all of those numbers. :)

Edit: also, you have a design error here.

public class ExMethods
{
    public static void main(String[] args)
    {
        // ALL program code to be executed at runtime goes here
    }

    public static int sumOfSquares(int num1, int num2, int num3, int num4, int num5)
    {
        // code for this method
    }
}


You have an output statement after your method declarations, which must be in the main method, not down there.

This post has been edited by Locke: 17 March 2009 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Sum of Squares

Posted 17 March 2009 - 08:32 PM

Your problem statement gives you the answer:

Quote

the sumSquares method would first calculate the sum of the squares equal to 4 * 4 + 5 * 5 + 2 * 2 + 1 * 1 + 0 * 0. The method sumSquares would then return the value of this calculated sum.

Your routine only uses one argument: public static int sumOfSquares( int num ) { although you are calling it with 5: sum = sumOfSquares(num1,num2,num3,num4,num5);
Was This Post Helpful? 0
  • +
  • -

#4 XxYippyxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 09

Re: Sum of Squares

Posted 19 March 2009 - 04:54 PM

View Postn8wxs, on 17 Mar, 2009 - 07:32 PM, said:

Your problem statement gives you the answer:

Quote

the sumSquares method would first calculate the sum of the squares equal to 4 * 4 + 5 * 5 + 2 * 2 + 1 * 1 + 0 * 0. The method sumSquares would then return the value of this calculated sum.

Your routine only uses one argument: public static int sumOfSquares( int num ) { although you are calling it with 5: sum = sumOfSquares(num1,num2,num3,num4,num5);


I'm not sure I understand. Are you telling me I only need the one argument and not bother with num1,num2, etc.....?
Was This Post Helpful? 0
  • +
  • -

#5 XxYippyxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 09

Re: Sum of Squares

Posted 19 March 2009 - 05:00 PM

View PostLocke, on 17 Mar, 2009 - 07:28 PM, said:

You need to complete the sumOfSquares method. It needs to take 5 parameters and return the...well...the sum of the squares of all of those numbers. :)

Edit: also, you have a design error here.

public class ExMethods
{
    public static void main(String[] args)
    {
        // ALL program code to be executed at runtime goes here
    }

    public static int sumOfSquares(int num1, int num2, int num3, int num4, int num5)
    {
        // code for this method
    }
}


You have an output statement after your method declarations, which must be in the main method, not down there.


So by doing it that way, it passes back to the main method? Why am I so lost on this!!!!!!!! poop! I have had no problems writing code before! I am not getting the passing code to another method concept at all!
Was This Post Helpful? 0
  • +
  • -

#6 Colin Hoernig  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 05-November 08

Re: Sum of Squares

Posted 19 March 2009 - 05:06 PM

View PostXxYippyxX, on 19 Mar, 2009 - 04:00 PM, said:

So by doing it that way, it passes back to the main method? Why am I so lost on this!!!!!!!! poop! I have had no problems writing code before! I am not getting the passing code to another method concept at all!


You need to use the return statement and call the sumOfSquares method from main (or another method).

public class ExMethods
{
    public static void main(String[] args)
    {
        // ALL program code to be executed at runtime goes here
    }

    public static int sumOfSquares(int num1, int num2, int num3, int num4, int num5)
    {
        // code for this method
        // use return statement to return final sum to main method (or another method
        // ex: return finalSum;
    }
}



Edit: Sorry, I'm not sure why my syntax highlighting isn't working, I probably messed it up, I copy and pasted from Locke and modified code.

This post has been edited by Colin Hoernig: 19 March 2009 - 05:10 PM

Was This Post Helpful? 1

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: Sum of Squares

Posted 19 March 2009 - 05:07 PM

When you call the method...it goes into that method and executes whatever code is in there before anything else is done in the main method. (or whatever mthod called it)(assuming there are no separate threads running)

You need to pass 5 numbers the the sumOfSquares() method. You then square each of those, add them...and use a return statement to return that sum value back to the main method.

Now, a common mistake is to forget to assign the value to a variable -- int sum = sumOfSquares(...);

You don't have to do that, you can just print out the method itself, but you can do it whichever way.

This post has been edited by Locke: 19 March 2009 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1