هذا السؤال عشان ماتخربطون
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: