Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java: Understanding Interfaces, Classes, and Exceptions in Complex Number Implementation, Study notes of Object Oriented Programming

An in-depth explanation of interfaces, classes, and exceptions in Java through the implementation of a Complex number class. It covers the definition and usage of interfaces, multiple implementations, and the benefits of information hiding. The document also includes examples and guidelines for best practices.

What you will learn

  • What are checked and unchecked exceptions in Java?
  • How does information hiding work in Java?
  • What are the benefits of using interfaces instead of classes?
  • How can multiple classes implement the same interface?
  • What is the role of an interface in Java?

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

freddye
freddye 🇺🇸

4.3

(11)

235 documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
15-214
School&of&
Computer&Science
Principles/of/Software/Construction:/
Objects,/Design,/and/Concurrency
Object-Oriented/Programming/in/Java
Josh&Bloch Charlie/Garrod
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download Java: Understanding Interfaces, Classes, and Exceptions in Complex Number Implementation and more Study notes Object Oriented Programming in PDF only on Docsity!

School of Computer Science

Principles of Software Construction:

Objects, Design, and Concurrency

Object-Oriented Programming in Java

Josh Bloch Charlie Garrod

Administrivia

• First homework due this Thursday, 11:59 PM

Collections usage example 3

Unfinished business from last lecture

Print index of first occurrence of each word

class Index {

public static void main(String[] args) {

Map<String, Integer> index = new TreeMap<>();

// Iterate backwards so first occurrence wins

for (int i = args.length - 1; i >= 0; i--) {

index.put(args[i], i);

System.out.println(index);

$ java Index if it is to be it is up to me to do it

{be=4, do=11, if=0, is=2, it=1, me=9, to=3, up=7}

Outline

I. Object-oriented programming basics

II. Information hiding

III. Exceptions

Classes

• Every object has a class

– A class defines methods and fields

– Methods and fields collectively known as members

• Class defines both type and implementation

– type ≈ where the object can be used

– implementation ≈ how the object does things

• Loosely speaking, the methods of a class are its

Application Programming Interface (API)

– Defines how users interact with instances

Class example – complex numbers

class Complex { private double re; // Real Part private double im; // Imaginary Part public Complex(double re, double im) { this.re = re; this.im = im; } public double realPart() { return re; } public double imaginaryPart() { return im; } public double r() { return Math.sqrt(re * re + im * im); } public double theta() { return Math.atan(im / re); } public Complex add(Complex c) { return new Complex(re + c.re, im + c.im); } public Complex subtract(Complex c) { ... } public Complex multiply(Complex c) { ... } public Complex divide(Complex c) { ... } }

Interfaces and implementations

• Multiple implementations of API can coexist

– Multiple classes can implement the same API

– They can differ in performance and behavior

• In Java, an API is specified by interface or class

– Interface provides only an API

– Class provides an API and an implementation

– A Class can implement multiple interfaces

An interface to go with our class

public interface Complex {
// No constructors, fields, or implementations!
double realPart();
double imaginaryPart();
double r();
double theta();
Complex plus(Complex c);
Complex minus(Complex c);
Complex times(Complex c);
Complex dividedBy(Complex c);

An interface defines but does not implement API

Modifying client to use interface

public class ComplexUser { public static void main(String args[]) { Complex c = new OrdinaryComplex(-1, 0); Complex d = new OrdinaryComplex(0, 1); Complex e = c.plus(d); System.out.println(e.realPart() + " + "

  • e.imaginaryPart() + "i"); e = c.times(d); System.out.println(e.realPart() + " + "
  • e.imaginaryPart() + "i"); } }

When you run this program, it still prints

  • 1.0 + 1.0i
  • 0.0 + - 1.0i

Interface permits multiple implementations class PolarComplex implements Complex { double r; double theta; public PolarComplex(double r, double theta) { this.r = r; this.theta = theta; } public double realPart() { return r * Math.cos(theta) ; } public double imaginaryPart() { return r * Math.sin(theta) ; } public double r() { return r; } public double theta() { return theta; } public Complex plus(Complex c) { ... } // Completely different impls public Complex minus(Complex c) { ... } public Complex times(Complex c) { ... } public Complex dividedBy(Complex c) { ... } }

Why multiple implementations?

• Different performance

– Choose implementation that works best for your use

• Different behavior

– Choose implementation that does what you want

– Behavior must comply with interface spec (“contract”)

• Often performance and behavior both vary

– Provides a functionality – performance tradeoff

– Example: HashSet, TreeSet

Java interfaces and classes

• Organize program program objects types

– Each type offers a specific set of operations

– Objects are otherwise opaque

• Interfaces vs. classes

– Interface: specifies expectations

– Class: delivers on expectations (the implementation)

Check your understanding

interface Animal {
void vocalize();
class Dog implements Animal {
public void vocalize() { System.out.println("Woof!"); }
class Cow implements Animal {
public void vocalize() { moo(); }
public void moo() {System.out.println("Moo!"); }

What Happens?

1. Animal a = new Animal();
a. vocalize();
2. Dog d = new Dog();
d.vocalize();
3. Animal b = new Cow();
b.vocalize();
4. b.moo();

Historical note: simulation and the origins

of OO programming

  • Simula 67 was the first object-oriented language
  • Developed by Kristin

Nygaard and Ole-Johan

Dahl at the Norwegian

Computing Center

  • Developed to support discrete-event simulation
    • Application: operations research, e.g. traffic analysis
    • Extensibility was a key quality attribute for them
    • Code reuse was another