Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Convert One Unit to Another
In this section, you will learn to convert Grams to Pounds, Grams to Ounce, Kilograms to Pounds and Pounds to Kilograms.
 
 

Convert One Unit to Another

                         

In this section, you will learn to convert Grams to Pounds, Grams to Ounce, Kilograms to Pounds and Pounds to Kilograms. The following program helps you in converting one unit to another. 

Code Description:

In this example we have taken a table of units as Grams, Kilograms, Pounds etc to convert them to another units. First of all it will ask to enter a choice then it will convert the unit. For the conversion process we have used GramsToPounds();, GramsToOunce();, KilogramsToPounds(); and PoundsToKilograms();. method. For the choice of entries we have used switch case here. At last we have applied the formulae for all the conversions as shown in the program.

We have also used conditional-OR operator to select the choice as 1,2 and so on. This means that the choice should be between 1 to 5 only. To take the input of the choice for TableChoice from the keyboard we have used the same method i.e Integer.parseInt(bf.readLine());.

import java.io.*;
import java.util.*;

public class UnitConverter{
 BufferedReader bf;
 public static void main (String[] argsthrows Exception{
  UnitConverter Un = new UnitConverter();
    }
 public UnitConverter() throws Exception{
  bf = new BufferedReader(new InputStreamReader(System.in));
  int TableChoice;
  boolean done = false;
  while (!done) {
   TableChoice = runMenu();
   switch (TableChoice) {
    case 1: System.out.println("convertGramsToPounds");
    convertGramsToPounds();
    break;
    case 2: System.out.println("convertGramsToOunce");
    convertGramsToOunce();
    break;
    case 3: System.out.println("convertKilogramsToPounds")
    convertKilogramsToPounds();
    break;
    case 4: System.out.println("convertPoundsToKilograms")
    convertPoundsToKilograms();
    break;
    default: System.out.println(""Quiting..."");
        System.exit(0); 
    break;
   }
  }
 }
 
 public int runMenu() throws Exception{
  int TableChoice;
  System.out.println();
  System.out.println("Conversion Table:");
  System.out.println("-------------------------");
  System.out.println("1. Grams To Pounds");
  System.out.println("2. Grams To Ounce");
  System.out.println("3. Kilograms To Pounds");
  System.out.println("4. Pounds To Kilograms");
  System.out.println("5. Quit");
  System.out.println("-------------------------");
  System.out.print("Choice: ");
  TableChoice = Integer.parseInt(bf.readLine());
  while (TableChoice < || TableChoice > 5) {
   System.out.print("Invalid choice, try again: ");
   TableChoice = Integer.parseInt(bf.readLine());
  }
  System.out.println();
  return TableChoice;
 }
 
 public void convertGramsToPounds() throws Exception{
  System.out.print("Enter the Grams:");
  int gram = Integer.parseInt(bf.readLine());
  double pound = gram * 0.00220462262;
  System.out.println("Pound: " + pound);
 }
 
 public void convertGramsToOunce() throws Exception{
  System.out.print("Enter the Grams:");
  int gram = Integer.parseInt(bf.readLine());
  double Ounce = gram * 0.035273962;
  System.out.println("Ounce:" + Ounce);
 }
 
 public void convertKilogramsToPounds() throws Exception{
  System.out.print("Enter the Kilogram:");
  int kilo = Integer.parseInt(bf.readLine());
  double pond = kilo * 2.20462262;
  System.out.println("Ponds: " + pond);
 }
 
 public void convertPoundsToKilograms() throws Exception{
  System.out.println("Enter the Pounds:");
  int pound = Integer.parseInt(bf.readLine());
  double kilo = pound * 0.45;
  System.out.println("Kilo: " + kilo);
 }
}

Output of the program:

C:\unique>javac UnitConverter.java

C:\unique>java UnitConverter

Conversion Table:
-------------------------
1. Grams To Pounds
2. Grams To Ounce
3. Kilograms To Pounds
4. Pounds To Kilograms
5. Quit
-------------------------
Choice: 1

convertGramsToPounds
Enter the Grams:1
Pound: 0.00220462262

Conversion Table:
-------------------------
1. Grams To Pounds
2. Grams To Ounce
3. Kilograms To Pounds
4. Pounds To Kilograms
5. Quit
-------------------------
Choice: 4

convertPoundsToKilograms
Enter the Pounds:
1
Kilo: 0.45

Conversion Table:
-------------------------
1. Grams To Pounds
2. Grams To Ounce
3. Kilograms To Pounds
4. Pounds To Kilograms
5. Quit
-------------------------
Choice: 3

convertKilogramsToPounds
Enter the Kilogram:1
Ponds: 2.20462262

Conversion Table:
-------------------------
1. Grams To Pounds
2. Grams To Ounce
3. Kilograms To Pounds
4. Pounds To Kilograms
5. Quit
-------------------------
Choice:

Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.