180 likes | 196 Views
This lecture provides an overview of sequences, interfaces, and abstract classes in Java system development. It focuses on implementing arithmetic and geometric sequences and includes examples and methods for printing terms and calculating sums. The lecture also explores the use of interfaces and abstract classes in sequence implementations.
E N D
System development with Java Instructors: Rina Zviel-Girshin Lecture 11 Rina Zviel-Girshin @ARC
Overview • Sequences • Interface • Abstract Rina Zviel-Girshin @ARC
Sequences • An infinite sequence is a function whose domain is the set of positive integers. • The function values a1,a2,a3,... are the terms of the sequence.. • If the domain of the function consists of the first n positive integers only, the sequence is called finite sequence. Rina Zviel-Girshin @ARC
Arithmetic sequence • A sequence (or a series) is arithmetic if the difference between any two consecutive terms is the same. • A sequence is arithmetic if there is a number d such that an-an-1=d for any n positive integer greater than 1. • The number d is called the common difference of the arithmetic sequence. • The nth term of an arithmetic sequence has the form: an=a1+d(n-1). Rina Zviel-Girshin @ARC
Arithmetic sequence example Example: • The sequence whose nth term is 4n+3 is arithmetic. • For this sequence, the common difference between consecutive terms is 4. • 7, 11, 15, 19, . . . 4n+3, . . . Rina Zviel-Girshin @ARC
Sum of an arithmetic sequence • The sum of a finite arithmetic sequence is (a1+an)n S(n)= 2 Rina Zviel-Girshin @ARC
Geometric sequence • A sequence is geometric if the ratio of any two consecutive terms is the same. • A a sequence is geometric if there is a number r 0, such that bn/bn-1=r for any n positive integer greater than 1. • The number r is called the common ratio of the geometric sequence. • The nth term of a geometric sequence has the form bn=b1*rn-1. Rina Zviel-Girshin @ARC
Geometric sequence example Example: • The sequence whose nth term is 2n is geometric. • For this sequence, the common ratio between consecutive terms is 2. • 2, 4, 8, 16, . . ., 2n, . . . Rina Zviel-Girshin @ARC
Sum of an geometric sequence • The sum of a finite arithmetic sequence is b1(rn -1) S(n)= • (r-1) Rina Zviel-Girshin @ARC
Question • We want to implement arithmetic, geometric and other series. • We want to print a current term and a sum. • Let’s see what both series have in common? • What is different? Rina Zviel-Girshin @ARC
Interface • We can define Series interface to hold the following methods: • next() • sum() • We can define Printable interface to hold the following method: • print() Each sequence will implement those interfaces. Rina Zviel-Girshin @ARC
Interfaces interface Series { void next(); double sum(); } interface Printable{ void print(); } Rina Zviel-Girshin @ARC
Arithmetic class Arithmetic implements Series, Printable{ int index, term, first, delta; public Arithmetic(int first,int delta) { this.delta=delta; this.first=first; term=first; index=1;} public void next() { term=first+delta* index++; } public double sum() { return (first+term)*index/2; } public void print() { System.out.print(“an=" + term + ";sum=" + sum());} } Rina Zviel-Girshin @ARC
Geometric // a little bit different implementation class Geometric implements Series, Printable{ int index, term, first, ratio; public Geometric(int first, int ratio) { this.ratio=ratio; this.first=first; term=first; index=1; } public void next() { term=term*ratio; index++; } public double sum() { return first*Math.pow(ratio,index-1)/(ratio-1); } public void print() { System.out.print(“bn=" + term + ";sum=" + sum());} } Rina Zviel-Girshin @ARC
Another implementation • Can we implement those classes differently? • Do we have the same data fields? • Do we have the same implementation for any methods? • print() method is the same for both series. System.out.print(“bn=" + term + ";sum=" + sum()); • So we can build an abstract class for this problem. Rina Zviel-Girshin @ARC
Abstract class abstract class AbstractSeries implements Series, Printable { int index=1, term=1, first; public void print() { System.out.println("an=" + term + ";sum=" + sum()); } } Rina Zviel-Girshin @ARC
Arithmetic implementation class Arithmetic extends AbstractSeries implements Series, Printable{ int delta; public Arithmetic(int first,int delta) { this.delta=delta; this.first=first; term=first; index=1;} public void next() { term=first+delta* index++; } public double sum() { return (first+term)*index/2; } } Rina Zviel-Girshin @ARC
Any Questions? Rina Zviel-Girshin @ARC