CSS 161 Fundamentals of Computing Introduction to Computers & Java

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 Finishing Chapter 1  This week : Chapter 2  Getting our home environment configured  Working on Assignment 1  See the website for more details! 
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
CS102--Object Oriented Programming Lecture 2: – the String class – Console Input/Output – Flow of control Copyright © 2008 Xiaoyan Li.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Expressions, Data Conversion, and Input
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
Chapter 2 Console Input and Output Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
COMP String and Console I/O Yi Hong May 18, 2015.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Building Java Programs
Chapter Topics The Basics of a C++ Program Data Types
Introduction to Java Applications
Input/Output.
Chapter 2 More on Math More on Input
Chapter 2 Introduction to C++ Programming
Console Input and Output
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Introduction to Java Applications
CSC 1051 – Data Structures and Algorithms I
Data Conversion & Scanner Class
Assignment and Arithmetic expressions
Console Input and Output
Java Programming: From Problem Analysis to Program Design, 4e
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
Chapter 2: Basic Elements of Java
A+ Computer Science INPUT.
MSIS 655 Advanced Business Applications Programming
Expressions and Assignment
Building Java Programs
A+ Computer Science INPUT.
CprE 185: Intro to Problem Solving (using C)
Introduction to Java Applications
Primitive Types and Expressions
Classes, Objects and Methods
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

CSS 161 Fundamentals of Computing Introduction to Computers & Java Joe McCarthy CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing Outline Updates / reminder Recap: Intro to Java Today: Chapter 1: Assignments, expressions, data types Chapter 2: Console Input & Output (I/O) Next time: Lab 2 (UW1-120) CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing Updates / Reminder UW1-120 Mondays (“lectures”) & Fridays (labs) Wednesdays: here (UW2-031) Modified office hours MW 10:15-10:45 [no change] MW 7:45-8:15 (UW1-220) TTh 7:15-7:45pm [no change] & by appointment (joemcc@uw.edu) Lab 1 Due Friday, Oct 7, 11am Assignment 1 Due Monday, Oct 10, 11am CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing Recap: Intro to Java Class Method main() Identifier Reserved word Variable Declaration Assignment Operator System.out.println() Output: 10 + 20 = 30 CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing

Assignments & Expressions Syntax: variable = expression Semantics: Evaluate expression, assign value to variable Expression: Constant (1, 1.23, 3.45e6, ‘a’, “css161”, true, false) final static int COURSENUMBER = 161; Variable (num2) expression operator expression Operators: *, /, % +, - … CSS 161: Fundamentals of Computing

Operators & Precedence Can use parentheses to change precedence 1 + 2 * 3 (1 + 2) * 3 CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing

Shorthand assignments Example: Equivalent To: count += 2; count = count + 2; sum -= discount; sum = sum – discount; bonus *= 2; bonus = bonus * 2; time /= rushFactor; time = time / rushFactor; change %= 100; change = change % 100; amount *= count1 + count2; amount = amount * (count1 + count2); CSS 161: Fundamentals of Computing

Increment & Decrement Operators Syntax: variable++ | ++variable | variable-- | --variable Semantics: Set variable to next / previous value Examples int num1 = 1; char char1 = ‘B’; num1++; char1--; Precedence: increment/decrement before/after use 2*(num1++)  2 2*(++num1)  4 CSS 161: Fundamentals of Computing

Assignment Compatibility byteshortintlongfloatdouble char CSS 161: Fundamentals of Computing

Type casting & coercion Syntax: variable = (type) expression Semantics Convert expression to type, assign value to variable Only allowed if expression type is compatible with variable type Type coercion: Automatic type casting: int  float  double Examples short num3 = (short) ‘A’; int num4 = (int) 1.9; double num5 = 123; // coercion int num6 = 1.23; // error CSS 161: Fundamentals of Computing

CSS 161: Fundamentals of Computing Console Input & Output System.out A Java object associated with the console (screen) print(), println() Methods associated with the System.out object Syntax: System.out.print(expression) System.out.println([expression]) Semantics: Evaluate expression, output its value println: expression is optional (hence the square brackets []) output a carriage return / line feed (newline) at end of line CSS 161: Fundamentals of Computing

Formatted output: printf Syntax: System.out.printf(format[,expression]*) Semantics: Use format string to output expression(s) Examples: System.out.printf(“Hello”); System.out.printf(“Hello%n”); System.out.printf(“%5.1f”, 1.25); System.out.printf(“%-5.1f”, 1.25); System.out.printf(“$.2f”, 1.2); CSS 161: Fundamentals of Computing

Format string specifiers CSS 161: Fundamentals of Computing

Console input: Scanner class Not built-in, have to import import java.util.Scanner; Next: create an instance of the class Syntax: classname variable = new classname([args]) Semantics: Create an instance of classname, initialize it with args, assign instance to variable Example: Scanner keyboard = new Scanner(System.in); Create an instance of the Scanner class (in java.util package) Initialize it to accept input from the system console (System.in) Assign it to keyboard (NB: keyboard not a reserved word) CSS 161: Fundamentals of Computing

Console Input Using the Scanner Class The method nextInt reads one int value typed in at the keyboard and assigns it to a variable: int numberOfPods = keyboard.nextInt(); The method nextDouble reads one double value typed in at the keyboard and assigns it to a variable: double d1 = keyboard.nextDouble(); Multiple inputs must be separated by whitespace and read by multiple invocations of the appropriate method Whitespace is any string of characters, such as blank spaces, tabs, and line breaks that print out as white space Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Console Input Using the Scanner Class The method next reads one string of non-whitespace characters delimited by whitespace characters such as blanks or the beginning or end of a line Given the code String word1 = keyboard.next(); String word2 = keyboard.next(); and the input line jelly beans The value of word1 would be jelly, and the value of word2 would be beans Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Console Input Using the Scanner Class The method nextLine reads an entire line of keyboard input The code, String line = keyboard.nextLine(); reads in an entire line and places the string that is read into the variable line The end of an input line is indicated by the escape sequence '\n' This is the character input when the Enter key is pressed On the screen it is indicated by the ending of one line and the beginning of the next line When nextLine reads a line of text, it reads the '\n' character, so the next reading of input begins on the next line However, the '\n' does not become part of the string value returned (e.g., the string named by the variable line above does not end with the '\n' character) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Keyboard Input Demonstration (Part 1 of 2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Keyboard Input Demonstration (Part 2 of 2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Another Keyboard Input Demonstration (Part 1 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Another Keyboard Input Demonstration (Part 2 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Another Keyboard Input Demonstration (Part 3 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Pitfall: Dealing with the Line Terminator, '\n' The method nextLine of the class Scanner reads the remainder of a line of text starting wherever the last keyboard reading left off This can cause problems when combining it with different methods for reading from the keyboard such as nextInt Given the code, Scanner keyboard = new Scanner(System.in); int n = keyboard.nextInt(); String s1 = keyboard.nextLine(); String s2 = keyboard.nextLine(); and the input, 2 Heads are better than 1 head. what are the values of n, s1, and s2? Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Pitfall: Dealing with the Line Terminator, '\n' Given the code and input on the previous slide n will be equal to "2", s1 will be equal to "", and s2 will be equal to "heads are better than" If the following results were desired instead n equal to "2", s1 equal to "heads are better than", and s2 equal to "1 head" then an extra invocation of nextLine would be needed to get rid of the end of line character ('\n') Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Methods in the Class Scanner (Part 1 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Methods in the Class Scanner (Part 2 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Methods in the Class Scanner (Part 3 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Java Documentation for Scanner http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html CSS 161: Fundamentals of Computing

Programming Tip: Prompt for Input A program should always prompt the user when he or she needs to input some data: System.out.println( "Enter the number of pods followed by"); "the number of peas in a pod:"); Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Programming Tip: Echo Input Always echo all input that a program receives from the keyboard In this way a user can check that he or she has entered the input correctly Even though the input is automatically displayed as the user enters it, echoing the input may expose subtle errors (such as entering the letter "O" instead of a zero) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Self-Service Checkout Line (Part 1 of 2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Self-Service Checkout Line (Part 2 of 2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

The Empty String A string can have any number of characters, including zero characters "" is the empty string When a program executes the nextLine method to read a line of text, and the user types nothing on the line but presses the Enter key, then the nextLine Method reads the empty string Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Other Input Delimiters The delimiters that separate keyboard input can be changed when using the Scanner class For example, the following code could be used to create a Scanner object and change the delimiter from whitespace to "##" Scanner keyboard2 = new Scanner(System.in); Keyboard2.useDelimiter("##"); After invocation of the useDelimiter method, "##" and not whitespace will be the only input delimiter for the input object keyboard2 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Changing the Input Delimiter (Part 1 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Changing the Input Delimiter (Part 2 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Changing the Input Delimiter (Part 3 of 3) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Next Time Lab 2 (UW1-120) Don’t forget: Lab 1 due Friday, 11am Assignment 1 due Monday, 11am