Fraction class class fraction { int num, den; fraction (int a, int b){ int common = gcd(a,b); num=a/common; den=b/common; } fraction (int a){ num=a; den=1;

Slides:



Advertisements
Similar presentations
Fractions. ADDING FRACTIONS  Build each fraction so that the denominators are the same  ADD the numerators  Place the sum of the two numerators on.
Advertisements

Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
Method main Point p1 = new Point(5, 6); Point p2 = new Point(10, 11); System.out.println(p1.getX()); System.out.println( Point.num );
Simplifying Fractions Multiplying & dividing Fractions.
WHEN MULTIPLYING LIKE BASES, YOU ADD THE EXPONENTS FOR EXAMPLE: NOW YOU TRY:
To Start: 10 Points.
Solving Equations with Brackets or Fractions. Single Bracket Solve 3(x + 4) = 24 3x + 12 = 24 3x + 12 – 12 = x = 12 x = 4 Multiply brackets out.
 To add numbers in scientific notation: 1) Add the constants 2) Keep the exponent the same  Example: (2.1 x 10 5 ) + (3.2 x 10 5 ) = ( ) x 10.
Algebraic Fractions and Forming Equations Learning Outcomes  Simplify algebraic fractions  Add, subtract, multiply and divide algebraic fractions  Solve.
Negative Exponents SWBAT express powers with negative exponents as decimals; express decimals as powers with negative exponents; simplify expressions with.
Inverse Operations ExpressionInverse Operation How do you get the variable by itself? x + 5 x x x ÷ 20 x3x3.
Adding, Subtracting, Multiplying, & Dividing Rational Expressions
Adding & Subtracting Whole Number and Fractions
Measurement Multiplying and Dividing Fractions.  We can add and subtract fractions with the same (common) denominator easily. Adding and Subtracting.
Divide. Evaluate power – 3 = – 3 EXAMPLE – 3 = 3 2 – – 3 = 6 – 3 Multiply. Evaluate expressions Multiply and divide from.
PROPERTIES OF EXPONENTS

Multiplying With Scientific Notation (3.8  102)  (5  104) = 1.) Change the order of the factors. 2.) Multiply and use the rules for exponents 3.) Make.
Jeopardy Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy Add fractions Subtract Fractions Multiply Fractions Divide.
Unit 2: Integers Unit Review. Multiplying Integers The product of two integers with the same sign is a positive. Eg: (+6) x (+4) = +24; (-18) x (-3) =
More Practice with Equations Containing Fractions.
Notes Over 8 – 5 Add. + Add the whole numbers. Add the fractions and reduce if needed.
Back to Basics. Basic Operations with Fractions 1. Adding and subtracting fractions: Get a common denominator Add or subtract the numerators Keep the.
FOUR RULES FOR FRACTIONS. numerator denominator The language of fractions improper fraction mixed number.
Measurement Adding and Subtracting Fractions with Different Denominators.
Fractions, Decimals, and Percents
The Order of Operations Chapter Evaluate inside grouping symbols ( ), { }, [ ], | |, √ (square root), ─ (fraction bar) 2.Evaluate exponents 3.Multiply.
Fill In The Blank Multiplying Fractions 1. Fraction Form 2. Multiply Numerators Simplify.
I’m Thinking of a Number
Addition Multiplication Subtraction Division. 1.If the signs are the same, add the numbers and keep the same sign = = If the.
CHAPTER 4 Negative Numbers. ADDITION AND SUBTRACTION USING NEGATIVE NUMBERS A number line is very useful when you have to do additions or subtractions.
 Anything to the power of zero is one  4 0 =1  X 0 =1  =?
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Are You Smarter Than a 5 th Grader? Fraction Edition.
Opener Evaluate when x = 4.. Test Review Simplifying Exponent Rules.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Solving 2 step equations. Two step equations have addition or subtraction and multiply or divide 3x + 1 = 10 3x + 1 = 10 4y + 2 = 10 4y + 2 = 10 2b +
Improper Fractions and Mixed Number.  An improper fraction is a fraction in which the numerator is larger than the denominator. Example: 7/3 The numerator.
1-2 Order of Operations Objective: Use the order of operations to evaluate expressions.
4-6 Solving Equations with Fractions What You’ll Learn ► To solve one – step equations ► To solve two – equations.
ADDING AND SUBTRACTING MULTIPLYING AND DIVIDING REAL NUMBERS.
In this lesson you are going to learn how to divide fractions by multiplying by the reciprocal.
Simplify MultiplyDivide Add & Subtract Proportion.
The Complex Number System. 1. Write each expression as a pure imaginary number. (similar to p.537 #26)
Rational Expressions and Equations
FOUR RULES FOR FRACTIONS
Multiplying and Dividing Fractions
Subtraction Addition Multiplication Fractions Division 1pt 1 pt 1 pt
Multiply the following rational expressions. Show work!
Lesson 9.2: Multiplying and Dividing Rational Expressions
Subtracting Real Numbers
Problem-Solving Steps Solve Problem-Solving Steps Solve
Domain 1: The Number System
Lesson 2: Power Rules I will understand how to expand and simplify basic and complex exponents.
Perform Function Operations and Composition
Operations with Fractions
To get y, multiply x by 3 then add 2 To get y, multiply x by negative ½ then add 2 To get y, multiply x by 2 then subtract 3 To get y, multiply x.
Math Jeopardy (Exponents)
Fractions, Decimals & Percentages
Add or Subtract? x =.
Divide the number in C by 10.
Equations with Fractions
Add, Subtract, Divide and Multiply Integers
Equations with Fractions
Section 7.5: Review.
Number properties and operations
EXPONENT RULES.
Converting between Percentages, Decimals and Fractions
Algebra with Whole Numbers
3. Fractions.
Presentation transcript:

Fraction class class fraction { int num, den; fraction (int a, int b){ int common = gcd(a,b); num=a/common; den=b/common; } fraction (int a){ num=a; den=1; } fraction add(fraction f){ int num = this.num*f.den + f.num*this.den; int den = this.den*f.den; return new fraction(num,den); } fraction subtract(fraction f){ int num = this.num*f.den - f.num*this.den; int den = this.den*f.den; return new fraction(num,den); } fraction multiply(fraction f){ int num = this.num*f.num; int den = this.den*f.den; return new fraction(num,den); }

contd fraction divide(fraction f){ int num = this.num*f.den; int den = this.den*f.num; return new fraction(num,den); } fraction power(int n){ int num = intpower(this.num, n); int den = intpower(this.den, n); return new fraction(num,den); } boolean isEqual(fraction f){ return (this.num==f.num)&(this.den==f.den); } boolean isLess(fraction f){ return (this.num*f.den < f.num*this.den); } boolean isGreater(fraction f){ return (this.num*f.den > f.num*this.den); }

contd void printfraction(){ System.out.println(this.num+"/"+this.den); } int intpower(int a, int n){ if (n==0) return 1; int result = 1; for (int i=1; i<=n; i++) result = result * a; return result; } int gcd(int m, int n){ if (m<0) m=-m; if (n<0) n=-n; while (m!=n){ if (m>n) m=m-n; else n=n-m; } return m; }

contd class fractiondemo{ public static void main(String args[]){ fraction f1 = new fraction(1,2); fraction f2 = new fraction(2,4); fraction f3 = new fraction(3); System.out.print("Fraction f1: ");f1.printfraction(); System.out.print("Fraction f2: ");f2.printfraction(); System.out.print("Fraction f3: ");f3.printfraction(); fraction f4 = f1.add(f3); f4.printfraction(); f4 = f1.subtract(f3); f4.printfraction(); f1.multiply(f3).printfraction(); f1.divide(f3).printfraction(); f1.power(3).printfraction(); System.out.println(f1.isLess(f3)); System.out.println(f1.isEqual(f2)); System.out.println(f1.isGreater(f3)); }

Access Control // Public vs private access. class MyClass { private int alpha; // private access public int beta; // public access int gamma; // default access (essentially public) /* Methods to access alpha. It is OK for a member of a class to access a private member of the same class. */ void setAlpha(int a) { alpha = a; } int getAlpha() { return alpha; } class AccessDemo { public static void main(String args[]) { MyClass ob = new MyClass(); /* Access to alpha is allowed only through its accessor methods. */ ob.setAlpha(-99); System.out.println("ob.alpha is " + ob.getAlpha()); // You cannot access alpha like this: ob.alpha = 10; // Wrong! alpha is private! // These are OK because beta and gamma are public. ob.beta = 88; ob.gamma = 99; }

Access control example /* This class implements a "fail-soft" array which prevents runtime errors. */ class FailSoftArray { private int a[]; // reference to array private int errval; // value to return if get() fails public int length; // length is public /* Construct array given its size and the value to return if get() fails. */ public FailSoftArray(int size, int errv) { a = new int[size]; errval = errv; length = size; } // Return value at given index. public int get(int index) { if(ok(index)) return a[index]; return errval; } // Put a value at an index. Return false on failure. public boolean put(int index, int val) { if(ok(index)) { a[index] = val; return true; } return false; } // Return true if index is within bounds. private boolean ok(int index) { if(index >= 0 & index < length) return true; return false; }

contd. // Demonstrate the fail-soft array. class FSDemo { public static void main(String args[]) { FailSoftArray fs = new FailSoftArray(5, -1); int x; // show quiet failures System.out.println("Fail quietly."); for(int i=0; i < (fs.length * 2); i++) fs.put(i, i*10); for(int i=0; i < (fs.length * 2); i++) { x = fs.get(i); if(x != -1) System.out.print(x + " "); } System.out.println(""); // now, handle failures System.out.println("\nFail with error reports."); for(int i=0; i < (fs.length * 2); i++) if(!fs.put(i, i*10)) System.out.println("Index " + i + " out-of-bounds"); for(int i=0; i < (fs.length * 2); i++) { x = fs.get(i); if(x != -1) System.out.print(x + " "); else System.out.println("Index " + i + " out-of-bounds"); }