User Tools

Site Tools


twod

Complete the code so that the main method produces the following output:

TwoD.java
/**
 * TwoD is a warm-up for dealling with initializing a 2 dimensional array
 * 
 * @author Your Name
 * @version Today's Date
 * 
 */
public class TwoD
{
    private int[][] x;
 
    /**
     * Constructor for objects of class TwoD
     */
    public TwoD(int n, int m)
    {
        x = new int[n][m];
    }
 
    /**
     * Use the this key word to complete this constructor
     * so that by using the other constructor
     */
    public TwoD(int n)
    {
        //Your code Here
    }
 
    /**
     * Row Major counts from the first row the across the columns
     */
    public void rowMajor()
    {
        // Your Code Here
    }
 
    /**
     * Column Major counts from the first column then down the rows:
     */
    public void columnMajor()
    {
        // Your code Here
    }
 
    public static void main(String[] args)
    {
        TwoD r = new TwoD(4,9);
        TwoD c = new TwoD(8,3);
        r.rowMajor();
        c.columnMajor();
        System.out.println("Row Major counts from the first row then across the columns:\n"+r);
        System.out.println("Column Major counts from the first column then down the rows:\n"+c);
        r = new TwoD(7);
        c= new TwoD(7);
        r.rowMajor();
        c.columnMajor();
        System.out.println("Row Major counts from the first row then across the columns:\n"+r);
        System.out.println("Column Major counts from the first column then down the rows:\n"+c);
 
    }
 
    public String toString()
    {
        String s="";
        for (int[] row:x)
        {
            for(int cell:row)
            {
                if (cell < 10)
                    s += " ";
                s += cell + " ";
            }
            s += "\n";
 
        }
        return s;
    }
}
twod.txt · Last modified: 2024/11/18 15:09 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki