-4

I had to make an array that counted the seating in a movie theater, the second class m2 represents another showtime. I do not know how to add both arrays together and print the result of the sum of these two arrays.

This the MovieSeats tester file:

public class MovieSeatsTester
{

    public static void main(String[] args)
    {
        MovieSeats m = new MovieSeats(3,3);
        MovieSeats m2 = new MovieSeats(3,3);
        m.seating(0,0);
        m.seating(0,1);
        m.seating(1,0);
        m.seating(2,2);
        m.seating(2,2);
        m2.seating(0,0);
        m2.seating(0,0);
        m.print();
        m.reset();
        m2.print();
        m2.reset();
    }
}

This is the MovieSeats file:

public class MovieSeats
{
    private int attendance[][];

    public MovieSeats()
    {
    }

    public MovieSeats(int rows, int columns)
    {
        attendance = new int[rows][columns];        
    }

    public void seating(int r, int c)
    {   
        attendance[r][c] += 1;
    }

    public void print()
    {
    for (int r = 0; r < attendance.length; r++)
    {
        for (int c = 0; c < attendance.length; c++)
        {
            System.out.println("At row " + r + " col " + c + ", There are " +   attendance[r][c] + " Sitting here.");
        } 

    }
        System.out.println();
    }

    public void reset()
    {
    for (int r = 0; r < attendance.length; r++)
    {
        for (int c = 0; c < attendance.length; c++)
        {
            attendance[r][c] = 0;
        } 
    }
    System.out.println();
    }
}    
Nurjan
  • 5,889
  • 5
  • 34
  • 54
Susamp
  • 1
  • 3
  • 1
    What do you mean by "add two different arrays"? You also might want to work on writing a [mcve]. A lot of the code here isn't relevant to the question. – 4castle Dec 29 '16 at 03:31
  • Use the reset method you wrote as a guide to generate your sum, e.g. sum = sum + attendance[r][c] – RamblinRose Dec 29 '16 at 03:32
  • 1
    Possible duplicate of [How can I concatenate two arrays in Java?](http://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) – dreamer Dec 29 '16 at 03:32

2 Answers2

0

I assume that by "add two different arrays" you mean "create a third array where each element in the third array is the sum of the corresponding element of each of the first two arrays."

1. Access Each Array

In order to add the two arrays together, you would first need access to each array somehow. Since the attendance field is private, you should add a getter method for it to the MovieSeats class.

Get attendance without encapsulation:

public int[][] getAttendance() {
    return attendance;
}

Get attendance with encapsulation:

public int[][] getAttendance() {
    int rows = attendance.length();
    int cols = attendance[0].length();
    int[][] copy = new int[rows][cols];

    for(int i = 0; i < rows; i++)
        for(int j = 0; j < cols; j++)
            copy[i][j]=current[i][j];

    return copy;
}

2. Add Two Arrays Element-Wise

Next, once you have access to both arrays in the same place (probably inside your main), you would then have to add them together. I would recommend putting the result in a third array. The most rudimentary way of adding these two arrays is the following:

int[][] attendance1 = m.getAttendance();
int[][] attendance2 = m2.getAttendance();
int[][] sum = new int[3][3];

for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        sum[i][j] = attendance1[i][j] + attendance2[i][j];

Then, you could print the total attendance by seat with the following:

for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        System.out.printf("Total Attendance for seat (%d,%d): %d\n",
                i, j, sum[i][j]);

Now, if instead what you want to do is to sum all of the elements of both arrays into a single value, then you would do the following.

Sum An Array

int sum = 0;

int[][] attendance1 = m.getAttendance();
for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        sum += attendance1[i][j];

int[][] attendance2 = m2.getAttendance();
for (int i = 0; i < 3; ++i)
    for (int j = 0; j < 3; ++j)
        sum += attendance2[i][j];
Travis
  • 2,135
  • 17
  • 29
  • How do I print out the total attendance for all the seats. For example at (0,0) in attendance 1 it is 3 then in attendance 2 it is 2 and I want to print 5 since that is the total for that seat. – Susamp Dec 30 '16 at 23:47
  • @Susamp Glad it works! Please make sure to upvote my answer, too – Travis Jan 01 '17 at 04:06
0

You can add the following method to the MovieSeat class:

public int sum() {
    int result = 0;
    for (int r = 0; r < attendance.length; r++) {
        for (int c = 0; c < attendance.length; c++) {
            result += attendance[r][c];
        }            
    }
    return result;
} 

And the call the sum method and find the total sum in the main method:

MovieSeats m = new MovieSeats(3, 3);
MovieSeats m2 = new MovieSeats(3, 3);
m.seating(0, 0);
m.seating(0, 1);
m.seating(1, 0);
m.seating(2, 2);
m.seating(2, 2);
m2.seating(0, 0);
m2.seating(0, 0);
m.print();

System.out.println("sum: " + m.sum());
System.out.println("---");

m2.print();
System.out.println("sum: " + m2.sum());

System.out.println("total sum: " + (m.sum() + m2.sum()));
m.reset();
m2.reset();

Note to reset after you call the sum method.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
  • in the line System.out.println("At row " + r + " col " + c + ", There are " + attendance[r][c] + " Sitting here.") in my MovieSeats class, it tells me how many people are sitting in a specific seat. In the total sum how would I print that line were it tells how many people have ever sat in that specific seat? – Susamp Dec 30 '16 at 20:47