Gui Help

Convert Miles to Kilometers/inches to centimeters

Page 1 of 1

8 Replies - 1495 Views - Last Post: 09 June 2009 - 09:30 PM Rate Topic: -----

#1 asharp03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-April 09

Gui Help

Posted 08 June 2009 - 07:12 PM

I have to write a GUI java program that will convert miles to kilometers and inches to centimeters. The user must be asked to choose one of the coversion types and then enter the appropriate input, miles or inches. The program will return the respective out put dependent on their choice and input. The user should be able to continue as long as they desire. The program may only end by the choice of the user and it must contain at least 1 method.

First off I must say that I suck at methods and do not understand them at all. I have the beginning where it asks for the miles or inches but when I choose one of the options it brings up the miles conversion. This is the code that I have so far. Can someone help me with the methods that will switch between the miles and the inches.
import javax.swing.*;

public class temp1
{
	public static void main(String [] args)
	{
	String fstring;
	double miles,km;
	boolean notdone = true;
	int YESorNO;
	int MorI;
	double inches, centimeter;
	

	//Get UI as string input

	while(notdone)
	{
	
	JOptionPane.showInputDialog(null, "Would you like to convert mi to km or in to cm?"); 
	
	fstring = JOptionPane.showInputDialog(null, "Enter Miles:");

	//Convert UI to double

	  miles = Double.parseDouble(fstring);

	//Calculate Celsius

	  km = (int) (miles * 1.609344f);

	//User Message

	JOptionPane.showMessageDialog(null, "Miles Entered" + miles +"Kilometers " +km, "Miles to Kilometers", JOptionPane.INFORMATION_MESSAGE);

		JOptionPane.showInputDialog(null, "Enter Inches:");

		//Convert inches to centimeters

		inches = Double.parseDouble(fstring);

		centimeter = (int) (inches * 2.54f);

		JOptionPane.showMessageDialog(null, "Inches Entered" + inches + "Centimeters" +centimeter, "Inches to Centimeters", JOptionPane.INFORMATION_MESSAGE);




	//Yes_No Window

	YESorNO = JOptionPane.showConfirmDialog(null, "End Program", "Message", JOptionPane.YES_NO_OPTION);

	if(YESorNO == JOptionPane.YES_OPTION)
	{
	System.exit(0);
	notdone = false;
	}//end if
	}//end while
	}//end main
}//end class

/**/


Is This A Good Question/Topic? 0
  • +

Replies To: Gui Help

#2 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Gui Help

Posted 08 June 2009 - 07:32 PM

all these are doube

double miles,km;
double inches, centimeter;

why do you convert them to int ?

km = (int) (miles * 1.609344f);
centimeter = (int) (inches * 2.54f);
Was This Post Helpful? 0
  • +
  • -

#3 asharp03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-April 09

Re: Gui Help

Posted 08 June 2009 - 07:47 PM

View Postpbl, on 8 Jun, 2009 - 06:32 PM, said:

all these are doube

double miles,km;
double inches, centimeter;

why do you convert them to int ?

km = (int) (miles * 1.609344f);
centimeter = (int) (inches * 2.54f);

Because that is what the professor said to do so that they rounded the numbers off.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Gui Help

Posted 08 June 2009 - 08:04 PM

View Postasharp03, on 8 Jun, 2009 - 06:47 PM, said:

View Postpbl, on 8 Jun, 2009 - 06:32 PM, said:

all these are doube

double miles,km;
double inches, centimeter;

why do you convert them to int ?

km = (int) (miles * 1.609344f);
centimeter = (int) (inches * 2.54f);

Because that is what the professor said to do so that they rounded the numbers off.

OK so where is your problem ?
Does not compile ?
Run time error ?
Behaviour is not the expected behaviour ?
Was This Post Helpful? 0
  • +
  • -

#5 asharp03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-April 09

Re: Gui Help

Posted 08 June 2009 - 08:10 PM

View Postpbl, on 8 Jun, 2009 - 07:04 PM, said:

View Postasharp03, on 8 Jun, 2009 - 06:47 PM, said:

View Postpbl, on 8 Jun, 2009 - 06:32 PM, said:

all these are doube

double miles,km;
double inches, centimeter;

why do you convert them to int ?

km = (int) (miles * 1.609344f);
centimeter = (int) (inches * 2.54f);

Because that is what the professor said to do so that they rounded the numbers off.

OK so where is your problem ?
Does not compile ?
Run time error ?
Behaviour is not the expected behaviour ?


It compiles and runs. The first box pops up asking miles or inches but no matter which is picked the miles conversion box comes up. I need to figure out how to get the correct conversion box to pop up. I also have to have a method in the program. I am sure that the method is what is needed for the correct box to come up.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Gui Help

Posted 08 June 2009 - 08:49 PM

First your first OptionPane returns a value that you do not check
You have to do something like:

String choice = JOptionPane.showInputDialog(null, "Would you like to convert mi to km or in to cm?");

now you have to determine what the user has to enter to select Km or cm

if(choice.equals("Km") {
...
}
else if(choice.equals("cm") {
...
}
else {
.... invalid choice
}
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: Gui Help

Posted 09 June 2009 - 01:55 AM

Quote

Because that is what the professor said to do so that they rounded the numbers off.


There won't be any rounding. The numbers will be truncated, so if you enter 0.9 you'll get 0 and your calculations won't work
Was This Post Helpful? 0
  • +
  • -

#8 asharp03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 27-April 09

Re: Gui Help

Posted 09 June 2009 - 05:11 AM

After wanting to throw the computer out the window :blink: I have finally gotten the program to work. My only problem is that I do not have a method anywhere in the program. Can someone suggest how to put one in there. Remember go easy on me I stink at methods. :) Here is the code that I have so far.
import javax.swing.*;
public class temp1 {
	public static void main(String [] args) {
	String fstring;
	String Option;
	double miles,km;
	boolean notdone = true;
	double inches, centimeter;
	//Get UI as string input
	while(notdone) {
	Option = JOptionPane.showInputDialog(null, "Would you like to convert mi to km or in to cm?\n" +
			"1.Miles to Kilometers\n" +
			"2.Inches to Centermeters\n" +
			"3.Close..."); 
	if(Option.equals("1")) {
		fstring = JOptionPane.showInputDialog(null, "Enter Miles:");
		miles = Double.parseDouble(fstring);
		km = (int) (miles * 1.609344f);
		JOptionPane.showMessageDialog(null, "Miles Entered" + miles +"Kilometers " +km, "Miles to Kilometers", JOptionPane.INFORMATION_MESSAGE);
	}
	else if(Option.equals("2")) {
		fstring = JOptionPane.showInputDialog(null, "Enter Inches:");
		inches = Double.parseDouble(fstring);
		centimeter = (int) (inches * 2.54f);
		JOptionPane.showMessageDialog(null, "Inches Entered" + inches + "Centimeters" +centimeter, "Inches to Centimeters", JOptionPane.INFORMATION_MESSAGE);
		}
	else if(Option.equals("3")) {
		notdone = false;
		System.exit(0);
		}
	else
		JOptionPane.showMessageDialog(null,"Your doing it wrong?");
		}	
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Gui Help

Posted 09 June 2009 - 09:30 PM

For the sake of it

import javax.swing.*;
public class temp1 {

	public static void main(String [] args) {
		String fstring;
		String Option;
		double miles,km;
		boolean notdone = true;
		double inches, centimeter;
		//Get UI as string input
		while(notdone) {
			Option = AskWhatToDo();
			if(Option.equals("1")) {
				fstring = JOptionPane.showInputDialog(null, "Enter Miles:");
				miles = Double.parseDouble(fstring);
				km = (int) (miles * 1.609344f);
				displayResults("Miles", miles, " Kilometers ", km, "Miles to Kilometers");
			}
			else if(Option.equals("2")) {
				fstring = JOptionPane.showInputDialog(null, "Enter Inches:");
				inches = Double.parseDouble(fstring);
				centimeter = (int) (inches * 2.54f);
				displayResults("Inches", inches, "Centimeters", centimeter, "Inches to Centimeters");
			}
			else if(Option.equals("3")) {
				notdone = false;
				System.exit(0);
			}
			else
				JOptionPane.showMessageDialog(null,"Your doing it wrong?");
		}	
	}
	static void displayResults(String unit, double number, String convertStr, double convertValue, String title) {
		JOptionPane.showMessageDialog(null, unit + " Entered "+ number + convertStr + convertValue, title, JOptionPane.INFORMATION_MESSAGE);
	}
	
	static String AskWhatToDo() {
		String str =  JOptionPane.showInputDialog(null, "Would you like to convert mi to km or in to cm?\n" +
				"1.Miles to Kilometers\n" +
				"2.Inches to Centermeters\n" +
				"3.Close...");
		
		return str;
	}

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1