1 / 25

COMP 1600

Week 3 - Monday. COMP 1600. Last time. What did we talk about last time? Review of input Basic math operations. Questions?. Project 1. System.out.format ().

rebekahc
Download Presentation

COMP 1600

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 3 - Monday COMP 1600

  2. Last time • What did we talk about last time? • Review of input • Basic math operations

  3. Questions?

  4. Project 1

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

  6. Advanced operations on numbers

  7. Adding is great, but… • 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

  8. 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

  9. 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 );

  10. 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

  11. Rounding • 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; inta = (int)Math.round(x); // rounds

  12. Other Math methods

  13. Pythagorean theorem • Compute the hypotenuse of a triangle • a2 + b2 = c2 c a b

  14. Operations on booleans

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

  16. The ! operator • The NOT operator • Changes a true into a false or a false into a true

  17. Combining booleanvalues • 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)

  18. The && operator • 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

  19. The || operator • 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

  20. The ^ operator • 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

  21. See how you're following… (!true && (false^(false||true))) • Is this expression true or false? • It's false

  22. Short circuit evaluation • 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

  23. Upcoming

  24. Next time… • charoperations • Stringoperations • Wrapper classes

  25. Reminders • Keep reading Chapter 3 of the textbook • Get started on Project 1

More Related