CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
More Recursion: Permutations and Towers of Hanoi COP 3502.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Introduction to Computer Programming Error Handling.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exception Handling How to handle the runtime errors.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Recursion occurs when a method calls itself. Google “recursion”
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Recursion Version 1.0.
Testing Verification and the Joy of Breaking Code
Chapter 7 User-Defined Methods.
Chapter 6 CS 3370 – C++ Functions.
Exceptions In this lecture:
Example: Vehicles What attributes do objects of Sedan have?
Templated recursive linked lists
Introduction Exception handling Exception Handles errors
CS1101: Programming Methodology Recitation 7 – Exceptions
Chapter 5: Control Structures II
Introduction to Exceptions in Java
Chapter 14: Exception Handling
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
User-defined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
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.
Arrays & Functions Lesson xx
Selection CIS 40 – Introduction to Programming in Python
CMSC 202 Exceptions.
Topics Introduction to File Input and Output
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Functions I Creating a programming with small logical units of code.
Lesson 04: Conditionals Class Chat: Attendance: Participation
Structured Program
User-defined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Exception Handling.
Module 4 Loops.
Building Java Programs
Functions, Part 1 of 3 Topics Using Predefined Functions
CIS 110: Introduction to Computer Programming
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Indefinite loop variations
Introduction to Computing Lecture 08: Functions (Part I)
Topics Introduction to File Input and Output
Functions, Part 1 of 3 Topics Using Predefined Functions
LCC 6310 Computation as an Expressive Medium
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CMSC 202 Exceptions.
CIS 110: Introduction to Computer Programming
Presentation transcript:

CIS 110: Introduction to Computer Programming Lecture 12 Authoring Solid Helper Methods (§ 4.4) 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

System.out.printf An alternative to println/print that lets you format the output. A format specifier. A placeholder for a thing to print. System.out.printf("Example of printf: %d %.2f %s", 12, 1.241, "Chowder"); > Example of printf: 12 1.24 Chowder Specifiers have the form: %<formatting><type> Need to provide one argument per format specifier. They are consumed in-order. See p. 260 of the book for more information about format specifiers. 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Helper Methods See PalindromeChecker.java 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Sample Problem Problem: write a program that reads in a String from the user, checks to see if that String is a palindrome, and informs the user of the results of the check. Example output > Enter a string to check: > abba > The reverse of the line is: abba > The line is a palindrome! 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Our Methodology Try some example inputs to get a feel for the problem. Start with a skeleton of the solution. Decompose the problem into sub-problems. Make helper methods to solve the sub-problems. Use those helper methods to solve your main problem. 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Helper Methods Critical pieces of code that do the work. Decomposition allows us to identify these methods and focus our time on getting them right. public static String reverse(String s) { String ret = ""; for (int i = 0; i < s.length(); i++) { ret = s.charAt(i) + ret; } return ret; 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

Restrictions on Method Parameters Like user input, sometimes we wish to limit what we can pass into a method. public static String gpaToGrade(double gpa) { if (gpa > 3.3) { return "A"; } else if (gpa > 2.5) { return "B"; } else if (gpa > 1.7) { return "C"; } else if (gpa > 0.7) { return "D"; } else { return "=("; } GPA should be non-negative and less than 4.0 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

Pre- and Post-Conditions Pre- and post-conditions formalize these restrictions. Pre-condition: a requirement on the parameters that must be true for the method to work correctly. Post-condition: a guarantee made by the method if all of its pre-conditions are met. // Given a GPA, returns a letter grade for that GPA. // pre: 0 <= gpa <= 4.0 // post: a letter grade or a sad face if the gpa is... // less than ideal. public static String gpaToGrade(double gpa) { // ... } 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

Exceptions We can use exceptions to enforce the pre-condition instead of trusting the programmer! Example of defensive programming. // Given a GPA, returns a letter grade for that GPA. // pre: 0 <= gpa <= 4.0 // post: a letter grade or a sad face if the gpa is... // less than ideal. public static String gpaToGrade(double gpa) { if (gpa < 0 || gpa > 4.0) { throw new IllegalArgumentException("GPA out of range"); } // ... Raises an error like we’ve seen with out-of-bounds charAt. 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

Anatomy of Throwing an Exception An informative message to be printed by the Exception throw raises the exception, immediately exiting successive methods until the entire program is aborted. throw new IllegalArgumentException("GPA out of range"); “new …” creates a new object of type IllegalArgumentException. We’ll learn how to catch exceptions and author our own later in the course. 7/26/2019 CIS 110 (11fa) - University of Pennsylvania

Control Flow in a Method Both throw and return allow us to exit a method prematurely. throw: with an error return: with a value Aside: we can return from methods that don’t return values to immediately stop execution. public static void printIfPositive(int x) { if (x < 0) { return; } System.out.println(x + " is positive!"); 7/26/2019 CIS 110 (11fa) - University of Pennsylvania