Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: A Java Clusterbomb

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A Java Clusterbomb

    I am having a problem with my final assignment. Now it began with an apology from my prof on the said assignment because for the pass 9 weeks we have done projects that has focused on a small part like change colors, stream, inheritance, etc. Now the final is to place it all together, but we haven't done any examples based on anything remotely close on this and he apologized yet again.
    So here is what he wanted:
    Create a Java GUI business application. This program may be any business application of your choice. A few examples include order entry, inventory management, payroll processing, or work order management. The application needs to read and write to a sequential access file. You will need to incorporate graphics, events, exception handling, and inheritance. I e-mail my prof and his comment was "yes the assignment is a bit vauge and it asked to combine everything you learned this semester. However, we never covered how to write a complete program and thus lies the great wall. Just do the best that you can." Honestly?! this is my final here. So then I went and try to create a program based on plane reservation like so:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Plane extends JApplet
               implements ActionListener {
       JTextField input;
       JLabel prompt1, prompt2;
       JButton yesButton, noButton;
       int section, seats[], smoking;
       int nonsmoking, people;
     
       public void init()
       {
          input = new JTextField( 4 );
          input.addActionListener( this );
          prompt1 = new JLabel( "Please type 1 for smoking" );
          prompt2 = new JLabel( "Please type 2 for nonsmoking" );
          yesButton = new JButton( "Yes" );
          noButton = new JButton( "No" );
          yesButton.addActionListener( this );
          noButton.addActionListener( this );
          yesButton.setEnabled( true );
          noButton.setEnabled( true );
     
          seats = new int[ 11 ];
          smoking = 6;      
          nonsmoking = 1;
     
          Container c = getContentPane();
          c.setLayout( new FlowLayout() );
          c.add( prompt1 );
          c.add( prompt2 );
          c.add( input );
          c.add( yesButton );
          c.add( noButton );
       }
     
       public void actionPerformed( ActionEvent e )
       {
          if ( e.getSource() == input && people <= 10 ) {
             section = Integer.parseInt( input.getText() );
             String s = "";
     
             if ( section == 1 ) {
     
                if ( smoking <= 10 && seats[ smoking ] == 0 ) {
                   s = "Smoking. Seat #" + smoking;
                   seats[ smoking++ ] = 1;
                   people++;
                }
                else if ( smoking > 10 && nonsmoking <= 5 ) {
     
                   // enable buttons
                   yesButton.setEnabled( true );
                   noButton.setEnabled( true );
     
                   s = "Smoking is full. Non-smoking?";
                }
                else
                   s = "Next flight leaves in 3 hours.";
             }
             else if ( section == 2 ) {
     
                if ( seats[ nonsmoking ] == 0 && nonsmoking <= 5 ) {
                   s = "Nonsmoking. Seat #" + nonsmoking;
                   seats[ nonsmoking++ ] = 1;
                   people++;
                }
                else if ( nonsmoking > 5 && smoking <= 10 ) {
     
                   // enable buttons
                   yesButton.setEnabled( true );
                   noButton.setEnabled( true );
     
                   s = "Nonsmoking is full. Smoking?";
                }
                else
                   s = "Next flight leaves in 3 hours.";
             }
             else
                s = "Invalid input.";
     
             showStatus( s );         
          }
          else if ( e.getSource() == yesButton ) {
     
             if ( section == 1 ) {
                showStatus( "Your seat assignment is " + nonsmoking );
                seats[ nonsmoking++ ] = 1;         
             }
             else {  // section is 2
                showStatus( "Your seat assignment is " + smoking );
                seats[ smoking++ ] = 1;     
             }
     
             ++people;
             noButton.setEnabled( true);
             yesButton.setEnabled( true );
          }
          else if ( e.getSource() == noButton ) {
             showStatus( "Next flight leaves in 3 hours." );
             noButton.setEnabled( true );
             yesButton.setEnabled( true );
          }
       }
    }

    Just as I thought I had it. It is just not working. I am in my wits ends and I need some guidance. It compiles but when it runs it don't run properly. What I have I done wrong? Please help.
    Last edited by JavaPF; June 24th, 2010 at 02:35 PM. Reason: new syntax highlighting test run :)


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: A Java Clusterbomb

    OK So am I to understand you don't want any help for the first two parts of code? Now if that is so I would recommend you remove them from the original post. Now you need to give us more to go on then "It is just not working". What is not working? Is it compiling but not working as you would expect? Or is it not allowing you to compile we need details to help you out.

    Now if you change this give something to work with I and I'm sure some others will be more than willing to help.

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A Java Clusterbomb

    Apology I was in a bit of a state of anger when I first wrote this. But I have done what you asked. My question is how can get this program working properly and any advise to incorporate the other items requested.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,218
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: A Java Clusterbomb

    how can get this program working properly
    What's improper about it now?

  5. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: A Java Clusterbomb

    OK so it compiles and runs. What exactly doesn't work? Does anything actually show on the screen?

  6. #6
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A Java Clusterbomb

    it show the textfield and the buttons, but after I enter my selection (1 or 2) and press yes, I get nothing. It is suppose to go to the next screen and it don't that is the problem.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,218
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: A Java Clusterbomb

    I enter my selection (1 or 2) and press yes, I get nothing
    Can you try debugging the code by adding println() statements to show where it is executing and what the values of the variables are?
    For example what are the values of these in the actionPerformed method:
    e.getSource(), input and people

  8. #8
    Junior Member
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: A Java Clusterbomb

    apologies, I was very burnt out right now. Here is my full code.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class FinalAssignment extends JFrame implements ActionListener
    {
      FlowLayout flow = new FlowLayout();
      private JLabel headTitle = new JLabel("Passenger List");
      private Font headFont = new Font ("Courier", Font.ITALIC, 18);
      private JLabel enterName = new JLabel ("Enter the passenger's Full Name");
      private JTextField inputName = new JTextField(10);
      private JLabel address = new JLabel("Address");
      private JTextField inputAddress = new JTextField(15);
      private JButton enterData = new JButton("Enter Data");
      int section, seats[], asile;
      int window, people;
      private JLabel win = new JLabel ("Press 1 for Window seat");
      private JLabel asi = new JLabel ("Press 2 for Asile");
      private JButton yButton = new JButton ("Yes");
      private JButton nButton = new JButton ("No");
      private JTextField input = new JTextField(5);
      private Container con = getContentPane();
      DataOutputStream outStream;
     
     
      public static void main(String[] args)
      {
        FinalAssignment fAssign = new FinalAssignmet();
        fAssign.setSize(600, 220);
        fAssign.setVisible(true);
      }
      String[] airlines = {"Jet Blue", "Delta", "American"};
      JComboBox airLines = new JComboBox(airlines);
      JLabel mainLabel = new JLabel("Please Select your Airline:");
      JLabel actionLabel = new JLabel("Selected Airline: None");
      FinalAssignment fA = new FinalAssignment();
     
      public FinalAssignment()
      {
        super("Victor E. Campudoni-Final-Assignment");
        try
        {
          outStream = new DataOutputStream(new FileOutputStream("PassList.dat"));
        }
        catch(IOException ex)
        {
          System.err.println("File not opened");
          System.exit(1);
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        headTitle.setFont(headFont);
        mainLabel.setFont(headFont);
        con.setLayout(flow);
        con.add(mainLabel);
        con.add(airLines);
        con.add(actionLabel);
        con.add(headTitle);
        con.add(enterName);
        con.add(inputName);
        con.add(address);
        con.add(inputAddress);
        con.add(enterData);
        con.add(win);
        con.add(asi);
        con.add(input);
        con.add(yButton);
        con.add(nButton);
        airLines.addActionListener(this);
        enterData.addActionListener(this);
        yButton.addActionListener(this);
        nButton.addActionListener(this);
        setVisible(true);
        yButton.setEnable(true);
        nButton.setEnable(true);
     
        seats = new int[11];
        asile = 6;
        window = 1;
      }
      public void actionPerformed (ActionEvent e1)
      {
        int i = airLines.getSelectedIndex();
        if(i < 0)
          return;
        actionLabel.setText("Your AirLine is: " + airLines[i]);
        {
        if (e.getSource() == input && people <= 10)
        {
          section = Integer.parseint(input.setText());
          String s = "";
          if (section == 1)
          {
            if (window <= 10 && seats [window] == 0)
            {
              s = "Window. Seat #" + window;
              seats [window++] = 1;
              people++;
            }
            else if (window > 10 && asile <= 5)
            {
              yButton.setEnabled(true);
              nButton.setEnabled(true);
              s = "Window is full. Asile seat?";
            }
            else
              s = "Your flight leaves in 3 hours.";
          }
          else if (section == 2)
          {
            if (seats [asile] == 0 && asile <= 5)
            {
              s = "Asile. Seat #" + asile;
              seats [asile++] = 1;
              people++;
            }
            else if (asile > 5 && window <= 10)
            {
              yButton.setEnabled(true);
              nButton.setEnabled(true);
              s = "Asile seating is full. Window Seat?";
            }
            else
              s = "Your flight leaves in 3 hours.";
          }
          else
            s = "Invalid Input.";
          showStatus (s);
        }
        else if (e.getSource() == yButton)
        {
          if (section == 1)
          {
            showStatus ("Your seat assignment is " + window);
            seats [window++] = 1;
          }
          ++people;
          nButton.setEnabled(true);
          yButton.setEnabled(true);
        }
        else if (e.getSource() == nButton)
        {
          showStatus ("Your flight leaves in 3 hours.");
          nButton.setEnabled(true);
          yButton.setEnabled(true);
        }
        try
        {
          outstream.writeUTF(inputName.getText());
          outStream.wruteUTF(inputAddress.getText());
     
          inputName.setText("");
          inputAddress.setText("");
        }
        catch(IOException e2)
        {
          System.err.println("Error writing file");
          System.exit(1);
        }
        }
      }
    }

    But I am getting 16 errors. I am just burnt out. (Working on two assignments and I am on my last nerve.)

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,218
    Thanks
    52
    Thanked 2,294 Times in 2,265 Posts

    Default Re: A Java Clusterbomb

    I am getting 16 errors
    Please copy and paste the full text of the error messages here.

  10. #10
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: A Java Clusterbomb

    After having a closer look at your code, I have been able to take out most of the mistakes. However, 3 remains, since you use an undeclared or initiated variable e

    else if (e.getSource() ==  yButton)

    It is used 3 times.

    Else it was a matter of typos so far. I can not test the code closer till i get more info on this e variable.

    What you as an editor? You should really look into getting a proper IDE, eclipse caught most of these errors right away.

    PS. Your code is very "ugly" in the sense that it is poorly formatted, there is not a single comment (comments are really essential when presenting code to other people) and you do a little of everything here and there and everywhere. Put related stuff in methods, if you dont feel it deserves its own classes.

    You seem to have the right idea on many points. Sometimes, the most effective way to solve a problem, is to actually taking a break and come back later. Hang in there, with practice and time you WILL get the hang of it.

    Typos removed:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class FinalAssignment extends JFrame implements ActionListener
    {
      FlowLayout flow = new FlowLayout();
      private JLabel headTitle = new JLabel("Passenger List");
      private Font headFont = new Font ("Courier", Font.ITALIC, 18);
      private JLabel enterName = new JLabel ("Enter the passenger's Full Name");
      private JTextField inputName = new JTextField(10);
      private JLabel address = new JLabel("Address");
      private JTextField inputAddress = new JTextField(15);
      private JButton enterData = new JButton("Enter Data");
      int section, seats[], asile;
      int window, people;
      private JLabel win = new JLabel ("Press 1 for Window seat");
      private JLabel asi = new JLabel ("Press 2 for Asile");
      private JButton yButton = new JButton ("Yes");
      private JButton nButton = new JButton ("No");
      private JTextField input = new JTextField(5);
      private Container con = getContentPane();
      DataOutputStream outStream;
     
     
      public static void main(String[] args)
      {
        FinalAssignment fAssign = new FinalAssignment();
        fAssign.setSize(600, 220);
        fAssign.setVisible(true);
      }
      String[] airlines = {"Jet Blue", "Delta", "American"};
      JComboBox airLines = new JComboBox(airlines);
      JLabel mainLabel = new JLabel("Please Select your Airline:");
      JLabel actionLabel = new JLabel("Selected Airline: None");
      FinalAssignment fA = new FinalAssignment();
     
      public FinalAssignment()
      {
        super("Victor E. Campudoni-Final-Assignment");
        try
        {
          outStream = new DataOutputStream(new FileOutputStream("PassList.dat"));
        }
        catch(IOException ex)
        {
          System.err.println("File not opened");
          System.exit(1);
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        headTitle.setFont(headFont);
        mainLabel.setFont(headFont);
        con.setLayout(flow);
        con.add(mainLabel);
        con.add(airLines);
        con.add(actionLabel);
        con.add(headTitle);
        con.add(enterName);
        con.add(inputName);
        con.add(address);
        con.add(inputAddress);
        con.add(enterData);
        con.add(win);
        con.add(asi);
        con.add(input);
        con.add(yButton);
        con.add(nButton);
        airLines.addActionListener(this);
        enterData.addActionListener(this);
        yButton.addActionListener(this);
        nButton.addActionListener(this);
        setVisible(true);
        yButton.setEnabled(true);
        nButton.setEnabled(true);
     
        seats = new int[11];
        asile = 6;
        window = 1;
      }
      public void actionPerformed (ActionEvent e1)
      {
        int i = airLines.getSelectedIndex();
        if(i < 0)
          return;
        actionLabel.setText("Your AirLine is: " + airLines.getItemAt(i));
        {
        if (e.getSource() == input && people <= 10)
        {
          section = Integer.parseInt(input.getText());
          String s = "";
          if (section == 1)
          {
            if (window <= 10 && seats [window] == 0)
            {
              s = "Window. Seat #" + window;
              seats [window++] = 1;
              people++;
            }
            else if (window > 10 && asile <= 5)
            {
              yButton.setEnabled(true);
              nButton.setEnabled(true);
              s = "Window is full. Asile seat?";
            }
            else
              s = "Your flight leaves in 3 hours.";
          }
          else if (section == 2)
          {
            if (seats [asile] == 0 && asile <= 5)
            {
              s = "Asile. Seat #" + asile;
              seats [asile++] = 1;
              people++;
            }
            else if (asile > 5 && window <= 10)
            {
              yButton.setEnabled(true);
              nButton.setEnabled(true);
              s = "Asile seating is full. Window Seat?";
            }
            else
              s = "Your flight leaves in 3 hours.";
          }
          else
            s = "Invalid Input.";
          System.out.println(s);
        }
        else if (e.getSource() == yButton)
        {
          if (section == 1)
          {
            System.out.println("Your seat assignment is " + window);
            seats [window++] = 1;
          }
          ++people;
          nButton.setEnabled(true);
          yButton.setEnabled(true);
        }
        else if (e.getSource() == nButton)
        {
          System.out.println("Your flight leaves in 3 hours.");
          nButton.setEnabled(true);
          yButton.setEnabled(true);
        }
        try
        {
          outStream.writeUTF(inputName.getText());
          outStream.writeUTF(inputAddress.getText());
     
          inputName.setText("");
          inputAddress.setText("");
        }
        catch(IOException e2)
        {
          System.err.println("Error writing file");
          System.exit(1);
        }
        }
      }
    }
    Last edited by Johannes; June 26th, 2010 at 11:12 AM.