COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 26, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 12, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming
COMP 110: Introduction to Programming Tyler Johnson Mar 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Basic Java Constructs and Data Types – Nuts and Bolts
Introduction to Programming
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Control Structures Selections Repetitions/iterations
Decisions If statements in C.
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS110 Programming Language I
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 110 Primitive Types, Strings, and Console I/O Tabitha Peck M.S. January 23, 2008 MWF 3-3:50 pm Philips
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
22/11/ Selection If selection construct.
CS 106 Introduction to Computer Science I 01 / 26 / 2007 Instructor: Michael Eckmann.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
COMP 110: Spring Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at.
Decisions, Decisions, Decisions Conditional Statements In Java.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Data Types and Expressions
CS0007: Introduction to Computer Programming
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
IDENTIFIERS CSC 111.
Introduction to C++ Programming
SELECTION STATEMENTS (2)
Documentation and Style
CS2011 Introduction to Programming I Selections (I)
CSC 1051 – Data Structures and Algorithms I
Unit 3: Variables in Java
Just Enough Java 17-May-19.
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Announcements Lab 1 due tomorrow by midnight TA Office Hours (SN 043) Nick Dragan - Tues/Thurs 2-3pm Georgi Tsankov – Tues 4-6pm

COMP 110: Spring Questions?

COMP 110: Spring Today in COMP 110 Documentation and Style Branching

COMP 110: Spring Documentation & Style The programs you write should be correct They should also be easy to read and understand

COMP 110: Spring Variable Names Part of creating good programs is choosing meaningful variable names If a variable stores a count, call it count, if a variable represents a cost, call it cost

COMP 110: Spring Variable Names Variables are typically named using only numbers and digits int count; int grade1, grade2; Underscores are generally avoided in variable names

COMP 110: Spring Capitalization in Variable Names Variable and object names are generally written in lower case double cost, ratio; Names consisting of multiple words should use capital letters at word boundaries int numberOfTries; double costOfLiving;

COMP 110: Spring Capitalization in Class Names Class names generally have the first letter capitalized Scanner, String

COMP 110: Spring Naming Examples String sentence; Scanner keyboard; String personalGreeting; YourClass yourObject;

COMP 110: Spring Comments The best programs are self- documenting It should be obvious what the program does just by reading the program itself

COMP 110: Spring Comments Choosing meaningful variables names is sometime not enough Comments allow the programmer to leave notes in the code to aid in understanding

COMP 110: Spring Comments in Java Two Types Single-line comment Begins with // int i; //this is a single-line comment, write whatever you want Multi-line comment Everything within /* */ public class Program { /* This is a multi-line comment. The compiler will completely ignore this text */ public static void main(String[] args) {

COMP 110: Spring Comments Using a program header is a great way to give basic information about a program Author Description of the program Inputs Outputs

COMP 110: Spring Comments You should use comments to explain any non-obvious details in your programs double radius; //in inches double area; //in square inches

COMP 110: Spring Indentation Programs have a nested structure Your indentation should reflect this import java.util.Scanner; public class FirstProgram { public static void main(String[] args) { System.out.println("Hello out there."); System.out.println("I will add two numbers for you."); System.out.println("Enter two whole numbers on a line:"); int n1, n2; Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt(); System.out.println("The sum of those two numbers is"); System.out.println(n1 + n2); } }

COMP 110: Spring Placement of Braces Some prefer public class FirstProgram { … } Others prefer public class FirstProgram { … } It doesnt matter what you choose, just be consistent

COMP 110: Spring Using Named Constants You should always give a name to constants such as PI It may not be obvious to the reader where such a number comes from final double PI = ; area = PI * radius * radius; //is much clearer than area = * radius * radius;

COMP 110: Spring Naming Constants The style for naming constants if different than for variables Typically use all caps with underscores separating words final int DAYS_PER_WEEK = 7; final double MASS_OF_EARTH = e24; //kg final float INTEREST_RATE = 5.55;

COMP 110: Spring Branching Sometimes, it is necessary to make decisions in programs Example The remainder operator can be used to determine if a number n is even or odd If n%2 equals 0, n is even If n%2 equals 1, n is odd

COMP 110: Spring Branching int result = n % 2; Evaluate result is 0? Execute Print n is even Execute Print n is odd true false

COMP 110: Spring If-Else Statement An if-else statement allows us to make decisions in a program int result = n % 2; if(result == 0) System.out.println(That number is even!); else System.out.println(That number is odd!);

COMP 110: Spring If-Else Example int n = 2 int result = n % 2; if(result == 0) System.out.println(That number is even!); else System.out.println(That number is odd!); System.out.println(Finished!); result = 0

COMP 110: Spring If-Else Example int n = 3 int result = n % 2; if(result == 0) System.out.println(That number is even!); else System.out.println(That number is odd!); System.out.println(Finished!); result = 1

COMP 110: Spring Boolean Expressions (result == 0) is a boolean expression Boolean expressions evaluate to either true or false Examples 10 > 5, (true) 4 > 6, (false) Integers are whole numbers, (true)

COMP 110: Spring Java Comparison Operators Math Java Name ===Equal to !=Not equal to >>Greater than >=Greater than or equal to <<Less than <=Less than or equal to

COMP 110: Spring If-Else Statement Syntax Syntax if(Boolean_Expression) Statement_1 else Statement_2 If Boolean_Expression is true, Statement_1 is executed; otherwise Statement_2 is executed

COMP 110: Spring Compound Statements Multiple statements can be included in each branch Called a compound statement Enclose between {…} if(Boolean_Expression) { Statements_1 } else { Statements_2 }

COMP 110: Spring If without Else Syntax if(Boolean_Expression) Statement_1 Example if(accntBalance < 0) //subtract a fee for overdrafts accntBalance = accntBalance – FEE;

COMP 110: Spring Common Comparison Mistakes Dont confuse the assignment operator (=) with the comparison operator (==)! if(x == y) //valid if(x = y) //syntax error

COMP 110: Spring Common Comparison Mistakes Dont use a semi-colon here if(x == y) ; //syntax error System.out.println(x equals y);

COMP 110: Spring Common Comparison Mistakes Dont use == to compare Strings Use string.equals(A_String) or string.equalsIgnoreCase(A_String) Example String s1 = keyboard.next(); //read in a string if(s1.equals(Hello)) System.out.println(The String is Hello.); else System.out.println(The String is not Hello.);

COMP 110: Spring The && Operator (AND) We can check for multiple conditions using the && (AND) operator Meaning is similar to that of English and if (temperature > 50 && temperature < 75) { // walk to school if 50 < temperature < 75 } else { //otherwise drive to school }

COMP 110: Spring The || Operator (OR) We can also join boolean expression with || (OR) Meaning is similar to that of English or boolean raining, runningLate; … if (raining || runningLate) { //drive to school }

COMP 110: Spring The ! Operator (NOT) Boolean negation !false is true !true is false Example boolean cloudy; … if (!cloudy) { // walk to school if its not cloudy }

COMP 110: Spring Effect of Boolean Operators ABA && BA || B!A true false truefalse truefalse truefalsetrue false true

COMP 110: Spring Boolean Expression Examples Using x = 5, y = 10, z = 15 (x x) (false && true) -> false (x x) (true || true) -> true (x > 3 || z != 15) (true || false) -> true (!(x > 3) && x + y == z) (false && true) -> false

COMP 110: Spring Avoiding the Negation Op !(A !(A > B) --> !(A >= B) --> !(A == B) --> !(A != B) --> (A >= B) (A > B) (A <= B) (A < B) (A != B) (A == B) Its best to avoid use of the negation operator (!) when possible

COMP 110: Spring Nested If-Statements Its possible to have if statements inside other if statements if(Boolean_Expression_1) { if(BooleanExpression_2) { … }

COMP 110: Spring Nested If Statements Example We want to perform some checks on the users account balance If the balance is >= 0, well add interest only if the interest rate is >= 0, and print an error message if interest rate is < 0. If the balance is < 0, well subtract a fee.

COMP 110: Spring Nested If Statements Evaluate balance >= 0 Evaluate INTEREST_RATE >= 0 Execute balance -= FEE truefalse Execute balance = balance + balance*INTEREST_RATE Execute Print Error Message a truefalse

COMP 110: Spring Nested If Statements if(balance >= 0) { if(INTEREST_RATE >= 0) balance = balance + INTEREST_RATE*balance; else System.out.println(Negative Interest!); } else balance = balance – FEE;

COMP 110: Spring Nested If Statements What if we didnt need to print the error message? if(balance >= 0 && INTEREST_RATE >= 0) balance = balance + INTEREST_RATE*balance; else balance = balance – FEE;

COMP 110: Spring Multi-Branch If Statements What if we need to decide between many possibilities? Example Given a numeric score (0..100) determine whether the grade is an A,B,C,D, or F

COMP 110: Spring Multi-Branch If Statements A if: score >= 90 B if: 90 > score >= 80 C if: 80 > score >= 70 D if: 70 > score >= 60 F in all other cases

COMP 110: Spring Multi-Branch If Statements We could write this as follows if(score >=90) grade = A; else if(score >=80) grade = B; else if(score >=70) grade = C; else if(score >=60) grade = D; else grade = F;

COMP 110: Spring Multi-Branch If Statements The preferred way to write this is if(score >=90) grade = A; else if(score >=80) grade = B; else if(score >=70) grade = C; else if(score >=60) grade = D; else grade = F;

COMP 110: Spring Multi-Branch If Statement Syntax if(Boolean_Expression_1) Action_1 else if(Boolean_Expression_2) Action_2 … else if(Boolean_Expression_n) Action_n else Default_Action

COMP 110: Spring Programming Demo Write a program to read in three distinct nonnegative integers from the keyboard and display them in increasing order

COMP 110: Spring Programming Demo Call the three numbers a,b,c Designing the algorithm Determine which of a,b,c is the smallest If its not a, is it b? If its not a or b, must be c Determine which of the two remaining integers is smaller than the other

COMP 110: Spring Programming Demo Evaluate a is smallest? true false a < b < ca < c < b Evaluate b < c true false b < a < cb < c < a Evaluate a < c true false c < a < bc < b < a Evaluate a < b true false Evaluate b is smallest? truefalse

COMP 110: Spring Programming Demo Pseudocode Ask user for three integers, a,b,c Determine which of a,b,c is the smallest Determine which of the remaining two is smaller Print a,b,c in ascending order

COMP 110: Spring Friday Recitation Bring Laptop (fully charged) Textbook Any questions about Program 1