Specification Copy the program Pi.java to your computer and do the following. Im
ID: 3724069 • Letter: S
Question
Specification
Copy the program Pi.java to your computer and do the following.
Implement the abstract methods defined in Oracle.com::class Number.
Implement the public PI() constructor method following the instructions given in the method's comment block.
Implement the public double tauValue() method following the instructions given in the method's comment block.
Modify the toString() method following the instructions given in the method's comment block.
Answer the three questions given in the program's file comment block.
The output of your program must match the following.
Explanation / Answer
SourceCode:
public class Pi extends Number {
private final static double PI = 3.14159265358979323846264338327950;
private final int intValue; // PI rounded down to nearest int
private final long longValue; // PI rounded up to nearest int
private final double floatValue; // PI rounded to nearest hundredth
private final double doubleValue; // PI rounded to nearest millionth
private final double tauValue; // two Pi
/*
* TBI (To Be Implemented)...
*
* The constructor assigns values to all of the instance
* variables defined above. The values assigned to the instance
* variables are documented using comments when the instance
* variables are defined (see above).
*/
public Pi() {
// implementation note: The expressions on the right side of
// the first four assignment statements must use PI in them.
// In addition, you are prohibited from using class Math methods.
intValue = (int)PI;
longValue = (long)PI;
floatValue = (float)PI;
doubleValue = PI;
tauValue = 2*doubleValue; // tauValue is a multiple of doubleValue
}
/*
* Creates a String representation of this Pi object.
*
* @return a String representation of this Pi object
*
* TBI (To Be Implemented)...
*
* Rewrite the toString() method so that a StringBuffer object
* is used to create the string respresentation of this Pi object.
*/
public String toString() {
return "byteValue(): " + byteValue() +
" shortValue(): " + shortValue() +
" intValue(): " + intValue() +
" longValue(): " + longValue() +
" floatValue(): " + floatValue() +
" doubleValue(): " + doubleValue() +
" tauValue(): " + tauValue();
}
/*
* TBI (To Be Implemented)...
*
* Implement the abstract methods defined in class Number.
*/
/*
* TBI (To Be Implemented)...
*
* Implement the tauValue() getter method.
*
* @return the tauValue instance variable
*/
double tauValue() {
return 2*doubleValue();
}
/*
* The main() method is used to test class Pi.
* You are prohibited from modifying main().
*/
public static void main(String[] argv) {
System.out.println(new Pi());
System.out.println(new Pi() == new Pi());
}
@Override
public int intValue() {
// TODO Auto-generated method stub
return (int)PI;
}
@Override
public long longValue() {
// TODO Auto-generated method stub
return (long)PI;
}
@Override
public float floatValue() {
// TODO Auto-generated method stub
return (float)PI;
}
@Override
public double doubleValue() {
// TODO Auto-generated method stub
return PI;
}
}
/*
* the output of your program must match the following
*
byteValue(): 3
shortValue(): 3
intValue(): 3
longValue(): 4
floatValue(): 3.14
doubleValue(): 3.14159
tauValue(): 6.28318
false
*
*/
/*
Sample run:
byteValue(): 3
shortValue(): 3
intValue(): 3
longValue(): 3
floatValue(): 3.1415927
doubleValue(): 3.141592653589793
tauValue(): 6.283185307179586
false
*/
//Hi, if you have any quiries please comment in here, and do rate the solution if it is helpful to you...Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.