Question

Define a utility class

Define a utility class for displaying values of type double. Call the class Double out. Include all the methods from the class dollarFormat in listing 6.14, all themethods from the class OutputFormat of Self-Test Question 30, and a method called scienceWrite that displays a value of type double using e notation, such as 2.13e-12.(This e notation is also called scientific notation, which explains the method name.) When displayed in e notation, the number should appear with exactly onenonzerodigit before the decimal point-unless the number is exactly zero. The method scienceWrite will not advance to the next line. Also add a method calledscienceWriteln that is the same as scienceWrite except that it does advance to the next line. All but the last two method definitions can simply be copied from thetext (or more easily from the source code for this book that is available on the Web.). Note that you will be overloading the method names write and writeln.

Write a driver program to test your method scienceWriteln. This driver program should use a stub for the method scienceWrite. (Note that this means you can write andtest scienceWriteln before you even write scienceWrite.) Then write a driver program to test the method scienceWrite. Finally, write a program that is a sort of superdriver program that takes double value as input and then displays it using the two writeln methods and the scienceWriteln method. Use the number 5 for the number ofdigits after the decimal point when you need to specify such a number. This super driver program should allow the user to repeat this testing with additional number sof type double until the user is ready to end the program.

//class DollarFormat
public class DollarFormat
{
public static void write(double amount)
{
if (amount >= 0)
{
System.out.print('$');
writePositive(amount);
}
else
{
double positiveAmount = -amount;
System.out.print('$');
System.out.print('-');
writePositive(positiveAmount);
}
}
private static void writePositive(double amount)
{
int allCents = (int)(Math.round(amount * 100));
int dollars = allCents / 100;
int cents = allCents % 100;
System.out.print(dollars);
System.out.print('.');

if (cents < 10)
System.out.print('0');
System.out.print(cents);
}
public static void writeln(double amount)
{
write(amount);
System.out.println( );
}
}

//class OutputFormat
public class OutputFormat
{
public static void write(double number, int digitsAfterPoint)
{
if (number >= 0)
writePositive(number, digitsAfterPoint);
else
{
double positiveNumber = -number;
System.out.print('-');
writePositive(positiveNumber, digitsAfterPoint);
}
}

public static void writeln(double number, int digitsAfterPoint)
{
write(number, digitsAfterPoint);
System.out.println( );
}

private static void writePositive(double number, int digitsAfterPoint)
{
int mover = (int)(Math.pow(10, digitsAfterPoint));
//1 followed by digitsAfterPoint zeros
int allWhole; //number with the decimal point
//moved digitsAfterPoint places
allWhole = (int)(Math.round(number * mover));
int beforePoint = allWhole / mover;
int afterPoint = allWhole % mover;
System.out.print(beforePoint);
System.out.print('.');
writeFraction(afterPoint, digitsAfterPoint);
}

private static void writeFraction(int afterPoint, int digitsAfterPoint)
{
int n = 1;
while (n < digitsAfterPoint)
{
if (afterPoint < Math.pow(10, n))
System.out.print('0');
n = n + 1;
}
System.out.print(afterPoint);
}

public static void main(String[] args)
{
double number = 123.456789;
System.out.println("Testing with the number 123.456789:");
writeln(number, 0);
writeln(number, 1);
writeln(number, 2);
writeln(number, 3);
writeln(number, 4);
writeln(number, 5);
writeln(number, 6);
writeln(number, 7);
}
}

Solutions

Expert Solution
No answers


Submit Your Answer