LAB SESSION-5
Download
Report
Transcript LAB SESSION-5
LAB SESSION-5
Topics covered
User input using keyboard. java
User input using JoptionPane
Arithmetic expression…………….
Math class……………………………
Testing………………………………….
Eg of keyboard. java
{
}
Public class key
Public static void main(String args)
{
String name;
System.out.println(“Enter name:”);
name = KeyBoard.readString();
System.out.println(“My name is: “+name);
}
Input:
Enter name: Anand
Output: My name is:Anand
JOPTION PANE
Eg program
import javax.swing.*;
Public class AsampleGuiProgram
{
Public static void main(String args[])
{
String name;
String temp;
Int age;
name= JOptionPane.showInputDialog(“Enter name”);
Temp= JOptionPane.showInputDialog(“Enter age”);
Age= Integer.Parseint (temp);
JOptionPane.showMessageDialog (“The age of” + name + ”is” +age););
System.exit(0);
}
i/p: Enter name:anand Enter age:23
o/p : The age of anand is 23
Two methods in JOptionPane class
JOptionPane.ShowInputDialog(“xxxx”);
JOptionPane.ShowMessageDialog(“xxxx”);
The first method displays a window to get
an input
The second method displays a window to
display an output
Arithmetic expression
An expression is a combination of one or more operators
and operands
the basic arithmetic operations defined for integer type
and floating point type are
addition(+),subtraction(-,multiplication(*),division(/).
Java has one more arithmetic operation remainder(%)
17%4 will return 1
a= b + c;
a,b,c – operands
+ -operator
Result=14+8/2;
ans=18
ans=11
correct ans is 18
operator precedence hierarchy
division/multiplication/%
add/sub /concatenation
assignment
Result=(14+8)/2;
ans=11;
result=((18-4)*(2+2));
=(14*4)
= 56
expression should be syntactically correct
no of left parenthesis should be =no of right parenthesis
Result=((19+6)%3)*3); //invalid
Result=2+8-6;
usually u start from left when same level of precedence
Testing
What to u mean by testing……..
After your code has been written and the program is
ready to be run…
U have to give different inputs in order to test that the
program runs for all conditions…
If u’r program runs only for certain inputs
then it is said to be inefficient
Eg if u are asked to enter a input between 1 cent to 100
cents
Give i/p - 0 cents,100cents,(boundary cases)
any i/p in the middle 30cents,44cents etc
or any exceptional cases
MATH CLASS
The math class provides a large no of
basic mathematical functions.
Math class is defined in java.lang package
It has a lot of methods
absolute, acos, asin, atan,cos,tan,sin
Exp,random,sqrt,pow
Sqrt method
Syntax
static double sqrt(double num)
Eg
Public static void main (string args[])
{
Double no=5.00;
Double square=0;
Square= Math.sqrt(no);
System.out.println (“The square of no is: “ + Square);
}
Output:The square of no is: 25
Power method
Syntax
static double pow(double x,double y)
Eg
Public static void main (string args[])
{
Double x=2;
Double y=3;
Double power;
power= Math.pow(x,y);
System.out.println (“The value of power is: “ + power);
}
Output:The value of power is:8
Exponent
Syntax
static double exp (double power)
Eg
Public static void main (string args[])
{
Double power=2;
power= Math.exp(power);
System.out.println (“The value is: “ + power);
}
The value is:ex
Random method
Syntax
static double random()
Eg
Public static void main (string args[])
{
Double ran;
ran= Math.random();
System.out.println (“The random no generated is: “ +
ran);
}
Absolute
Syntax
static int abs(int num)
Eg
Public static void main (string args[])
{
Int no=4;
Int ran;
ran= Math.abs(no);
System.out.println (“The absolute value of no is: “ + ran);
}
Syntax
static double sin (double angle)
Eg
Public static void main (string args[])
{
Double angle=30;
Double ran;
ran= Math.sin(angle);
System.out.println (“The value of sin( “+angle+”) is :“+
ran);
o/p The value of sin(30) is: 0.5
Similarly we can find for tan,cos
Acos,asin,atan etc