GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Oct-2005, 13:55
MOHAMMEDALI1989 MOHAMMEDALI1989 is offline
New Member
 
Join Date: Oct 2005
Posts: 2
MOHAMMEDALI1989 is on a distinguished road

Simple Calculator Application


SALAM SIR,
HOW ARE YOU SIR?Hey whats up? I'm a new Java student and I am lost, can anyone help me?PLEASE HELP ME ABOUT HOW CAN MADE SIMPLE CALCULATOR APPLICATION IN JAVA?PLEASE YOU GIVE ME TUTORIALS ABOUT SIMPLE CALCULATOR APPLICATION AND SOURCES CODE.
MY ENGLISH LANGUAGE IS VERY WEAK.MY WEBSITE ADDRESS IS www.geocities.com
THANKS SIR
  #2  
Old 06-Oct-2005, 17:00
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Please You Give Me Tutorials About Simple Calculator Application And Sources Code.


hi mohammed.
Welcome to the GIDForums.
Please read the Guidelines before posting..

Ok. here is how you write a program in java:
JAVA Code:
// A first program in Java.

class helloworld {

public static void main( String args[] )          // main method begins execution of Java application
{

System.out.println( "Hello world!" );

}                                                           // end method main

}                                                           // end class helloworld

where,
1.
JAVA Code:
// A first program in Java.
is a single-line comment that describes the purpose of the program.
2.
JAVA Code:
public class helloworld {
begins a class definition for class helloworld. Every program in Java consists of at least one class definition. These classes are known as user-defined classes.
3.
JAVA Code:
public static void main( String args[] )
is a part of every Java application. Java applications begin executing at main. The parentheses after main indicate that main is a program building block called a method.
4.
JAVA Code:
System.out.println( "Welcome to Java Programming!" );
instructs the computer to priint the string of characters contained between the double quotation marks.
6.
JAVA Code:
} // end method main
specifies the closing right brace (}) of method main.
7.
JAVA Code:
} // end class helloworld
specifies the closing right brace (}) of class helloworld.




How to get input from the user?

//1. import the java.io package needed for BufferedReader, InputStreamReader, etc.
JAVA Code:
    import java.io.*;

// 2. Create an InputStreamReader using the standard input stream.
JAVA Code:
    InputStreamReader isr = new InputStreamReader( System.in );

// 3. Create a BufferedReader using the InputStreamReader created.
JAVA Code:
    BufferedReader stdin = new BufferedReader( isr );

// 4. Use the BufferedReader to read a line of text from the user.
JAVA Code:
    String input = stdin.readLine();

//5.Use the Integer class to parse the string of characters into an integer.
JAVA Code:
    int number = Integer.parseInt( input );



What if the user types letters instead of digits?
The parseInt method declares that it may throw a NumberFormatException.

Exceptions indicate that an error occurred in a program. A programmer can write code to recover from an exception and continue program execution instead of abnormally terminating the program. You can catch the NumberFormatException using the try and catch statements..
You can find more about exceptions in this link





so here is a sample program to get input from the user and print the value:
JAVA Code:
import java.io.*;      //import the java.io package 
class calc 
{
     public static void main ( String [] args ) throws IOException   //throw any exceptions
     {
          BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );

            //note the previous line. you can write the statements 2 and 3 in a single line
            // 2&3. Create a single shared BufferedReader for keyboard input.

            System.out.print( " Enter any integer " );

            String input = stdin.readLine();

            int i = Integer.parseInt( input );

            System.out.println( "The integer you typed is: " +i);

      }               // end main method

}                     // end class calc 




Now on to creating a calculator application:
What should we do?
1. Get two inputs from the user say a and b.
2. Display a menu to the user like:
Quote:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice....
3. Calculate and print the result accordingly..


so this is a sample calculator application:
JAVA Code:
import java.io.*;                 //import the java.io package 

class calc 
{
     public static void main ( String [] args ) throws IOException     //throw any exceptions
     {
          BufferedReader stdin =  new BufferedReader(new InputStreamReader( System.in ));

            System.out.print( " Enter integer a  " );
            String input   =   stdin.readLine();             //get integer a
            int a    =   Integer.parseInt( input );

            System.out.print( "Enter integer b  " );
            input   =   stdin.readLine();                      //get integer b
            int b    =   Integer.parseInt( input );

            System.out.print( "     Enter your choice:  \n
                                          1. Addition             \n 
                                          2. Subtraction         \n 
                                          3. Multiplication       \n
                                          4. Division               \n");

            //Display the menu in the previous line

            input        =  stdin.readLine();         //get the choice of the user
            int choice  =  Integer.parseInt( input );

            switch( choice )                         //switch to the value of choice
	    {
		case 1:                                //if choice is 1 then:
		System.out.println( a +" + "+ b + " = " + (a+b) );
		break;
		
		case 2:                                //if choice is 2
		System.out.println( a + " - " + b +" = " + (a-b) );
		break;

		case 3:                                 //if choice is 3
		System.out.println( a + " * " + b +" = " + (a*b) );
		break;

		case 4:                                 //if choice is 4
		System.out.println( a + " / " + b +" = " + (float)(a/b) );    
                 break;
                 
                 //convert the result to float in the previous expression because a/b may not be a whole number always
                 

		default:                                //if the user enters any other value
		System.out.println( "Enter a valid choice..." );

            }
      }                                                    // end main method

}                                                          // end calc 



You can know more about java from learning this tutorial

Bye,
Paramesh
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.