Question

this is for java class Define a utility class for displaying values of type double. Call...

this is for java class

Define a utility class for displaying values of type double. Call the class DoubleOut. Include all the methods from the class DollarFormat in Listing 6.14, all the methods 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 one nonzero digit before the decimal point—unless the number is exactly zero. The method scienceWrite will not advance to the next line. Also add a method called scienceWriteln 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 the text (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 and test 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 super driver program that takes a double value as input and then displays it using the two writeln methods and the scienceWriteln method. Use the number 5 for the number of digits 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 numbers of type double until the user is ready to end the program.

Listing 6.14 The Corrected Class DollarFormat
public class DollarFormat
{
/**
Displays amount in dollars and cents notation.
Rounds after two decimal places.
Does not advance to the next line after output.
*/
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);
}
}
//Precondition: amount >= 0;
//Displays amount in dollars and cents notation. Rounds
//after two decimal places. Omits the dollar sign.
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);
}
/**
Displays amount in dollars and cents notation.
Rounds after two decimal places.
Advances to the next line after output.
*/
public static void writeln(double amount)
{
write(amount);
System.out.println();
}
}

are there supposed to be different java files? how are they supposed to look?

thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class DollarFormat {
  
/**
Displays amount in dollars and cents notation.
Rounds after two decimal places.
Does not advance to the next line after output.
*/
public static void write(double amount)
{
if(amount >= 0)
{
System.out.print('$');
writePositive(amount);
}
else
{
double positiveAmount = (amount * -1);
System.out.print('$');   
System.out.print('-');   
writePositive(positiveAmount);
}
}
  
//Precondition: amount >= 0;
//Displays amount in dollars and cents notation. Rounds
//after two decimal places. Omits the dollar sign.
public 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);
}
  
/**
Displays amount in dollars and cents notation.
Rounds after two decimal places.
Advances to the next line after output.
*/
public static void writeln(double amount)
{
write(amount);
System.out.println();
}
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
char yesNo;
  
do
{
System.out.print("Please enter an amount: $");
double amount = Double.parseDouble(sc.nextLine().trim());
System.out.print("Result: ");
writeln(amount);
  
System.out.print("Continue? [Y/N]: ");
yesNo = sc.nextLine().charAt(0);
System.out.println();
if(yesNo == 'N' || yesNo == 'n')
{
System.out.println("Thank you..Goodbye!\n");
System.exit(0);
}
}while(yesNo != 'N' || yesNo != 'n');
}
}

************************************************************* SCREENSHOT **************************************************************

Add a comment
Know the answer?
Add Answer to:
this is for java class Define a utility class for displaying values of type double. Call...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • CIT-111 Homework #5, Part A                                    Chapter 5, part 1

    CIT-111 Homework #5, Part A                                    Chapter 5, part 1 and alternate part 2 (page 341) A breakdown of the problem is as follows: Class name:                        Money Instance variables:           private, integer:                dollars, cents Constructor methods:    Money () – sets dollars and cents both to zero                                                 Money (int bucks) – sets dollars to bucks, cents to zero                                                 Money (int buck, int pennies) – sets dollars to bucks, cents to pennies Accessor methods:          getDollar () – returns value of dollars for...

  • Implement a Java class that is called RainFall that uses a double array to store the...

    Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...

  • Java please answer A to I please dont type the answer on paper please use the...

    Java please answer A to I please dont type the answer on paper please use the computer A.   Explain why alpha cannot be accessed by other members of its class. B.   In the program, how can you determine the type of access modifier? C.   Describe the differences and similarities of beta and gamma in program, within the context of access specifiers and class member accessibility. D.   Explain how objects, a and b, are passed to the method. E.    Why...

  • Assume the only console output method available is display(c), as shown below, which you include in...

    Assume the only console output method available is display(c), as shown below, which you include in your solution. This means you are not permitted to use System.out.print(), System.out.println(), nor System.out.printf() in your methods. public static void display (charc) { System.out.print (c); Implement the following methods in a program class, that has a main() method that simply tests each. Apply the concept of overloading, where best, and that the methods can call other new methods to reduce repeating the same code....

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

  • Question B1 You are given the following Java classes: public class Queue { private static class...

    Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...

  • This is the assignment..... Write a class DataSet that stores a number of values of type...

    This is the assignment..... Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a method public void addValue(double value) that add a value provided there is still room. Provide methods to compute the sum, average, maximum and minimum value. ​This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE;​ are there so I don't need to create...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT