Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB SESSION-5 Topics covered  User input using keyboard. java  User input using JoptionPane  Arithmetic expression…………….  Math class……………………………  Testing………………………………….

Similar presentations


Presentation on theme: "LAB SESSION-5 Topics covered  User input using keyboard. java  User input using JoptionPane  Arithmetic expression…………….  Math class……………………………  Testing…………………………………."— Presentation transcript:

1 LAB SESSION-5 Topics covered  User input using keyboard. java  User input using JoptionPane  Arithmetic expression…………….  Math class……………………………  Testing………………………………….

2 Eg of keyboard. java Public class key Public class key{ Public static void main(String args) Public static void main(String args) { String name; String name; System.out.println(“Enter name:”); System.out.println(“Enter name:”); name = KeyBoard.readString(); name = KeyBoard.readString(); System.out.println(“My name is: “+name); System.out.println(“My name is: “+name); }} Input: Enter name: Anand Input: Enter name: Anand Output: My name is:Anand Output: My name is:Anand

3 JOPTION PANE Eg program Eg program import javax.swing.*; 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);); JOptionPane.showMessageDialog (“The age of” + name + ”is” +age););System.exit(0);} i/p: Enter name:anand Enter age:23 i/p: Enter name:anand Enter age:23 o/p : The age of anand is 23

4 Two methods in JOptionPane class Two methods in JOptionPane class JOptionPane.ShowInputDialog(“xxxx”); JOptionPane.ShowInputDialog(“xxxx”); JOptionPane.ShowMessageDialog(“xxxx”); JOptionPane.ShowMessageDialog(“xxxx”); The first method displays a window to get an input The first method displays a window to get an input The second method displays a window to display an output The second method displays a window to display an output

5 Arithmetic expression An expression is a combination of one or more operators and operands 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 the basic arithmetic operations defined for integer type and floating point type are addition(+),subtraction(-,multiplication(*),division(/). addition(+),subtraction(-,multiplication(*),division(/). Java has one more arithmetic operation remainder(%) Java has one more arithmetic operation remainder(%) 17%4 will return 1 17%4 will return 1 a= b + c; a= b + c; a,b,c – operands a,b,c – operands + -- operator + -- operator

6 Result=14+8/2; Result=14+8/2; ans=18 ans=11 ans=18 ans=11 correct ans is 18 correct ans is 18 operator precedence hierarchy operator precedence hierarchy division/multiplication/% division/multiplication/% add/sub /concatenation add/sub /concatenation assignment assignment

7 Result=(14+8)/2; Result=(14+8)/2; ans=11; ans=11; result=((18-4)*(2+2)); result=((18-4)*(2+2)); =(14*4) =(14*4) = 56 = 56 expression should be syntactically correct expression should be syntactically correct no of left parenthesis should be =no of right parenthesis no of left parenthesis should be =no of right parenthesis Result=((19+6)%3)*3); //invalid Result=((19+6)%3)*3); //invalid Result=2+8-6; Result=2+8-6; usually u start from left when same level of precedence usually u start from left when same level of precedence

8 Testing What to u mean by testing…….. What to u mean by testing…….. After your code has been written and the program is ready to be run… 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… 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 If u’r program runs only for certain inputs then it is said to be inefficient 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 any i/p in the middle 30cents,44cents etc or any exceptional cases or any exceptional cases

9 MATH CLASS The math class provides a large no of basic mathematical functions. The math class provides a large no of basic mathematical functions. Math class is defined in java.lang package Math class is defined in java.lang package It has a lot of methods It has a lot of methods absolute, acos, asin, atan,cos,tan,sin absolute, acos, asin, atan,cos,tan,sin Exp,random,sqrt,pow Exp,random,sqrt,pow

10 Sqrt method Syntax Syntax static double sqrt(double num) 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); System.out.println (“The square of no is: “ + Square);} Output:The square of no is: 25

11 Power method Syntax Syntax static double pow(double x,double y) 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); System.out.println (“The value of power is: “ + power);} Output:The value of power is:8

12 Exponent Syntax Syntax static double exp (double power) 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); System.out.println (“The value is: “ + power);} The value is:e x The value is:e x

13 Random method Syntax Syntax static double random() static double random()Eg Public static void main (string args[]) { Double ran; ran= Math.random(); System.out.println (“The random no generated is: “ + ran); System.out.println (“The random no generated is: “ + ran);}

14 Absolute Syntax Syntax static int abs(int num) 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); System.out.println (“The absolute value of no is: “ + ran);}

15 Syntax Syntax static double sin (double angle) 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); System.out.println (“The value of sin( “+angle+”) is :“+ ran); o/p The value of sin(30) is: 0.5

16 Similarly we can find for tan,cos Similarly we can find for tan,cos Acos,asin,atan etc Acos,asin,atan etc


Download ppt "LAB SESSION-5 Topics covered  User input using keyboard. java  User input using JoptionPane  Arithmetic expression…………….  Math class……………………………  Testing…………………………………."

Similar presentations


Ads by Google