Link to home
Start Free TrialLog in
Avatar of tamihall
tamihall

asked on

If statement?

I am trying to write a code that converts SAE to metric. I have to have Volume, Weight, and Lenght. under each one of those I need the option of gallon, quart, pint,cup under Volume. Under Weight Pound and ounce and under Length mile, yard, foot and inch.

currently I have a total of 4 classes. I have a Volume class, a Weight Class and a Length class that will accept the number inputed and do the conversion and send it to the main class.

I am trying to do it as an applet I start with the first 3 buttons when a choice is made those buttone are removed and the next set of buttons are redrawn. My problem is when the next choice is made it goes nowhere. I have the different options set up as methods and I am calling the methods.

This is the code I have so far for the main class. if I remove                          if(source == gallonButton)
       gallon();
it will compile but I don't know how to get to the next step. I know it should be simple I am just not getting it.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;

public class MetricProject12 extends JApplet implements ActionListener
{
      JButton volumeButton = new JButton("Volume");
      JButton lenghtButton = new JButton("Lenght");
      JButton weightButton = new JButton("Weight");
      
      JButton gallonButton = new JButton();
      JButton quartButton = new JButton();
      JButton pintButton = new JButton();
      JButton cupButton = new JButton();
      
      JButton poundButton = new JButton();
      JButton ounceButton = new JButton();
      
      JButton mileButton = new JButton();
      JButton yardButtton = new JButton();
      
      FlowLayout flow = new FlowLayout();
      Container con = getContentPane();
      
            JLabel mainLabel = new JLabel("Metric Conversion Project");
      JLabel optionExplainLabel = new JLabel("Please choose converstion type desired");


      
            public void init()
            {
            con.setLayout(flow);
            
            con.add(volumeButton);
            con.add(lenghtButton);
            con.add(weightButton);
            
            volumeButton.addActionListener(this);
            lenghtButton.addActionListener(this);
            weightButton.addActionListener(this);
            }//end init
            
            public void volume()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  
                  FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
                  con.setLayout(flow);
                  
                  JButton gallonButton = new JButton();
                  JButton quartButton = new JButton();
                  JButton pintButton = new JButton();
                  JButton cupButton = new JButton();
                                    
                  con.add(gallonButton);
                  con.add(quartButton);
                  con.add(pintButton);
                  con.add(cupButton);
                  
                  gallonButton.addActionListener(this);
                  quartButton.addActionListener(this);
                  pintButton.addActionListener(this);
                  cupButton.addActionListener(this);
                  
                  gallonButton.setText("Gallon");
                  quartButton.setText("Quart");
                  pintButton.setText("Pint");
                  cupButton.setText("Cup");
                  
            }//end volume
            
            public void weight()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  
                  FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
                  con.setLayout(flow);
                  
                  JButton poundButton = new JButton();
                  JButton ounceButton = new JButton();
                                    
                  con.add(poundButton);
                  con.add(ounceButton);
                  
                  poundButton.addActionListener(this);
                  ounceButton.addActionListener(this);
                  
                  poundButton.setText("Pound");
                  ounceButton.setText("Ounce");
                  


            }//end weight
            
            public void length()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  
                  FlowLayout flow3 = new FlowLayout(FlowLayout.CENTER);
                  con.setLayout(flow3);

                  JButton mileButton = new JButton("Mile");
                  JButton yardButtton = new JButton("Yard");
                  JButton footButton = new JButton("Foot");
                  JButton inchButton = new JButton("inch");

                  con.add(mileButton);
                  con.add(yardButtton);
                  con.add(footButton);
                  con.add(inchButton);
            
                  mileButton.addActionListener(this);
                  yardButtton.addActionListener(this);
                  footButton.addActionListener(this);
                  inchButton.addActionListener(this);
                  
                  mileButton.setText("Mile");
                  yardButtton.setText("Yard");
                  footButton.setText("Foot");
                  inchButton.setText("Inch");
                  

            }//end lenght()
            
            public void gallon()
            {
                  remove (volumeButton);
                  remove (lenghtButton);
                  remove (weightButton);
                  repaint();
                  

                  String sGallon = JOptionPane.showInputDialog(null,"Please enter the number to be converted:");
                  double gallon = Double.parseDouble(sGallon);
                  
                  Volume vol = new Volume();
                  //creat an object from the Volume class
                  
                  vol.getGallon(gallon);
                  //pass the variable gallon to the getGallon method in the Volume class
                  
                  double liters;
                  liters = vol.getConvertGallon();

                  JLabel outputLabel = new JLabel();
                  outputLabel.setText(sGallon + "Gallons converted into liters is " + liters);
                  
            }//end gallon()
            
      public void actionPerformed(ActionEvent e)
      {
            Object source = e.getSource();
            {
                        if(source== volumeButton)
                        
                        volume();
                         if(source == gallonButton)
                         gallon();
                        
                        else if(source == weightButton)
                        
                              weight();
                        
                        else if(source == lenghtButton)
                        
                              length();
                        
            }//end object source
      }//end action performed
}//end class
Avatar of Mick Barry
Mick Barry
Flag of Australia image

call validate() after changing the layout.
Avatar of tamihall
tamihall

ASKER

I'm sorry, I am still pretty new at this. what do you mean?
eg.

          public void length()
          {
               con.remove (volumeButton);
               con.remove (lenghtButton);
               con.remove (weightButton);
               
               FlowLayout flow3 = new FlowLayout(FlowLayout.CENTER);
               con.setLayout(flow3);

               JButton mileButton = new JButton("Mile");
               JButton yardButtton = new JButton("Yard");
               JButton footButton = new JButton("Foot");
               JButton inchButton = new JButton("inch");

               con.add(mileButton);
               con.add(yardButtton);
               con.add(footButton);
               con.add(inchButton);
         
               mileButton.addActionListener(this);
               yardButtton.addActionListener(this);
               footButton.addActionListener(this);
               inchButton.addActionListener(this);
               
               mileButton.setText("Mile");
               yardButtton.setText("Yard");
               footButton.setText("Foot");
               inchButton.setText("Inch");
               
               con.validate();
               repaint();
          }//end lenght()
I tried wat you said and tried the gallon() method linked to the mile button

else if(source == lenghtButton)
length();
if(source == mileButton)
 gallon();

complied it and it still did not work.
did you fix the gallon() method?
theres nothing to fixing the gallon method. It is just being called. It is not reling on anything else in the main class I just changed how it was called.
the gallon() method has the same problem, you need to call validate().

you also need to remoive buttons from the content pane and not from the applet.
I put
con.validate();
repaint();

at the end of the gallon() method and added
remove(mileButton);
remove(yardButtton);

to the method. I tried to remove the other two buttons and received an error. Complied it and still nothing.
you shouldn't be removing them from applet.
you add them to the content pane and thats where you should be removing them.
Thank you for being so patent with me. I am not sure what you are trying to tell me to do. If you look in the other methods like volume() and weight(). I tell it to remove the buttons and repaint() and it worked there. So why wouldn't it work with the Gallon() method? And how would that keep the gallon() method from running?
are any exceptions being thrown?
what exactly happens when the gallon button is pressed?
the way it stands right now, no. It just doesn't work. It compiles fine, but when I click on the mile button nothing happens.
nothing.
>              JButton gallonButton = new JButton();

you create a local gallonButton
should be:

             gallonButton = new JButton();

though you don't need to recreate the button everytime.
Maybe I am not explaining myself well enought. probably because I am so tired and frustrated.

When the applets starts I get a frame that has 3 buttons on it.

Volume     Weight      Lenght

If I press Volume I get a redrawn frame with 4 buttons

Gallon    Quart    pint    Cup

If I had pressed Weight I would get a redrwn screen with 2 buttons

Pound    Ounce

And if I had pressed Length I would get 4 buttons

mile    yard    foot   inch

This is what I get right now as it stands.
 I only have the gallon method written right now so that s the one I am useing to test.

After I press the Volume button and I get the next screen and I press the Gallon button it just sits there. nothing happens. I see all the buttons but I am not getting any reaction from them.

Object source = e.getSource();
   {
        if(source== volumeButton)
        volume();
            if(source == gallonButton)
       gallon();

        else if(source == weightButton)
                        
            weight();
                        
         else if(source == lenghtButton)
      length();

I want to be able to run if statements under the next set of buttons. Kind on the way I have it here. IE
if(source==gallonButton)
   gallon()
if(source == quartButton)
  quart()
if(source == pintButton)
  pint()
if(souce==cupButton)
  cup()

but it does not see any of the method and does not respond at all.

If I place the gallon() method at the same level as the volume() method it does see it and run
IE
Object source = e.getSource();
{
if(source== volumeButton)
volume();
gallon():

it will run like this so I know it is not the method.

I am going to be going to bed because I have 2 babys to chase tomorrow. I will check any responces tomorrow.

Thank you for your time and effort.


ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, in each of the volume(), length(), and weight() methods, just remove the lines that look like this:
               JButton mileButton = new JButton("Mile");
               JButton yardButtton = new JButton("Yard");
               JButton footButton = new JButton("Foot");
               JButton inchButton = new JButton("inch");
Since you've used the same names for local and class variables, things should compile.

I recommend you make and call a method like
    void clearWindow()
    {
               remove (volumeButton);
               remove (lenghtButton);
               remove (weightButton);
               repaint();
    }
Rather than repeating those lines all over.  You're going to find when things start working that there are more components to clear...
objects

Thank you so much for all the help. I don't know why I didn't see that myself. I already had the buttons on top but I also had them in the method and that was why I was having so much trouble. Thank you again.
No worries :)