Week 3 - Wednesday CS 121.

Slides:



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

Methods Java 5.1 A quick overview of methods
Building Java Programs
Return values.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
1 Working with String Duo Wei CS110A_ Empty Strings An empty string has no characters; its length is 0. Not to be confused with an uninitialized.
Java Programming Strings Chapter 7.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 7:
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.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Week 3 - Wednesday.  What did we talk about last time?  Math methods  Lab 2.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
CS177 Week2: Recitation Primitive data types and Strings with code examples.
Chapter 7: Characters, Strings, and the StringBuilder.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
Week 15 - Monday.  What did we talk about last time?  File I/O.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
COMP Primitive and Class Types Yi Hong May 14, 2015.
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
Functions Venkatesh Ramamoorthy 21-March Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS 201 California State University, Los Angeles.  Various Mathematical Functions  Characters  Strings.
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Week 4 - Friday.  What did we talk about last time?  Examples  switch statements.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Java String Methods - Codehs
Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.
Week 2 - Wednesday CS 121.
Chapter 2 Variables.
Chapter 4 Mathematical Functions, Characters, and Strings
Week 4 - Friday CS 121.
Strings, StringBuilder, and Character
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3: Expressions and Interactivity.
Fundamental of Java Programming Basics of Java Programming
Week 3: Basic Operations
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.
String Objects & its Methods
Lesson 2: Building Blocks of Programming
CS 177 Week 3 Recitation Slides
Introduction to Programming
Chapter 7: Strings and Characters
Unit-2 Objects and Classes
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 2 Variables.
Introduction to Primitive Data types
Coding Concepts (Data- Types)
CHAPTER 3: String And Numeric Data In Python
Introduction to Programming
Chapter 2 Variables.
Welcome back! October 11, 2018.
Introduction to Primitive Data types
Week 7 - Monday CS 121.
Week 3 - Friday COMP 1600.
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Week 3 - Wednesday CS 121

Last time What did we talk about last time? Math methods boolean operations char operations

Questions?

Project 1

System.out.format() For Project 1, the easiest way to print out data with 2 decimal places is put "%.2f" in the formatting string for System.out.format() If you want, you can include other things in the formatting string double x = 5.74961; System.out.format("%.2f", x); //prints 5.75 System.out.format("Total = $%.2f", 15.7777); //prints Total = $15.78

Review of math methods

Methods A method is a piece of Java code that has been packaged up so that you can use it over and over Usually, a method will take some input and give some output System.out.println() is an example of a method Using a method (calling a method) always requires parentheses

Method example with sin() The sin() method allows you to find the sine of an angle (in radians) This method is inside the Math class The answer that it gives back is of type double To use it, you might type the following: double value = Math.sin( 2.4 );

result = class.method( input ); Method syntax Unless the method is inside your class, you must supply a class name and a dot If your method takes input, you put it inside the parentheses, if not, you leave them empty result = class.method( input ); You can store the result of the method, as long as the variable matches the type that the method gives back Next, you must give the method name that you are calling

Other Math methods Return type Name Job double sin( double theta ) Find the sine of angle theta cos( double theta ) Find the cosine of angle theta tan( double theta ) Find the tangent of angle theta exp( double a ) Raise e to the power of a (ea) log( double a ) Find the natural log of a pow( double a, double b ) Raise a to the power of b (ab) long round( double a ) Round a to the nearest integer random() Create a random number in [0, 1) sqrt( double a ) Find the square root of a toDegrees( double radians ) Convert radians to degrees toRadians( double degrees ) Convert degrees to radians

Example Write a program that takes a base b and an exponent x Print the result of raising bx

Operations on String values

Concatenation The only operator that we will use directly with String values is the + (concatenation) operator This operator creates a new String that is the concatenation of the two source Strings As with numerical types, the + operator does not change the two Strings being concatenated String word; word = "tick" + "tock"; // word is "ticktock"

Concatenation with other types Concatenation is a great tool for merging lots of different types into a String Confusion can arise: String word; word = 99 + " problems"; // word is // "99 problems" String word; word = "love potion #" + 4 + 5; // word is "love potion #45" word = "love potion #" + (4 + 5); // word is "love potion #9"

Strings are objects Objects have data inside of them but also have the ability to do things with methods Among other things, a String can: Compare itself with other Strings Find its length Say which character is located at position i Generate a substring

String comparison To see if two Strings are identical, use the equals() method: If they are the same (including case), the method will return true If they are not, the method will return false String word1 = "lettuce"; String word2 = "let us"; boolean same = word1.equals ( word2 ); // false

String comparison To see which String goes first in the dictionary, use the compareTo() method: If word1 comes first, value will be a negative number If word2 comes first, value will be a positive number If they are the same, value will be 0 String word1 = "hard work"; String word2 = "success"; int value = word1.compareTo( word2 ); // < 0

String length To find the length of a String, use the length() method: It is possible to have a String of length 0: String word = "a mile long"; int length = word.length(); // length = 11 String nothing = ""; int length = nothing.length(); // length = 0

char at position i To find the char at position i in a String, use the charAt() method: Woe betide the man (or woman) who asks for a character out of range: String word = "walnut"; char c = word.charAt(3); // c = 'n' String word = "short"; char c = word.charAt(10); // ouch!

Getting a substring To get a substring of a String, use the substring() method: The first int tells which char to start on, the second int says which char to stop before String word1 = "disco fever"; String word2 = word1.substring(3,7); //word2 = "co f"

Example Write a program that reads a first and a last name Then, output only the person's initials

Wrapper Classes

Classes and objects are useful There are certain things that are difficult to do with the operations we've shown you For example, how do you turn a String representation of a number like "847" into the actual int 847? Wrapper classes!

Wrapper classes Each primitive data type in Java has a wrapper class We will focus on 3: Integer Double Character

Integer class The main uses of the Integer class are converting ints to and from Strings To convert a String to an int, use the parseInt() method To convert an int to a String, use the toString() method (or just concatenate) String number = "345"; int value = Integer.parseInt(number); int value = 543; String number = Integer.toString(value);

Double class The Double class is much like the Integer class To convert a String to a double, use the parseDouble() method To convert a double to a String, use the toString() method (or just concatenate) String number = "-0.4581"; double value = Double.parseDouble(number); double value = 6.02e23; String number = Double.toString(value);

Character class The Character class is mostly useful for getting information about a particular char For example, you can find out whether a char is a digit, is a letter, is uppercase, or is lowercase by calling the isDigit(), isLetter(), isUpperCase(), or isLowerCase() methods, respectively char c = '8'; boolean value = Character.isDigit(c); //true

Quiz

Upcoming

Next time… Introduction to if-statements Lab 3

Reminders Keep reading Chapter 3 of the textbook Keep working on Project 1 Due next Friday