9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

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.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
CMT Programming Software Applications
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
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
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Variable Declaration  It is possible to declare multiple variables of the same data type on the same line.  Ex. double hours, rate, total;  Variables.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Ch 6: JavaScript Introduction to scripting part 2.
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
 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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Mathematical Calculations in Java Mrs. C. Furman.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
2/18: Assignment Operators About Average2.java –while loop use –explicit casting –twoDigits object Assignment Operators Increment & Decrement Operators.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Review for Nested loops & Math class methods & User defined methods.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Object Oriented Programming Object and Classes Lecture 3 MBY.
JAVA 1.COMPARISON: JAVA AND C++ 2.KEYBOARD INPUT 3.OUTPUT 4.CONVERSION FROM STRING 5.OPERATORS AND EXPRESSIONS 6.DIALOG BOX – USER PROMPT January
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
Introduction to Java Applications
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Introduction to Scripting
Chapter 2 - Introduction to Java Applications
Java I.
Chapter 2 - Introduction to Java Applications
Java I.
MSIS 655 Advanced Business Applications Programming
Chapter 2 - Introduction to Java Applications
Chapter 3 – Introduction to C# Programming
Chapter 2: Java Fundamentals
Object Oriented Programming
Chapter 2 - Introduction to Java Applications
F II 2. Simple Java Programs Objectives
Presentation transcript:

9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators

Addition.java import javax.swing.JOptionPane; public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum; firstNumber = JOptionPane.showInputDialog ( "Enter a number" ); secondNumber = JOptionPane.showInputDialog ( "And another" ); number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); sum = number1 + number2; JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); } import statement class header method header declaring variables: Strings & ints

Variables & Data Types String – a series of characters. –EX: Ann, 1450, var30, g, YES –to declare a String variable, put the variable type String before the name of the variable. String firstNumber ; –to declare more than one String variable at the same time, separate the variable names with commas. String firstNumber, secondNumber ; A declaration is a statement – must end with a semicolon.

Variables & Data Types int – an integer-type number. –EX: 45, -1001, 3, –to declare an int variable, put the variable type int before the name of the variable. int number1 ; –to declare more than one int variable at the same time, separate the variable names with commas. int number1, number2 ; –other number formats: float, double, long, short

Addition.java import javax.swing.JOptionPane; public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum; firstNumber = JOptionPane.showInputDialog ( "Enter a number" ); secondNumber = JOptionPane.showInputDialog ( "And another" ); number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); sum = number1 + number2; JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); } initializing firstNumber & secondNumber

Inputs: How we did it. We initialized (gave an initial value to) firstNumber & secondNumber by the lines firstNumber = JOptionPane.showInputDialog ( "Enter a number" ); secondNumber = JOptionPane.showInputDialog ( "And another" ); JOptionPane.showInputDialog panes accept String type inputs. Even if it looks like a number, Java sees it as a String.

Addition.java import javax.swing.JOptionPane; public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum; firstNumber = JOptionPane.showInputDialog ( "Enter a number" ); secondNumber = JOptionPane.showInputDialog ( "And another" ); number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); sum = number1 + number2; JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); } initializing number1 & number2

Inputs: How we did it. We then initialized (gave an initial value to) number1 & number2 by the lines number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); These lines convert the String values of firstNumber and secondNumber into int values and store them as number1 and number2.

Addition.java import javax.swing.JOptionPane; public class Addition { public static void main ( String args[] ) { String firstNumber, secondNumber; int number1, number2, sum; firstNumber = JOptionPane.showInputDialog ( "Enter a number" ); secondNumber = JOptionPane.showInputDialog ( "And another" ); number1 = Integer.parseInt ( firstNumber ); number2 = Integer.parseInt ( secondNumber ); sum = number1 + number2; JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); System.exit ( 0 ); } initializing sum

Arithmetic Operators

Order of Operation Just like algebra inside parentheses first multiplication, division, & modulus next addition & subtraction last left to right EX: 2 * % 2 - (4 / ) = ?

Order of Operation Example 2 * % 2 - (4 / ) = ? 2 * % 2 - ( ) = ? 2 * % 2 - ( 7 ) = ? % 2 - ( 7 ) = ? ( 7 ) = ? = 2

1 st Program of the Day: pg. 58 Pg. 58: Comparison.java –Pay attention to the comparison operators ( =, etc.) Second Half: –Comparison.java in depth: –the if structure –comparison operators –assigning new values to an old variable.

Part 2: Comparison Operators Comparison.java Using the if structure Equality operators Relational operators

Comparison.java import javax.swing.JOptionPane; public class Comparison { public static void main( String args[] ) { String firstNumber, secondNumber, result; int number1, number2; firstNumber = JOptionPane.showInputDialog( "Enter first int.:" ); secondNumber = JOptionPane.showInputDialog( "Enter 2nd int:" ); number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); result = ""; import statement class header method header declaring variables: Strings & ints initializing firstNumber & secondNumber initializing number1 & number2 initializing result

Comparison.java (pt. 2) if ( number1 == number2 ) result = number1 + " == " + number2; if ( number1 != number2 ) result = number1 + " != " + number2; if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; “If number1 is equal to number2, then set result to be number1 plus a double equal sign plus number2.”

The if structure Use the if structure to have statements execute ONLY under certain conditions. if ( condition ) rest of statement ; Note that the if ( condition ) is not a statement by itself ( just like English ). The spacing and line structure is good practice.

Comparison.java (pt. 2) if ( number1 == number2 ) result = number1 + " == " + number2; if ( number1 != number2 ) result = number1 + " != " + number2; if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; “If number1 is less than number2, set result to be itself plus a new line escape sequence plus number1 plus a ‘less than’ sign plus number2.”

Equality & Relational Operators To compare values. Equality operators: Equal to, not equal to Relational operators: Greater than, less than

Equality Operators

Relational Operators

Comparison.java (pt. 3) JOptionPane.showMessageDialog( null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } result, whatever it is after executing the if statements, is put in the main body of the MessageDialog box.

Program of the Day Problem 2.17 (pg. 73): write a program Work in pairs Use Comparison.java as a help/template in creating the program