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

[تنبيه - عنوان غير مناسب : ]شرح كود جافا

بدأه abdalh22 في 24 أكتوبر 2011 · 1 رد · 369 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

اخواني فزعتكم اريد احد يشرح لي الكود خطوه بخطوه. الله يرحم والديكم

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package infsys3816exam1;

interface Convertible{

public Object convertTo(Object o);

@Override

public String toString();

} // end of interface Convertible

abstract class Currency{

private double Value;

private String Symbol;

public Currency(String s, double v){

Symbol = s;

Value = v;

}

public String getSymbol(){

return Symbol;

}

protected double getValue(){

return Value;

}

protected void setValue(double v){

Value=v;

}

@Override

public abstract String toString();

} // end of class Currency

class Dollar extends Currency implements Convertible{

public Dollar(){

this(0.0);

}

public Dollar(double d){

super("$", d);

}

@Override

public String toString(){

String FormattedValue = String.format("%,9.2f", getValue());

return(getSymbol() + FormattedValue);

}

@Override

public Object convertTo(Object o) {

Currency C=null;

if (o instanceof Convertible){

if (o instanceof Euro){

C=(Euro) o;

C.setValue(getValue()/1.5);

}

else{ // assume o is Dollar

C = (Dollar) o;

C.setValue(getValue());

}

}

return C;

}

} // end of class Dollar

class Euro extends Currency implements Convertible{

public Euro(){

this(0.0);

}

public Euro(double d){

super("\u20ac", d);

}

@Override

public String toString(){

String FormattedValue = String.format("%,9.2f", getValue());

return(getSymbol() + FormattedValue);

}

@Override

public Object convertTo(Object o) {

Currency C=null;

if (o instanceof Convertible){

if (o instanceof Dollar){

C=(Dollar) o;

C.setValue(getValue()*1.5);

}

else{ // assume o is Euro

C = (Euro) o;

C.setValue(getValue());

}

}

return C;

}

}// end of class Euro

public class ConversionDriver {

/**

* @param args the command line arguments

*/

public static void main(String[] args){

Convertible L = new Dollar(10000);

Object C = L.convertTo(new Euro()); // convert Dollar to Euro

System.out.println(L + " ==> " + (C!=null? C : "Error: Object is not a valid currency"));

System.out.println();

L = new Euro(10000);

C = L.convertTo(new Dollar()); // convert Euro to Dollar

System.out.println(L + " ==> " + (C!=null? C : "Error: Object is not a valid currency"));

System.out.println();

L=new Euro(1000);

C = L.convertTo(new String());// convert Euro to String, will return null object

System.out.println(L + " ==> " + (C!=null? C : "Error: Object is not a valid currency") );

}

}// end of class CurrencyDriver

#2

الأخ الكريم/الأخت الكريمة

السلام عليكم ورحمة الله وبركاته

مرحباً بكم في منتدى الفريق العربي للبرمجة

نود تنبيهك أن العنوان غير مناسب.

الرجاء اختيار عنوان مناسب أكثر في مواضيعك القادمة، وهذا بمثابة إشعار للأهمية.

وفي حال التكرار سيتعرض موضوعك للإغلاق والحذف!!!

قواعد المشاركة

/index.php?showtopic=29343

شاكرين لكم حُسن تعاونكم

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

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

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

728x90.png

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…