Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
Chapter 2 Data Types, Declarations, and Displays
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Hello, world! Dissect HelloWorld.java Compile it Run it.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
A Computer Science Tapestry 3.1 Programs that Respond to Input l Programs in chapters one and two generate the same output each time they are executed.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Building Java Programs Primitive Data and Definite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CompSci Today’s topics Parsing Java Programming Reading Great Ideas, Chapter 3 & 4.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CompSci Today’s topics Parsing Java Programming Notes from Tammy Bailey Reading Great Ideas, Chapter 3 & 4.
Data Types Declarations Expressions Data storage C++ Basics.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CompSci Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
A Computer Science Tapestry 3.1 Tools we have, Tools we need l We know about output  Streams, we’ve seen strings, we’ll see numbers  Streams support.
A Computer Science Tapestry 3.1 Programs that Respond to Input l Programs in chapters one and two generate the same output each time they are executed.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Today’s topics Java Looping Upcoming Arrays in Java Reading Great Ideas, Chapter 3.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
HelloWorld.java import java.awt.*; public class HelloWorld extends
Chapter 4 Assignment Statement
Chapter 3 Syntax, Errors, and Debugging
Building Java Programs
Chapter 4 – Fundamental Data Types
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
Building Java Programs
Building Java Programs
Chapter 2 Variables.
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Summary of what we learned yesterday
Building Java Programs
Building Java Programs Chapter 2
Programs that Respond to Input
Building Java Programs
Building Java Programs
Presentation transcript:

Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2

Java Details Variable: an item of data named by an identifier Constants (also called self-defining term) ”Hello” Operators Arithmetic Relational and conditional Assignment Other Expression: a series of variables, operators, and method calls that evaluates to a single value

Syntax, Semantics, and Style Syntax Grammar Java requires it be perfect Compiler will generate error messages Semantics Meaning Compiler will not (can not !) check most of it You can write incorrect (or stupid, or inefficient) programs Style Make program more readable for humans Actually very important! Helps understanding and writing correct programs

Data Input For program to be versatile it must have Input Have used Buttons as a form a input It’s one way to make our wishes known Need more flexibility Can input text: String Can input numbers –Whole numbers called integers: int –Real numbers (allow fractions) called doubles: double Use getText of TextField class to input strings Use getInt of IntField class to input integers

Text (string) Input Use TextField s to read in string data Use the getText method of the TextField class After creating a TextField object we can use method Syntax (we’ve used it before) is object.method() For example note following code fragment: // declare and create TextField instr TextField instr = new TextField(50); // declare message (new not needed) String message; // message gets value from TextField instr message = instr.getText();

Text Input Example public class DupThree extends java.applet.Applet implements ActionListener { TextField m1, m2, m3, m4, m5; Button b1; String message; public void init () { m1 = new TextField(80); m2 = new TextField(80); m3 = new TextField(80); m4 = new TextField(80); m5 = new TextField(80); b1 = new Button("button"); m1.setText( "Please enter some text below, then press button"); add(m1); add(m2); add(b1); add(m3); add(m4); add(m5); b1.addActionListener(this); }

Text Input Example (continued) public void actionPerformed(ActionEvent event) { // since there is only one button, no if needed message = m2.getText(); m3.setText(message); m4.setText(message); m5.setText(message); }

Dealing with numbers Primitive data type: int Does not require a new operator to create Primitive type is not a class Must declare Should initialize Other primitive types include: boolean, char, double Operations using integers +, -, *, /, % Operator Precedence

Some arithmetic details Java adheres to traditional order of operations * and / have higher precedence than + and – int x = * 6; int y = (3 + 5) * 6; Parentheses are free, use them liberally Arithmetic expressions are evaluated left-to-right in the absence of parentheses int x = 3 * 4 / 6 * 2; int y = (3*4)/(6*2); There are limits on int and double value, be aware of them.

Numeric Input Use IntField s to read in numeric data Use the getInt method of the IntField class After creating an IntField object we can use method Syntax (we’ve used it before) is object.method() For example note following code fragment // declare and create IntField intin IntField intin = new IntField(20); // declare n (new not needed) int n; // n reads value from IntField intint n = intin.getInt();

Game Example with Integer Input public class AboveBelow extends java.applet.Applet implements ActionListener { TextField m1, m2; IntField i1; Button b1, b2; int secret, guess; public void init () { m1 = new TextField(80); m1.setText( "Enter number between 0 and 100 below, then push SECRET"); i1 = new IntField(40); m2 = new TextField(80); b1 = new Button("SECRET"); b2 = new Button("GUESS"); add(m1); add(b1); add(i1); add(b2); add(m2); b1.addActionListener(this); b2.addActionListener(this); }

Game Example (page 2) public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == b1) { secret = i1.getInt(); i1.setInt(); m1.setText( "Now, enter your guess below, then press GUESS"); } if (cause == b2) { guess = i1.getInt(); if (guess == secret) m2.setText("You've got it!"); if (guess < secret) { i1.setInt(); m2.setText("The number is greater than "+guess); }

Game Example continued: if (guess > secret) { i1.setInt(); m2.setText("The number is less than "+guess); } What is best strategy to play this game? Where have we seen it before?

Types for Numbers The type String is not a built-in type, actually it’s a class There are many numerical types in Java. We’ll use two int, represents integers: {…-3,-2,-1,0,1,2,3,…} –Conceptually there are an infinite number of integers, but the range is limited to [-2 31, ] or [Integer.MIN_VALUE,Integer.MAX_VALUE] –A lternatives? Why is range limited? double, represents real numbers like ,  2 –Not represented exactly, so expressions like 100*0.1 may yield unexpected results –Double precision floating point numbers, another type float exists, but it’s a terrible choice (can generate poor results)

GIGO: program as good as its data? In calculations involving floating point numbers it’s easy to generate errors because of accumulated approximations: What is ? When is (x + y) + z different from x + (y + z) ? The type int is severely constrained on 16-bit computers, e.g., running DOS, largest value is 32,767 ( ) Even on 32-bit machines, how many seconds in a millennium? 60*60*24*365*1000, problems? On UNIX machines time is measure in seconds since 1970, problems? What was Y2K all about?

What arithmetic operations exist? Syntax and semantics for arithmetic operations Addition, subtraction: + and –, int and double x + y d – Multiplication: *, int and double 23 * 4 y * 3.0 d * 23.1 * 4 Division: /, different for int and double 21 / 4 21 / 4.0 x / y Modulus: %, (think of it as remainder) only for int 21 % 4 17 % 2 x % y Mixed type expressions are converted to “higher” type Associativity of operators determines left-to-right behavior Use parentheses liberally Without ( ) use operator precedence : *, /, %, before +, -