One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Saturday, January 22, 2011

How to get user inputs using keyboard?

Tutorial


01. Write a Java program to convert a given temperature in Celsius to Fahrenheit or to
convert  a  given  temperature  in  Fahrenheit  to  Celsius.  Get  all  the  inputs  as
keyboard  inputs.  Your  program  should  have  two  methods
           celsiusToFahrenheit()
                       and
            fahrenheitToCelsius()
to  perform  the  operations  mentioned  above.    When
ever you run the program main method should display  an output as given below.
Enter
          1 – To convert Celsius to Fahrenheit
          2 - To convert Fahrenheit to Celsius
         Any other number – To exit

        *  When the use enters a value 1 or 2, get the temperature in Celsius or in
             Fahrenheit convert to the other form and display.

        * Then you  must display the menu given above again.

        * You exit the program wh en the user  enter any number other than 1 or 2.

        * You may use loop and switch statements.


              Tc = (5/9)*(Tf-32);
              Tf = (9/5)*Tc+32;

Answer :



import java.util.Scanner;


class Temp{
public static void main(String args[]){
System.out.println("Enter\n1 - To convert Celsius to Fahrenheit\n2 - To convert Fahrenheit   to Celsius\nAny other number - To exit");

Scanner sc1 = new Scanner(System.in);
int option = sc1.nextInt();


switch(option){
case 1 : celsiusToFahrenheit();
break;
case 2 : fahrenheitToCelsius();
break;
default : System.exit(0);
}
}
public static void celsiusToFahrenheit(){
System.out.println("Enter celsius value - ");


Scanner sc2 = new Scanner(System.in);
double temp = sc2.nextDouble();

                          System.out.print("Fahrenheit value - "+((9.0/5.0)*temp+32));
}
public static void fahrenheitToCelsius(){
System.out.println("Enter fahrenheit value - ");


Scanner sc2 = new Scanner(System.in);
double temp = sc2.nextDouble();

                          System.out.print("Celsius value - "+((5.0/9.0)*(temp-32)));
}
}




02. Write a Java program to calculate the numb er of digits and number of letters in a
word. Get the word as a  command line argument.
Note:
              You may use isDigit() and isLetter() methods in Character class.

Answer :



import java.io.*;


class Text{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the text : ");
String text = br.readLine();

int digits=0, letters=0;
for(int i=0; i<text.length(); i++){
if(Character.isDigit(text.charAt(i)))
digits++;
else if(Character.isLetter(text.charAt(i)))
letters++;
}
System.out.println("Number of letter - "+digits);
System.out.println("Number of digits - "+letters);
}
}

3 comments:

  1. This is cool machan. I didn't know that Scanner thing exactly. First I read ur coding and understood the logic. Then did it on my own. It worked. Thanks macho.

    ReplyDelete
  2. wlcm bro.................... :)))

    ReplyDelete
  3. thnxx machn........ its working....:))

    ReplyDelete