Download presentation
Presentation is loading. Please wait.
1
1 TCSS 143, Autumn 2004 Lecture Notes Review
2
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
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
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
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
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
7 Example statements assignment int x = 3 + 4 * 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
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
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
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 http://faculty.washington.edu/stepp/scanner/ http://faculty.washington.edu/stepp/scanner/
11
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
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
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
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
15 References Koffman, Appendix A: Introduction to Java, pp. 669 - 694, 698 - 710, 713 - 723, 731 - 735
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.