Java program: weight conversion help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Java program: weight conversion help

  1. #1
    Join Date
    Jun 2005
    Posts
    2

    Java program: weight conversion help

    Hi,

    I have an assignment in school, please help me. here is the problem.

    Write a program that will read in a weight in ounces and pounds and will ouput the equivalent weight in kilograms and grams.

    I have already design the form using netbeans, but i don't know how to implement it without using netbeans or any other IDE since we are not allowed to use it. i attached a preview of how I want it to look. please help me do it.
    Attached Images Attached Images

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Nice looking form. However, you have run into the "danger" of using IDE generated GUI elements without knowing how to produce them "by hand". I guess you're going to get the experience of doing it by hand ...

    Can you copy the generated GUI code into a .java file outside of Netbeans? It should contain the stub of the methods which fire events and listen to events.

  3. #3
    Join Date
    Jun 2005
    Posts
    2
    Yes, but it produces an error. Anybody could help me?

  4. #4
    Join Date
    Dec 2005
    Posts
    97
    so, you want us to create the GUI which you have posted a picture of or what? i wouldnt bother with GUI's untill you are able to create them from scratch. just create a simple program that uses the command prompt instead. remember, functionality is more important than eye appeal.

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The "error messages" are the compiler's help to you about identifying the problems it is encountering with your code. If you post those error messages, and the code segments the error messages refer to, others might be able to help you with your assignment.

    I seem to remember information on the Netbeans site - newsletters, discussion threads, etc. - which relates to the modification of the Matisse generated GUI elements by hand.

    I agree with anubis: get it right before you make it pretty. We should be about functionality first, then presentation.
    Last edited by nspils; 09-30-2006 at 10:30 AM.

  6. #6
    Join Date
    Apr 2006
    Posts
    28
    My dear
    Let me know the fomular to convert weight so that i can write the program for you,
    Are you using the JPanel form ? so you should unsdertand the gettting and setting varialble from Form .

  7. #7
    Join Date
    Dec 2005
    Posts
    97
    1kg = 2.2lb
    1kg = 1000g
    1lb = 16 ounces

    you really shouldnt write it for him...maybe some pointers, but not the entire thing...

  8. #8
    Join Date
    Apr 2006
    Posts
    28
    Add some code that you want to change, also add validation for number ("keyReleased") I hope i can contribute a little bit.

    /*
    * Convert.java
    *
    * Created on 1 October 2006, 12:11
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package Week17;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Convert extends JFrame
    {
    JPanel p;
    WeightConverter w;
    int i=0;
    public Convert()
    {
    initialize();
    w.jradOunce.addItemListener(new ItemListener()
    {
    public void itemStateChanged(ItemEvent evt)
    {
    if(ItemEvent.SELECTED==evt.getStateChange())
    i=1;
    else i=2;
    }
    });
    w.jradPound.addItemListener(new ItemListener()
    {
    public void itemStateChanged(ItemEvent evt)
    {

    }
    });
    w.jbtConvert.addActionListener(new ActionListener()
    {
    public void actionPerformed (ActionEvent e)
    {
    convert();
    }
    });

    }
    public void convert()
    {
    w.jtfResultInGram.setText("");
    w.jtfResultInKilogram.setText("");
    String stNum=w.jtfInputWeight.getText();
    double dbNum=Double.parseDouble(stNum);

    if (i==1)
    w.jtfResultInGram.setText(dbNum*1000+"");
    else
    w.jtfResultInKilogram.setText(dbNum*10+"");
    }
    public void initialize()
    {
    w=new WeightConverter ();
    w.jtfResultInGram.setEditable(false);
    w.jtfResultInKilogram.setEditable(false);
    p=new JPanel();
    p.setLayout(new FlowLayout());
    p.add(w);
    getContentPane().add(p);
    setBounds(0,0,400,400);
    setVisible(true);
    setTitle("Weight Converter");
    setResizable(false);
    setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
    public static void main (String[] args)
    {
    new Convert();
    }
    }

  9. #9
    Join Date
    Oct 2006
    Posts
    5
    Here is a GUI for you to use...I hope it helps


    Code:
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.border.TitledBorder;
    
    public class convert extends JFrame implements ActionListener, ItemListener{
    	//declare variables
    	JRadioButton radOunce, radPound;
    	JTextField txtInput;
    	JButton btnConvert;
    	JLabel lblKilo, lblgram, lblkilotxt, lblgramtxt;
    	JPanel p1, p2, p3; 
    	
    	public convert(){
    		super("Weight Converter");
    		//setup container 
    		Container c = getContentPane();
    		c.setLayout(null);
    		//initialize varialbles 
    		//Ounce radio button
    		radOunce    = new JRadioButton("Ounce");
    		radOunce.addItemListener(this);
    		//Pound radio button
    		radPound    = new JRadioButton("Pound");
    		radPound.addItemListener(this);
    		//Input 
    		txtInput    = new JTextField(10);
    		//Convert Button
    		btnConvert  = new JButton("Convert");
    		btnConvert.addActionListener(this);
    		//Labels
    		lblKilo     = new JLabel("          ");
    		lblkilotxt  = new JLabel("Equivalent in kilograms");
    		lblgram     = new JLabel("          ");
    		lblgramtxt  = new JLabel("Equivalent in grams");
    		//Panels
    		p1 = new JPanel();
    		p2 = new JPanel();
    		p3 = new JPanel();
    		
    		//setBounds(x,y,width,height)
    		
    		//first section
    		radOunce.setBounds(10,25,75,26);
    		c.add(radOunce);
    		radPound.setBounds(150,25,75,26);
    		c.add(radPound);
    		p1.setBorder(new TitledBorder("What do you want to convert?"));
    		p1.setBounds(5,5,225,60);
    		c.add(p1);
    		
    		//second section
    		txtInput.setBounds(15,100,95,26);
    		c.add(txtInput);
    		btnConvert.setBounds(130,100,95,26);
    		c.add(btnConvert);
    		p2.setBorder(new TitledBorder("Input Weight Value to Covert"));
    		p2.setBounds(5,75,225,60);
    		c.add(p2);
    		
    		//third section
    		lblkilotxt.setBounds(10,160,150,26);
    		c.add(lblkilotxt);
    		lblgramtxt.setBounds(10,180,150,26);
    		c.add(lblgramtxt);
    		lblKilo.setBounds(170,160,150,26);
    		c.add(lblKilo);
    		lblgram.setBounds(170,180,150,26);
    		c.add(lblgram);
    		p3.setBorder(new TitledBorder("Result"));
    		p3.setBounds(5,145,225,75);
    		c.add(p3);
    		
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLocation(150,150);
    		//sets size of display(x,y) width, height
    		setSize( 250, 270 );
    		//disables resizable option 
    		setResizable( false );
    		show();
    	}
    	public void actionPerformed(ActionEvent e) {
    		//you put what you need to here 
    	}
    
    	public void itemStateChanged(ItemEvent e) {
    		//you put what you need to here
    	}
    	
    	public static void main(String[] arg){
    		new convert().show();  	
    	}
    }

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 03:16 PM
  2. Problem with java program
    By jareivy05 in forum Java
    Replies: 9
    Last Post: 12-07-2005, 09:11 PM
  3. Replies: 1
    Last Post: 04-18-2002, 07:36 AM
  4. clearin the screen from a java program
    By arun chakravarty in forum Java
    Replies: 1
    Last Post: 12-01-2000, 10:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center