Week 3 - Wednesday.  What did we talk about last time?  Math methods  Lab 2.

Slides:



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

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 7:
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Primitive Types CSE 115 Spring 2006 April 3 &
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
String Escape Sequences
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
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,
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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. The Week’s Goal.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Chapter 7: Characters, Strings, and the StringBuilder.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
Chapter 2 Variables.
Week 15 - Monday.  What did we talk about last time?  File I/O.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
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.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
CS 201 California State University, Los Angeles.  Various Mathematical Functions  Characters  Strings.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Week 4 - Friday.  What did we talk about last time?  Examples  switch statements.
Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
Week 4 - Friday CS 121.
Week 3 - Wednesday CS 121.
Introduction to Computer Science / Procedural – 67130
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Fundamental of Java Programming Basics of Java Programming
Java Programming: From Problem Analysis to Program Design, 4e
Week 3: Basic Operations
CS 177 Week 3 Recitation Slides
Chapter 2 Variables.
Introduction to Primitive Data types
Coding Concepts (Data- Types)
CHAPTER 3: String And Numeric Data In Python
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Introduction to Primitive Data types
Week 3 - Friday COMP 1600.
Building Java Programs
Presentation transcript:

Week 3 - Wednesday

 What did we talk about last time?  Math methods  Lab 2

 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 = ; System.out.format("%.2f", x); //prints 5.75 double x = ; System.out.format("%.2f", x); //prints 5.75 System.out.format("Total = $%.2f", ); //prints Total = $15.78

 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

 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 );

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

Return typeNameJob doublesin( double theta ) Find the sine of angle theta doublecos( double theta ) Find the cosine of angle theta doubletan( double theta ) Find the tangent of angle theta doubleexp( double a ) Raise e to the power of a (e a ) doublelog( double a ) Find the natural log of a doublepow( double a, double b ) Raise a to the power of b (a b ) longround( double a ) Round a to the nearest integer doublerandom() Create a random number in [0, 1) doublesqrt( double a ) Find the square root of a doubletoDegrees( double radians ) Convert radians to degrees doubletoRadians( double degrees ) Convert degrees to radians

 The boolean type seems so simple  What on earth would we want to do with it?  Just like numerical types, we can combine boolean s in various ways  You might be familiar with these operations if you have taken a course in logic

 The NOT operator  Changes a true into a false or a false into a true x!x truefalse true

 We can combine statements in logic together to make other interesting statements  The way we combine them makes a difference, e.g.  Politicians lie.(True)  Cast iron sinks.(True)  Politicians lie in cast iron sinks.(Absurd)

 The AND operator  It gives back true only if both things being combined are true  If I can swim AND the pool is not filled with acid, then I will survive xyx && y true false truefalse

 The OR operator  It gives back true if either or both things being combined are true  If I get punched in the face OR kicked in the stomach, then I will be in pain xyx || y true falsetrue falsetrue false

 The XOR operator, sort of like what people often mean when they say "or" in English  It gives back true if one but not both things are true  If I get 1 apple XOR 3 oranges, then I will have an odd number of fruit xyx ^ y true false truefalsetrue falsetrue false

(!true && (false^(false||true)))  Is this expression true or false ?  It's false

 In some circumstances, Java doesn't check the whole expression:  (true || (some complicated expression) )  Ignores everything after || and gives back true  (false && (some complicated expression) )  Ignores everything after && and gives back false

 Multiplication and division don't seem to make sense  We can increment and decrement a char char letter; letter = 'x';// letter contains 'x' letter++;// letter contains 'y' letter++; // letter contains 'z' letter++; // letter contains ? char letter; letter = 'x';// letter contains 'x' letter++;// letter contains 'y' letter++; // letter contains 'z' letter++; // letter contains ?

 It is possible to convert a char into an int  It can be more useful to get the offset from a starting point int number; number = 'a';// letter contains 97 int number; number = 'a';// letter contains 97 char letter = 'r'; int number; number = letter – 'a' + 1; //number is 18 char letter = 'r'; int number; number = letter – 'a' + 1; //number is 18

 Everything in the computer is 1's and 0's  Each character has a number associated with it  These numbers can be listed in tables  The ASCII table only covers 7 bits of information (0-127)  NEVER EVER TYPE THESE NUMBERS IN CODE

 Remember that we use single quotes to designate a char literal: 'z'  What if you want to use the apostrophe character ( ' )?  apostrophe: '\''  What if you want to use characters that can't be printed, like tab or newline?  tab: '\t'  newline: '\n'  The backslash is a message that a special command called an escape sequence is coming

 You can put escape sequences into String literals as well  You do not have to escape apostrophes in a String  But you do have to escape quotation marks String blanks = "\t\t\t\t\n"; String quote = "He said, \"Attack!\""; String blanks = "\t\t\t\t\n"; String quote = "He said, \"Attack!\"";

 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 String s  As with numerical types, the + operator does not change the two String s being concatenated String word; word = "tick" + "tock"; // word is "ticktock" String word; word = "tick" + "tock"; // word is "ticktock"

 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 = 99 + " problems"; // word is // "99 problems" String word; word = "love potion #" ; // word is "love potion #45" word = "love potion #" + (4 + 5); // word is "love potion #9" String word; word = "love potion #" ; // word is "love potion #45" word = "love potion #" + (4 + 5); // word is "love potion #9"

 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 String s  Find its length  Say which character is located at position i  Generate a substring

 To see if two String s 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 word1 = "lettuce"; String word2 = "let us"; boolean same = word1.equals ( word2 ); // false

 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 word1 = "hard work"; String word2 = "success"; int value = word1.compareTo( word2 ); // < 0

 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 word = "a mile long"; int length = word.length(); // length = 11 String nothing = ""; int length = nothing.length(); // length = 0 String nothing = ""; int length = nothing.length(); // length = 0

 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 = "walnut"; char c = word.charAt(3); // c = 'n' String word = "short"; char c = word.charAt(10); // ouch! String word = "short"; char c = word.charAt(10); // ouch!

 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" String word1 = "disco fever"; String word2 = word1.substring(3,7); //word2 = "co f"

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

 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!

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

 The main uses of the Integer class are converting int s to and from String s  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); String number = "345"; int value = Integer.parseInt(number); int value = 543; String number = Integer.toString(value); int value = 543; String number = Integer.toString(value);

 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 = " "; double value = Double.parseDouble(number); String number = " "; double value = Double.parseDouble(number); double value = 6.02e23; String number = Double.toString(value); double value = 6.02e23; String number = Double.toString(value);

 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 char c = '8'; boolean value = Character.isDigit(c); //true

 Introduction to if -statements  Lab 3

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