Download presentation
Presentation is loading. Please wait.
Published byLorraine Newman Modified over 9 years ago
1
CS115 Math Class
2
Printing Out The command : System.out.println(“ Something that we want to print out on the screen as output”); Prints out in one line whatever is enclosed within the double quotation marks, is this example the message: Something that we want to print out on the screen as output Anything enclosed within double quotation marks is called a String.
3
Using Escape Sequences Let us consider the case of tab command : \t It is an instruction that means to the computer: – move one tab of space before you print something Suppose we wrote the String “\t*”. Should the computer print out the entire String? i.e \t* No, in this case what we want to print only the asterisk but we want to advance the tab by one tab before it prints out the asterisk. The system should be able to tell that even though we have enclosed everything with double quotation marks (meaning that we want to print everything within the quotation marks), certain symbols are not to be printed out but instead they should be interpreted as a command. Therefore \t is a command and not something to be printed out (thus the term "escape" from normal consideration) and the system should be able to tell the diffrence.
4
EXERCISE 3 – USING TABS Example: System.out.println(“\t*\t\t*”)’ In printing out the above The system first moves one tab to the right The system next prints the asterisk The system next moves two tabs to the right The system next prints the second asterisk
5
Exercise 4 The Math class is a library class. Library classes are pre written java programs that do certain tasks. The Math class has many methods available that perform certain mathematical functions and provide the result. You can find out the methods available in the Math library class by going to http://java.sun.com/javase/6/docs/api/ Look up Math from the left frame In your program you use it for finding the result of raising a number a to an exponent b and capturing the result with a new identifier by using: double result=Math.pow( a, b); a and b can be double data types.
6
Library Class Math Certain library classes like the Math class are attached (become available) to our programs (classes) automatically without us writing any commands to do so. Thus, the Math class and its methods are available for us to use automatically. We command one of the Math classes method sto execute my using the name of the class (Math) followed by a dot i.e.
7
Math Library Class double result=Math.pow( a, b); This means calculate the power of the number stored in memory location a by raising to the number b (the exponent). Store the result in a memory location called result (or any other name that we choose) of type double.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.