4 Replies - 729 Views - Last Post: 14 February 2013 - 07:11 AM Rate Topic: -----

#1 JoshAlex123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

method function squares and sum of squares

Posted 13 February 2013 - 03:20 PM

HI i have been given a task to write a program as follows:





Incorporate the function methods square and sumOfSquares into a Java program (need to call sumOfSquares
from main method).
(a) Amend so that the sum of squares from 1 to 10 (i.e. using the value n = 10) is output to screen.
(B) Amend your program so that the user is asked to input the value of n before sum of squares is output to
screen.
© Further amend your program so that the user is repeatedly asked to input a value for n (and the sum of
squares output to screen) until -1 is input, which terminates the program.

i have had several attempts at this and cant find a solution...

this is the code i have so far:

public class sumofsquares
{


	public static void main (String [] args)
	{
		int number, sum, square;
		Scanner Key = new Scanner(System.in);
		System.out.println("Please enter a number");
		number = Key.nextInt();

		
	}


	public static int square(int n)
	{
		return n*n;
	}

	public static int sumofsquares(int n) 
	{
		int sum = 0;
		int i;
		for (i = 1; i<= n; i++)	
		{
			sum = i + square(i);
		}
			return sum;
			System.out.println(+sum);

	}
}


i am very new to java and could do with as much help as possible.

i have been trying for hours and nothing seems to work :(

Is This A Good Question/Topic? 0
  • +

Replies To: method function squares and sum of squares

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,905
  • Joined: 06-March 08

Re: method function squares and sum of squares

Posted 13 February 2013 - 03:31 PM

		System.out.println("Please enter a number");
		number = Key.nextInt();
                // I guess you need something like this
                System.out.println("Square is: " + square(number));
                sumofsquares(number);
                // to call your methods



After a return nothing happens
you will have to put your println() before the return

		
			return sum;
			System.out.println(+sum);

	}
}



Also respect Java cionventions. The method name should be sumOfSquares()
Was This Post Helpful? 1
  • +
  • -

#3 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: method function squares and sum of squares

Posted 13 February 2013 - 04:13 PM

Hello Friend;
I have taken a gander at your code, and, have tweaked it a tad. I tried my best to make it as clean as possible however; I am still learning. It similar to your original, however, makes use of exception handling and more user input based decisions, making the code more diverse. Feel free to edit code as you please or ask questions.
package javaquestion1;
import java.util.InputMismatchException;
import java.util.Scanner;
class JavaQuestion1 {
    private static Scanner console = new Scanner(System.in);
    private static int baseValue, exponentValue, amountOfValuesToSum, sumValue, tempVariable, iterationVariable;
    private static boolean acceptableUserInput = false;
    public static void main(String[] args) {
        while(true){
            do{
                try{
                    //Get initial value
                    System.out.println("Please enter value to be modified: ");
                    baseValue = getUserInput();
                
                    //ask user to what exponent they want to have input go to
                    System.out.println("Please enter exponent: ");
                    exponentValue = getUserInput();
                
                    //display value after modifying user input
                    System.out.println("Value after applying conditions: " + modifyUserInput(baseValue, exponentValue));
                
                    //ask user to input number of values to print sum of
                    System.out.println("Please enter desired amount of values to find sum of: ");
                    amountOfValuesToSum = getUserInput();
                
                    //display value after summing desired amount of values
                    System.out.println("Value after sum: " + getSumOfValues(amountOfValuesToSum, exponentValue));
                
                    acceptableUserInput = true;
                }
                catch(InputMismatchException e){
                    System.err.println(e.toString());
                    console.next();
                    continue;
                }
                catch(Exception e){
                    System.err.println(e.toString());
                    console.next();
                    continue;
                }
            }while(!(acceptableUserInput));
        }
    }
    
    static int getUserInput(){
        tempVariable = 1;
        tempVariable = console.nextInt();
        return tempVariable;
    }
    
    static int modifyUserInput(int x, int y)
    {
        return (int)Math.pow(x, y);
    }
    
    static int getSumOfValues(int x, int y) 
    {
        sumValue = 0;
        for (iterationVariable = 0; iterationVariable < x; iterationVariable++)	
        {
            sumValue = x + modifyUserInput(iterationVariable, y);
        }
        return sumValue;
    }
}



EDIT: If you do utilize global variables as I have, please be careful this may cause bad things to happen

This post has been edited by k3y: 13 February 2013 - 04:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 JoshAlex123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-February 13

Re: method function squares and sum of squares

Posted 14 February 2013 - 05:12 AM

Cheers for the help but i have not been taught exceptions yet so that program is still to complex..

If you could help me out with this:
(a) Amend so that the sum of squares from 1 to 10 (i.e. using the value n = 10) is output to screen.
(B) Amend your program so that the user is asked to input the value of n before sum of squares is output to
screen.
© Further amend your program so that the user is repeatedly asked to input a value for n (and the sum of
squares output to screen) until -1 is input, which terminates the program.

This is the code i have so far and i need to put a,b,c into my program and i am really finding it difficult. please be aware i am very NEW to java....


thanks guys :D
Was This Post Helpful? 0
  • +
  • -

#5 k3y  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 205
  • Joined: 25-February 12

Re: method function squares and sum of squares

Posted 14 February 2013 - 07:11 AM

I apologize for creating code that you found out of your range, however; you could modify it and try to figure it out for your self. If your a tactile learner like me then that will greatly help you. I simply made it more diverse in operation. Try to modify it as you go, and post back on here with any questions. :gunsmilie:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1