(Java program問題) 請救我(4)
Design a class named MyInteger. The class contains:
An int data field named value that stores int value represented by this object
A constructor that creates a MyInteger object for the specified int value
A get method that returns the int value
Methods isEven(), isOdd(), and isPrime() that return ture if value is even, odd, or prime
Static methods isEven(int), isOdd(int), and isPrime(int) that returns true if the specified value is even, odd, or
prime
Static methods isEven(Myinteger), isOdd(Myinteger), and isPrime(Myinteger) that returns true if the specified value
is even, odd, or prime
Methods equals(int) and equals(MyInteger) that return true if the value in the object is equalto the specified
value
A static method parseInt (int) that converts a string to an int value
回答 (3)
✔ 最佳答案
ur question is so long and cannot write the code within 3000 words. can u separate it to small questions?
2008-03-11 10:11:09 補充:
have sent the code file to you. pls chk.
/* sorry a little later than the previous, but hope this helps */
import java.lang.Math.*;
public class MyInteger
{
private int value;
public MyInteger(int ival){set(ival);}
private void set(int ival){value=ival;}
public int get(){return value;}
public boolean isEven(){return get()%2==0;}
public boolean isOdd(){return get()%2==1;}
public boolean isPrime()
{
int v=get();
if(v==1)return false;// 1 is not considered a prime
if(this.isEven())if(v>2)return false; else return true;
if(v%3==0)if(v>3)return false; else return true;
int maxFactor=(int)java.lang.Math.sqrt((double)v);
for(int i=5;i<=maxFactor;i+=6)if(v%i==0)return false;
for(int i=7;i<=maxFactor;i+=6)if(v%i==0)return false;
return true;
}
public boolean equals(int ival){return (ival==this.get())?true:false;}
public boolean equals(MyInteger iObj){return (iObj.get()==this.get())?true:false;}
public static boolean isEven(MyInteger iObj){return iObj.isEven();}
public static boolean isOdd(MyInteger iObj){return iObj.isOdd();}
public static boolean isPrime(MyInteger iObj){return iObj.isPrime();}
public static int parseInt(String s){return (int)Integer.parseInt(s);}
public static void main(String[] args)
{
MyInteger k=new MyInteger(1013);
System.out.println(""+k.get()+"="+((k.isEven())?"is even":"is not even"));
System.out.println(""+k.get()+"="+((k.isOdd())?"is odd":"is not odd"));
System.out.println(""+k.get()+"="+((k.isPrime())?"is prime":"is not prime"));
System.out.println(""+k.get()+"="+((k.equals(1013))?"eq 1013":"<>1013"));
}
}
my email is
[email protected]
you can send it to my email
but the question is a single question~
i will give you best answer as you send it to me right away~
you can write you name CW Wan to prove the answer is given by you
收錄日期: 2021-04-13 15:15:45
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080310000051KK00012
檢視 Wayback Machine 備份