This preview shows pages 1–12. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations 2 Announcements Project 1 was posted last Friday. It is due on February, 4 th at 9pm TAs started consulting hours this week TueThu 6:00pm to 9:00 pm For the project, make sure to: Properly indent your code In Dr. Java, select all (CTRL+A) and then press TAB Comment the variables you use YOU WILL LOSE POINTS IF THIS IS NOT DONE! 3 QUESTIONS??? 4 Basic Operations Recap Operations On Integers +, , *, / Addition, subtraction, multiplication and division Note that division of integers drops the fractional part +=, =, *=, /= a += 4; a = a + 4; c *= 5; c = c * 5; ++,  a ++; a = a + 1; % Remainder of division a = 10 % 3; // a contains 1 5 b = 3; b = b  3; d /= 2; b = b / 2; b ; b = b  1; Question What are the ways we can write to increment variable a by 1? a = a + 1; a += 1; a ++; 6 7 Basic Operations w/Integers Code Example 1 public class IntegerOperations { public static void main(String args) { int a; int b; a = 13 + 20; // a contains 33 b = a / 11; // b contains 3 System.out.println("a = " + a); System.out.println("b = " + b); a = 7; System.out.println("a = " + a); b = a / b; // Result = 8.66 but int type means 8 b++; // Increment b from 8 to 9 System.out.println("b = " + b); } } Output a = 33 b = 3 a = 26 b = 9 8 Basic Operations On Doubles Same operations as integers But the fractional part is retained 9 Basic Operations w/Doubles Code Example 1 public class DoubleOperations { public static void main(String args) { double a; double b; double c; double d; a = 4.0 / 3.0; b = a  1; c = b + b + b * 2; d = 9 / 4; d; System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); } } Output a = 1.3333333333333333 b = 0.3333333333333333 c = 1.3333333333333333 d = 1.0 Question Variable d is a double or an integer? 10 Casting in Java int a = (int) 1.8; // convert double 1.8 into an int 1 Convert one data type to another loss of precision float int, number is rounded down Let the Java compiler know that you are aware that you are going to lose some information, but you are ok with that. 11 Math Library in Java Java contains a Math class that includes a variety of math functions that can be used to perform other operations on integers and/or doubles The term library just means that it is a collection of predefined methods/functions that a programmer can leverage...
View
Full
Document
This note was uploaded on 02/23/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue UniversityWest Lafayette.
 Spring '08
 Staff

Click to edit the document details