-2

Hi I am trying to write a code for an assignment where you have a constant resistance and increasing current, then calculate power. I wanted to put all the data into an array just to make it neat and simple. But I am struggling to fill it, I have no idea on the syntax to use, but I have initialized the array, I think. Honestly anything would help, thanks!

package assignment_10_18_2018;

public class lab_10_18_2018_a {

    public lab_10_18_2018_a() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub 
        final int LENGTH_FOR_CURRENT = 11 ; 
        int resistance = 10 ; 
        int[][] circuitArray = new int [10][3]; 

        for(int i = 0; i < 10 ; i++) { 
            for(int r = 0; r < ...; r++) {
                circuitArray[i][r] = ...;  
            }
        }
    }
}
meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
JVinitsky
  • 1
  • 1
  • What do you want to use (which values) for filling the array? – lealceldeiro Oct 18 '18 at 14:11
  • @lealceldeiro I wanted resistance to be a constant, say 10 ohms and the current to increase from 0 to 10 A. I know I should use a For loop for the filling but idk how – JVinitsky Oct 18 '18 at 14:12
  • @lealceldeiro I also am unsure of how to place calculated power into the third column – JVinitsky Oct 18 '18 at 14:13
  • Do you have a *mathematical* solution which you are struggling to convert to code, or neither? SO can only help with the second, and then only if you have shown actual effort besides a few lines of auto-generated boilerplate. – meowgoesthedog Oct 18 '18 at 14:16
  • If you make your resistance value constant then this problem can be solved using 1d array – suvojit_007 Oct 18 '18 at 14:25
  • @JVinitsky, from what I recall, Electric power is (Current)^2*Resistance, right? So, do you want current in the first column, Resistance in the second and power in the third? – mettleap Oct 18 '18 at 14:35
  • @mettleap exactly – JVinitsky Oct 18 '18 at 14:35

3 Answers3

0

You will have to use simple if loops to put different things in different columns, like so:

//Pseudo code
public class lab_10_18_2018_a {

public lab_10_18_2018_a() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    // TODO Auto-generated method stub 
    final int LENGTH_FOR_CURRENT = 11 ; 
    int resistance = 10 ; 
    int[][] circuitArray = new int [10][3]; 

    for(int i = 0; i < 10 ; i++) { 
        for(int r = 0; r < 3; r++) {
            if(r==0){ //first column
                circuitArray[i][r] = i+1; //current will go from 1 to 10 in this case in the first column. Modify appropriately to suit your needs
            }  
            else if(r==1){ //second column
                circuitArray[i][r] = resistance;
            } 
            else if(r==2){//third column
                circuitArray[i][r] = circuitArray[i][r-2]*circuitArray[i][r-2]*circuitArray[i][r-1]; //Electric Power formula
            } 
        }
    }
}

}

Also, it would be better if you follow the Java naming conventions. You could read up more on https://www.oracle.com/technetwork/java/codeconventions-135099.html

mettleap
  • 1,390
  • 8
  • 17
0

Here we need only 1 row as the resistance is constant(say 10) but as per the question we need to change the current from 0 to 10. So we need an array having 1 row and 11 column.

int power[][] = new int[1][11]; 

            //current increases from 0-10 

            int resistance = 10,i,j;

            for(i=0;i<power.length;i++)
            {
                for(j=0;j<power[i].length;j++)
                {
                    power[i][j] = j*j*resistance;

                }

            }

Calculate the value of power for each current value and store them in the power array.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23
0
public static void main(String[] args) {
    int resistance = 10 ; 
    //you need 11 rows and 3 columns
    int[][] circuitArray = new int [11][3]; 
    //for each row i set first cell to i 
    //second cell to your constant
    //third cell with callculated value (first_cell * firs_cell * second_cell)=(I*I*R)
    for(int i = 0; i < 11 ; i++) {            
        circuitArray[i][0] = i; 
        circuitArray[i][1] = resistance;
        circuitArray[i][2] = circuitArray[i][0] * circuitArray[i][0] * circuitArray[i][1];
    }
    //print header
    System.out.printf("%-10s %-10s %-10s%n","I(amps)","R(ohms)","P(watts)");
    //print values
    for(int[] row : circuitArray){
       System.out.printf("%-10d %-10d %-10d%n",row[0],row[1],row[2]); 
    }
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28