Week 3 - Monday.  What did we talk about last time?  Using Scanner to get input  Basic math operations  Lab 2.

Slides:



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

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.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
String Escape Sequences
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
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 3 - Wednesday.  What did we talk about last time?  Math methods  Lab 2.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Operators, Functions and Modules1 Pattern Matching & Recursion.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
Chapter 2 Variables.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Week 4 - Friday.  What did we talk about last time?  Examples  switch statements.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CS 106A, Lecture 4 Introduction to Java
Week 2 - Wednesday CS 121.
Chapter 2 Variables.
Week 4 - Friday CS 121.
Week 3 - Wednesday CS 121.
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Week 3 - Friday CS222.
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
Chapter 2 Basic Computation
Chapter 2: Basic Elements of Java
Building Java Programs
Objects and Primitive Data
Chapter 2 Variables.
Introduction to Primitive Data types
Building Java Programs
Building Java Programs
Topics Designing a Program Input, Processing, and Output
Building Java Programs
Building Java Programs
Building Java Programs
Summary of what we learned yesterday
Topics Designing a Program Input, Processing, and Output
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Building Java Programs
Introduction to Primitive Data types
Building Java Programs
Presentation transcript:

Week 3 - Monday

 What did we talk about last time?  Using Scanner to get input  Basic math operations  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

There are three parts to using Scanner for input 1. Include the appropriate import statement so that your program knows what a Scanner object is 2. Create a specific Scanner object with a name you choose 3. Use the object you create to read in data

 Given a temperature in Celsius, what is the equivalent in Fahrenheit?  T F = (9/5)T C + 32

 How complex can expressions get?  What’s the value of a ?  18! int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d; int a = 31; int b = 16; int c = 1; int d = 2; a = b + c * d – a / b / d;

 Order of operations holds like in math  You can use parentheses to clarify or change the precedence  Now a is 16 int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d; int a = 31; int b = 16; int c = 1; int d = 2; a = (((b + c) * d) – a / b) / d;

 You cannot directly store a double value into an int variable  However, you can cast the double value to convert it into an int  Casting tells the compiler that you want the loss of precision to happen  You can always store an int into a double int a = 2.6;// fails! int a = (int)2.6;// succeeds! (a = 2)

 There are operations beyond +, -, *, /, and % that you probably want to do with numbers  Java has those built-in because the computer can do those directly  A number of other operations can be done by calling methods  Methods will end up being very important to us later

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

 In Java, the conversion of a double into an int does not use rounding  As in the case of integer division, the fractional part is dropped  For positive numbers, that's like using floor  For negative numbers, that's like using ceiling  The right way to do rounding is to call Math.round() double x = 2.6; int a = (int)Math.round(x);// rounds double x = 2.6; int a = (int)Math.round(x);// rounds

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

 Compute the hypotenuse of a triangle  a 2 + b 2 = c 2 b a c

 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!\"";

 Finish boolean and char operations  String operations  Wrapper classes

 Keep reading Chapter 3 of the textbook  Get started on Project 1