1 TCSS 143, Autumn 2004 Lecture Notes Review. 2 Computer programming computers manipulate data data is often categorized into types numbers (integers,

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Evan Korth Scanner class Evan Korth NYU. Evan Korth Java 1.5 (5.0)’s Scanner class Prior to Java 1.5 getting input from the console involved multiple.
Lecture 3 Java Basics Lecture3.ppt.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
18 File handling1June File handling CE : Fundamental Programming Techniques.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
© 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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Chapter 2 Section 2.2 Console Input Using The Scanner CLASS Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Java Programming: From the Ground Up
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
File I/O (Input and Output). Why use files? Things stored in files are more permanent than things stored in variables in programs. Things stored in files.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
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.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
CS 46B: Introduction to Data Structures June 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to programming in java
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CompSci 230 S Programming Techniques
File I/O (Input and Output)
Introduction to programming in java
String Comparison, Scanner
Input/Output.
Chapter 4 Assignment Statement
Building Java Programs
Chapter 3 Assignment Statement
Chapter 5: Control Structures II
Topic 11 Scanner object, conditional execution
Building Java Programs
BIT115: Introduction to Programming
Program Style Console Input and Output
Building Java Programs
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Computers & Programming Languages
Introduction to Classes and Methods
A+ Computer Science INPUT.
Building Java Programs
Fundamental Error Handling
Building Java Programs
Building Java Programs
Introduction to Java Brief history of Java Sample Java Program
A+ Computer Science INPUT.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Building Java Programs
Building Java Programs
Indefinite loop variations
Building Java Programs
Presentation transcript:

1 TCSS 143, Autumn 2004 Lecture Notes Review

2 Computer programming computers manipulate data data is often categorized into types numbers (integers, real numbers) written text (characters, strings of characters) a program can be thought of as a sequence of operations to perform on a set of data to understand a program's behavior ("semantics") is to understand its data flow and its control flow

3 Algorithmic thinking a general maxim: "You don't really understand something until you can explain it to someone else." Donald Knuth's computer science version: "A person does not really understand something until [s]he can teach it to a computer." algorithmic thinking: the understanding of how to translate behavior to/from a language that a computer can understand

4 Why are algorithms important? algorithm (n): A procedure or formula for solving a problem. Consider the problem of finding a phone number in the white pages, for a person whose name is known. What is a correct, easy-to-describe (1 sentence) algorithm to solve this problem? Is there a more efficient algorithm? Which is simpler? Which is better? What assumptions did we rely on for our algorithms to work? Are the assumptions the same for each?

5 Anatomy of a Java program statement: smallest unit of execution in Java statements are grouped into sequences called methods methods must be grouped into classes a program consists of one or more classes program execution begins by executing the statements in the method main, in order

6 Java statements all categories of Java statements: class declaration assignment method call,(return) selection(if, if/else, switch) repetition(while, do/while, for, break, continue) error handling (throw, try, catch, finally) concurrency (synchronized)

7 Example statements assignment int x = * 5 + 1; String s = 3 + "4 * 5" + 1; Random rand = new Random(); method call System.out.println("hello 1+2"); Math.max(4, 20);

8 Example statements selection double d = 1.0 / 3.0; // one third if (d == 1/3) { System.out.println("equal"); } else { System.out.println("nonequal"); } repetition for (int n = 1024; n > 1; n = n / 2) { System.out.println(n); }

9 Example statements class declaration public class BankAccount { private int m_id; private double m_balance = 0.0; public BankAccount(int id) { m_id = id; } public int getID() { return m_id; } public double getBalance() { return m_balance; } // public void deposit(double amount) {... } // public void withdraw(double amount) {... } // public String toString() {... } }

10 The Scanner class Scanner reads input from the keyboard or files Scanner can read a line at a time from the input, or chop it up into tokens by whitespace and convert each token into int, double,... Class java.util.Scanner will be included in a future version of Java Instructor provides us with a temporary version to use for now You must place Scanner.java in the same folder as your programs, or else they will not compile When using Scanner, write import java.util.*; at the top of your program Download Scanner.java from

11 Input using Scanner public Scanner(InputStream source) Constructs a scanner to read input from the given source; to read from the keyboard, pass System.in. public boolean hasNext() public boolean hasNextBoolean() public boolean hasNextDouble() public boolean hasNextInt() public boolean hasNextLine() Returns true if the scanner will successfully be able to read a value from the input of the given type. public Object next() public boolean nextBoolean() public double nextDouble() public int nextInt() public String nextLine() Reads and returns a value from the input of the given type. If the scanner is unable to read the value, a NoSuchElementException will be thrown.

12 Scanner example Prompt the user to enter some number of integers, read that many integers, then print the largest of the integers. Scanner in = new Scanner(System.in); System.out.print("How many numbers? "); int numNumbers = in.nextInt(); int largest = 0; for (int i = 0; i < numNumbers; i++) { System.out.print("Enter a number: "); int currentNumber = in.nextInt(); if (i == 0 || currentNumber > largest) largest = currentNumber; } System.out.println("Largest: " + largest);

13 More Scanner examples Repeatedly read lines of input from the keyboard. Reverse each line as it's read and print the reversed line. When the user enters a blank line, end the program. The same as above, except instead of reversing the lines, reverse the letters in each word of the line, but not the lines themselves.

14 Our programming environment TCSS labs TextPad editor H drive to store your files Catalyst web system for e-submit Scanner class for input

15 References Koffman, Appendix A: Introduction to Java, pp , , ,