Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Similar documents
Warm-up: What type of variable should we create to store the following information:

Warm-Up: COMP Programming with Iterations 1

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

COMP-202 Unit 2: Java Basics. CONTENTS: Printing to the Screen Getting input from the user Types of variables Using Expressions and Variables

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

Midterm Examination (MTA)

COMP-202: Foundations of Programming

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

Chapter 5 Lab Methods

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Conditional Programming

Chapter 3. Selections

AP CS Unit 3: Control Structures Notes

Top-Down Program Development

COMP-202 Unit 4: Programming with Iterations

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

St. Edmund Preparatory High School Brooklyn, NY

Java Coding 3. Over & over again!

Repetition, Looping. While Loop

1 Short Answer (10 Points Each)

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Last Class. While loops Infinite loops Loop counters Iterations

ASSIGNMENT 1 Expressions, Data Types, and Simple Calculations

Algorithms and Conditionals

CEN 414 Java Programming

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Practice Midterm 1. Problem Points Score TOTAL 50

Fundamentals of Programming Data Types & Methods

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Loops. CSE 114, Computer Science 1 Stony Brook University

Lecture 8 " INPUT " Instructor: Craig Duckett

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

What two elements are usually present for calculating a total of a series of numbers?

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Chapter 5 Lab Methods

Topics. The Development Process

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Java Classes: Math, Integer A C S L E C T U R E 8

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

AP COMPUTER SCIENCE A

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

COMP 202 Java in one week

Object Oriented Programming. Java-Lecture 6 - Arrays

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Flow of Control: Loops. Chapter 4

COMP 202. Java in one week

Lecture 5: Methods CS2301

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Chapter 2. Elementary Programming

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Introduction to Java Applications

CS111: PROGRAMMING LANGUAGE II

Topic 11 Scanner object, conditional execution

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Topic 11 Scanner object, conditional execution

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Lesson 7 Part 2 Flags

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

AP Computer Science Unit 1. Programs

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Oct Decision Structures cont d

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

COMP 110 Programming Exercise: Simulation of the Game of Craps

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Place your name tag here

Elementary Programming

CS 112 Introduction to Programming

CS 112 Introduction to Programming

1 Short Answer (10 Points Each)

Chapter 6 Lab Classes and Objects

Loops. GEEN163 Introduction to Computer Programming

4. Java language basics: Function. Minhaeng Lee

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Chapter 6 Lab Classes and Objects

b. Suppose you enter input from the console, when you run the program. What is the output?

CS141 Programming Assignment #6

Chapter 2: Data and Expressions

CS 101 Spring 2007 Midterm 2 Name: ID:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Question: Total Points: Score:

CSC 1051 Data Structures and Algorithms I

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

CPSC Introduction to Computer Science for Computer Science Majors II Monir Zaman (Md Moniruzzaman)

Lab Exercise 1. Objectives: Part 1. Introduction

Transcription:

Warm up Exercise What are the types and values of the following expressions: 3.0+4 * (3 + 1) 3 / 2 + 1.0 1.0 / 2.0 (int)1.0 / 2 COMP-202 - Programming Basics 1

Warm up Exercise What are the types and values of the following expressions: 3.0+4 * (3 + 1) double 28 3 / 2 + 1.0 double 2 1.0 / 2.0 double.5 (int)1.0 / 2 int 0 COMP-202 - Programming Basics 2

Warm-up: What type of variable should we create to store the following information: The number of school days in a year. The cost of a meal at a restaurant The number of minutes you will spend on COMP-202 during the term A PIN for an ATM card Whether it snowed on January 3 rd The number of games the Rangers will win in the next 20 years The number of games the Canadiens will win in the next 20 years COMP-202 - Introduction 3

Warm-up: What type of variable should we create to store the following information: The number of school days in a year. (int) The cost of a meal at a restaurant (double or int) The number of minutes you will spend on COMP-202 during the term (int/short/long --depends on how optimistic you are!) A PIN for an ATM card (int or String) Whether it snowed on January 3 rd (boolean) The number of games the Rangers will win in the next 20 years (definitely a short) The number of games the Canadiens will win in the next 20 years (a long?) COMP-202 - Introduction 4

Practice exercises : Void method Write a method called saygreeting(). The method should take as input two Strings. One String should be the name of the speaker, the other String should be the name of the listener. Now try calling this method. Hint: Think about what the method header should be for this method (on the next slide) COMP-202 - Programming Basics 5

Practice exercises : Void method Write a method called saygreeting(). The method should take as input two Strings. One String should be the name of the speaker, the other String should be the name of the listener. Now try calling this method from the main method. Hint: The method header for this would be: public static void saygreeting(string speaker, String listener) the method does not return a value The method takes as input a String for speaker and a String for listener COMP-202 - Programming Basics 6

Practice exercises : Void method public class Exercise { public static void saygreeting(string speaker, String listener){ System.out.println( Hi + listener + my name is + speaker + it's great to finally meet you!); public static void main(string[] args) { saygreeting( Jim, John ); //can put any expressions with type of String saygreeting( Jo + hn, Ji + 'm'); COMP-202 - Programming Basics 7

Practice exercises : Non-void Method Write a method called computeareacircle() The method should take as input the radius of the circle and return a double representing the area of the circle. Remember the formula for area of a circle is Area = PI * r^2 Now ask the user to enter 3 numbers (one at a time) and for each of these output the area of a circle with that radius. Hint: You can use the Math library to get the value of PI Hint: Think about what the method header should be for this method (on the next slide) COMP-202 - Programming Basics 8

Practice exercises : Non-void Method public static double computeareacircle(double radius) COMP-202 - Programming Basics 9

Practice exercises : Non-void Method public static double computeareacircle(double radius) { return Math.PI * Math.pow(radius, 2); COMP-202 - Programming Basics 10

Warm up Exercise Write a method called calculateareatriange Your method should take as input 2 doubles base and height and return a double representing the area of a triangle with those dimensions. Remember that the area of a triangle is ½ * b * h How would you call this method from another method to calculate the area of a triangle with B=3, H=4 Hint: If you are stuck, first try to think of the the method header. COMP-202 - Programming Basics 11

Warm up Exercise public static double calculateareatriangle(double base, double height) { return.5 * base * height; You could call the method from another method by writing: calculateareatriangle(3,4); COMP-202 - Programming Basics 12

Warm up Exercise public static double calculateareatriangle(double base, double height) { return.5 * base * height; You could call the method from another method by writing: calculateareatriangle(3,4); or double base = 4.0; double height = 3.0; calculateareatriange(height, base); <---probably a bug! COMP-202 - Programming Basics 13

Warm up Exercise : Eek! public static double calculateareatriangle(double base, double height) { return 1 / 2 * base * height; Careful! There's an integer division issue above! Since 1 / 2 is 0. COMP-202 - Programming Basics 14

Example: Careful public static int confusing(int x, int y) { y++; public static void main(string[] args) { int x = 3; int y = 4; confusing(y,x); COMP-202 - Programming Basics 15

Example: Careful Executing the previous code has no real effect. -confusing(y,x) ---> evaluate expression y and evaluate expression x -First expression has value 4, second has value 3 -These 2 expressions get copied into DIFFERENT variables that also are called x and y inside confusing() -At the beginning of the method confusing(), x has the value 4 and y has the value 3. -We add one to the variable y inside confusing(), but no change in the main method COMP-202 - Programming Basics 16

Exercise: Write a method absolutevalue that takes as input a double and returns the absolute value of that number. COMP-202 - Programming Basics 17

First step: Method header public static double absolutevalue(double x) { COMP-202 - Programming Basics 18

First step: Method header public static double absolutevalue(double x) { if ( x > 0 ) { return x; else { return -x; COMP-202 - Programming Basics 19

First step: Method header public static double absolutevalue(double x) { if ( x > 0 ) { return x; else { return -x; COMP-202 - Programming Basics 20

Overall program public class Numbers public static double absolutevalue(double x) { if ( x > 0 ) { return x; return -x; public static void main(string[] args) { System.out.println(absoluteValue(-3)); double difference = absolutevalue(3- -3);... COMP-202 - Programming Basics 21

Exercise: Complete the main() method of the MinOfThree class by adding code which determines which of the three numbers entered by the user is the smallest number, and displays that number COMP-202 - Conditional Programming 22

MinOfThree.java import java.util.scanner; public class MinOfThree { public static void main(string[] args) { Scanner keyboard = new Scanner(System.in); int num1, num2, num3, min; System.out.print("Enter a number: "); num1 = keyboard.nextint(); System.out.print("Enter another number: "); num2 = keyboard.nextint(); System.out.print("Enter a third number: "); num3 = keyboard.nextint(); // Add your code here COMP-202 - Conditional Programming 23

Warm-Up: Suppose I have a method with header: public static boolean foo(boolean a,int b) { if (a) return true; return b > 0; Which of the following are valid ways to call the method and what is the result? int x = foo(true, 3 + 4); System.out.println(foo(!true, -10)); System.out.println(foo(true && false, 1.0 / 2)); System.out.println(foo(foo(false,0), (int)1.0 / 2)); boolean a = foo(foo(false true, 3), -10); COMP-202 - Programming with Iterations 24

Warm-Up: Suppose I have a method with header: public static boolean foo(boolean a,int b) { if (a) return true; return b > 0; Which of the following are valid ways to call the method and what is the result? int x = foo(true, 3 + 4); does not compile (foo returns boolean) System.out.println(foo(!true, -10)); -->prints false System.out.println(foo(true && false, 1.0 / 2)); -->does not compile (1.0/2 is double) System.out.println(foo(foo(false,0), (int)1.0 / 2)); -->prints false boolean a = foo(foo(false true, 3), -10); stores into variable a value true COMP-202 - Programming with Iterations 25