3 Replies - 3804 Views - Last Post: 08 August 2011 - 07:24 PM Rate Topic: -----

#1 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Temperature Conversion Program --using the string.format

Posted 07 August 2011 - 01:09 PM

Assignment:You will write a temperature conversion program. The GUI and event handling setup should be done in the constructor of the class that implements the GUI. Your GUI class should contain a JFrame member variable. Do not use any of the GUI editing capabilities of Eclipse for this assignment. The temperature conversion application should have a JLabel and JTextField in which the user inputs a value. There should be a set of six JButtons on the display representing the following temperature conversions:

F-to-C F: Fahrenheit
C-to-F C: Celsius
K-to-C K: Kelvin
C-to-K
K-to-F
F-to-K

Event handling should be set up so that clicking on any one of the six buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point.



The code I have in place works as designed, but I'm curious to know how I could use the string.format to set the output field to 3 digits after the decimal point instead of using "import java.text.DecimalFormat;"??? I'm specifically referring to lines 6, 25, & 125. Below is the program:
package temp1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

@SuppressWarnings("serial")
public class  Temp1 extends JFrame
implements ActionListener {
private JButton button1, button2, button3, button4, button5, button6;
@SuppressWarnings("unused")
private JPanel panel;
@SuppressWarnings("unused")
private JFrame frame;
@SuppressWarnings("unused")
private JTextField Text;
@SuppressWarnings("unused")
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Enter Input Value Here:");
private JLabel outlabel = new JLabel("Converted Temperature Output:");

DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output

public static void main(String[] args) {
	Temp1 frame = new Temp1();
	frame.createFrame();
	frame.createButton1();
	frame.createButton2();
	frame.createButton3();
	frame.createButton4();
	frame.createButton5();
	frame.createButton6();
	frame.addLabels();
	}

public void createFrame() {
	Container window = getContentPane();
	window.setLayout(new FlowLayout() ); //FlowLayout manager
	setSize(600, 400); //setting the size 
	setVisible(true); //allows for manual size 
	setTitle("Temperature Converter Application"); 
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	panel = new JPanel(new GridLayout());
	add(new JLabel("Temperature Conversions:"+ " C: Celsius"+ " F: Farhrenheit"+ " K: Kelvin")); 
	}

public void createButton1() {
	//Fahrenheit to Celsius button
	button1 = new JButton("F-to-C");
	add(button1);
	button1.addActionListener((ActionListener) this);//this performs action when button1 is clicked
	}

public void createButton2() {
	//Fahrenheit to Kelvin button
	button2 = new JButton("C-to-F");
	add(button2); 
	button2.addActionListener((ActionListener) this);//this performs action when button2 is clicked
	}

public void createButton3() {
	//Celsius to Kelvin button
	button3 = new JButton("K-to-C");
	add(button3); 
	button3.addActionListener((ActionListener) this);//this performs action when button3 is clicked
	}

public void createButton4() {
	//Celsius to Fahrenheit button
	button4 = new JButton("C-to-K");
	add(button4); 
	button4.addActionListener((ActionListener) this);//this performs action when button4 is clicked
	}

public void createButton5() {
	//Kelvin to Celsius button
	button5 = new JButton("K-to-F");
	add(button5); 
	button5.addActionListener((ActionListener) this);//this performs action when button5 is clicked
	}

public void createButton6() {
	//Kelvin to Fahrenheit button
	button6 = new JButton("F-to-K");
	add(button6);
	button6.addActionListener((ActionListener) this);//this performs action when button6 is clicked
	}

public void addLabels() {
	add(inlabel);
	add(infield);
	add(outlabel);
	add(outfield);
	outfield.setEditable(false);//exception handling
	}

public void actionPerformed(ActionEvent i) { 
	int temp;
	double newtemp = 0;
	String inputString;
	inputString = infield.getText();//computes input
	temp = Integer.parseInt(inputString);
	
	if(i.getSource() == button1) {
		newtemp = (5/9) * (temp - 32);//F to C formula
	}
	else if(i.getSource() == button2) {
		newtemp = ((9/5) * temp) + 32;//C to F formula
	}
	else if(i.getSource() == button3) {
		newtemp = temp - 273;//K to C formula
	}
	else if(i.getSource() == button4) {
		newtemp = temp + 273;//C to K formula
	}
	else if(i.getSource() == button5) {
		newtemp = ((temp - 273) * (9/5)) + 32;//K to F formula
	}
	else {
		newtemp = (5/9) * (temp - 32) + 273;//F to K formula
	}
	outfield.setText(" "+ three.format(newtemp));
	}
	}



Any help would be greatly appreciated.

View Postvolvera215, on 07 August 2011 - 01:05 PM, said:

Assignment:You will write a temperature conversion program. The GUI and event handling setup should be done in the constructor of the class that implements the GUI. Your GUI class should contain a JFrame member variable. Do not use any of the GUI editing capabilities of Eclipse for this assignment. The temperature conversion application should have a JLabel and JTextField in which the user inputs a value. There should be a set of six JButtons on the display representing the following temperature conversions:

F-to-C F: Fahrenheit
C-to-F C: Celsius
K-to-C K: Kelvin
C-to-K
K-to-F
F-to-K

Event handling should be set up so that clicking on any one of the six buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celsius, Kelvin to Fahrenheit, Celsius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display three digits after the decimal point.



The code I have in place works as designed, but I'm curious to know how I could use the string.format to set the output field to 3 digits after the decimal point instead of using "import java.text.DecimalFormat;"??? I'm specifically referring to lines 6, 25, & 125. Below is the program:
package temp1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

@SuppressWarnings("serial")
public class  Temp1 extends JFrame
implements ActionListener {
private JButton button1, button2, button3, button4, button5, button6;
@SuppressWarnings("unused")
private JPanel panel;
@SuppressWarnings("unused")
private JFrame frame;
@SuppressWarnings("unused")
private JTextField Text;
@SuppressWarnings("unused")
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Enter Input Value Here:");
private JLabel outlabel = new JLabel("Converted Temperature Output:");

DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output

public static void main(String[] args) {
	Temp1 frame = new Temp1();
	frame.createFrame();
	frame.createButton1();
	frame.createButton2();
	frame.createButton3();
	frame.createButton4();
	frame.createButton5();
	frame.createButton6();
	frame.addLabels();
	}

public void createFrame() {
	Container window = getContentPane();
	window.setLayout(new FlowLayout() ); //FlowLayout manager
	setSize(600, 400); //setting the size 
	setVisible(true); //allows for manual size 
	setTitle("Temperature Converter Application"); 
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	panel = new JPanel(new GridLayout());
	add(new JLabel("Temperature Conversions:"+ " C: Celsius"+ " F: Farhrenheit"+ " K: Kelvin")); 
	}

public void createButton1() {
	//Fahrenheit to Celsius button
	button1 = new JButton("F-to-C");
	add(button1);
	button1.addActionListener((ActionListener) this);//this performs action when button1 is clicked
	}

public void createButton2() {
	//Fahrenheit to Kelvin button
	button2 = new JButton("C-to-F");
	add(button2); 
	button2.addActionListener((ActionListener) this);//this performs action when button2 is clicked
	}

public void createButton3() {
	//Celsius to Kelvin button
	button3 = new JButton("K-to-C");
	add(button3); 
	button3.addActionListener((ActionListener) this);//this performs action when button3 is clicked
	}

public void createButton4() {
	//Celsius to Fahrenheit button
	button4 = new JButton("C-to-K");
	add(button4); 
	button4.addActionListener((ActionListener) this);//this performs action when button4 is clicked
	}

public void createButton5() {
	//Kelvin to Celsius button
	button5 = new JButton("K-to-F");
	add(button5); 
	button5.addActionListener((ActionListener) this);//this performs action when button5 is clicked
	}

public void createButton6() {
	//Kelvin to Fahrenheit button
	button6 = new JButton("F-to-K");
	add(button6);
	button6.addActionListener((ActionListener) this);//this performs action when button6 is clicked
	}

public void addLabels() {
	add(inlabel);
	add(infield);
	add(outlabel);
	add(outfield);
	outfield.setEditable(false);//exception handling
	}

public void actionPerformed(ActionEvent i) { 
	int temp;
	double newtemp = 0;
	String inputString;
	inputString = infield.getText();//computes input
	temp = Integer.parseInt(inputString);
	
	if(i.getSource() == button1) {
		newtemp = (5/9) * (temp - 32);//F to C formula
	}
	else if(i.getSource() == button2) {
		newtemp = ((9/5) * temp) + 32;//C to F formula
	}
	else if(i.getSource() == button3) {
		newtemp = temp - 273;//K to C formula
	}
	else if(i.getSource() == button4) {
		newtemp = temp + 273;//C to K formula
	}
	else if(i.getSource() == button5) {
		newtemp = ((temp - 273) * (9/5)) + 32;//K to F formula
	}
	else {
		newtemp = (5/9) * (temp - 32) + 273;//F to K formula
	}
	outfield.setText(" "+ three.format(newtemp));
	}
	}



Any help would be greatly appreciated.



REVISED program: I sent the wrong one the first time. Same line of question still apply.

package temp1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

@SuppressWarnings("serial")
public class  Temp1 extends JFrame
implements ActionListener {
private JButton button1, button2, button3, button4, button5, button6;
@SuppressWarnings("unused")
private JPanel panel;
@SuppressWarnings("unused")
private JFrame frame;
@SuppressWarnings("unused")
private JTextField Text;
@SuppressWarnings("unused")
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Enter Input Value Here:");
private JLabel outlabel = new JLabel("Converted Temperature Output:");

DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output

public static void main(String[] args) {
	Temp1 frame = new Temp1();
	frame.createFrame();
	frame.createButton1();
	frame.createButton2();
	frame.createButton3();
	frame.createButton4();
	frame.createButton5();
	frame.createButton6();
	frame.addLabels();
	}

public void createFrame() {
	Container window = getContentPane();
	window.setLayout(new FlowLayout() ); //FlowLayout manager
	setSize(600, 400); //setting the size 
	setVisible(true); //allows for manual size 
	setTitle("Temperature Converter Application"); 
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
	panel = new JPanel(new GridLayout());
	add(new JLabel("Temperature Conversions:"+ " C: Celsius"+ " F: Farhrenheit"+ " K: Kelvin")); 
	}

public void createButton1() {
	//Fahrenheit to Celsius button
	button1 = new JButton("F-to-C");
	add(button1);
	button1.addActionListener((ActionListener) this);//this performs action when button1 is clicked
	}

public void createButton2() {
	//Fahrenheit to Kelvin button
	button2 = new JButton("C-to-F");
	add(button2); 
	button2.addActionListener((ActionListener) this);//this performs action when button2 is clicked
	}

public void createButton3() {
	//Celsius to Kelvin button
	button3 = new JButton("K-to-C");
	add(button3); 
	button3.addActionListener((ActionListener) this);//this performs action when button3 is clicked
	}

public void createButton4() {
	//Celsius to Fahrenheit button
	button4 = new JButton("C-to-K");
	add(button4); 
	button4.addActionListener((ActionListener) this);//this performs action when button4 is clicked
	}

public void createButton5() {
	//Kelvin to Celsius button
	button5 = new JButton("K-to-F");
	add(button5); 
	button5.addActionListener((ActionListener) this);//this performs action when button5 is clicked
	}

public void createButton6() {
	//Kelvin to Fahrenheit button
	button6 = new JButton("F-to-K");
	add(button6);
	button6.addActionListener((ActionListener) this);//this performs action when button6 is clicked
	}

public void addLabels() {
	add(inlabel);
	add(infield);
	add(outlabel);
	add(outfield);
	outfield.setEditable(false);//exception handling
	}

public void actionPerformed(ActionEvent i) { 
	int temp;
	double newtemp = 0;
	String inputString;
	inputString = infield.getText();//computes input
	temp = Integer.parseInt(inputString);
	
	if(i.getSource() == button1) {
		newtemp = (5.0/9.0) * (temp - 32);//F to C formula
	}
	else if(i.getSource() == button2) {
		newtemp = ((9.0/5.0) * temp) + 32;//C to F formula
	}
	else if(i.getSource() == button3) {
		newtemp = temp - 273;//K to C formula
	}
	else if(i.getSource() == button4) {
		newtemp = temp + 273;//C to K formula
	}
	else if(i.getSource() == button5) {
		newtemp = ((temp - 273) * (9.0/5.0)) + 32;//K to F formula
	}
	else {
		newtemp = (5.0/9.0) * (temp - 32) + 273;//F to K formula
	}
	outfield.setText(" "+ three.format(newtemp));
	}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Temperature Conversion Program --using the string.format

#2 volvera215  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 08-March 11

Re: Temperature Conversion Program --using the string.format

Posted 07 August 2011 - 01:39 PM

Please bear in mind that I am a newbie with Java...

How can I be certain I am using an inner class??

What is meant by the statement: Do not use any of the GUI editing capabilities of Eclipse for this assignment

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,273
  • Joined: 27-December 08

Re: Temperature Conversion Program --using the string.format

Posted 08 August 2011 - 07:08 AM

First, you don't need the @SuppressWarnings annotations. It's a setting you have that makes Eclipse complain. You should turn it off, as it will make your code a lot cleaner. Second, why have a method for each JButton? Your constructor should be used to initialize the JButtons. You can use a JButton[] to store the various buttons, which will allow you to automate adding them to the JPanel and adding the ActionListener to them.

As for an inner class, you aren't using one. You are instead implementing ActionListener with your outer class. There are two types of inner classes- named and anonymous.

For a named inner class, you will have to instantiate it in your outer class and add the Inner object as the ActionListener to the JButtons.
class Outer extends JFrame{

    class Inner implements ActionListener{

          public void actionPerformed(ActionEvent e){}
    }
}



Or you can add an anonymous inner listener to the JButtons.
myButton.addActionListener(new ActionListener(){
 
     public void actionPerformed(ActionEvent e){}
});



Many programmers see the named inner class style as cleaner.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Temperature Conversion Program --using the string.format

Posted 08 August 2011 - 07:24 PM

Compiler's warning are there to help you identify potential problems
It really scares me to see so many "suppress warning" is such a few lines of code

Remove you "suppress warning" and fix the potential problems
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1