Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

Looking for help on two dimensional arrays

Thread view: 
Orudis - 16 Apr 2005 21:17 GMT
I need hlep with a program that calculates prices for a movie theater based
on the seating arrangement at a single movie show. I was able to set the
two dimensional array but I am having trouble adding up the movie tickets
seatings. There are five different types of tickets. Seat A = 5.50, Seat B
= 4.00, Seat C = 6.50, Seat D = 8.50, and Seat E = 5.00. Can anyone help???
The code I have so far is:

public class TheaterSeating {
public static void main(String[] args) {

// Seating Arrangement Array
char[][] seatingArrangement = {
{'A', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'D', 'B', 'D', 'C', 'C', 'D', 'A', 'E', 'A', 'D'},
{'E', 'D', 'D', 'A', 'C', 'B', 'E', 'E', 'A', 'D'},
{'C', 'B', 'A', 'E', 'D', 'C', 'E', 'E', 'A', 'D'},
{'A', 'B', 'D', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'B', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'B', 'B', 'A', 'C', 'C', 'D', 'E', 'E', 'A', 'D'},
{'E', 'B', 'E', 'C', 'C', 'D', 'E', 'E', 'A', 'D'}};

System.out.print("Theather Seating are as follows: " + "\n");
for (int i=0; i<seatingArrangement.length; i++) {
if(i>=0) System.out.print(seatingArrangement[i]);

if (i>=0) System.out.println(" is the seating arrangement for seat " +
i);

}

}
}
Boudewijn Dijkstra - 17 Apr 2005 11:54 GMT
>I need hlep with a program that calculates prices for a movie theater based
> on the seating arrangement at a single movie show. I was able to set the
> two dimensional array but I am having trouble adding up the movie tickets
> seatings.

What is the problem?  What did you come up with?

> There are five different types of tickets. Seat A = 5.50, Seat B
> = 4.00, Seat C = 6.50, Seat D = 8.50, and Seat E = 5.00. Can anyone help???
[quoted text clipped - 21 lines]
> i);
> }}}

Why are you testing on i?
Bjorn Abelli - 17 Apr 2005 15:04 GMT
"Orudis" wrote...

>I need hlep with a program that calculates prices for a movie
> theater based on the seating arrangement at a single movie
[quoted text clipped - 28 lines]
> }
> }

You have a two dimensional array of chars, but after that you "address" it
as if it's only one dimension.

If you only work with "procedural" coding, you could simply use something
like:

public static float getPrice(char[][] seats, int row, int col)
{
  char seat = seats[row][col];

  float price = 0f;

  switch (seat)
  {
     case 'A': price = 5.50f; break;
     case 'B': price = 4.00f; break;
     case 'C': price = 6.50f; break;
     case 'D': price = 8.50f; break;
     case 'E': price = 5.00f; break;
  }
  return price;
}

This could of course be structured in other ways as well, with the
"seatingArrangement" as a static variable instead of a local, etc...

However, if you want to make it a bit more OO, you should think in terms of
"what can we know about a Seat?", e.g.

class SeatType
{
 private char  type;
 private float price;

 public SeatType(char c, float f)
 {
   type = c;
   price = f;
 }

 public char getType() { return type; }
 public float getPrice() { return price; }
}

...........

So, when you create you array, you could make it this way instead:

...

static SeatType a = new SeatType('A', 5.50f);
static SeatType b = new SeatType('B', 4.00f);
static SeatType c = new SeatType('C', 6.50f);
static SeatType d = new SeatType('D', 8.50f);
static SeatType e = new SeatType('E', 5.00f);

static SeatType [][] seatingArrangement;

...

seatingArrangement = {
{a, b, a, c, c, d, e, e, a, d},
{d, b, d, c, c, d, a, e, a, d},
{e, d, d, a, c, b, e, e, a, d},
{c, b, a, e, d, c, e, e, a, d},
{a, b, d, c, c, d, e, e, a, d},
{b, b, e, c, c, d, e, e, a, d},
{b, b, a, c, c, d, e, e, a, d},
{e, b, e, c, c, d, e, e, a, d}};

...

...and get type and price directly from the row and col of the seat, without
the need of a "switch"...

public static float getPrice(int row, int col)
{
  SeatType seat = seatingArrangement[row][col];
  return seat.getPrice();
}

You could also think further according to what you need to know about each
seat...

class Seat
{
  SeatType seatType;
  int seatNumber;

  public Seat(SeatType st, int number)
  {
     seatType = st;
     seatNumber = number;
  }

 public SeatType getType() { return seatType; }
 public int      getNumber() { return seatNumber; }
}

....

...etc...

We could go much further on this, but I suggest you start with addressing
the two dimensional array, as a two dimensional array, not a one dimensional
array...

// Bjorn A
Thomas Schodt - 17 Apr 2005 15:57 GMT
> ... I was able to set the
> two dimensional array but I am having trouble adding up the movie tickets
> seatings.

> public class TheaterSeating {
>   public static void main(String[] args) {
>     // Seating Arrangement Array
>     char[][] seatingArrangement = {

Yes, that works.
This may help you understand.

      char[][] seatingArrangement = {
    "ABACCDEEAD".toCharArray(),
    "DBDCCDAEAD".toCharArray(),
    "EDDACBEEAD".toCharArray(),
    "CBAEDCEEAD".toCharArray(),
    "ABDCCDEEAD".toCharArray(),
    "BBECCDEEAD".toCharArray(),
    "BBACCDEEAD".toCharArray(),
    "EBECCDEEAD".toCharArray()};

>     System.out.print("Theatre Seating are as follows: " + "\n");
>     for (int i=0; i<seatingArrangement.length; i++) {

        char[] row = seatingArrangement[i];
        System.out.print(new String(row));

>       //System.out.print(seatingArrangement[i]);
>       System.out.println(" is the seating arrangement for seat^H^H^H^Hrow " + i);
>     }
>   }
> }
Orudis - 18 Apr 2005 06:12 GMT
Thanks for all the help. I will try it out. Thanks again.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.