Lecture 2: Static Methods, if statements, homework uploader.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

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.
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Building Java Programs
CS0007: Introduction to Computer Programming
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
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.
11-Jun-15 Just Enough Java. 2 What is Java? Java is a programming language: a language that you can learn to write, and the computer can be made to understand.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Lecture 11. Today’s topic Conditional statement –Relational operators –if statement –if-else statement –If-elseif statement.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSC 204 Programming I Loop I The while statement.
Copyright © Curt Hill Mathematics Functions An example of static methods.
Lecture 2: Classes and Objects, using Scanner and String.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSC Programming I Lecture 5 August 30, 2002.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Lecture 18: 10/31/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Truth and while Today 15 Minutes online time to finish the random/Swing programs. Truth tables: Ways to organize results of Boolean expressions. Note Taking:
Arithmetic expressions containing Mathematical functions.
The Math Class Methods Utilizing the Important Math Operations of Java!
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Decision Statements, Short- Circuit Evaluation, Errors.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
Lesson 4: Introduction to Control Statements 4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Chapter 5 The if Statement
CS0007: Introduction to Computer Programming
Introduction to programming in java
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 4 Conditionals.
Truth tables: Ways to organize results of Boolean expressions.
Truth tables: Ways to organize results of Boolean expressions.
Building Java Programs
Relational Operators.
Introduction to Computer Science I.
Comparing Data & the ‘switch’ Statement
Just Enough Java 17-May-19.
Presentation transcript:

Lecture 2: Static Methods, if statements, homework uploader

Primitive Review Suppose we have double x = 4; double y = 3; x = y + x; y = y*2; what are the final values of x and y ?

Intro to Classes in Java Classes in Java come in two forms: – classes that package statements into static methods used to perform a routine computation (such as the Math class) or applications such as MathTrick.java which have – public static void main(String [] args) – classes that are used to make new data types that go beyond the limits of the primitive variables we have used so far. These classes create objects (more sophisticated data variables) that we can manipulate in our program.

Today: Math class Java's Math class is of the first type. – contains static methods for doing math – use the class name in front of each method – javadoc for Math class – google "java math class" javadoc – the expression Math.sqrt(x) computes the square-root of the number currently in x suppose y = 9 then Math.sqrt(y) evaluates to 3 the expression Math.sqrt(y) is replaced by the value 3

Math.sqrt() takes an argument When you say Math.sqrt( radius ) – the variable radius is the "argument" of sqrt method – Math.sqrt( 24); now 24 is the argument – this is the value it will find the square root of Arguments can be numbers, variables, or expressions. suppose y = 16. Find – Math.sqrt( 16); – Math.sqrt( 4*y ); – Math.sqrt( y + 9 );

Storing or Printing the result STORE: – To store the result of a Math.sqrt() expression – assign the expression to another variable – y = Math.sqrt(x); // now y gets the 3 – then you can use the result for more computation PRINT: – Enclose the expression in a print statement: – System.out.println( Math.sqrt(x));

Example – Pythagorean Theorem double width, height, hypotenuse; // sides of a triangle width = 42.0; height = 17.0; hypotenuse = Math.sqrt( width*width + height*height ); System.out.print("A triangle with sides 42 and 17 has hypotenuse "); System.out.println(hypotenuse);

Other Math static methods abs(double a) abs – Returns the absolute value of a double value. cos(double a) cos – Returns the trigonometric cosine of an angle. exp(double a) exp – Returns Euler's number e raised to the power of a double value. random() random – Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

Math.random() takes no argument When you say Math.sqrt( radius ) – the variable radius is the "argument" of sqrt method Math.random() doesn't need anything in () – it always gives a random value between 0 and 1 System.out.print("\nHere is a random #: "); System.out.println( Math.random() );

Code BLOCKS In Java we use { } to make code "blocks" Examples:

Let's "trace" that last example

Basic If Statement Simplest if uses a condition test to determine whether to execute a code block example

Let's see what this does to x and y suppose x = 4 and y = 2 now x = ? and y = ? what if x was 3 and y was 9 originally?

Other Boolean expressions Every if needs a boolean expression to test whether it should do the code block In addition to ">" there are a total of 6 comparison operators:, =, !=, ==

Comparison Operators

Try these: determine True or False Assume x = 18, y = 20, z = 4 (x < y ) (z > 6 ) (y < z ) (x != y) (z == 4) (y != x )

More if variations if can also be used with else to choose between one of two possible code blocks: if the boolean is true we do the first block otherwise (else) we skip it and do the second block

Example

Multi-way if-else A final option is to put another if statement right after "else" if ( score > 90 ) { System.out.println("Grade is A"); } else if (score > 80) { System.out.println("Grade is B"); } else {  No Boolean here. Default for "none of above" System.out.println("Grade is C"); }

Lab 2 Create Lab2 project in BlueJ We experiment with – generating random numbers – using if and if-else to simulate coin toss heads, tails, edge, fall off table compare scores from two coin tosses Turn in procedure. When finished... – Project>Create Jar File, "include source files", Lab2 – Click "Homework Uploader" link – Enter passcode, estimated score, browse to Lab2.jar – then click upload