Using Variables Getting Values Chapter 3 Show 2
Using Variables & Getting Values Chapter 3, Show 2 1
Division in Java 5. 0 / 2. 0 is real number division and yields 2. 5 5 / 2 is integer division and yields 2 5 % 2 gives the remainder of the division and would yield 1 9%2=1
Modulus Use Suppose an object is 5965 millimeters in length. Break this value up into m, cm and mm. Explain how you would do this: Remember: 1000 mm = 1 m 10 mm = 1 cm Start with a) 5965 / 1000 b) Find the remainder of a) and divide by 10. m c) Find the remainder of b) cm mm 3
Modulus Use Suppose an object is 5965 millimeters in length. Break this value up into m, cm and mm. How would you code this? int value = 5965; int m = value / 1000; int cm = (value % 1000) / 10 int mm = (value % 1000) %10 Remember: 1000 mm = 1 m 10 mm = 1 cm 4
Task 8 You have an integer number 1723986 , split this number up into its units of powers of 10. To do this write a main method, declare a variable that holds the value, then show a table like the following without destroying the original number: 1723986 by units: 1 million(s) 7 hundred thousand(s) 2 ten thousand(s) 3 thousand(s) 9 hundred(s) 8 ten(s) 6 one(s)… 5
Math Class • No import needed to use Math class part of basic JVM import java. lang. Math; • Static method use: Class. Name. method. Name(Parameters); Example: • System. out. println("Square root is " + Math. sqrt(10)); 6
Useful Math Class Methods • Math. sqrt(x) • Math. pow(x, y) • Math. sin(x), Math. cos(x) and Math. tan(x) 7
Working out Mathematical Expressions • Write the code to work out the answer to: 8
Task 9 Part 1: Write a main method to compute the following Mathematical Expressions then print its value. You need “Im excepting” statements. a) a) a) 9
Task 9 Part 2: Come up with 5 of your own mathematical equations by using the ones in Slide 7. First write them down on paper and work out the answer, then once they are done, pass them on to the person next to you and have them code it. You need “I except” statements. 10
Homework! Implement a class Quadratic. Equation whose constructor receives the coefficients a, b, c of the quadratic equation ax 2 + bx + c = 0. Supply methods get. Solution 1 and get. Solution 2 that get the solutions, using the quadratic formula. Write a test class Quadratic. Equation. Tester that constructs a Quadratic. Equation object, and prints the two solutions. You need “Im Expecting” statements and class comments! Ill also be looking at how effectively you test your code, so you will need to make more than just one object. HAND IN CODE NEXT CLASS. EMAIL ME AT reza. rowghani@tdsb. on. ca or come see me in Room 336 at lunch tomorrow.
- Slides: 11