Jump to content

GUI and Method Help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
asharp03

asharp03

    Newbie

  • Members
  • PipPip
  • 14 posts
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




#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
I will help as soon I am back...
Cheers !
Posted Image

#3
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Well, not the best ...
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?");

		}	

	}

}

Hopefully it will work?
Cheers !
Posted Image

#4
asharp03

asharp03

    Newbie

  • Members
  • PipPip
  • 14 posts
When I try to compile and run the code I get an error that says...
temp1.java:18: variable Option might not have been initialized
if(Option.equals(("1"))) {

#5
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
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?");

		}	

	}

}
EDIT'D
Posted Image

#6
asharp03

asharp03

    Newbie

  • Members
  • PipPip
  • 14 posts
Thank you thank you thank you. Now all that I need to do is figure out how to put a method somewhere into the program and it is finished.

#7
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

asharp03 said:

Thank you thank you thank you. Now all that I need to do is figure out how to put a method somewhere into the program and it is finished.

No problem?
Posted Image

#8
asharp03

asharp03

    Newbie

  • Members
  • PipPip
  • 14 posts
Can someone give me a push in the right direction to get the method in the program.

#9
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
I will do one thing with a method so, try to understand !

import javax.swing.*;
public class temp1 {
	static double miles;
	static double km;
	static String fstring;
	static String Option;
	static boolean notdone = true;
	static double inches;
	static double centimeter;
	public static void main(String [] args) {
	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"))) {
//calling the method...
		milestokilometers(Option);
	}
	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?");
		}
	}
//Method....
	public static void milestokilometers(String fstring) {
		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);
	}
}

Posted Image

#10
asharp03

asharp03

    Newbie

  • Members
  • PipPip
  • 14 posts
Thank you for the help. One more small favor and I will leave you alone :). Can you explain how the method works for me...I see where the method is called and where the method is but what exactly does the method do? My professor just does not explain this very well and I would like to try and get a better understanding of it. Thanks again.

#11
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

asharp03 said:

Thank you for the help. One more small favor and I will leave you alone :). Can you explain how the method works for me...I see where the method is called and where the method is but what exactly does the method do? My professor just does not explain this very well and I would like to try and get a better understanding of it. Thanks again.

Sure, okay I will do my best...


First of all a method is like a function for a math problem.(This is how I got hang of it. However in general a method is just a way to present the functions your after without showing the whole process and can be used multiplied times anywhere in the code...)
So to sum it all.

Method #1
public class TestHello{

public static void main(String[] arg) {

Hello();

}

public static void Hello() {

System.out.println("Hello World");

}

}

Method #2
public class TestHello {

	public static void Hello() {

		System.out.println("Hello World");

	}

	public static void main(String[] arg ) {

		Hello();

	}

}
Okay no difference in content, however the structure might look different however it isn't... The method is inside the constructor in both ways which means the program will automatically load the operation into the main part...

public static void main() <--- The main part of application.
main(String[] args) or (String args[]) doesn't matter...The inside things of a main is called parameters which defines what types are going to be used and need to be used to "execute" the method.
So the method is a simpler way to use the program "more" OOP with java...
Anyways... Hello(); is used to "call"(is a reference) to the method. Which will execute the methods function.
So most likely...
Car has motor, motor connects to the X-axle and when car refers to motor, motor executes itself and you know the rest...
Thats all I can I guess ask someone else with more teaching skills :)
Posted Image

#12
asharp03

asharp03

    Newbie

  • Members
  • PipPip
  • 14 posts
Thanks!!! That actually makes sense to me....Our professor just did not really explain what the method actually did.