![Java: An Introduction to Problem Solving and Programming (7th Edition)](https://www.bartleby.com/isbn_cover_images/9780133766264/9780133766264_largeCoverImage.gif)
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...
![Check Mark](/static/check-mark.png)
Want to see the full answer?
Check out a sample textbook solution![Blurred answer](/static/blurred-answer.jpg)
Chapter 6 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
- In 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_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 Subtract, Sum and Multiply. These three classes should have their constructors which has a para meter 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_forwardFOR 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_forward
- Design 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_forwardin 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_forward
- Program63.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_forwardWrite 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_forwardTHIS NEEDS TO BE DONE IN JAVA Modify the Account class to provide a debit method that withdraws money from an Account. Ensure that the debit amount does not exceed the Account's balance. If it does, the balance should be left unchanged and the method should print out a message indicating "Debit amount exceeded amount balance." Modify class AccountTest to test method debit. Below are links to the Account and AccountTest programs. //This is for the account class public class Account {private String name; //instance variableprivate double balance; //instance variable//Account constructor that receives two parameterspublic Account(String name, double balance){this.name = name; //assing name to instance varialbe name//validate that the balance is greater than 0.0; if it's not,//instance varisble balance keeps its default initial value of 0.0if (balance > 0.0){ //if the balance is valaidthis.balance = balance; //assign it to instance variable balance}}//method that deposits (adds)…arrow_forward
- Write 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_forwardPLEASE USE PYTHON PROGRAMMING Create a class called Numbers, which has a single class attribute called MULTIPLIER, and a constructor which takes the parameters x and y (these should all be numbers). Write a method called add which returns the sum of the attributes x and y. Write a class method called multiply, which takes a single number parameter aand returns the product of a and MULTIPLIER. Write a static method called subtract, which takes two number parameters, b and c, and returns b - c. Write a method called value which returns a tuple containing the values of x and y. Make this method into a property, and write a setter and a deleter for manipulating the values of x and y. class Numbers: # TODO: create a class attribute called MULTIPLIER def __init__(self, x, y): self.x = x self.y = y def add(self): # TODO: return x + y def multiply(self, a): # TODO: return the attribute MULTIPLIER * by a. @staticmethod def subtract(b, c):…arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
![Text book image](https://www.bartleby.com/isbn_cover_images/9781337102100/9781337102100_smallCoverImage.gif)