Agenda Warmup Lesson 1.4 (double precision, String methods, etc)

Slides:



Advertisements
Similar presentations
Strings Testing for equality with strings.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Constants A constant is a variable whose value is expected to remain the same throughout a program It is considered “good programming style” to use constants.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Fundamental Programming Structures in Java: Strings.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CPS120: Introduction to Computer Science
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Declaring variables The type could be: int double char String name is anything you want like lowerCaseWord.
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.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Repetition Statements
Topic 2 Elementary Programming
A variable is a name for a value stored in memory.
Agenda Warmup Lesson 1.4 (double precision, String methods, etc)
Chapter 2 Basic Computation
Chapter 2: Introduction to C++
Strings, StringBuilder, and Character
Agenda Warmup Finish 2.4 Assignments
Object Oriented Programming
Agenda Warmup AP Exam Review: Litvin A2
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
EGR 2261 Unit 4 Control Structures I: Selection
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 2: Introduction to C++
Chapter 2 Basic Computation
Math in C The math blocks you've used in Scratch can all be recreated in C!
Control Statement Examples
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Primitive and Reference Data Values
Object Oriented Programming (OOP) LAB # 8
Type Conversion, Constants, and the String Object
Chapter 7: Strings and Characters
Introduction to C++ Programming
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Exposure Java 2015 Pre-AP®CS Edition Chapter 12 Slides String Methods
Week 9 – Lesson 1 Arrays – Character Strings
Numbers.
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Module 4 Loops.
Coding Concepts (Data- Types)
Chapter 3: Selection Structures: Making Decisions
Welcome to AP Computer Science A!
Welcome to AP Computer Science A!
Agenda Warmup Lesson 2.6 (Constructors, Encapsulation)
Introduction to Primitives
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Chapter 3: Selection Structures: Making Decisions
Other types of variables
Primitive Types and Expressions
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Unit 3: Variables in Java
Agenda Warmup Lesson 2.2 (parameters, etc)
Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)
Variables in C Topics Naming Variables Declaring Variables
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Agenda Warmup Lesson 1.9 (random #s, Boolean variables, etc)
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

Agenda Warmup Lesson 1.4 (double precision, String methods, etc) Guided Practice (1.4 Assignments) Closure Activity Students will be able to: Understand what libraries and packages are in Java Import a class from a library/package Solve the double precision problem Use String methods to manipulate Strings See how today's lesson fits into the unit and the course as a whole

socrative.com Room: EASTQUINN 43 / 5 = 1234 % 500 = 3 / 2.0 = (double)11 / 2 = (double)(11 / 2) = if ((8/5 == 1.6) || (4/3 == 1)) // true or false? 7) 9 % 7 * 10 / 4 – 3 % 5 / 2 = 8) If (word = = “bird”); // find 3 errors.

Quiz: FRIDAY

if (score >= 60 && score <=100) { System. out if (score >= 60 && score <=100) { System.out.print(“Pass”); } if (score >= 0) System.out.print(“Fail”);

if (score >= 60 && score <=100) { System. out if (score >= 60 && score <=100) { System.out.print(“Pass”); } if (score >= 0 && score < 60) System.out.print(“Fail”);

if (score >= 60 && score <=100) { System. out if (score >= 60 && score <=100) { System.out.print(“Pass”); } if (score >= 0 && score < 60) System.out.print(“Fail”); else System.out.print(“Invalid Score”);

Constants A constant is a variable whose value is expected to remain the same throughout a program It is considered “good programming style” to use constants for certain variables – for instance: interest rate on a loan, pi, etc Also, constants should be written in uppercase To declare a constant, add the word final: final double PI= 3.1415; final int HOURS_IN_A_DAY= 24; PI = 3.7; // this would cause an error

Class Libraries and Packages A class library is a set of packages, each of which contains classes (files) of code that can be used by programmers You can use this code simply by importing a package into your program, with a single line of code Top line of code: import library.package.class This is why Object-Oriented languages like Java are so popular – you can use code someone else has written, over and over again

Roundoff error In Java, doubles are slightly imprecise. For example: double x = 4.35; int y = (int)(100 * x); In this example, y = 434. This is because computers use binary, and some decimals cannot be exactly represented in binary. So, x actually equals 4.3499999999… 100 * x = 434.999999 Therefore, (int)(100 * x) truncates the .9999999, and the result is y = 434. This is known as roundoff error. It only happens with doubles.

You can use the Math.round( ) method to fix this. Math.round( ) does not truncate the decimal. Instead, it correctly rounds to the nearest whole number: double a = Math.round(7.5); // a = 8.0 double b = Math.round(13.4); // b = 13.0 int c = (int)Math.round(16.8); // c = 17

Now, we can use Math.round( ) to solve the previous problem: double x = 4.35; int y = (int)(Math.round (100 * x) ); Here, 434.9999999 gets correctly rounded to 435. You can also round off to a certain amount of decimal places. To do this, we import and use the DecimalFormat class. Note: the result is a String. Demo: DecimalRound

Algebra Review…  

Answer: C

Working with Strings Characters in a String can be identified by their index #. The first character in a String has index # 0. You need to know the following String methods: length( ) returns the number of characters in the String charAt(index) returns the char at the given index indexOf(char) reverse of charAt. returns the first index # of the given char substring(Start, End) returns the chars from index Start to index (End – 1) substring(Start) when only the starting index is given, returns the chars from start to the end of the String toUpperCase, toLowerCase converts the entire String to upper or lowercase

Examples… String coin = “Nickel”; Demo: StringMethods code result coin.length( ) 6 (even though the last letter is at index #5) coin.toUpperCase( ) NICKEL coin.charAt(1) i coin.indexOf(“k”) 3 coin.indexOf(“g”) –1 (since g was not found) coin.substring(1,3) “ic” (notice that letters 1 and 2 are the result, but not 3) coin.substring(2) “ckel” (starts at 2 and goes to the end)

String x = “September”; int y = x. indexOf(‘e’); // y equals 1 String x = “September”; int y = x.indexOf(‘e’); // y equals 1. // indexOf only finds the first occurrence. int z = x.indexOf(“pte”); // z equals 2. // indexOf can be used to find > 1 letter.

Although it is unusual, variables are not necessary when using String methods. For example: System.out.print(“dogs”.charAt(2)); // displays g System.out.print(“cats”.indexOf(‘s’)); // displays 3 int x = “frogs”.length( ); // x now equals 5

Assignments (Strings1) Prompt the user to enter a word. (Assume that it’s at least 3 letters long.) If the word is “apple,” then ask the user to enter a letter. If that letter is a or b, display “banana.” If it’s another letter, display “cantaloupe.” If the word is “kiwi,” then ask the user to enter a number. If the number is odd and divisible by 9, display “orange.” If the number is negative or equals 2112, display “pineapple.” If the word is something other than apple or kiwi, display the 3rd letter of the word. Display how many letters are in the word. Using only one String method, display the 2nd and 3rd letters. Display whether or not “o” is in the word. If it is, display which index it first occurs at. (see next slide)

2. (Middle) Prompt the user to enter their middle name. If the 1st letter is a consonant, display “Consonant,” otherwise, display “Vowel” If there are more than 8 letters in the name, display “long”, if between 5 and 8 (inclusive), display “medium”, otherwise, display “short.” Display whether the # of letters in the name is even or odd. Also, if it’s even, display whether or not it is divisible by 4. If it’s odd, display whether or not it is divisible by 5. If the letter ‘a’ is in the name, display the first index # it was found at (example: “The letter a was first found at index #3”) If it is not found, then say “The letter a was not found.” If the first and last letters in the name are the same, display “same,” otherwise “different” If the 2nd letter and the 2nd-to-last letter are both k, display “YES K” Display the 1st through the 3rd letters. Display all letters from the 2nd to the end of the name.