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

ماني فاهمه ليش فيه غلط بكود الmain

بدأه *en* في 25 أكتوبر 2010 · 4 رد · 521 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هذا السؤال عشان ماتخربطون

Q3) Write a class MyMath that includes two instance variables: x (type int) and n (type int). The

variable x must be a positive number. If x has a value less than 0 the class should set it to 0. In

addition to the constructor, the class should have the following public methods:

• long factorial() : returns the factorial x! (x! = x(x-1)(x-2)(x-3)……..(2)(1))

• double exp(double x, long n) : returns the e x , where:

2 3 4

1 ...

1! 2! 3! 4! !

n

x x x x x x

e

n

= + + + + +

, if n is less than 10 then the method should set it to 10

• void setX(int x) : sets the value of x

• int getX() : returns the value of x

Write a test application to demonstrate the class MyMath. The test application must ask the user for

an integer number and use the methods of MyMath to calculate the factorial and Exponential. Then

the test application should display the results in the command window

public class MyMath {



		private int x;

		private int n;

		public   MyMath( int x1, int n1){

		if( x > 0 )

		x = x1;

		else x = 0;
		}

		public long fact(int x1){

		if ( x1 == 0 || x1 == 1)

		return 1;

		return x1 * fact(x1-1);

		}
        public double exp(double x1, long n){


        	double e = 0;


        	if ( n < 10){

        		double sum = 0;

        		for (int i = 1; i <= 10 ;i++){

        		double z =	Math.pow(x1,i);



        		sum += (z /fact(i)) ;	

        		}


			 e =   1 + (sum)	;

        	}
        	return e;

        		}






		public void setX(int x1){
 			if (x > 0)
 				x= x1;

		}

		public int getX(){
			return x;
		}



		}

الاول مافيه أغلاط الثاني اهو اللي فيه جم غلط ماني عارفه لييييش !!

import java.util.Scanner;


public class MyMathtest {
	public static void main(String[] args){


	int x_;
	int n_;


	Scanner input = new Scanner(System.in);




	System.out.println("enter the value of x :") ;

	x_= input.nextInt();

	System.out.println("enter the value of y :") ;

	n_= input.nextInt();

	MyMath m = new MyMath( x_  , n_ );


	m.fact(x_);

	m.exp(x_, n_ );



	System.out.printf("the factorial: %l " ,((m.fact(x_) );

	System.out.printf("Exponential:  %.3f",(m.exp(x_ , n_ ) );


	}
}

احااول اعرف السبب ماني قادره أفهمه !؟

:wacko:

#2

تفضلى

import java.util.Scanner;

public class MyMathtest {

    public static void main(String[] args) {
        int x_;
        int n_;
        Scanner input = new Scanner(System.in);
        System.out.println("enter the value of x :");
        x_ = input.nextInt();
        System.out.println("enter the value of y :");
        n_ = input.nextInt();
        MyMath m = new MyMath(x_, n_);
        m.fact(x_);

        m.exp(x_, n_);
        System.out.printf("the factorial: %d ", ((m.fact(x_))));
        System.out.printf("Exponential:  %.3f", (m.exp(x_, n_)));
    }
}

الخطا فى ها السطر البرمجى بيكون اقواس وحرف الـ l مكانه بيكون d

        System.out.printf("the factorial: %d ", ((m.fact(x_))));

وهنيت قوس

        System.out.printf("Exponential:  %.3f", (m.exp(x_, n_)));

public class MyMath {

    private int x;
    private int n;

    public MyMath(int x1, int n1) {
        if (x > 0) {
            x = x1;
        } else {
            x = 0;
        }
    }

    public long fact(int x1) {
        if (x1 == 0 || x1 == 1) {
            return 1;
        }
        return x1 * fact(x1 - 1);
    }

    public double exp(double x1, long n) {
        double e = 0;
        if (n < 10) {
            double sum = 0;
            for (int i = 1; i <= 10; i++) {
                double z = Math.pow(x1, i);
                sum += (z / fact(i));
            }
            e = 1 + (sum);
        }
        return e;
    }

    public void setX(int x1) {
        if (x > 0) {
            x = x1;
        }
    }

    public int getX() {
        return x;
    }
}

تم تعديل هذه المشاركة بواسطة Java2010 في 25 أكتوبر 2010 في 20:48

#3

خطأ غبي !!

ماتوقعت عبالي خطأ كبييييييييييييير

=(

يزاك الله خير أخي الكريم جافا2010 على مساعدتك

ربي يوفقك ويرزقك كل خير يااااارب

#4

تفضلي الكود بعد الاختصار والتصحيح ..


import java.util.Scanner;

public class MyMathtest {

public static void main(String[] args) {

int x_;
int n_;

Scanner input = new Scanner(System.in);

System.out.println("enter the value of x :");

x_ = input.nextInt();

System.out.println("enter the value of y :");

n_ = input.nextInt();

MyMath m = new MyMath(x_, n_);

System.out.printf("the factorial: %d \n", ((m.fact(x_))));
System.out.printf("Exponential: %.3f\n", (m.exp(x_, n_)));
}
}


تحياتي

ملحوظة : لم انتبه لرد أخي جافا 2010 الا بعد اضافة الرد

تم تعديل هذه المشاركة بواسطة ToBeOrNotToBe في 25 أكتوبر 2010 في 21:01

#5

جزااكم الله خير على مسااعدنكم الطيبه

^_^

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