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 (7th Edition)
- A java programarrow_forwardWrite a program that accepts two numbers. Use Scanner class to get the user input. Create 4 classes, the first one is the Main Class that has the main method, the other three classes are Su m, Subtract and Multiply. These three classes should have their constructors which has a parameter of type int. Create a method for each class that returns the result of the computation of two numbers. In the main method, after accepting the user input, it should call the three methods from the three classes and display its result.arrow_forwardIn Public Class, create a static void method called Info_AHW that takes 3 parameters - an int and two double. When the method is called, we pass along the age (A), then the height (H), and then the weight (W), which is used inside the method to print the person's age, then the height and then the weight. NOTE: The Print out must follow the output below Afterward, in the main method, get the User input using the Scanner class to get the three input - the int age, the double height and the double weight. Then, call your Info_AHW method and pass your arguments (age, height, weight) into that method. Hint: When getting the int input, use -> sc.nextInt(); When getting the double input, use -> nextDouble(); Input: 5 65.5 70.2 Output: Your age is 5 ; your height is 65.5 ; and your weight is 70.2 Note: -There is a single space between every word and semicolon -There is no space at the endarrow_forward
- FOR JAVA Create a class named Product that has ID, price and stock as fields. This class must have two constructors; one constructor must take all its fields as parameters, second constructor must take ID and price as parameters and set stock to 0. Also implement the following methods: • String toString(): Returns a brief summary about the object. • void sell(int): Takes a parameter that represents the amount to sell. Checks if stocks are sufficient. If so, it updates the stock and prints the total price. Else it prints a warning to user. Create 3 instances of Product class and test your methods.arrow_forwardDesign a class that holds the following personal data: name, address, age, and phone number. Write appropriate accessor and mutator methods. Also, design a program that creates three instances of the class. One instance should hold your information, and the other two should hold your friends’ or family members’ information. very basic ALGORITHM in PSUEDOCODE NOT PYTHON. Use fictitious information.arrow_forwardThe formula for converting a temperature from Fahrenheit to Celsius is: C = 5/9(F – 32) where F is the temperature in Fahrenheit and C is the temperature in Celsius. Write a class called TemperatureConversion that includes methods that: • Convert Celsius to Fahrenheit. The Celsius temperature is passed as a parameter and it returns the corresponding Fahrenheit temperature. • Convert Fahrenheit to Celsius. The Fahrenheit temperature is passed as a parameter and it returns the corresponding Celsius temperature. Demonstrate the methods by using loops to display the Fahrenheit temperatures 32 through 212 and their Celsius equivalents and the Celsius temperatures between 0 and 100 and their Fahrenheit equivalents. in c#arrow_forward
- in JAVA Assume that a receipt id contains characters that represent information or that can be used as a code to determine something about the purchase. For example: if an id has an 'x' followed by two 'y's, it means that the purchase was a final sale and cannot be returned. Or if an id has an 'a' followed by three 'b's, it means that a coupon was used for purchase. Write a static method for the Receipt class to test whether a receipt id meets such a criteria. Does the id meet the criteria? The criteria is described by three pieces of data, passed in as parameters: char firstTarget char second Target int countOfSecond After First A receipt id meets the criteria if: the id contains the first target character and the second target character appears exactly the specified number of times after the first target character appears Note: the first and second char could be the same char! Carefully review the provided driver program for examples of ids that meet and don't meet criteria. For full…arrow_forwardProgram63.javaWrite a program that estimates the cost of carpet for one or more rooms with rectangular floors. Begin by prompting for the price of carpet per square yard and the number of rooms needing this carpet. Use a loop to prompt for the floor dimensions of each room in feet. In this loop, call a value-returning method with the dimensions and carpet price as arguments. The method should return the carpet cost for each room to main, where it will be printed and accumulated. After all rooms have been processed, the program should display the total cost of the job.Sample Output (image below) Program64.javaWrite a program that demonstrates method overloading by defining and calling methods that return the area of a triangle, a rectangle, or a square.arrow_forwardJAVA Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12 the least frequent.arrow_forward
- Write a program that converts Fahrenheit temperature to Celsius and Kelvin temperature. You should have two static methods in the class Temperature Conversion. One method is fahrenheitToKelvin and other is fahrenheitToCelsius. Both methods accept Fahrenheit temperature as an argument. These methods should return the converted temperature such as Celsius and kelvin to the calling environment. Use a loop to call methods 20 times and increment Fahrenheit by 0.1 each time. I have used format specifiers to format the output of my program. C:\Users\janna\Desktop\Programming in Java\Mid Term Exam\Q2>java TemperatureConversion FAHRENHEIT CELCIUS KELVIN 98.60 98.70 98.80 98.90 99.00 99.10 99.20 99.30 99.40 99.50 99.60 99.70 99.80 99.90 100.00 100.10 100.20 100.30 100.40 100.50 37.00 37.06 37.11 37.17 37.22 37.28 37.33 37.39 37.44 37.50 37.56 37.61 37.67 37.72 37.78 37.83 37.89 37.94 38.00 38.06 310.15 310.21 310.26 310.32 310.37 310.43 310.48 310.54 310.59 310.65 310.71 310.76 310.82 310.87…arrow_forwardWrite a java test program that prompts the user to:*Create a MailService object and pass the values of post fee and delivery address.*Call the payment() method and display the total information using displayInfo() method.*Create a CargoService object and pass the values of cargo fee and weight.* Call the payment() method and display the total information using displayInfo() method.arrow_forwardCreate a class called Student to represent a Student exam grades. The class has four instance variables: A String variable for the name, and three double variables to store three exams grades for the student. 1. Write a mutator method that takes four parameters-A String and three doubles. Use these parameters to initialize the instance variables declared earlier. 2. Write a getAverage method that calculates and returns the average of the three exam grades. 3. Write a separate program that creates 2 Student objects to hold the following data. First Name Exam#1 Exam#2 Exam#3 Ahmed 83.0 95.5 90.0 Kareema 75.25 83.0 89.0 Output the name and average grade for each of the above students.arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,