COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Some basic I/O.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
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;
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
1 Course Lectures Available on line:
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
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.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
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:
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CompSci 230 S Programming Techniques
Chapter 2 Basic Computation
Building Java Programs
Introduction to Computer Science / Procedural – 67130
Lecture 2: Data Types, Variables, Operators, and Expressions
Primitive Data, Variables, Loops (Maybe)
SELECTION STATEMENTS (1)
Building Java Programs
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
The keyboard is the standard input device.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
Building Java Programs
Building Java Programs Chapter 2
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis

Announcements n Anyone tried the assignment yet?

Review n What are the 2 parts of a program? n What are the 2 types of data in Java? n What is a data type? n What are the 4 primitive data types we’ll be using in this class? n What is a method?

Review part 2 n What is a string literal? n How do we concatenate things onto a string? n How do we put newline, \, and " in a string? What are these things called?

Today n Input/Output (I/O) n Expressions n Boolean Expressions

Input/Output n How do we get data into the program from the keyboard? n How do we display data to the user?

Output n You’ve already seen the basics of this one: System.out.println("Beefcake!!!");

What about displaying data? n We talked yesterday about concatenation… int level = 3; System.out.println("You are on level: " + level); double price = 2.75; System.out.println("Please pay: $" + price);

Input n Input is a little more complicated n We are not using the "Keyboard" class described in the book. n Let's take a look at the current assignment...

Part of program P1 BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in )); int age; // Holds the age of the person String name; // Holds name. // Ask the user's name System.out.println( "\"Hello\""); System.out.print( "What is your name? -> " ); name = stdin.readLine();

Part of program P1 BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in )); int age; // Holds the age of the person String name; // Holds name. // Ask the user's name System.out.println( "\"Hello\""); System.out.print( "What is your name? -> " ); name = stdin.readLine(); This is some setup code. We create an object stdin. You don't have to understand the details of this.

Part of program P1 BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in )); int age; // Holds the age of the person String name; // Holds name. // Ask the user's name System.out.println( "\"Hello\""); System.out.print( "What is your name? -> " ); name = stdin.readLine(); Here we actually read in what the user types.

Another piece of program P1 // Ask the user's age System.out.print( "Please enter your age (doesn't have to be true). -> "); age = Integer.parseInt(stdin.readLine( ) ); And here we read it in and then convert it to an integer

Input Summary n Put this at beginning (on one line) To read in a String To read in an int To read in a double BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); = stdin.readLine(); = Integer.parseInt(stdin.readLine()); = (new Double(stdin.readLine())).doubleValue();

Expressions: the revenge n We talked about +, -, *, / and % n Just like with arithmetic, there is an order n * and / have higher precedence than + and - 18 / is 4, not 3

Using parenthesis n There are many levels of precedence (see pg. 69 in book) n You can force whatever order you want using parenthesis 18 / is 4, BUT 18 / (5 + 1) is 3

Mixing data types n Avoid this if you can n Guidelines –only mix int and double –the result should be double double total, shoePrice = 54.99; int numShoes = 4; total = shoePrice * numShoes; Should be a double

Methods can be in expressions n If a method returns a value, then it can be in an expression too. n This brings us to a special kind of method... age = Integer.parseInt(stdin.readLine( )) - 21;

Class Methods n Normally, you have to instantiate an object of a class before you can use its methods But not if the method has the word static in front of it n Where have you seen this before?

Class (or static) methods n How about main? public class Simple { public static void main(String[] args) { System.out.println(“Hello!”); }

Aside: method data types public class Simple { public static void main(String[] args) { System.out.println(“Hello!”); } What's this?

Aside: method data types public class Simple { public static void main(String[] args) { System.out.println(“Hello!”); } It's a data type! It tells you whether the method returns a value (if it can be used in an expression)

This can be very useful The Math class contains many methods that are useful in expressions: double result; // calculate the absolute value result = Math.abs(-17.2); // calculate the sin of pi radians result = Math.sin(3.1416);

Boolean expressions These are expressions that result in a value of the data type boolean n They almost always result from the use of equality operators, relational operators or logical operators

Equality operators n These are == and != –== means "equal to" –!= means "not equal to" n They go between two expressions: 2 == 2 evaluates to true 2 == 5 evaluates to false 15 != 7 evaluates to true 14 != 14 evaluates to false

Equality Operators n Of course you can also use variables and more complex expressions int myAnswer = 5; int trueAnswer = 7; myAnswer == trueAnswer evaluates to false int myRaise = 0; int yourRaise = ; myRaise != yourRaise evaluates to true

Relational Operators n These are and >= –< means "less than" –<= means "less than or equal to" –> means "greater than" –>= means "greater than or equal to" n They work the same way as the equality operators

Last bunch of boolean operators n Logical operators: !, && and || –! means "NOT" –&& means "logical AND" –|| means "logical OR"

Truth tables

Logical operators in action n Much like relational operators, but each side has to be a boolean expression (3<5) && (6<=7) evaluates to true !(2!=5) evaluates to false (3!=3) || (2==2) evaluates to true

Examples

Homework n Read , 3.4 n Don't forget that P1 is due tomorrow!