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

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 2 - Introduction to Java Applications
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Computer Programming Lab(5).
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Constants public class Car { //This class produces cars with 18 MPG private String color; private String make; private String model; private double gas;
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
Java Fundamentals Asserting Java Chapter 2: Introduction to Computer Science ©Rick Mercer.
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Mathematical Calculations in Java Mrs. G. Chapman.
***** SWTJC STEM ***** Chapter 3-1 cg 39 Math Class The Math class provides a convenient way to access higher math functions. The Math class is automatically.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
Dialog Boxes.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Mathematical Calculations in Java Mrs. C. Furman.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
The Math Class Methods Utilizing the Important Math Operations of Java!
Java Review if Online Time For loop Quiz on Thursday.
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Doing math In java.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
CompSci 230 S Programming Techniques
2.5 Another Java Application: Adding Integers
Operators and Expressions
OPERATORS (1) CSC 111.
Operators Laboratory /11/16.
F II 2. Simple Java Programs Objectives
Random Numbers while loop
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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