● Basic Computation Recitation – 09/(04,05)/2008 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Flow of Control Recitation – 09/(11,12)/2008 CS 180 Department of Computer Science, Purdue University.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank M. Carrano. ISBN © 2008 Pearson Education, Inc.,
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.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Chapter 2 - Introduction to Java Applications
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Chapter 21 Primitive Types, Strings, and Console I/O Chapter 2.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
Basic Computation Module 2. Objectives Describe the Java data types used for simple data Write Java statements to declare variables, define named constants.
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
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.
Chapter 2Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 2 l Primitive Data types l Strings: a class l Assignment l.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank M. Carrano. ISBN © 2009 Pearson Education, Inc.,
Primitive Types, Strings, and Console I/O Chapter 2.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 2Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Reminders/Info l Project 1 available (due Thursday 01/27, 10:30 pm)
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank M. Carrano. ISBN © 2009 Pearson Education, Inc.,
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
Chapter 21 Primitive Types, Strings, and Console I/O Recitation Week 2.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank M. Carrano. ISBN © 2008 Pearson Education, Inc.,
JOptionPane. Import javax.swing.JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string.
Chapter 21 Primitive Types, Strings, and Console I/O Chapter 2.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
String class.
2.5 Another Java Application: Adding Integers
Basic Computation Chapter 2.
Examples of Primitive Values
Chapter 2 Edited by JJ Shepherd
Chapter 2 - Introduction to Java Applications
Basic Computation Chapter 2.
Chapter 2 Part 2 Edited by JJ Shepherd
Basic Computation Chapter 2.
Basic Computation Chapter 2.
Basic Computation Chapter 2.
Primitive Types and Expressions
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

● Basic Computation Recitation – 09/(04,05)/2008 CS 180 Department of Computer Science, Purdue University

22 ●Project 1 Now posted on the class webpage. Due Wed, Sept. 10 at 10 pm. Start early. All questions on the class newsgroup. Evening consulting hours.  LWSN B146. MTW 7-10 pm.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 33 ●Announcements Mentor program:  Debbie Perouli.  Tue 5-7. LWSN B116.  Weekly study group meeting.  Chance to catch up and/or vent.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 44 ●Announcements Class newsgroup:  purdue.class.cs180  Information regarding projects and answers to questions will be posted to the newsgroup; posts by grad TAs are official You are responsible for knowing all information posted, including changes to assignments A newsgroup client such as Mozilla Thunderbird or Microsoft Outlook can access the class newsgroup.

Declaring vairables Examples Bad! int totalEggs, numberOfBaskets, eggsPerBasket; Good! int totalEggs; //total eggs in all baskets int numberOfBaskets; //baskets available int eggsPerBasket=6; //basket capacity

Java Identifiers Identifiers may not contain any spaces, dots (. ), asterisks ( * ), or other characters: 7-11 firefox.com util.* (not allowed) Identifiers can be arbitrarily long. Since Java is case sensitive, stuff, Stuff, and STUFF are different identifiers. But using these in your program can cause confusion. Come up with unique names!

Type Casting A type cast temporarily changes the value of a variable from the declared type to some other type. For example, double distance; distance = ; int points; points = (int)distance; Illegal without (int)

Type Casting The value of (int)distance is 9, The value of distance, both before and after the cast, is Any nonzero value to the right of the decimal point is truncated rather than rounded.

Implicit Type Casting For example, int points; points = 9; double distance; distance = points; Legal. Value of distance is 9.0.

The mod Operator The mod ( % ) operator is used with operators of integer type to obtain the remainder after integer division. 14 divided by 4 is 3 with a remainder of 2. Hence, 14 % 4 is equal to 2. The mod operator has many uses, including determining if an integer is odd or even determining if one integer is evenly divisible by another integer.

Increment and Decrement Operators Used to increase (or decrease) the value of a variable by 1 Easy to use, important to recognize The increment operator count++ or ++count The decrement operator count-- or --count

Increment and Decrement Operators equivalent operations count++; ++count; count = count + 1; count--; --count; count = count - 1;

Increment and Decrement Operators in Expressions after executing int m = 4; int result = 3 * (++m) result has a value of 15 and m has a value of 5 after executing int m = 4; int result = 3 * (m++) result has a value of 12 and m has a value of 5

The Class String We've used constants of type String already. "Enter a whole number from 1 to 99." A value of type String is a Sequence of characters Treated as a single item.

String Constants and Variables Declaring String greeting; greeting = "Hello!"; or String greeting = "Hello!"; or String greeting = new String("Hello!"); Printing System.out.println(greeting);

Concatenation of Strings Two strings are concatenated using the + operator. String greeting = "Hello"; String sentence; sentence = greeting + " officer"; System.out.println(sentence); Any number of strings can be concatenated using the + operator.

Concatenating Strings and Integers String solution; solution = "The answer is " + 42; System.out.println (solution); The temperature is 72 The answer is 42

String Methods An object of the String class stores data consisting of a sequence of characters. Objects have methods as well as data The length() method returns the number of characters in a particular String object. String greeting = "Hello"; int n = greeting.length();

The Method length() The method length() returns an int. You can use a call to method length() anywhere an int can be used. int count = command.length(); System.out.println("Length is " + command.length()); count = command.length() + 3;

Figure 2.4 Positions start with 0, not 1. The 'J' in "Java is fun." is in position 0 A position is referred to an an index. The ' f' in "Java is fun." is at index 8. String Indices

String Methods Figure 2.5a

String Methods Figure 2.5b

String Methods Figure 2.5c

String Methods Figure 2.5d

String Processing No methods allow you to change the value of a String object. But you can change the value of a String variable. View sample program StringDemo listing 2.4sample program Sample Screen Output

Indentation Indentation should communicate nesting clearly. A good choice is three or four spaces for each level of indentation. Indentation should be consistent. Indentation should be used for second and subsequent lines of statements which do not fit on a single line.

Indentation Indentation does not change the behavior of the program. Proper indentation helps communicate to the human reader the nested structures of the program

JOptionPane View sample program class JOptionPaneDemo, listing 2.10sample program Sample Screen Output

JOptionPane JOptionPane can be used to construct windows that interact with the user. The JOptionPane class is imported by import javax.swing.JApplet; The JOptionPane class produces windows for obtaining input or displaying output.

JOptionPane Use showInputDialog() for input. Only string values can be input. To convert an input value from a string to an integer use the parseInt() method from the Integer class, use appleCount = Integer.parseInt(appleString);

JOptionPane Output is displayed using the showMessageDialog method. JOptionPane.showMessageDialog(nu ll, "The total number of fruits = " + totalFruitCount);

JOptionPane Syntax Input String_Variable = JOptionPane.showInputDialogue (String_Expression); Output JOptionPane.showMessageDialog (null, String_Expression); System.exit(0) ends the program.

JOptionPane Cautions If the input is not in the correct format, the program will crash. If you omit the last line ( System.exit(0) ), the program will not end, even when the OK button in the output window is clicked. Always label any output.

Inputting Numeric Types JOptionPane.showInputDialog can be used to input any of the numeric types. Figure 2.8 Methods for converting strings to numbers

Multi-Line Output Windows To output multiple lines using the method JOptionPane.showMessageDialog, insert the new line character '\n' into the string used as the second argument. OptionPane.showMessageDialog(nul l, "The number of apples\n" + "plus the number of oranges\n" + "is equal to " + totalFruit);

Multi-Line Output Windows Figure 2.9 A dialog window containing multiline output