الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Convertion Tool In Java

بدأه areej ahmad في 3 أبريل 2008 · 6 رد · 1,150 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

مرحبا ,, كيفكم ؟

انا عندي برنامج يحول جميع القياسات بس في مشكلة عندي في METHOD RANGE

و هذا السؤال

? A method that Converts from Celsius to Fahrenheit

public static double celsiusToFahrenheit (double celsius)

?

A method that converts from Fahrenheit to Celsius

public static double fahrenheitToCelsius(double fahrenheit)

?

A method that converts from Feet to Meters

public static double feetToMeter (double feet)

?

A method that converts from Meter To Feet

public static double meterToFeet(double meter)

?

A method that converts from Kilogram to Pound

public static double kgToPound (double Kilogram)

?

A method that converts from Pound to Kilogram

public static double poundToKg(double pound)

?

A method that converts milliseconds to hours, minutes and seconds. Look at exercise 5.25

public static String convertMillisToTime(long millis)

?

A method that converts hours, minutes and seconds to milliseconds

public static long convertTimetoMillis(int hours, int minutes, int seconds)

?

هنا المشكلة في ال convert range method

A method that Convert range. This method converts from one measure to another using a range of values. The method accepts four inputs, startingValue, endingValue, incrementStep, conversionMethodName. The method prints a conversion table as explained in the following example. Let's assume that we invoke the method with the parameters

هنا لازم على اليوزر انه يظهر هذه القائمة و ما يعادلها في الفهرنهايت

(31, 40, 1, "celsiusToFahrenheit" ) , then the method will print the following table.

Celsius Fahrenheit

40 105.0

39 102.2

.... .......

32 89.6

31 87.8

Hint: Use overloaded method to call the proper conversion method. **/



  1. ?

getUserChoise method. This method displays the following menu to the user and accepts his/her choice:

Conversion Menu

1

1 Convert from Celsius to Fahrenheit

2

2 Convert from Fahrenheit to Celsius

3

3 Convert from Feet to Meters

4

4 Convert from Meter To Feet

5

5 Converts from Kilogram to Pound

6

6 Converts from Pound to Kilogram

7

7 Convert milliseconds to hours, minutes and seconds.

8

8 Convert hours, minutes and seconds to milliseconds

9

9 Convert range.

10

10 Quit

**/

  1. ?

The main method will be running in a infinite loop invoking getUserChoise method. Then the method accepts the proper input and invokes the proper method according to user choice. When user choice is Quit then the program will terminate. In case of Convert range choice a sub menu is displayed to ask the user to choose the conversion method.

و هذا كودي اللي اشتغلت عليه

public class conversionTool{


/**
* Main routine runs a loop that shows the menu, get the user's
* choice, and responds.
*/

public static void main(String[] args) {

int menuChoice; // The number of the option that the user

// selects from the menu.

double celsius; // The Celsius temperature measurement.

double fahrenheit; // The Fahrenheit temperature measurement.

double feet; // The Feet tempreture measurement.

double meter; // The Meter tempreture measurement . 

double Kilogram; // The Meter tempreture measurement.

double pound; // The Meter tempreture measurement.


while (true) {

System.out.println();

showMenu();

System.out.print("Enter the number your choice (1, 2, or 3): ");

menuChoice = TextIO.getlnInt();

if (menuChoice == 1) {

// Convert Fahrenheit to Celsius.

System.out.print("\nEnter temperature in degrees Fahrenheit: ");

fahrenheit = TextIO.getlnDouble();

celsius = fahrenheitToCelsius(fahrenheit);

System.out.println(fahrenheit + " degrees Fahrenheit is "

+ celsius + " degrees Celsius");

}

else if (menuChoice == 2) {

// Convert Celsius to Fahrenheit.

System.out.print("\nEnter temperature in degrees Celsius: ");

celsius = TextIO.getlnDouble();

fahrenheit = celsiusToFahrenheit(celsius);

System.out.println(celsius + " degrees Celsius is "

+ fahrenheit + " degrees Fahrenheit");

}

else if (menuChoice == 3) {

// Convert Feet to Meters.

System.out.print("\nEnter lenght in Feet: ");

feet = TextIO.getlnDouble();

meter = feetToMeter(feet);

System.out.println(feet + " Feet length is " + meter + " Meter lenght");

}

else if (menuChoice == 4) {
// Convert Meters to Feet. 
System.out.print("\nEnter lenght in Meter: ");
meter = TextIO.getlnDouble();
feet = meterToFeet(meter);
System.out.println(meter + " meter length is " + feet + " feet length");

}

else if (menuChoice == 5) {
// Convert Kilogram to Pound. 
System.out.print("\nEnter Weight in kilogram: ");
Kilogram = TextIO.getlnDouble();
pound = kgToPound (Kilogram);
System.out.println(Kilogram + " weight in Kilogram " + pound + " weight in pound");

}


else if (menuChoice == 6) {
// Convert Pound to Kilogram. 
System.out.print("\nEnter Weight in kilogram: ");
pound = TextIO.getlnDouble();
Kilogram = kgToPound (pound);
System.out.println(pound + " weight in pound " + Kilogram + " weight in Kilogram");

}

else if (menuChoice == 7) {

// exit from while loop and end program

break; 
}
else {

System.out.println("Sorry, " + menuChoice + " is not a valid option.");

System.out.println("Please try again.");

System.out.println();
}

} // end while

System.out.println("\nOK. Exiting from program.\n");


} // end main()


public static double celsiusToFahrenheit(double celsius){

double fahrenheit;

fahrenheit = (celsius - 32)* (5.0/9.0);

return fahrenheit;

}

public static double fahrenheitTocelsius(double fahrenheit){

double celsius;

celsius = (celsius + 32)* (5.0/9.0);

return celsius;

}

public static double feetToMeter(double feet){

double feet;

meter = (feet * 0.3048);

return meter;

}

public static double meterToFeet(double meter){

double meter;

feet = (feet * 3.2808);

return feet;

}

public static double kgToPound (double Kilogram){

double Kilogram;

pound = (Kilogram * 2.2046);

return pound;

}

public static double poundTokg (double pound){

double pound;

Kilogram = (pound * 0.4536);

return Kilogram; 
}

سوري على عدم الايضاح ,, بس المشكلة في الكونفرت ميثود و القيت يوزر ميثود ما عرفت كيف اشتغل عليهم ,, اتمنى اني وضحته لكم

تم تعديل هذه المشاركة بواسطة areej ahmad في 4 أبريل 2008 في 11:49

#2

يعني يا أختي ساعدينا شوية

رتبي موضوعك

حطي كلامك في

تحياتي
حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3

فعلا لو مرتب كنت فهمت شي ... على ما اضن

وعن ايه قياسات تتكلمي التي سيحول اليها...

#4

السلام عليكم

أولا هناك بعض الأخطاء في توابع التحويل التي ذكرتها .. و هذا تصحيحها :

public static double celsiusToFahrenheit(double celsius) {		
	// To convert Celsius to Fahrenheit, multiply by 1.8 and add 32 degrees. 
	double fahrenheit = celsius * 1.8 + 32;
	return fahrenheit;
}

public static double fahrenheitTocelsius(double fahrenheit) {
	// To convert Fahrenheit to Celsius, subtract 32 degrees and divide by 1.8. 		
	double celsius = (fahrenheit - 32) / 1.8;
	return celsius;
}

public static double feetToMeter(double feet) {
	// 1 foot = 0.3048 meters		
	double meter = feet * 0.3048;
	return meter;
}

public static double meterToFeet(double meter) {
	// 1 meter = 3.2808399 feet
	double feet = meter * 3.2808;
	return feet;
}

public static double kgToPound(double Kilogram) {
	// 1 kilogram = 2.20462262 pounds
	double pound = Kilogram * 2.2046;
	return pound;
}

public static double poundTokg(double pound) {
	// 1 pound = 0.45359237 kilograms	
	double Kilogram = pound * 0.4536;
	return Kilogram;
}

ثانياً :

تابع التحويل المجالي range convert :

public static void convertRange(int startingValue, int endingValue, int incrementStep, String conversionMethodName){
	if(conversionMethodName.equalsIgnoreCase("celsiusToFahrenheit")){
		System.out.println("Celsius"+"\t"+"Fahrenheit");
		for (int step = startingValue; step <= endingValue; step += incrementStep) {
			System.out.println(step+"\t"+celsiusToFahrenheit(step));
		}
	} else if(conversionMethodName.equalsIgnoreCase("fahrenheitTocelsius")){
		System.out.println("Fahrenheit"+"\t"+"Celsius");
		for (int step = startingValue; step <= endingValue; step += incrementStep) {	
			System.out.println(step+"\t"+fahrenheitTocelsius(step));
		}
	} else if( ...){
		// .....
	}
}

ثالثاً :

http://www.uk-dave.com/bytes/java/time_long_to_string.html

أبو خالد السوري

#5

اشكرك اخي ابو خالد ,, و الله انك رجل شهم و ما قصرت و الله يبيض وجهك ,,

و لكن ماذا عن

(public static long convertTimetoMillis(int hours, int minutes, int seconds)

و getUserChoice method

وجزك الله خير

تم تعديل هذه المشاركة بواسطة areej ahmad في 5 أبريل 2008 في 19:05

#6

أهلاً بك أخت أريج ..

// A method that converts hours, minutes and seconds to milliseconds 
public static long convertTimetoMillis(int hours, int minutes, int seconds) {
	long millis = (seconds + minutes * 60 + hours * 3600) * 1000;
	return millis;
}

public static int getUserChoise() {
	String msg = "";
	msg += "\n1.	Convert from Celsius to Fahrenheit. ";
	msg += "\n2.	Convert from Fahrenheit to Celsius. ";
	msg += "\n3.	Convert from Feet to Meters. ";
	msg += "\n4.	Convert from Meter To Feet. ";
	msg += "\n5.	Converts from Kilogram to Pound. ";
	msg += "\n6.	Converts from Pound to Kilogram. ";
	msg += "\n7.	Convert milliseconds to hours, minutes and seconds.";
	msg += "\n8.	Convert hours, minutes and seconds to milliseconds. ";
	msg += "\n9.	Convert range. ";
	msg += "\n10.   Quit ";

	msg += "\n\nEnter your choice : \n";
	String str = JOptionPane.showInputDialog(msg);
	int n = Integer.parseInt(str);
	return n;
}

public static int subMenu() {
	String title = "Convert Range / sub menu : ";

	String msg = "";
	msg += "\n1.	Convert from Celsius to Fahrenheit. ";
	msg += "\n2.	Convert from Fahrenheit to Celsius. ";
	msg += "\n3.	Convert from Feet to Meters. ";
	msg += "\n4.	Convert from Meter To Feet. ";
	msg += "\n5.	Converts from Kilogram to Pound. ";
	msg += "\n6.	Converts from Pound to Kilogram. ";
	msg += "\n7.	Convert milliseconds to hours, minutes and seconds.";
	msg += "\n8.	Convert hours, minutes and seconds to milliseconds. ";		
	msg += "\n\nEnter your choice : \n";
	String str = JOptionPane.showInputDialog(null,msg,title,JOptionPane.QUESTION_MESSAGE);
	int n = Integer.parseInt(str);
	return n;
}

public static void main(String[] args) {	

	while (true) {
		// The number of the option that the user selects from the menu.
		int menuChoice = getUserChoise();
		switch (menuChoice) {
		case 1:
			// Convert from Celsius to Fahrenheit.
			String title = "Convert from Celsius to Fahrenheit";
			String msg = "Enter temperature in Celsius : ";
			String str = JOptionPane.showInputDialog(null, msg, title, JOptionPane.QUESTION_MESSAGE);
			double celsius = Double.parseDouble(str); 
			double fahrenheit = ConversionTool.celsiusToFahrenheit(celsius);
			JOptionPane.showMessageDialog(null, celsius + " Celsius = " + fahrenheit + " Fahrenheit "); 
			break;
		case 2:
			// Convert from Fahrenheit to Celsius.
			...
			break;

		...
		...

		case 9:
			// Range Convert.
			title = "Range Convert";

			String start = JOptionPane.showInputDialog("Enter start value : ");
			String end = JOptionPane.showInputDialog("Enter end value : ");
			String step = JOptionPane.showInputDialog("Enter increment step : ");

			int subMenuChoice = subMenu();
			String convertMethod = "";
			switch(subMenuChoice){
			case 1: 
				convertMethod = "celsiusToFahrenheit";
				break;
			case 2:
				convertMethod = "fahrenheitTocelsius";
				break;
			...		
			...

			}

			ConversionTool.convertRange(Integer.parseInt(start), Integer.parseInt(end), 
				Integer.parseInt(step), convertMethod);
			break;
		case 10:
			System.exit(0);
			break;

	}
}

أبو خالد السوري

#7

جزاك الله اخي الكريم ,, و جعله الله في ميزان اعمالك ,, هذا اللي استطيع ان اقوله

مواضيع مشابهة