JCreator Problem. Please help!

Java programming topics
Post Reply
User avatar
jishan
Posts: 2
Joined: Sat Apr 17, 2010 8:00 am

JCreator Problem. Please help!

Post by jishan » Sat Apr 17, 2010 8:04 am

Im suppose to make this program for my class. My friend gave me his project and told me to make it look different and submit it to the professor. Here is the project He did on jcreator. is there ANYWAY you can make it look different but function the same thing? it will really help i have 2 more days to submit it.

Code: Select all

import javax.swing.JOptionPane;
public class convert
  {
  public static void main(String []args)
  {
    boolean loop = true;
    double meters;
    String choice;
    try {meters = Double.parseDouble(JOptionPane.showInputDialog("Enter a distance in meters"));}
    catch (Exception e) {meters = 500;} //try and catch is an exception that your prof will show you later.
    while (loop == true)
    {
      choice = menu();
      if (choice.equals("1"))
        showInches(meters);
      else if (choice.equals("2"))
        showFeet(meters);
      else if (choice.equals("3"))
        showKilometers(meters);
      else if (choice.equals("4"))
        loop = false;
      else
        JOptionPane.showMessageDialog(null, "Invalid choice!!!");
    }
   }
 //--------------------------------------------------------------------
   public static void showKilometers (double meters)
   {
     double kilometers;
     kilometers = meters * 0.001;
     System.out.println(meters + " are equal to " + kilometers);
   }
//--------------------------------------------------------------------
   public static void showInches (double meters)
   {
     double inches;
     inches = meters * 39.37;
     System.out.println(meters + " are equal to " + inches);
   }
//--------------------------------------------------------------------
   public static void showFeet (double meters)
   {
     double feet;
     feet = meters * 3.281;
     System.out.println(meters + " are equal to " + feet);
   }
//--------------------------------------------------------------------
   public static String menu()
   {
     String input ;
     input = JOptionPane.showInputDialog("1. Convert to inches \n2. Convert to feet \n3. Convert to kilometers \n4. End \nEnter Choice: ");
     return input;
   }
//--------------------------------------------------------------------
 }
Last edited by Neo on Sat Apr 17, 2010 2:21 pm, edited 1 time in total.
Reason: placed code within CODE blocks
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: JCreator Problem. Please help!

Post by Neo » Sat Apr 17, 2010 6:54 pm

Welcome to ROBOT.LK Jishan !!!!
Go to profile (in User Control Panel (see top left corner)) and update information like country, etc...

I must tell you that it is not a not a good thing to get your assignments done by someone else like this. One day, you'll have to face interviews and write programs yourself.

Since you have been introduced here by one of the very important sources to ROBOT.LK, I'll help you this time. It will take me few minutes to do this (I need to download JDK to my laptop as well). However I'll let you have it today. Make sure you go through the code and ask me any doubtful point you get on this.

In return, I expect you to introduce ROBOT.LK to all your colleagues and friends and ask them to join with us to share knowledge in technology.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: JCreator Problem. Please help!

Post by Neo » Sat Apr 17, 2010 8:34 pm

Save following code to MeasurementConversion.java

Code: Select all


import java.io.*;


public class MeasurementConversion
{

	void showKilometers (double distance_in_m)
	{
		double distance_in_km = distance_in_m * 0.001;
		System.out.println(distance_in_m + " meters is " + distance_in_km + " kilometers\n\n");
	}
	
	
	void showInches (double distance_in_m)
	{
		double distance_in_inches = distance_in_m * 39.37;
		System.out.println(distance_in_m + " meters is " + distance_in_inches + " inches\n\n");
	}
	
	
	void showFeet (double distance_in_m)
	{
		double distance_in_feets = distance_in_m * 3.281;
		System.out.println(distance_in_m + " meters is " + distance_in_feets + " feets\n\n");
	}
	
	
	int menu()
	{
		int menu_input = 0;
		boolean valid;
		BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("\n1. Convert to inches\n2. Convert to feet\n3. Convert to kilometers\n4. End\n");
		
		// take a valid input from menu
		do {
			try {
				System.out.println("\nEnter Choice: ");
				menu_input = Integer.parseInt(buff.readLine());
	
				if (menu_input <= 0 || menu_input > 4){
					System.out.println("\nInvalid entry (Type between 1 to 4)");
					valid = false;
				}
				else{
					valid = true;
				}
			}
			catch (Exception e) {
				System.out.println("\nInvalid entry");
				valid = false;
			}
		} while (!valid);
		
		return menu_input;
	}
	
	
	
	
	
	public static void main(String []args)
	{
		boolean loop = true;
		double distance_in_m = 0;
		boolean valid, cont = true;
		BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
		MeasurementConversion myCls = new MeasurementConversion();
		
		
		// loop until we get a valid input to distance then parse it as a double
		do {
			try {
				System.out.println("\nEnter a distance in meters: ");
				distance_in_m = Double.parseDouble(buff.readLine());
				if (distance_in_m >= 0){
					valid = true;
				}
				else{
					System.out.println("\nDistance can't be negative");
					valid = false; // negative inputs are invalid
				}
			}
			catch (Exception e) {
				System.out.println("\nInvalid Entry");
				valid = false;
			}
		} while (!valid);
		
		
		
		do {
		
			switch (myCls.menu()){ // take menu input
				case 1:
					myCls.showInches(distance_in_m);
					break;
				case 2:
					myCls.showFeet(distance_in_m);
					break;
				case 3:
					myCls.showKilometers(distance_in_m);
					break;
				case 4:
					cont = false;	// exit from loop
					break;
			}
		} while (cont);
	}
}
User avatar
jishan
Posts: 2
Joined: Sat Apr 17, 2010 8:00 am

Re: JCreator Problem. Please help!

Post by jishan » Sat Apr 17, 2010 10:50 pm

its giving me 1 error =\

--------------------Configuration: <Default>--------------------
C:\Users\Jishan\Documents\Distance.java:4: class MeasurementConversion is public, should be declared in a file named MeasurementConversion.java
public class MeasurementConversion
^
1 error

Process completed.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: JCreator Problem. Please help!

Post by Neo » Sat Apr 17, 2010 11:46 pm

Save the file to MeasurementConversion.java and give it a try.
Post Reply

Return to “Java Programming”