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...
Want to see the full answer?
Check out a sample textbook solutionChapter 6 Solutions
Java: An Introduction To Problem Solving And Programming Plus Mylab Programming With Pearson Etext -- Access Card Package (8th Edition)
- Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975. Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method three times. Output each floating-point value with two digits after the decimal point, which can be achieved as follows:System.out.printf("%.2f", yourValue); The output ends with a newline. Ex: If the input is: 20.0 3.1599 the output is: 1.58 7.90 63.20 Your program must define and call a method:public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon)arrow_forwardWrite 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 one-line sentence as input and then displays the following response: If the sentence ends with a question mark (?) and the input contains an even number of characters, display the word Yes. If the sentence ends with a question mark and the input contains an odd number of characters, display the word No. If the sentence ends with an exclamation point (!), display the word Wow. In all other cases, display the words You always say followed by the input string enclosed in quotes. Your output should all be on one line. Be sure to note that in the last case, your output must include quotation marks around the echoed input string. In all other cases, there are no quotes in the output. Your program does not have to check the input to see that the user…arrow_forwardWrite 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 two strings from the user’s keyboard. Display each string, along with its width, on two separate lines. Then create a new string by joining the two strings with a space between them. Display the new string and its length on a new separate line. 2. 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. 3. Write a method that asks the user to enter a favorite color, a favorite…arrow_forward
- I just want you to do the second part only using java class, but i have attached the answer for assignment 1. Assignment 1 question Loan Account Class: Create class LoanAccount. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable principal indicating the amount the person is borrowing. Provide method: public double calculateMonthlyPayment(int numberOfPayments) to calculate the monthly payment by using the following formula: double monthlyPayment = principal * ( monthlyInterest / (1 - Math.pow(1 + monthlyInterest, -numberOfPayments))); where monthly interest = annualInterestRate/12. Provide a static method setAnnualInterestRate that sets the annualInterestRate to a new value. Set the initial loan amount (Principal) for a new loan through the constructor. Write a program to test class LoanAccount. Instantiate two LoanAccount objects, loan1 and loan2, with principal loan…arrow_forwardWrite a class that prompts the user for the day’s high and low temperatures. If the high is greater than or equal to 90 degrees, display the message, “Heat warning.” If the low is less than 32 degrees, display the message “Freeze warning.” If the difference between the high and low temperatures is more than 40 degrees, display the message, “Large temperature swing.”arrow_forwardWrite a class RangeInput that allows users to enter a value within a range of values that is provided in the constructor. An example would be a temperature control switch in a car that allows inputs between 60 and 80 degrees Fahrenheit. The input control has “up” and “down” buttons. Provide up and down methods to change the current value. The initial value is the midpoint between the limits. As with the preceding exercises, use Math.min and Math.max to limit the value. Write a sample program that simulates clicks on controls for the passenger and driver seats. PLEASE USE THE TEMPLATES PROVIDED AND ENSURE THAT IT WORKS WITH THE TEST CLASSarrow_forward
- Write a class RangeInput that allows users to enter a value within a range of values that is provided in the constructor. An example would be a temperature control switch in a car that allows inputs between 60 and 80 degrees Fahrenheit. The input control has “up” and “down” buttons. Provide up and down methods to change the current value. The initial value is the midpoint between the limits. As with the preceding exercises, use Math.min and Math.max to limit the value. Write a sample program that simulates clicks on controls for the passenger and driver seats. Please ensure that it works with the test class provided.arrow_forwardI have already written the program and have it right, but I need help write print statement for the testorder class so that the display result looks as shown in the images. I am having trouble getting that right. I will paste the program below so you can run it and attatch the image of display result. Program for Order Class: package homework; import java.util.Date; public class Order { //Declare final variables public static final int ON_ORDER = 0; public static final int CANCELLED = 1; public static final int SHIPPED = 2; //Declare local variables private int totalOrder; private String orderName; private Date date; private int status; private String shippingAddress; private String phoneNumber; private String billingAddress; //Constructor public Order(String name) { this.totalOrder += 1; this.orderName = name; this.status = ON_ORDER; this.date = new Date();} //Cancel the placed order…arrow_forwardFor this lab task, you will work with classes and objects. Create a class named text that works similar to the built-in string class. You will need to define a constructor that can be used to initialize objects of this new class with some literal text. Next, define three methods: to_upper() that will convert all characters to uppercase, reverse() that will reverse the text and length() that will return the length of the text. After you have completed this part, copy the following mainfunction to your code and check if the implementation is correct. int main() { text sample = "This is a sample text"; cout << sample.to_upper(); // This should display "THIS IS A SAMPLE TEXT" cout << endl;cout << sample.reverse(); // This should display "txet elpmas a si sihT"cout << endl; cout << sample.length(); // This should display 21 }arrow_forward
- 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) 1. For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards you will get the original word:banana dresser grammar potato revive uneven assessWrite a method that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters the same as lowercase letters. Display how many words you found when you finish.arrow_forwardWrite a Java program to create a class named oddeven. Accept an integer value form a user at the keyboard. Create a non static method named “find” and pass the accepted value to this method to check whether the given number is odd or even.arrow_forwardWrite 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); Repeat the calorie counting program from problem 5 in a new method. This time ask the user to input the string “M” if the user is a man and “W” if the user is a woman. Use only the male formula to calculate calories if “M” is entered and use only the female formula to calculate calories if “W” is entered. Output the number of chocolate bars to consume as before. 2. For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards you will get the original word:banana dresser grammar potato revive uneven assessWrite a method that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters the same as lowercase letters.…arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,