Exercise Sheet 1 - Solutions

Similar documents
Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Objects and Iterators

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

coe318 Lab 2 ComplexNumber objects

This exam is open book. Each question is worth 3 points.

Inheritance and Interfaces

Exercise Sheet 4 - Solutions

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Introduction to Programming Using Java (98-388)

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Language Features. 1. The primitive types int, double, and boolean are part of the AP

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

TeenCoder : Java Programming (ISBN )

CIT 590 Homework 6 Fractions

COP 3330 Final Exam Review

תרגול 9 מחלקות ואובייקטים

COMP-202 More Complex OOP

CS 302: Introduction to Programming in Java. Lecture 15

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

COP 3337 Test 3. private int minimumposition(int from) { Part 1: Selection Sort

CSE 143 Lecture 2. reading:

CompuScholar, Inc. 9th - 12th grades

Answers to review questions from Chapter 2

This page intentionally left blank

More About Objects and Methods

Polymorphism: Inheritance Interfaces

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

Software Construction

Java Object Oriented Design. CSC207 Fall 2014

EECS2030 Week 7 worksheet Tue Feb 28, 2017

Collections Algorithms

CO Java SE 8: Fundamentals

APCS Semester #1 Final Exam Practice Problems

Ryerson University Vers HAL6891A-09 School of Computer Science CPS109 Midterm Test Fall 09 page 1 of 7

CIS133J. Working with Numbers in Java

List ADT. Announcements. The List interface. Implementing the List ADT

IMACS: AP Computer Science A

Building Java Programs Chapter 13


Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CIT Special final examination

CMSC 132, Object-Oriented Programming II Summer Lecture 5:

CS 520 Theory and Practice of Software Engineering Fall 2018

Written Test 2. CSE Section M, Winter p. 1 of 8. Family Name: Given Name(s): Student Number:

CSC 1351 The Twelve Hour Exam From Hell

Course materials Reges, Stuart, and Stepp, Martin. Building Java Programs: A Back to Basics Approach. 2d ed. (Boston: Addison-Wesley, 2011).

Comparing Objects 3/14/16

CS 520 Theory and Practice of Software Engineering Fall 2017

Java 1.8 Programming

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

Java Programming with Eclipse

CS 520 Theory and Practice of Software Engineering Fall 2017

Index COPYRIGHTED MATERIAL

Cloning Enums. Cloning and Enums BIU OOP

Fundamental Java Methods

Inheritance (Part 2) Notes Chapter 6

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Topic 7: Algebraic Data Types

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

The Java Collections Framework and Lists in Java Parts 1 & 2

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Collections, Maps and Generics

ClassCastException. ClassCastException

Solving Equations with Inverse Operations

Contents Chapter 1 Introduction to Programming and the Java Language

AP Computer Science Summer Work Mrs. Kaelin

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

Interview Questions I received in 2017 and 2018

Recitation #2 Abstract Data Types, Collection Classes, and Linked List

Chapter 4 Defining Classes I

CS1020 Data Structures and Algorithms I

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

MODULE 3q - An Extended Java Object

Java Magistère BFA

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Exam Duration: 2hrs and 30min Software Design

M257 Past Paper Oct 2008 Attempted Solution

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

MAT 3670: Lab 3 Bits, Data Types, and Operations

ECM1406. Answer Sheet Lists

Generic BST Interface

Inheritance. Transitivity

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Programming Problems 22nd Annual Computer Science Programming Contest

Test 1: CPS 100. Owen Astrachan. October 1, 2004

Summary. Recursion. Overall Assignment Description. Part 1: Recursively Searching Files and Directories

JAVA MOCK TEST JAVA MOCK TEST II

Csci 102: Sample Exam

Java Review: Objects

(Constructor) public A (int n){ for (int i = 0; i < n; i++) { new A(i); } System.out.println("*"); }

Review. these are the instance variables. these are parameters to the methods

The return Statement

Last class. -More on polymorphism -super -Introduction to interfaces

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Inheritance. Inheritance

Transcription:

Exercise Sheet 1 - Solutions Alessandro Gnoatto October 23, 2014 1 Exercise 1 / 2 4 package de. lmu. firstclass ; 6 / @author Alessandro Gnoatto 8 10 public class ComplexNumber { //fields 12 private double real ; private double imag ; 14 16 //specify a constructor public ComplexNumber (double real, double imag ){ 18 this. real = real ; this. imag = imag ; 20 22 //get methods public double getreal (){ 24 return this. real ; 26 public double getimag (){ 28 return this. real ; 30 //set methods 32 public void setreal (double real ){ this. real = real ; 34 36 public void setimag (double imag ){ this. imag = imag ; 38 40 //some methods public s t a t i c ComplexNumber add ( ComplexNumber a, ComplexNumber b){ 42 return new ComplexNumber (a. real +b.real,a. imag +b. imag ); 44 1

public s t a t i c ComplexNumber subtract ( ComplexNumber a, ComplexNumber b){ 46 return new ComplexNumber (a.real -b.real,a.imag -b. imag ); 48 public s t a t i c ComplexNumber multiply ( ComplexNumber a, ComplexNumber b){ 50 return new ComplexNumber ( a. real * b. real - a. imag * b. imag,a. imag * b. real + a. real * b. imag ); 52 54 public s t a t i c ComplexNumber divide ( ComplexNumber a, ComplexNumber b){ return new ComplexNumber (( a. real * b. real + a. imag * b. imag ) 56 /(b. real * b. real - b. imag * b. imag ), (a. imag * b. real - a. real * b. imag ) 58 /(b. real * b. real - b. imag * b. imag )); 60 public s t a t i c double abs ( ComplexNumber a){ 62 return Math. sqrt (a. real *a. real +a. imag *a. imag ); 64 66 2 Exercise 2 2.1 Interface / 2 (c) Copyright Christian P. Fries, Germany. All rights reserved. Contact: email@christian fries.de. 4 Created on 23.11.2012 6 package net. finmath. time ; 8 import java. util. ArrayList ; 10 / @author Christian Fries 12 public interface TimeDiscretizationInterface extends Iterable < Double > { 14 / 16 @return Returns the number of time discretization points. 18 int getnumberoftimes (); 20 / @return Returns the number of time steps (= number of discretization points 1). 22 int getnumberoftimesteps (); 24 / 26 Returns the time for the given time index. 2

28 @param timeindex Time index @return Returns the time for a given time index. 30 double gettime ( int timeindex ); 32 / 34 Returns the time step from the given time index to the next one. 36 @param timeindex Time index @return Returns the time step 38 double gettimestep ( int timeindex ); 40 / 42 Returns the time index for the given time. If the given time is not in the time discretization the method returns a negative number being ( insertionpoint 1). 44 @param time The time. 46 @return Returns the time index for a given time. 48 int gettimeindex (double time ); 50 / Returns the time index for the time in the time discretization which is the nearest 52 to the given time, being less or equal (i.e. max(i : timediscretization[i] time where timediscretization[i] timediscretization[j]). 54 @param time Given time. 56 @return Returns a time index 58 int gettimeindexnearestlessorequal (double time ); 60 / Returns the time index for the time in the time discretization which is the nearest 62 to the given time, being greater or equal (i.e. min(i : timediscretization[i] time where timediscretization[i] timediscretization[j]). 64 @param time Given time. 66 @return Returns a time index 68 int gettimeindexnearestgreaterorequal (double time ); 70 / Return a clone of this time discretization as <code> double[]</code>. 72 @return The time discretization as <code>double[]</code> 74 double[] getasdoublearray (); 76 / Return a clone of this time discretization as <code> ArrayList<Double></code>. 78 Note that this method is costly in terms of performance. 3

80 @return The time discretization as <code>arraylist< Double></code> 82 ArrayList < Double > getasarraylist (); 84 / 86 Return a new time discretization where all time points have been shifted by a given time shift. 88 @param timeshift A time shift applied to all discretization points. 90 @return A new time discretization where all time points have been shifted by the given time shift. 92 TimeDiscretizationInterface gettimeshiftedtimediscretization (double timeshift ); 2.2 Class / 2 (c) Copyright Christian P. Fries, Germany. All rights reserved. Contact: email@christian fries.de. 4 Created on 20.04.2008 6 package net. finmath. time ; 8 import java. io. Serializable ; import java. util. ArrayList ; 10 import java. util. Arrays ; import java. util. Iterator ; 12 / 14 This class represents a set of discrete points in time. <br> 16 It handles the mapping from time indices to time points and back. It uses a time tick size ("quantum") of 1.0 / (365.0 24.0) (which corresponds to one hour if 1.0 is a non leap year): 18 Times are rounded to the nearest multiple of 1.0 / (365.0 24.0). 20 Objects of this class are immutable. 22 @author Christian Fries @version 1.5 24 public class TimeDiscretization implements Serializable, TimeDiscretizationInterface { 26 28 private s t a t i c f i n a l long serialversionuid = 6880668325019167781 L; private f i n a l double[] timediscretization ; 30 private f i n a l double timeticksize = 1.0 / (365.0 * 24.0) ; 32 public enum ShortPeriodLocation { 4

SHORT_PERIOD_AT_START, 34 SHORT_PERIOD_AT_END 36 / 38 Constructs a time discretization from a given set of doubles. 40 @param times Given array or arguments list of discretization points. 42 public TimeDiscretization (double... times ) { super(); 44 this. timediscretization = times. clone (); java. util. Arrays. sort ( this. timediscretization ); 46 48 / Constructs a time discretization from a given set of Doubles. 50 @param times Given array or arguments list of discretization points 52 public TimeDiscretization ( Double [] times ) { 54 super(); this. timediscretization = new double[ times. length ]; 56 for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) this. timediscretization [ timeindex ] = roundtotimeticksize ( times [ timeindex ]); java. util. Arrays. sort ( this. timediscretization ); 58 60 / Constructs a time discretization from a given ArrayList of Doubles. 62 @param timediscretization Given ArrayList of discretization points 64 public TimeDiscretization ( ArrayList < Double > timediscretization ) { 66 super(); this. timediscretization = new double[ timediscretization. size () ]; 68 for ( int timeindex =0; timeindex < timediscretization. size (); timeindex ++) this. timediscretization [ timeindex ] = roundtotimeticksize ( timediscretization. get ( timeindex )) ; java. util. Arrays. sort ( this. timediscretization ); 70 72 / Constructs an equi distant time discretization with points timediscretization[i] being 74 <code>for(i=0; i timesteps; i++) timediscretization[ i] = initial + i deltat;</code> 76 @param initial First discretization point. @param numberoftimesteps Number of time steps. 78 @param deltat Time step size. 5

80 public TimeDiscretization (double initial, int numberoftimesteps, double deltat ) { super(); 82 timediscretization = new double[ numberoftimesteps +1]; for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) timediscretization [ timeindex ] = roundtotimeticksize ( initial + timeindex * deltat ); 84 86 / Constructs an equi distant time discretization with stub periods at start or end. 88 @param initial First discretization point. 90 @param last Last time steps. @param deltat Time step size. 92 @param shortperiodlocation Placement of the stub period. 94 public TimeDiscretization (double initial, double last, double deltat, ShortPeriodLocation shortperiodlocation ) { super(); 96 int numberoftimesteps = ( int )(( last - initial )/ deltat + 0.5) ; 98 // Adjust for short period, if any i f ( roundtotimeticksize ( initial + numberoftimesteps * deltat ) < roundtotimeticksize ( last )) numberoftimesteps ++; 100 timediscretization = new double[ numberoftimesteps +1]; 102 i f ( shortperiodlocation == ShortPeriodLocation. SHORT_PERIOD_AT_END ) { for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) timediscretization [ timeindex ] = roundtotimeticksize ( initial + timeindex * deltat ); 104 timediscretization [ timediscretization. length -1] = last ; 106 else { for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) timediscretization [ timeindex ] = roundtotimeticksize ( last - ( numberoftimesteps - timeindex ) * deltat ); 108 timediscretization [0] = initial ; 110 112 @Override public int getnumberoftimes () { 114 return timediscretization. length ; 116 @Override 118 public int getnumberoftimesteps () { return timediscretization. length -1; 120 122 @Override public double gettime ( int timeindex ) { 124 return timediscretization [ timeindex ]; 126 6

@Override 128 public double gettimestep ( int timeindex ) { return timediscretization [ timeindex +1] - timediscretization [ timeindex ]; 130 132 @Override public int gettimeindex (double time ) { 134 int index = java. util. Arrays. binarysearch ( timediscretization, roundtotimeticksize ( time )); return index ; 136 138 @Override public int gettimeindexnearestlessorequal (double time ) { 140 int index = java. util. Arrays. binarysearch ( timediscretization, roundtotimeticksize ( time )); i f ( index < 0) index = - index -2; 142 return index ; 144 @Override 146 public int gettimeindexnearestgreaterorequal (double time ) { int index = java. util. Arrays. binarysearch ( timediscretization, time ); 148 i f ( index < 0) index = - index -1; return index ; 150 152 @Override public double[] getasdoublearray () { 154 // Note: This is a deep copy return timediscretization. clone (); 156 158 @Override public ArrayList < Double > getasarraylist () { 160 ArrayList < Double > times = new ArrayList < Double >( timediscretization. length ); for (double atimediscretization : timediscretization ) times. add ( atimediscretization ); 162 return times ; 164 @Override 166 public TimeDiscretizationInterface gettimeshiftedtimediscretization (double timeshift ) { double[] newtimediscretization = new double[ timediscretization. length ]; 168 for ( int timeindex =0; timeindex < timediscretization. length ; timeindex ++) newtimediscretization [ timeindex ] = roundtotimeticksize ( timediscretization [ timeindex ]+ timeshift ); 170 java. util. Arrays. sort ( this. timediscretization ); return new TimeDiscretization ( newtimediscretization ); 172 174 @Override public Iterator < Double > iterator () { 176 return this. getasarraylist (). iterator (); 178 7

@Override 180 public String tostring () { return " TimeDiscretization [ timediscretization =" 182 + Arrays. tostring ( timediscretization ) + ", timeticksize =" + timeticksize + "]"; 184 186 private double roundtotimeticksize (double time ) { return Math. rint ( time / timeticksize )* timeticksize ; 188 8