1 - University of Virginia, Department of Computer Science
Download
Report
Transcript 1 - University of Virginia, Department of Computer Science
Announcements
Are you getting my e-mail?
CS 101 Slides and Screencasts (with audio!)
available on Collab
“Lab” Assignments
HW-1 posted on Collab. Due: Jan. 28th by 11:55
PM
TA Office Hours will be posted this weekend.
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Recap
/*******************************************
* Prints "Hello, World"
* Everyone's first Java program.
*******************************************/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
HelloWorld.java
2
Variables
Every data item used by a program is
stored in a variable
Variables also provide a way of
communicating to the machine hardware
that the data needs to be stored
somewhere
A variable has three parts:
1.
2.
3.
Name (e.g., “x”)
Type (e.g., int, double, String)
Value
3
Variables
Variables are declared by the programmer
– E.g.,
int x;
How does a variable get a value?
– Can be initialized in the program
Literal Value
E.g., int x = 10
– Can be computed when the program is running
E.g., x = y + z; // y and z are integers too
4
Methods
Methods have a name and invoke some
operation or function on some data.
– System.out.println("Hello,
World!");
Some methods return a value that can be
used.
– Math.sqrt(100)
Methods like these are part of Java's
Standard Library.
5
1.2 Built-In Types of Data
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Built-in Data Types
Data type. A set of values and operations defined on those values.
type
set of values
literal values
operations
char
characters
'A'
'@'
compare
String
sequences of
characters
"Hello World"
"CS is fun"
concatenate
int
integers
17
12345
add, subtract,
multiply, divide
double
floating point
numbers
3.1415
6.022e23
add, subtract,
multiply, divide
boolean
truth values
true
false
and, or, not
7
Basic Definitions
Variable. A name that refers to a
value.
Assignment statement. Associates a
value with a variable.
8
Basics
Definitions.
Trace.
9
Another Example
10
Text
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Text
String data type. Useful for program input and output.
12
Subdivisions of a Ruler
public class Ruler {
public static void main(String[] args) {
String ruler1 = "1";
String ruler2 = ruler1 + " 2 " + ruler1;
String ruler3 = ruler2 + " 3 " + ruler2;
String ruler4 = ruler3 + " 4 " + ruler3;
System.out.println(ruler4);
}
}
"1"
"1 2 1"
"1 2 1 3 1 2 1"
string concatenation
% java Ruler
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
13
Integers
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Integers
int data type. Useful for expressing algorithms.
15
Integer Operations (Dr. Java Demo)
public class IntOps {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
int prod = a * b;
int quot = a / b;
int rem = a % b;
System.out.println(a + " + " + b + " =
System.out.println(a + " * " + b + " =
System.out.println(a + " / " + b + " =
System.out.println(a + " % " + b + " =
}
}
command-line
arguments
"
"
"
"
+
+
+
+
sum);
prod);
quot);
rem);
Java automatically converts
a, b , and rem to type String
16
Initializing Variables
Q. What happens if I forget to initialize the variable a or b?
Java compiler does not allow this.
Caveat: in other languages, variable initialized to arbitrary value.
Q. What do you mean, arbitrary?
int main( int argc, char *argv[] ) {
int whoops;
printf( "%d\n", whoops );
}
% gcc -o uninit uninit.c
% uninit
-1073746048
uninitialized
variable
C code
17
Floating-Point Numbers
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Floating-Point Numbers
double data type. Represent approximations of real numbers. Useful
in scientific applications.
The precision of these
numbers depends in the
number of bits used to
represent them in
hardware
19
Math Library
20
Quadratic Equation (Dr. Java Demo)
Ex. Solve quadratic equation x2 + bx + c = 0.
b b 2 4 c
roots
2
public class Quadratic {
public static void main(String[] args) {
// parse coefficients from command-line
double b = Double.parseDouble(args[0]);
double c = Double.parseDouble(args[1]);
// calculate roots
double discriminant = b*b - 4.0*c;
double d = Math.sqrt(discriminant);
double root1 = (-b + d) / 2.0;
double root2 = (-b - d) / 2.0;
}
}
// print them out
System.out.println(root1);
System.out.println(root2);
21
Testing (Dr. Java Demo)
Testing. Some valid and invalid inputs.
% java Quadratic –3.0 2.0
2.0
1.0
command-line arguments
% java Quadratic –1.0 –1.0
1.618033988749895
-0.6180339887498949
% java Quadratic 1.0 1.0
NaN
NaN
not a number
x2 – 3x + 2
x2 – x - 1
x2 + x + 1
% java Quadratic 1.0 hello
java.lang.NumberFormatException: hello
22
Booleans
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Booleans
boolean data type. Useful to control logic and flow of a program.
24
Comparisons
Comparisons. Take operands of one type and produce an operand of
type boolean.
25
Leap Year
Q. Is a given year a leap year?
A. Yes if either (i) divisible by 400 or
(ii) divisible by 4 but not 100.
public class LeapYear {
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
boolean isLeapYear;
// divisible by 4 but not 100
isLeapYear = (year % 4 == 0) && (year % 100 != 0);
// or divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0);
System.out.println(isLeapYear);
}
}
% java LeapYear 2004
true
% java LeapYear 1900
false
% java LeapYear 2000
true
26
Type Conversion
CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 •
Type Conversion
Type conversion. Convert from one type of data to another.
Automatic: no loss of precision; or with strings.
Explicit: cast; or method.
28
Summary
A data type is a set of values and operations on those values.
String
text processing.
double, int
mathematical calculation.
boolean
decision making.
Be aware.
Declare type of values.
Convert between types when necessary.
In 1996, Ariane 5 rocket exploded after takeoff
because of bad type conversion.
29
Standard Input and Output
Needed for HW-1
More details with Lab 1
Command-line Input vs. Standard Input
Command line inputs.
Use command line inputs to read in a few user values.
Not practical for many user inputs.
Input entered before program begins execution.
Standard input.
Flexible OS abstraction for input.
By default, stdin is received from Terminal window.
Input entered while program is executing.
31
Standard Input and Output
Standard input. We provide library StdIn to read text input.
Standard output. We provide library StdOut to write text output.
32
Standard Input and Output
public class Add {
public static void main(String[] args) {
StdOut.print("Type the first integer: ");
int x = StdIn.readInt();
StdOut.print("Type the second integer: ");
int y = StdIn.readInt();
int sum = x + y;
StdOut.println("Their sum is " + sum);
}
}
% java Add
Type the first integer: 1
Type the second integer: 2
Their sum is 3
33