0

Hey,
I'm pretty new to Java and I was hoping someone could help me out. I wanted to know how to store the information that a loop shows. I've been asked to create a project which adds up the squares of the numbers from a to c, and this is my code so far.
Class 1:

public class sumSquares {
		
		public sumSquares(int a, int c)
		{	while (a<=c)
		{	u=(a*a);
			a=a+1;
			System.out.println(u);
		}
		
		}
				
	
	private int u;
	}

Class 2:

public class sumSquaresTester {
	public static void main(String[] args){
	sumSquares sum=new sumSquares(1,5);
	System.out.println(sum);
	}
}

I am able to get the squares that i need from the variable u, but how do i store the values i get after each cycle of the loop? Thanks in advance for the help.

4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by saurabh92
-1

Just let the attribute "u" be an int array of size (c-a),
And in your constructor instead of after displaying the values just store the squares as
below:-

public class sumSquares {
  public sumSquares(int a, int c){
    // Since we need the squares of c and a also
    u=new int[c-a+1]
    while (a<=c) {
      u[u.length-(c-a+1)]=(a*a);
      a=a+1;
      System.out.println(u[u.length-(c-a+1)]);
    }
  }
  private int[] u;
}
Votes + Comments
At least take the time to ensure your code works when you post an example.
0

I just wanted to say that I think you shouldn't do your calculations in your constructor. you could give the values in constructor maybe. but if I were you I would write a separate method to do the calculation. just to use the logic of object oriented programming. second think is you are printing your values from your sumSquares class. I would return the values to the main method and print it there. when you write a class you should write it to have a more general purpose. for example. your sumSquares class should calculate the values and return it back so your main method could print them or write them to a file or do whatever it supposed to do with them. of course this is a very small program to discuss object oriented concepts but it is a good idea to use that logic in all of your programs. here is a example method signiture for you public int[] calculate(int a,int b) another class could be:

public class SumSquares {
    public SumSquares(int a , int c)
{
/*gets the values to calculate.
it probably will do somethig like 
this.a=a;
this.b=b
*/
}

   public int[] calculate(){
/*this method should do the calculations and return the calculated values back to the main method.*/
}
}
0

Just let the attribute "u" be an int array of size (c-a),
And in your constructor instead of after displaying the values just store the squares as
below:-

public class sumSquares {
  public sumSquares(int a, int c){
    // Since we need the squares of c and a also
    u=new int[c-a+1]
    while (a<=c) {
      u[u.length-(c-a+1)]=(a*a);
      a=a+1;
      System.out.println(u[u.length-(c-a+1)]);
    }
  }
  private int[] u;
}

Thanks for the reply, but i'm still getting an error. It tells me to insert ArrayInitializer for some reason. Why is this?

1

Who knows? Never heard of "ArrayInitializer". Though that code does not compile due to a missing semi-colon, and it does not produce a correct result if corrected to compile.

Votes + Comments
stephen84s knows
0

Hey,
I'm pretty new to Java and I was hoping someone could help me out. I wanted to know how to store the information that a loop shows. I've been asked to create a project which adds up the squares of the numbers from a to c, and this is my code so far.
Class 1:

public class sumSquares {
		
		public sumSquares(int a, int c)
		{	while (a<=c)
		{	u=(a*a);
			a=a+1;
			System.out.println(u);
		}
		
		}
				
	
	private int u;
	}

Class 2:

public class sumSquaresTester {
	public static void main(String[] args){
	sumSquares sum=new sumSquares(1,5);
	System.out.println(sum);
	}
}

I am able to get the squares that i need from the variable u, but how do i store the values i get after each cycle of the loop? Thanks in advance for the help.

Hey guys,
thanks for the help. i was able to get rid of all the errors by putting the whole thing into one class and making 2 different methods.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.