Question

Using the JOptionPane class for both input and output How could I code this? *Calculate Inches...

Using the JOptionPane class for both input and output How could I code this?

*Calculate Inches to Centimeters, Meters, Feet, Yards, and Kilometers

*Calculate Feet to Meters, Yards, Kilometers, and Miles

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer for your question in Java Programming Language.

NOTE : IDE Used - Netbeans 8.0

CODE :

package java_examples;

import java.io.*;
import java.util.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class Calculations {

static DecimalFormat form = new DecimalFormat("##.######");
public static void main(String[] args)
{
String choice = "";
//Loop repeats until user says No - "N"
while(!"N".equals(choice))
{
//Displaying user greeting
System.out.println("\nMenu\n 1.Inches to Centimeters,Meters,Feet,Yards and Kilometers\n 2.Feet to Meters,Yard, Kilometers and Miles");
int option = 0;
//Reading user input
Scanner s = new Scanner(System.in);
System.out.println("\nEnter your choice : ");
option = s.nextInt();
//Switch case for calculateInches and calculateFeet
switch(option)
{
case 1: calculateInches();
break;
case 2: calculateFeet();
break;
default : System.out.println("Invalid Option");
break;
}
//Askig user choice t continue the rogram
System.out.println("Do you want to continue[Y/N]?");
choice = s.next();
}
}
//Calculating Inches to Centimeters,Meters,Feet,Yards and Kilometers
static void calculateInches()
{
//Reading input for Option Pane
String inches = "";
inches = JOptionPane.showInputDialog("Enter Inches :");
double inch = Double.valueOf(inches);
//Converting them to required measurements
double centimeters = inch*(2.54);
double meters = inch*(0.0254);
double feet = inch*(0.0833333);
double yards = inch*(0.0277778);
double kilometers = inch*(0.000254);
//Creating resultnt string
String result = inches + " inches are equal to : \n\nCentimeters : " + Double.toString(centimeters) + "\n\nMeters : " + Double.toString(meters)
+ "\n\nFeet : " + Double.toString(feet) + "\n\nYards : " + Double.toString(yards) + "\n\nKilometers : " + Double.toString(kilometers)
+ "\n";
//Displaying it in Message Box
JOptionPane.showMessageDialog(null, result);
}
//Calculating Feet to Meters,Yard, Kilometers and Miles
static void calculateFeet()
{
//Reading input for Option Pane
String feet = "";
feet = JOptionPane.showInputDialog("Enter feet :");
double foot = Double.valueOf(feet);
//Converting them to required measurements
double meters = foot*(0.3048);
double kilometers = foot*(0.0003048);
double yards = foot*(0.333333);
double miles = foot*(0.000189394);
//Creating resultnt string
String result = feet + " feet are equal to : \n\nMeters : " + Double.toString(meters) + "\n\nYard : " + Double.toString(yards)
+ "\n\nKilometers : " + Double.toString(kilometers) + "\n\nMiles : " + Double.toString(miles)+"\n";
//Displaying it in Message Box
JOptionPane.showMessageDialog(null, result);
}
}

SCREENSHOTS :

Please see the screenshots of the code for indentations of the code.

OUTPUT :

Any doubts regarding this can be explained with pleasure :)

Add a comment
Know the answer?
Add Answer to:
Using the JOptionPane class for both input and output How could I code this? *Calculate Inches...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the valu...

    Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: From To Conversion Miles - Kilometers: 1 mile = 1.6093 kilometers Kilometers - Miles: 1 kilometer = 0.6214 miles Feet - Meters: 1 foot = 0.3048 meters Meters - Feet: 1 meter = 3.2808 feet Inches - Centimeters: 1 inch = 2.54 centimeters Centimeters - Inches: 1...

  • In this exercise, you’ll add code to a form that converts the value the user enters...

    In this exercise, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: 1. Open the Conversions project. Display the code for the form, and notice the rectangular array whose rows contain the value to be displayed in the combo box, the text for the labels that identify the two text boxes, and the multiplier for the conversion as shown above. Note: An array...

  • So far I have this code but I'm not sure if it's right or how to...

    So far I have this code but I'm not sure if it's right or how to finish it. #include "distance.h" #include <iostream> #include <iomanip> #include <string> using namespace std; Distance::Distance() { feet = 0; miles = 0; yards = 0; inches = 0; } Distance::Distance(int inch) {    } Distance::Distance(int m, int y, int f, double i) { if(m < 0 || y < 0 || f < 0) { feet = 0; miles = 0; yards = 0; }...

  • I need a code that would give me the desired result (the ones in green) from...

    I need a code that would give me the desired result (the ones in green) from the input (in blue) im using c++. I'm so lost someone please help me. All I know is that there has to be a "while" loop and there is no need for "if" statements. 11. Examples of input and output Here are some examples of input and output for the above program code. Here is an example input from file input1.txt 6 inches 1.5...

  • Jalan P ET 170 Conversion Homework 1. 78.3 MilesKilometers 1. 78.3 Miles 2. 26.8 Ounces 3....

    Jalan P ET 170 Conversion Homework 1. 78.3 MilesKilometers 1. 78.3 Miles 2. 26.8 Ounces 3. 17.0 LitersCubic inches 4, 9,.17 GallonsCubic inches 5. 93.0 Kilometers per hour 57.6 Miles per hour 6. 151 Fluid ounces 4.47 Liters Kilograms ns- Cubic inches 7. 480 InchesMeters 7. 8. 852 illiliters 400 Quarts 9. 618 Cubit inches-10 Liters 10. 22.0 Meters 11. 688 Pounds Kilograms 12. How much do you weight in kilograms? If you are from a country other than the...

  • C++ programming language , I need help writing the code . Please explain if you can...

    C++ programming language , I need help writing the code . Please explain if you can . Exercise 10: Unit conversion (10 points) Write a console program that converts meters into feet and inches. The program starts by asking the user for a measurement in meters. The program should accept a floating point number. After getting the number, the program computes the number of feet and inches equal to the specified number of meters. The program outputs feet and inches...

  • use  JOptionPane to display output Can someone please help write a program in Java using loops, not...

    use  JOptionPane to display output Can someone please help write a program in Java using loops, not HashMap Test 1 Grades After the class complete the first exam, I saved all of the grades in a text file called test1.txt. Your job is to do some analysis on the grades for me. Input: There will not be any input for this program. This is called a batch program because there will be no user interaction other than to run the program....

  • For this Java program I have to be implementing a Pedometer class. The driver file will...

    For this Java program I have to be implementing a Pedometer class. The driver file will be provided for you ( PedometerDriver.java ) and can be downloaded in Canvas. Write a stand alone class. Name your class Pedometer and source file Pedometer.java . Program 6 Overview Goals Class and File Naming Here is the Unified Modeling Language (UML) diagram for the Pedometer class. See the end of this document for information on how to read a UML document. Pedometer -...

  • I need help with this C code Can you explain line by line? Also can you...

    I need help with this C code Can you explain line by line? Also can you explain to me the flow of the code, like the flow of how the compiler reads it. I need to present this and I want to make sure I understand every single line of it. Thank you! /* * Converts measurements given in one unit to any other unit of the same * category that is listed in the database file, units.txt. * Handles...

  • (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectan...

    (NEED FULL CODE PLEASE)write a java program class to calculate and display the areas of 10 rectangles using arrays. * input validation sizes must be between 0-50 inches output to be displayed on the screen and send to an output file. *the output must appear in the following format. rectangle length width area 1    2.0    3.0    6.0 2 - - - - 10 display the rectangle with largest area. rectangle1 area= display the rectangle with smallest area. rectangle2 area=

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT