Question

In: Computer Science

A simple way to write the code to convert millileters to ounces in java

A simple way to write the code to convert millileters to ounces in java

Solutions

Expert Solution

as per the question we need to write a java code for the conversion of milliliters to ounces

so we r taking input from user so we have to import scanner class of util package .

i demonstrate using two different classes because this is best practice in java otherwise we can do this in main method itself

/*-------*/

import java.util.Scanner;

public class Main{

/ we will make a function so we can called it from main method and our code is reused

//in question data type of ml not given so I consider it as double we can consider as our requirement it does not //affect anything

public static double toOunce(double milliliters){

//first we should check given value of ml is valid or not because it should always positive

if(milliliters <0)

return -1;//as standard we choose -1 because it signifies error but it is not necessary

double inOunce = (double)milliliters*0.033814;

return inOunce;

}

public static void main(String args[]){

Scanner scanner = new Scanner(System.in);

System.out.println("enter the value in milliltete to convert");

double inMl = scanner.nextDouble();

System.out.println("value of "+inMl+"milliltete in Ounces is :"+OunceConverter.toOunce(inMl));

}

}

/*-----*/

btw this is answer this question but we can make it much better by using while loop so for every value we don't have to run again and again and other thing is we should make a different file for toOunce method as in best practice


Related Solutions

In simple python code 1) Write a function to convert the image to grayscale. 2Write a...
In simple python code 1) Write a function to convert the image to grayscale. 2Write a function to convert an image to black and white 3)Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows: newR = (R × 0.393 + G × 0.769 + B × 0.189) newG = (R × 0.349 + G × 0.686 + B × 0.168) newB = (R ×...
Java Script Ask the user for a Fahrenheit temperature Write the code needed to convert a...
Java Script Ask the user for a Fahrenheit temperature Write the code needed to convert a Fahrenheit temperature into a Celsius temperature. Use an alert box to show the result Ask the user for a Celsius temperature Write a function to convert from Celsius to Fahrenheit; use an alert box to show the results Decide on two other conversions (meters to feet, pounds to kilos, other) Ask the user for numbers to be converted; be sure to tell them what...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of...
Convert this C++ code to Java code this code is to encrypt and decrypt strings of characters using Caesar cipher please attach samples run of the code #include <iostream> #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string> #define MAX_SIZE 200 void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string. void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string. using namespace std; int main() { char input_str[MAX_SIZE];...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for...
Convert the following UML diagram into the Java code. Write constructor, mutator and accessor methods for the given class. Create an instance of the class in a main method using a Practice class. Note: The structure of the class can be compiled and tested without having bodies for the methods. Just be sure to put in dummy return values for methods that have a return type other than void.      
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use...
Write a simple Java code to make a Binary Tree for the following tree: Don’t use serializable interface implantation /** Class to encapsulate a tree node. */ protected static class Node implements Serializable {
write and explain two dimensional parity check code and hamming codes in simple way please
write and explain two dimensional parity check code and hamming codes in simple way please
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Write an algorithm to describe how to convert a user input from ounces to grams, drams...
Write an algorithm to describe how to convert a user input from ounces to grams, drams and pounds. Use enough detail that you have between five and ten steps. List any assumptions.          a) Assumptions          b) Steps
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT