Java help needed

I am at the tail end of a 13-week college class for Java. It has gotten a lot more difficult the past couple of weeks. It was a miracle that I was able to do last week’s assignment. This week it is looking even worse. I had to email the professor for help. He gave me a paragraph explaining the concept of the assignment. I get all that. I wrote back to say that it’s the actual code I need help with. If anybody wants to help me out, that would be great.

"Using only the file Main.java, write a method called multiplier that includes a double array parameter and an int multiplier parameter (you can call this parameter multiply, or something other than multiplier).

Your method multiplier must multiply every element (or item) in the array by the integer parameter. Once each element has been changed, your method should return the new array that stores the new values. You must use a for loop to complete this task.

Some test cases are already included in the main method, but you are welcome to create your own."

Our code is to go at the end, beneath the comment, “//YOUR CODE HERE”.

class Main
{
	
	public static void main(String[] args)
	{
		//feel free to test code here
    //starter arrays
    double arr1 [] = {0.5, 1.5, 2.5, 3.5, 4.5};
    double arr2 [] = {12.3, 4.7, 8.1, 9.1, 6.8};
    double arr3 [] = {-5.2, -0.9, 6.8, 10.0, 20.0};

    //arrays to hold changed values
    double arr1Changed [] = new double [arr1.length];
    double arr2Changed [] = new double [arr2.length];
    double arr3Changed [] = new double [arr3.length];

    //assign changed value arrays to changed values
    arr1Changed = multiplier(arr1, 2);
    arr2Changed = multiplier(arr2, 5);
    arr3Changed = multiplier(arr3, 3);

    //display the new values
    printArray(arr1Changed);
    printArray(arr2Changed);
    printArray(arr3Changed);
    
	}//end main method

  //method to make it easier to display array values
  public static void printArray(double [] arr){
    for(int i=0; i<arr.length; i++){
      System.out.print( arr[i] + " ");
    }

    System.out.println();
    System.out.println();
  }//end printArray method

  

  //YOUR CODE HERE
  // write the method multiplier as described in the README.md file

 public static double[] multiplier(double[] arr, int multiply) {
    for(double i=0; i<arr.length; i++){
      arr[i] = i*10;
      
    }
    
  }


}

The code I added is giving an error: “Type mismatch: cannot convert from double to int”. I don’t know why I’m getting that or what to do about it either. I have no idea what I’m doing.

I’m pretty sure array indexes in java are integers, not floating point numbers. So you don’t want to use data type double for the counter variable i in the for loop.

In the multiplier method you are supposed to create a new array to store the new values.

2 Likes

This is what I have for my code section now. The teacher didn’t help much. I am still lost:

  public static double[] multiplier(double[] arr, int multiply) {
    double emptyList [] = new double [arr.length];
    
    for(int i=0; i<arr.length; i++){
      arr[i] = i*10;
     
    }
    
    return emptyList;
    
  }

You are creating a new array now but you aren’t using it. You are still changing the values in the arr passed into the method instead of adding them to the new array. Also, you aren’t making use of the multiplier passed into the function, you are just hardcoding the value 10 in there.

So you need to work on the inside of your for loop to fill up the new array properly.

2 Likes

This line is modifying arr. I’m not sure that is what your function should be doing.

You seem to really need to develop debugging skills. What have you tried to try to understand what is wrong with your code?

It’s working now. My teacher did almost all of it for me. I just finished the calculation in the for loop.

  public static double[] multiplier(double[] arr, int multiply) {
    double emptyList [] = new double [arr.length];
    
    for(int i=0; i<arr.length; i++){
      //arr[i] = i*10;
      emptyList[i] = arr[i] * multiply;
    }
    
    return emptyList;
    
  }

This makes sense to me now. Thank you.

I don’t know if it’s debugging skills I need or just more coding skills because I’m still learning basic things. I don’t think I understand enough to have been able to debug this.

I need to get away from it for a while and come back to it when my eyes and brain are fresh. Now that the solution is there, I think I need to spend time reading it and pick it apart to understand what it’s doing and how it’s doing it. I just feel like there’s so many different array names all over the screen and it’s hard to keep track of what’s doing what and where. This is very humbling (and slightly demoralizing at times).

Totally, learning to code is totally humbling at times.

I find it very helpful to print out values of different variables as my code runs to help see what is going on. This helps me debug issues because I can check if the printed values match what I expect/planned.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.