Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 6.3, Problem 30STQ

Explanation of Solution

Program:

File name: “OutputFormat.java”

//Define the class "OutputFormat"

public class OutputFormat

{

    //Define the method "write()"

private static void write(double number, int digitsAfterPoint)

    {

        //Check the condition

        if (number >= 0)

/*True, call the method "writePositive()"*/

            writePositive(number, digitsAfterPoint);

        //Otherwise

        else

        {

            //Assign the value

            double positiveNumber = -number;

            //Print statement

            System.out.print('-');

            //Call the method "writePositive()"

writePositive(positiveNumber, digitsAfterPoint);

        }

    }

    //Define the method "writePositive()"

    private static void writePositive(double number,

    int digitsAfterPoint)

    {

/*Call the method "Math.pow()" to move a decimal point*/

int mover = (int)(Math.pow(10, digitsAfterPoint));

        //Declare the variable

        int allWhole;

/*Call the method "Math.round()" to round the decimal point */

        allWhole = (int)(Math.round(number*mover));

        //Calculate the value of before decimal point

        int beforePoint = allWhole/mover;

        //Calculate the value of after decimal point

        int afterPoint = allWhole%mover;

        //Print the value of before point

        System.out.print(beforePoint);

        //Print the dot

        System.out.print('.');

        //Call the method "writeFraction()"

        writeFraction(afterPoint, digitsAfterPoint);

    }

    //Define the method "writeFraction()"

    private static void writeFraction(int afterPoint,

     int digitsAfterPoint)

    {

        //Variable initialization

        int n = 1;

        //Check the condition

        while (n < digitsAfterPoint)

        {

            //Check the condition

            if (afterPoint < Math...

Blurred answer
Students have asked these similar questions
I need to write a jave program to keep track of a team sport. Write a class, Team.java, that has the following: * two private instance variables: a String named color and an int named score. * a default constructor with no parameters that initializes color to "Red" and score to zero. * a constructor that takes two parameters (one for each instance variable) and sets the values of the instance variables. * a public getter and setter method for each instance variable. * a toString method that returns the color and score in the following format: "Red Team with 0 points."
You are given a Conversion class and need to complete it by adding two static methods. The first method should be called gallonsToPints and take a double gallons and return the number of pints as a double (1 gallon = 8 pints). The second method should be called feetToInches, also taking a double number of feet and returning the number of inches as a double (1 foot = 12 inches). Test your program in the ConversionTester class by printing out two conversions.   In java
Write a program that calls methods for each of the following mini problems from its main method. Us a single Scanner for the program declared in the class asprivate Static Scanner keyboard = new Scanner(System.in); Write a method that reads a line of text from the keyboard and then displays the line, but with the first occurrence of hate changed to love. For example, a possible sample dialogue might beEnter a line of text.I hate you.I have rephrased that line to read:I love you.You can assume that the word hate occurs in the input. If the word hate occurs more than once in the line, your program will replace only its first occurrence. Write a method that asks the user to enter a favorite color, a favorite food, a favorite animal, and the first name of a friend or relative. The program should then print the following two lines, with the user’s input replacing the items in italics: I had a dream that Name ate a Color Animal and said it tasted like Food!For example, if the user entered…

Chapter 6 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 6.2 - Can you reference an instance variable by name...Ch. 6.2 - Is the following valid, given the class...Ch. 6.2 - Prob. 13STQCh. 6.2 - Prob. 14STQCh. 6.2 - Prob. 15STQCh. 6.2 - Is the following valid, given the class...Ch. 6.2 - What values are returned by each of the following?...Ch. 6.2 - Suppose that speed is a variable of type double...Ch. 6.2 - Repeat the previous question, but instead assign...Ch. 6.2 - Suppose that nl is of type int and n2 is of type...Ch. 6.2 - Define a class CircleCalculator that hat only two...Ch. 6.2 - Which of the following statements are legal?...Ch. 6.2 - Write a Java expression to convert the number in...Ch. 6.2 - Consider the variable 5 of type String that...Ch. 6.2 - Repeat the previous question, but accommodate a...Ch. 6.2 - Write Java code to display the largest and...Ch. 6.3 - Prob. 27STQCh. 6.3 - Consider the variable allCents in the method...Ch. 6.3 - What is wrong with a program that starts as...Ch. 6.3 - Prob. 30STQCh. 6.3 - In your definition of the class OutputFormat. In...Ch. 6.4 - Prob. 32STQCh. 6.4 - Prob. 33STQCh. 6.4 - Prob. 34STQCh. 6.4 - Consider the class Species in Listing 5.19 of...Ch. 6.4 - Repeat the previous question for a method...Ch. 6.4 - Still considering the class Species in Listing...Ch. 6.4 - Rewrite the method add in Listing 6.16 so that it...Ch. 6.4 - In Listing 6.16, the set method that has a String...Ch. 6.5 - Give the definitions of three accessor methods...Ch. 6.6 - If cardSuit is an instance of Suit and is assigned...Ch. 6.7 - Suppose you want to use classes in the package...Ch. 6.7 - Prob. 43STQCh. 6.7 - Can a package have any name you might want, or are...Ch. 6.7 - On your system, place the class Pet (Listing 6.1)...Ch. 6.8 - The previous section showed you how to change the...Ch. 6 - Prob. 1ECh. 6 - Prob. 2ECh. 6 - Write a default constructor and a second...Ch. 6 - Write a constructor for the class...Ch. 6 - Consider a class characteristic that will be used...Ch. 6 - Create a class RoomOccupancy that can be used to...Ch. 6 - Write a program that tests the class RoomOccupancy...Ch. 6 - Sometimes we would like a class that has just a...Ch. 6 - Create a program that tests the class Merlin...Ch. 6 - In the previous chapter, Self-Test Question 16...Ch. 6 - Create a class Android whose objects have unique...Ch. 6 - Prob. 12ECh. 6 - Modify the definition of the class Species in...Ch. 6 - Prob. 2PCh. 6 - Using the class Pet from Listing 6.1, write a...Ch. 6 - Do Practice Program 4 from Chapter 5 except define...Ch. 6 - The following class displays a disclaimer every...Ch. 6 - Do Practice Program 5 from Chapter 5 but add a...Ch. 6 - We can improve the Beer class from the previous...Ch. 6 - Define a utility class for displaying values of...Ch. 6 - Write a new class TruncatedDollarFormat that is...Ch. 6 - Complete and fully test the class Time that...Ch. 6 - Complete and fully test the class Characteristic...Ch. 6 - Write a Java enumeration LetterGrade that...Ch. 6 - Complete and fully test the class Per n that...Ch. 6 - Write a Temperature class that represents...Ch. 6 - Repeat Programming Project 8 of the previous...Ch. 6 - Write and fully test a class that represents...Ch. 6 - Write a program that will record the votes for one...Ch. 6 - Repeat Programming Project 10 from Chapter 5, but...Ch. 6 - Create a JavaFX application that displays a button...
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,