1 / 16

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;

diedrich
Download Presentation

LAB SESSION-5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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

  3. 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

  4. 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

  5. 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

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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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); }

  14. 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); }

  15. 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

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

More Related