DOC PREVIEW
UVA CS 101 - LECTURE NOTES

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

AnnouncementsRecapVariablesSlide 4Methods1.2 Built-In Types of DataBuilt-in Data TypesBasic DefinitionsBasicsAnother ExampleTextSlide 12Subdivisions of a RulerIntegersSlide 15Integer Operations (Dr. Java Demo)Initializing VariablesFloating-Point NumbersSlide 19Math LibraryQuadratic Equation (Dr. Java Demo)Testing (Dr. Java Demo)BooleansSlide 24ComparisonsLeap YearType ConversionSlide 28SummaryStandard Input and OutputCommand-line Input vs. Standard InputSlide 32Slide 33CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 • http://www.cs.Princeton.EDU/IntroCSAnnouncements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.2RecapHelloWorld.java/******************************************* * Prints "Hello, World" * Everyone's first Java program. *******************************************/public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}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. Name (e.g., “x”)2. Type (e.g., int, double, String)3. Value3VariablesVariables are declared by the programmer–E.g., int x;How does a variable get a value?–Can be initialized in the programE.g., int x = 10–Can be computed when the program is runningE.g., x = y + z; // y and z are integers too4Literal ValueMethods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.5CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 • http://www.cs.Princeton.EDU/IntroCS1.2 Built-In Types of Data7Built-in Data TypesData type. A set of values and operations defined on those values.add, subtract, multiply, divide3.14156.022e23floating point numbersdoubleadd, subtract, multiply, divide1712345integersintand, or, nottruefalsetruth valuesbooleansequences of characterscharactersset of values operationsliteral valuestypecompare'A''@'charStringconcatenate"Hello World""CS is fun"8Basic DefinitionsVariable. A name that refers to a value.Assignment statement. Associates a value with a variable.9BasicsDefinitions.Trace.Another Example10CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 • http://www.cs.Princeton.EDU/IntroCSText12TextString data type. Useful for program input and output.13Subdivisions of a Ruler% java Ruler1 2 1 3 1 2 1 4 1 2 1 3 1 2 1"1 2 1""1 2 1 3 1 2 1""1"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); }} string concatenationCS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 • http://www.cs.Princeton.EDU/IntroCSIntegers15Integersint data type. Useful for expressing algorithms.16public 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 + " = " + sum); System.out.println(a + " * " + b + " = " + prod); System.out.println(a + " / " + b + " = " + quot); System.out.println(a + " % " + b + " = " + rem); }}Integer Operations (Dr. Java Demo)command-lineargumentsJava automatically converts a, b , and rem to type String17Initializing VariablesQ. 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 );}C codeuninitializedvariable% gcc -o uninit uninit.c% uninit-1073746048CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 • http://www.cs.Princeton.EDU/IntroCSFloating-Point Numbers19Floating-Point Numbersdouble 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 hardware20Math Library21Quadratic Equation (Dr. Java Demo)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); }} Ex. Solve quadratic equation x2 + bx + c = 0.Testing. Some valid and invalid inputs.22% java Quadratic –3.0 2.02.01.0% java Quadratic –1.0 –1.01.618033988749895-0.6180339887498949% java Quadratic 1.0 1.0NaNNaN% java Quadratic 1.0 hellojava.lang.NumberFormatException: helloTesting (Dr. Java Demo)command-line argumentsnot a numberx2 – 3x + 2x2 – x - 1x2 + x + 1CS101: Introduction to Computer Science • Slides adapted from Sedgewick and Wayne • Copyright © 2007 • http://www.cs.Princeton.EDU/IntroCSBooleans24Booleansboolean data type. Useful to control logic and flow of a program.25ComparisonsComparisons. Take operands of one type and produce an operand of type boolean.26Leap 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 2004true% java LeapYear 1900false% java LeapYear 2000trueCS101: Introduction to Computer


View Full Document

UVA CS 101 - LECTURE NOTES

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?