COMS W1004 (CS I)
Intro to CS and Java Programming
Lecture 19: November 11, 2004
Lecture Outline
- A prediction from the past
- Review
- Computer science as a discipline
- Theory
- Scanner class
- Programming assignment #5
- Reading assignment
2. Review
- Representing data
- Floating-point numbers
- Images
- Sound
3. Computer science as a discipline
- Computer science is the study of
- computation
- computers
- complexity
- informatics
- Computer science is the study of computers and what they can do - the inherent powers and limitations of abstract computers, the design and characteristics of real computers, and the innumerable applications of computers to solving problems.
- The scientific method:
- Formulate a hypothesis.
- Design an experiment to test the hypothesis.
- Conduct the experiment.
- Analyze the results to see if they support the hypothesis.
- Computer science encompasses hardware, software and theory
- Subfields of computer science
- Algorithms and data structures
- Artificial intelligence
- Computational biology
- Computational science
- Computer engineering
- Computer vision
- Database systems
- Graphics
- Human-computer interaction
- Machine architecture
- Machine learning
- Networking
- Programming languages and compilers
- Operating systems
- Quantum computing
- Robotics
- Software engineering
- Security
4. Theory
- Turing machine: Reed, Fig. 10.4
- Is there a most powerful computer?
- Universal Turing machine
- We can construct a Turing machine that takes as input a specification of another Turing machine M and an input x, and simulates the behavior of M on x.
- Church-Turing thesis
- The set of Turing-computable functions is the same as the set of all computable functions.
- Colloquially, anything that can be computed can be computed by a Turing machine.
- Halting problem
- Given a program and an input to the program, answer "yes" if the program will eventually stop with this input; otherwise, answer "no."
- We can prove no such program exists.
- We say the halting problem is noncomputable (or undecidable).
- Nondeterminism
- A nondeterministic Turing machine can have moves in which it can make a choice of next move.
- Acceptance is defined if there exists a finite sequence of moves that leads to success.
- Nondeterministic Turing machines are no more powerful than deterministic Turing machines.
- P versus NP
- P is the set of problems that can be solved in polynomial time on a deterministic Turing machine.
- NP is the set of problems that can be solved in polynomial time on a nondeterministic Turing machine.
- Does P = NP? There is a one million prize for answering this question!
- NP-complete problems
- An NP-complete problem is a problem in NP that is as hard as any other problem in NP. The Traveling Salesman Problem is an example of an NP-complete problem.
5. Scanner class
- Java 1.5 has introduced the
Scanner class for keyboard input and parsing
- Note: use
Scanner scan = new Scanner(System.in);
- rather than
Scanner scan = Scanner.create(System.in);
- Here is a Java program that uses the
Scanner class to read and print a line of text entered by the user:
//*********************************
// Echo.java
// Java program to read a line of text
// and then print it
// Created by Al Aho on November 10, 2004
// Last modified on November 10, 2004
//*********************************
import java.util.Scanner;
public class Echo
{
//--------------------------------------
// Read a character string from the user
// and then print it
//--------------------------------------
public static void main (String[] args)
{
String message;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a line of text:");
message = scan.nextLine();
System.out.println("You entered: \"" + message + "\"");
}
}
Here is a Java program that asks for two integers from the keyboard, adds them, and then prints the sum.
//**************************************
// Addition.java
// Java program to add two integers
// entered via keyboard
// Created by Al Aho on November 10, 2004
// Last modified on November 10, 2004
//**************************************
import java.util.Scanner;
public class KeyAddition
{
//------------------------------------------
// Adds two integers entered via keyboard
//------------------------------------------
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int number1;
int number2;
int sum;
String again;
do {
System.out.print("Enter first integer: ");
number1 = scan.nextInt();
System.out.print("Enter second integer: ");
number2 = scan.nextInt();
sum = number1 + number2;
System.out.println("The sum is " + sum);
System.out.print("Do Another? (y/n): ");
again = scan.next();
}
while (again.equals("y"));
System.exit(0);
}
}
7. Reading assignment
- Lewis and Loftus, Sections 2.6 and 4.1-4.4
aho@cs.columbia.edu