简体   繁体   中英

Java: filling a 2D array to find Power of a circuit

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] = ...;  
            }
        }
    }
}

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

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.

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]); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM