Results 1 to 7 of 7
  1. #1
    delacroix24 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default Problem on my work nid help

    hi all. im new in netbeans

    any one can help me on my work..
    my problem are how can i put equivalent 1 if the grade compo2 grade = 90-100, and an equivalent of 2 if the grade is 80-89, and a equivalent of 3 if the grade are 75-79, and equivalent of 5 if the grade is 50-74 when u click compute button.
    Thanks in advance for those who'll help ^_^! heres my code

    Java Code:
    import java.applet.Applet;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    public class computation extends Applet implements ActionListener
    {
        Font a,b,c;
        public void paint (Graphics g)
        {
            a = new Font("Arial",Font.BOLD,30);
            b = new Font("Arial",Font.BOLD,20);
            c = new Font("Arial",Font.BOLD,12);
    
    
            g.setFont(a);
            g.drawString("Computation of Grade", 150,30);
    
            g.setColor(Color.BLACK);
            g.setFont(c);
            
            g.drawString("Prelim",20,95);
            g.drawString("Midterm",220,95);
            g.drawString("Finals",420,95);
            g.drawString("Prelim",20,330);
            g.drawString("Midterm",220,330);
            g.drawString("Finals",420,330);
    
    
    
    
    
            g.setFont(b);
            g.drawString("LECTURE",20,65);
            g.drawString("LABORATORY",20,300);
    
        }
    
        TextField ppq1,ppq2,ppcs,ppg,plg,mmq1,mmq2,mmcs,mmg,mmlg,ffq1,ffq2,ffcs,ffg,fflg,lppq1,lppq2,lppcs,lppg,lplg,lmmq1,lmmq2,lmmcs,lmmg,lmmlg,lffq1,lffq2,lffcs,lffg,lfflg,eq,comp;
        Label pq1,pq2,pcs,pg,lg,mq1,mq2,mcs,mg,mlg,fq1,fq2,fcs,fg,flg,lpq1,lpq2,lpcs,lpg,llg,lmq1,lmq2,lmcs,lmg,lmlg,lfq1,lfq2,lfcs,lfg,lflg,equi,compo;
        Button com,r,e,rem;
       
    
    
    
        public void init()
    
        {
            setLayout (null);
    
            ppq1 = new TextField ("",30);
            ppq1.setBounds(130,110,50,20);
            add(ppq1);
            pq1 = new Label ("PQ1");
            pq1.setBounds(20,110,100,20);
            add(pq1);
    
            ppq2 = new TextField ("",30);
            ppq2.setBounds(130,140,50,20);
            add(ppq2);
            pq2 = new Label ("PQ2:");
            pq2.setBounds(20,140,100,20);
            add(pq2);
    
            ppcs = new TextField ("",30);
            ppcs.setBounds(130,170,50,20);
            add(ppcs);
            pcs = new Label ("PCS:");
            pcs.setBounds(20,170,150,20);
            add(pcs);
    
            pg = new Label ("Grade:");
            pg.setBounds(20,200,100,20);
            add(pg);
            ppg = new TextField("",30);
            ppg.setBounds(130,200,50,20);
            add(ppg);
    
            lg = new Label("Lecture Grade:");
            lg.setBounds(20,230,100,20);
            add(lg);
            plg = new TextField("",30);
            plg.setBounds(130,230,50,20);
            add(plg);
            plg.setEditable(false);
    
            mmq1 = new TextField ("",30);
            mmq1.setBounds(330,110,50,20);
            add(mmq1);
            mq1 = new Label ("MQ1:");
            mq1.setBounds(220,110,50,20);
            add(mq1);
    
            mmq2 = new TextField ("",30);
            mmq2.setBounds(330,140,50,20);
            add(mmq2);
            mq2 = new Label ("MQ2:");
            mq2.setBounds(220,140,50,20);
            add(mq2);
    
            mmcs = new TextField ("",30);
            mmcs.setBounds(330,170,50,20);
            add(mmcs);
            mcs = new Label ("PCS:");
            mcs.setBounds(220,170,150,20);
            add(mcs);
    
            mg = new Label ("Grade:");
            mg.setBounds(220,200,100,20);
            add(mg);
            mmg = new TextField("",30);
            mmg.setBounds(330,200,50,20);
            add(mmg);
    
    
            mlg = new Label("Lecture Grade:");
            mlg.setBounds(220,230,100,20);
            add(mlg);
            mmlg = new TextField("",30);
            mmlg.setBounds(330,230,50,20);
            add(mmlg);
            mmlg.setEditable(false);
    
            ffq1 = new TextField ("",30);
            ffq1.setBounds(530,110,50,20);
            add(ffq1);
            fq1 = new Label ("FQ1:");
            fq1.setBounds(420,110,50,20);
            add(fq1);
    
            ffq2 = new TextField ("",30);
            ffq2.setBounds(530,140,50,20);
            add(ffq2);
            fq2 = new Label ("FQ2:");
            fq2.setBounds(420,140,50,20);
            add(fq2);
    
            ffcs = new TextField ("",30);
            ffcs.setBounds(530,170,50,20);
            add(ffcs);
            fcs = new Label ("PCS:");
            fcs.setBounds(420,170,150,20);
            add(fcs);
    
            fg = new Label ("Grade:");
            fg.setBounds(420,200,100,20);
            add(fg);
            ffg = new TextField("",30);
            ffg.setBounds(530,200,50,20);
            add(ffg);
    
    
            flg = new Label("Lecture Grade");
            flg.setBounds(420,230,100,20);
            add(flg);
            fflg = new TextField("",30);
            fflg.setBounds(530,230,50,20);
            add(fflg);
            fflg.setEditable(false);
    
            lppq1 = new TextField ("",30);
            lppq1.setBounds(130,360,50,20);
            add(lppq1);
            lpq1 = new Label ("MP1");
            lpq1.setBounds(20,360,100,20);
            add(lpq1);
    
            lppq2 = new TextField ("",30);
            lppq2.setBounds(130,390,50,20);
            add(lppq2);
            lpq2 = new Label ("MP2:");
            lpq2.setBounds(20,390,100,20);
            add(lpq2);
    
            lppcs = new TextField ("",30);
            lppcs.setBounds(130,420,50,20);
            add(lppcs);
            lpcs = new Label ("PCS:");
            lpcs.setBounds(20,420,150,20);
            add(lpcs);
    
            lpg = new Label ("Grade:");
            lpg.setBounds(20,450,100,20);
            add(lpg);
            lppg = new TextField("",30);
            lppg.setBounds(130,450,50,20);
            add(lppg);
    
            llg = new Label("Laboratory Grade:");
            llg.setBounds(20,480,100,20);
            add(llg);
            lplg = new TextField("",30);
            lplg.setBounds(130,480,50,20);
            add(lplg);
            lplg.setEditable(false);
    
            lmmq1 = new TextField ("",30);
            lmmq1.setBounds(330,360,50,20);
            add(lmmq1);
            lmq1 = new Label ("MP1:");
            lmq1.setBounds(220,360,50,20);
            add(lmq1);
    
            lmmq2 = new TextField ("",30);
            lmmq2.setBounds(330,390,50,20);
            add(lmmq2);
            lmq2 = new Label ("MP2:");
            lmq2.setBounds(220,390,50,20);
            add(lmq2);
    
            lmmcs = new TextField ("",30);
            lmmcs.setBounds(330,420,50,20);
            add(lmmcs);
            lmcs = new Label ("PCS:");
            lmcs.setBounds(220,420,150,20);
            add(lmcs);
    
            lmg = new Label ("Grade:");
            lmg.setBounds(220,450,100,20);
            add(lmg);
            lmmg = new TextField("",30);
            lmmg.setBounds(330,450,50,20);
            add(lmmg);
    
    
            lmlg = new Label("Laboratory Grade:");
            lmlg.setBounds(220,480,100,20);
            add(lmlg);
            lmmlg = new TextField("",30);
            lmmlg.setBounds(330,480,50,20);
            add(lmmlg);
            lmmlg.setEditable(false);
    
            lffq1 = new TextField ("",30);
            lffq1.setBounds(530,360,50,20);
            add(lffq1);
            lfq1 = new Label ("MP1:");
            lfq1.setBounds(420,360,50,20);
            add(lfq1);
    
            lffq2 = new TextField ("",30);
            lffq2.setBounds(530,390,50,20);
            add(lffq2);
            lfq2 = new Label ("MP2:");
            lfq2.setBounds(420,390,50,20);
            add(lfq2);
    
            lffcs = new TextField ("",30);
            lffcs.setBounds(530,420,50,20);
            add(lffcs);
            lfcs = new Label ("PCS:");
            lfcs.setBounds(420,420,150,20);
            add(lfcs);
    
            lfg = new Label ("Grade:");
            lfg.setBounds(420,450,100,20);
            add(lfg);
            lffg = new TextField("",30);
            lffg.setBounds(530,450,50,20);
            add(lffg);
    
    
            lflg = new Label("Laboratory Grade:");
            lflg.setBounds(420,480,100,20);
            add(lflg);
            lfflg = new TextField("",30);
            lfflg.setBounds(530,480,50,20);
            add(lfflg);
            lfflg.setEditable(false);
    
            eq = new TextField("",30);
            eq.setBounds(130,550,50,20);
            eq.setEditable(false);
          
            equi = new Label("Equivalent:");
            equi.setBounds(20,550,100,20);
      
    
            comp = new TextField("",30);
            comp.setBounds(130,520,50,20);
            comp.setEditable(false);
            compo = new Label("COMPO2 GRADE");
            compo.setBounds(20,520,100,20);
           
    
            com = new Button("Compute");
            com.setBounds(510,520,70,20);
            add(com);
    
            r = new Button("Reset");
            r.setBounds(580,520,50,20);
            add(r);
    
            e = new Button("Exit");
            e.setBounds(620,520,50,20);
            add(e);
    
            rem = new Button("Remove");
            rem.setBounds(670,520,50,20);
            add(rem);
            r.setEnabled(true);
    
           
    
    
    
    
    
            //addActionListener
            com.addActionListener(this);
            r.addActionListener(this);
            e.addActionListener(this);
            rem.addActionListener(this);
    
             }
        public void actionPerformed(ActionEvent e)
         {
            if(e.getSource()==rem)
            {
                ppq1.setText(ppq1.getText());
                ppq2.setText(ppq2.getText());
                ppcs.setText(ppcs.getText());
                mmq1.setText(mmq1.getText());
                mmq2.setText(mmq2.getText());
                mmcs.setText(mmcs.getText());
                ffq1.setText(ffq1.getText());
                ffq2.setText(ffq2.getText());
                ffcs.setText(ffcs.getText());
                lppq1.setText(lppq1.getText());
                lppq2.setText(lppq2.getText());
                lppcs.setText(lppcs.getText());
                lmmq1.setText(lmmq1.getText());
                lmmq2.setText(lmmq2.getText());
                lmmcs.setText(lmmcs.getText());
                lffq1.setText(lffq1.getText());
                lffq2.setText(lffq2.getText());
                lffcs.setText(lffcs.getText());
                
    
                r.setEnabled(true);
            }
            else if(e.getSource()==r)
    
            {
                ppq1.setText("");
                ppq2.setText("");
                ppcs.setText("");
                ppg.setText("");
                mmq1.setText("");
                mmq2.setText("");
                mmcs.setText("");
                mmg.setText("");
                ffq1.setText("");
                ffq2.setText("");
                ffcs.setText("");
                ffg.setText("");  
                lppq1.setText("");
                lppq2.setText("");
                lppcs.setText("");
                lppg.setText("");
                lmmq1.setText("");
                lmmq2.setText("");
                lmmcs.setText("");
                lmmg.setText("");
                lffq1.setText("");
                lffq2.setText("");
                lffcs.setText("");
                lffg.setText("");
                remove(eq);
                remove(equi);
                remove(comp);
                remove(compo);
              
    
    
            }
             
     
             else if(e.getSource()==com)
            {
             int num1 = Integer.parseInt(ppq1.getText());
             int num2 = Integer.parseInt(ppq2.getText());
             int num3 = Integer.parseInt(ppcs.getText());
             int num4 = Integer.parseInt(ppg.getText());
             int num5 = Integer.parseInt(mmq1.getText());
             int num6 = Integer.parseInt(mmq2.getText());
             int num7 = Integer.parseInt(mmcs.getText());
             int num8 = Integer.parseInt(mmg.getText());
             int num9 = Integer.parseInt(ffq1.getText());
             int num10 = Integer.parseInt(ffq2.getText());
             int num11 = Integer.parseInt(ffcs.getText());
             int num12 = Integer.parseInt(ffg.getText());
             int num13 = Integer.parseInt(lppq1.getText());
             int num14 = Integer.parseInt(lppq2.getText());
             int num15 = Integer.parseInt(lppcs.getText());
             int num16 = Integer.parseInt(lppg.getText());
             int num17 = Integer.parseInt(lmmq1.getText());
             int num18 = Integer.parseInt(lmmq2.getText());
             int num19 = Integer.parseInt(lmmcs.getText());
             int num20 = Integer.parseInt(lmmg.getText());
             int num21 = Integer.parseInt(lffq1.getText());
             int num22 = Integer.parseInt(lffq2.getText());
             int num23 = Integer.parseInt(lffcs.getText());
             int num24 = Integer.parseInt(lffg.getText());
       
    
    
    
             //int sum=0;
             int psum = num1 + num2 + num3 + num4 ;
             int msum = num5 + num6 + num7 + num8 ;
             int fsum = num9 + num10 + num11 + num12 ;
             int lpsum = num13 + num14 + num15 + num16 ;
             int lmsum = num17 + num18 + num19 + num20 ;
             int lfsum = num21 + num22 + num23 + num24;
             int csum = psum + msum + fsum + lpsum + lmsum + lfsum;
    
             plg.setText(""+psum/4);
             mmlg.setText(""+msum/4);
             fflg.setText(""+fsum/4);
             lplg.setText(""+lpsum/4);
             lmmlg.setText(""+lmsum/4);
             lfflg.setText(""+lfsum/4);
             comp.setText(""+csum/24);
    
             add(eq);
             add(equi);
             add(comp);
             add(compo);
             
    
           
            }
         else
    
         
         {
             System.exit(0);
    
            
           
        }
    
    }
    }
    Last edited by Fubarable; 08-10-2009 at 05:34 PM. Reason: Code tags added for readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Suggestions:

    1) If it's allowed, I'd suggest moving from an AWT application (an Applet with TextFields, Labels, etc) to a Swing application (a JApplet with JTextFields, JLabels, etc). Swing is a more robust and flexible platform.

    2) There's way too much redundency in that code and cryptic variable names. I suggest that you create code that creates one Panel (or if Swing JPanel). This class can have a String that holds the title Label's text (for instance "Prelim"), a String array that holds the Label text for your TextFields (i.e. {"PQ1", "PQ2", "PCS", "Grade", "Lecture Grade"). The class should have a method that reads in the 1st three textfield numbers, averages them, displays this average, and then calculates the letter grade and displays it. Then you should Place 6 of these Panels in your main app using GridLayouts. This would eliminate over 90% of your code greatly simplifying things, making it much easier to debug and extend your app.

    3) Avoid absolute positioning but instead use nested Panels (or JPanels if Swing) each using an appropriate layout manager.

    Also, when posting code, please use code tags to improve readability. I have added them to your post above. The FAQ will tell you how to do this.

    Best of luck and HTH.

  3. #3
    delacroix24 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default sorry

    sorry we are not allowed to use any application except AWT application,
    any help or suggestion how to use case statement or switch statement regarding on equivalent of compo2 grade and can be apply on AWT application thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    OK, even if you need to use only AWT, you will do yourself a favor to get rid of all those unreadable variables and create a class that holds a Label (whose text can be set in the constructor), and holds a couple of Labels and TextFields (again the label text can be set by the constructor), and then do the code for your calculation only once.

    Here's an example of something similar to your code, but in Swing, just to give you an idea of what I mean:

    NumberPanel.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class NumberPanel {
      private static final int FIELD_LENGTH = 10;
      private JTextField[] textFields; //all the textfiels
      private JPanel mainPanel = new JPanel(new GridBagLayout());
      
      public NumberPanel(String title, String[] fieldLabelText) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.BOTH;
        
        //create a main title jlabel and add to main panel
        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 16f));
        gbc = setGBC(gbc, 0, 0, 1, 1, GridBagConstraints.NORTHWEST);
        mainPanel.add(titleLabel, gbc);
        gbc.gridy = 1;
        mainPanel.add(new JLabel(" "), gbc);
        
        // create jtextfields and corresponding labels
        // and add to the main panel
        textFields = new JTextField[fieldLabelText.length];
        for (int i = 0; i < fieldLabelText.length; i++) {
          JLabel label = new JLabel(fieldLabelText[i]);
          gbc = setGBC(gbc, 0, i + 2, 1, 1, GridBagConstraints.WEST);
          mainPanel.add(label, gbc);
          
          textFields[i] = new JTextField(FIELD_LENGTH);
          if (i > fieldLabelText.length - 3) {
            textFields[i].setEditable(false);
            textFields[i].setFocusable(false);
          }
          gbc = setGBC(gbc, 1, i + 2, 2, 1, GridBagConstraints.EAST);
          mainPanel.add(textFields[i], gbc);
        }
    
      }
      
      // method to call to calculate the sum and average
      // of the numbers in the textfields
      public void calcAndShowSumAndAverages() throws NumberFormatException {
        try {
          int sum = 0;
          for (int i = 0; i < textFields.length - 2; i++) {
            sum += Integer.parseInt(textFields[i].getText());
          }
          double average = (double)sum / (textFields.length - 2);
          textFields[textFields.length - 2].setText(String.valueOf(sum));
          textFields[textFields.length - 1].setText(String.format("%.2f", average));
        } catch (NumberFormatException e) {
          for (int i = 0; i < textFields.length; i++) {
            textFields[i].setText("");
          }
          throw new NumberFormatException("Non-integer numbers entered");
        }
      }
      
      public JPanel getMainPanel() {
        return mainPanel;
      }
      
      // utility method to simplify use of GridBagConstraints with the
      // GridBagLayout
      private GridBagConstraints setGBC(GridBagConstraints gbc, int x, int y, 
          int width, int height, int anchor) {
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.weightx = (double)width;
        gbc.weighty = (double)height;
        gbc.anchor = anchor;
        
        return gbc;
      }
      
    }

    NumberPanelTest.java
    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    public class NumberPanelTest {
      private static final String[][] PANEL_NAMES = {
        {"Fubar 1", "Fubar 2", "Fubar 3"},
        {"Snafu 1", "Snafu 2", "Snafu 3"}
      };
      private static final String[] LABEL_NAMES = {"Value 1", "Value 2", "Value 3", "Value 4", 
        "Sum", "Average"};
      private JPanel mainPanel = new JPanel();
      int rows = PANEL_NAMES.length;
      int cols = PANEL_NAMES[0].length;
      private NumberPanel[][] panels = new NumberPanel[rows][cols];
      
      public NumberPanelTest() {
        JPanel gridPanel = new JPanel(new GridLayout(rows, cols, 30, 30));
        for (int row = 0; row < panels.length; row++) {
          for (int col = 0; col < panels[row].length; col++) {
            panels[row][col] = new NumberPanel(PANEL_NAMES[row][col], LABEL_NAMES);
            JPanel panel = panels[row][col].getMainPanel();
            panel.setBorder(BorderFactory.createLineBorder(Color.blue));
            gridPanel.add(panel);
          }
        }
        
        JButton calcSumButton = new JButton(new CalcSumAction("Calculate Sum"));
        JButton exitButton = new JButton(new ExitAction("Exit"));
        
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 10));
        buttonPanel.add(calcSumButton);
        buttonPanel.add(exitButton);
        
        int gap = 15;
        mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
        mainPanel.setLayout(new BorderLayout(20, 20));
        mainPanel.add(gridPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
      }
    
      public JComponent getPanel() {
        return mainPanel;
      }
      
      @SuppressWarnings("serial")
      private class CalcSumAction extends AbstractAction {
        
        
        public CalcSumAction(String text) {
          super(text);
        }
    
        public void actionPerformed(ActionEvent e) {
          boolean nfeThrown = false;
          for (int i = 0; i < panels.length; i++) {
            for (int j = 0; j < panels[i].length; j++) {
              try {
              panels[i][j].calcAndShowSumAndAverages();
              } catch (NumberFormatException nfe) {
                nfeThrown = true;
              }
            }
          }
          if (nfeThrown) {
            JOptionPane.showMessageDialog(mainPanel, 
                "Please enter integer numbers only", 
                "Invalid Input", JOptionPane.ERROR_MESSAGE);
          }
        }
      }
      
      private class ExitAction extends AbstractAction {
        public ExitAction(String text) {
          super(text);
        }
        
        public void actionPerformed(ActionEvent arg0) {
          Window win = SwingUtilities.getWindowAncestor(mainPanel);
          win.dispose();
        }
      }
      
      private static void createAndShowGUI() {
        JFrame frame = new JFrame("NumberPanelTest Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(
            new NumberPanelTest().getPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }
    }
    Last edited by Fubarable; 08-11-2009 at 12:58 AM.

  5. #5
    delacroix24 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default ahmm

    can i ask what application to use on your example

  6. #6
    delacroix24 is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    anyway can you give an example code of switch that have case in applet project thanks. ^_^

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Sure, you can find a decent description of a Switch statement with example code here: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics).

    I still feel that you'll have a much easier time of it if you sub-divide your code into classes and by dividing conquer this beast. I hope someone has the patience to go through the huge amount of code that you've posted, but the size alone may by too daunting to those who would otherwise help you. In the least if you make your code shorter and simpler, someone will probably be more willing to go through it all.

    Best of luck.

Similar Threads

  1. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  2. Will this applet ever work?
    By willemjav in forum Java Applets
    Replies: 4
    Last Post: 04-20-2008, 05:40 PM
  3. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  4. Work On Lucene
    By peiceonly in forum Lucene
    Replies: 1
    Last Post: 08-07-2007, 05:47 PM
  5. Program don't work
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-04-2007, 09:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •