160 likes | 172 Views
AP Java. 12/15/2015. Learning Objectives. Be able to read programs with… Loops Methods Recursion Be able to write and test a class from scratch. You will write a Fraction class.
E N D
AP Java 12/15/2015
Learning Objectives • Be able to read programs with… • Loops • Methods • Recursion • Be able to write and test a class from scratch. You will write a Fraction class. • Note: Thursday: Quiz over Classes, Objects, methods, and all the other stuff covered in this class to this point.
Dry Run #1 for(int num = 20; num >= 0; num--) { if(num % 4 == 2) System.out.println(num); }
Dry Run #2 for(int num = 5; num > 0; num = num - 2) { for(int star = 0; star < num; star++) System.out.print("*"); System.out.println(); }
Dry Run #3 int value = 13; int modulus = 5; while(value % modulus != 0 && modulus != 0) { value += 3; modulus -= 1; } System.out.println(value + ” “ + modulus);
Dry Run #4 public void change(int value) { if(value < 5) System.out.print("" + value % 5); else { System.out.print("" + value % 5); change(value/5); } } What will be printed as a result of the call change(29)?
Dry Run #5 public int zoom(int val) { if(val >= 100) return 2 * val; else return zoom(2*val); } What will be returned by the call zoom(30)?
if (a < b){ if (b < c){ if (c < 10){ System.out.println("one") } else if (c < a){ System.out.println("two") } } } else{ if (c < a){ System.out.println("three") } else { System.out.println("four") } } Dry Run #6 • For which values of a, b, and c will the code print “one”? • I. a = 5, b = 6, c = 7 • II. a = 8, b = 7, c = 6 • III. a = 10, b = 20, c = 30
Dry Run #7 String phrase = "Here is the word"; int psn = phrase.indexOf("e"); while (psn >= 0) { System.out.print(psn + " "); phrase = phrase.substring(psn + 1); psn = phrase.indexOf("e"); }
Going from idea to code: Fractions • What attributes does a fraction have. Data, information, variables, … • Numerator • Denominator • How should the fraction be created? Constructors • Given a numerator and denominator • A default value if nothing is given. • What behaviors does a fraction have? Verbs, operations, functions, methods. • Public • +, -, *, / • Private • Reduce • Find the greatest common factor to help reducing.
For starters • Write the fraction class • Create constructors • Sample call statement • Fraction fraction = new Fraction(25, 30); • Fraction fraction2 = new Fraction(); • Show fraction • Multiply fractions Write the code for the constructors. We’ll go over it in class in a bit.
Private data and constructors // Fraction.java //Class example program //import java.util.Scanner; public class Fraction { private int num, denom;//Private data public Fraction () //Constructor: No return type and the name matches the class name. { num=0; //Default constructor, since no parameters were passed. denom=1; } public Fraction (int n, int d) //Constructor: n and d give the initial values for the fraction {//Function overloading/Polymorphism: Same function with different options. num=n; denom=d; //reduce(); }
Now add the following methods. • show • Will display the fraction to the screen • Sample call statement. • fractionObject.show(“The answer is”); • mult • To multiply one fraction by another. • Sample call statement. • fractionObject1.mult(fractionObject2);
Fraction Methods public Fraction mult(Fraction temp) { Fraction prod = new Fraction(); prod.num=num*temp.num; prod.denom= denom*temp.denom; //prod.reduce(); //This will reduce prod, member function return prod; } public void show(String descrip) { System.out.println(descrip + num + "/" + denom); //You can also System.out.println(descrip + this.num + "/“ + this.denom); } }
Sample FractionTest program //FractionTest.java //This program is to check the Fraction class public class FractionTest { public static void main(String args[] ) { Fraction fraction = new Fraction(25, 30); fraction.show("Fraction "); Fraction fraction2 = new Fraction(3,4); fraction2.show("Fraction 2 "); fraction2=fraction.mult(fraction2); fraction2.show("After multiplying "); } }
For today • Add the following methods to the fraction class. • toString() • divide • Add (Include creating common denominators) • Pushes • subtract • Reduce • Test it using the Object Bench in BlueJ or by using a driver class.