1
\$\begingroup\$

I'm new to Java so I have given myself small projects to help myself learn the ropes of java. My project was to create a function that would summate an expression no matter how high the degree or value of n.

I ended up being able to complete it but I wanted someone who knew what they were doing to look over it and see if I was doing things the right way.

A goal was to use no pre-built packages but I ended up using the java.lang.Math.pow() function (I didn't import it).

Also, what recommendations do you have for learning for beginners? I do have some experience programming but I feel like I never learned the correct way...

Code:

package TestSpace;
public class MathClass {
 
/*
 Steps:
 1. Get array of any length and n
 2. Reverse list so highest degree coefficients are last
 3. Run for loop for each time n increases
 4.Run for loop inside of that for each entry in array
*/
 
 //args consists of the coefficients of x; must include all degrees of x counting down from the highest degree
 public static float sigma (int[] args, int n) {
 float value = 0;
 int length = args.length;
 //reverses list
 int[] reverselist = reverselist(args);
 
 for(int i = 1;i<n+1;i++) {
 
 for(int v = 0; v < length;v++) {
 // Multiplies coefficient by "x" raised to the power of its degree
 value += reverselist[v] * java.lang.Math.pow(i, v);
 }
 }
 return value;
 }
 
//Reverses the order of the given array so that highest degree is at the end
 public static int[] reverselist(int[] args) {
 int length = args.length;
 int[] newint = new int[length];
 for(int i = 0;i<length;i++) {
 newint[i] = args[length - (i+1)]; 
 }
 return newint;
 }
 
//Makes printing simple
 public static void print(Object obj) {
 try {
 System.out.println(obj);
 }catch(Exception e){
 System.out.println(e);
 } 
 }
 
 //main function
 public static void main(String[] args) {
 print(sigma(new int[] {5,2,7,4},4));
 }
}
Chocolate
1,0145 silver badges21 bronze badges
asked Jan 1, 2021 at 20:58
\$\endgroup\$
1
  • \$\begingroup\$ That print is not making printing simple. Which exeption you are triing to catch? \$\endgroup\$ Commented Feb 7, 2022 at 23:33

1 Answer 1

1
\$\begingroup\$
  1. Follow Java naming conventions. Your package name should be all lowercase - 'testspace'. 'reverselist' should be 'reverseList'.
  2. Don't use fully qualified 'java.lang.Math', just 'Math'. You don't need to import classes from the java.lang package.
  3. Indent your comments to match the code.
  4. Use descriptive variable names, e.g. 'v' could be 'exponent' or 'power'. Why use 'i' and then describe it as 'x' in the comment? Should 'reverselist' be 'reverseArray'?

In general your algorithms are reasonable.

answered Jan 1, 2021 at 22:21
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.