1 / 18

System development with Java

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.

johnhill
Download Presentation

System development with Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System development with Java Instructors: Rina Zviel-Girshin Lecture 11 Rina Zviel-Girshin @ARC

  2. Overview • Sequences • Interface • Abstract Rina Zviel-Girshin @ARC

  3. 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

  4. 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

  5. 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

  6. Sum of an arithmetic sequence • The sum of a finite arithmetic sequence is (a1+an)n S(n)= 2 Rina Zviel-Girshin @ARC

  7. 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

  8. 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

  9. Sum of an geometric sequence • The sum of a finite arithmetic sequence is b1(rn -1) S(n)= • (r-1) Rina Zviel-Girshin @ARC

  10. 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

  11. 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

  12. Interfaces interface Series { void next(); double sum(); } interface Printable{ void print(); } Rina Zviel-Girshin @ARC

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Any Questions? Rina Zviel-Girshin @ARC

More Related