1 / 15

COMP 114

COMP 114. Weekly Recitation November 14, 2003. What’s Happening Monday?. The Second exam !. So What’s Happening Today?. We will cover… A few review questions An example using Object Editor. Question 1 (Recursion).

fraley
Download Presentation

COMP 114

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. COMP 114 Weekly Recitation November 14, 2003

  2. What’s Happening Monday? The Second exam!

  3. So What’s Happening Today? We will cover… A few review questions An example using Object Editor

  4. Question 1 (Recursion) Write a recursive function that, given an array of integers, returns the sum of its elements.

  5. Question 1 (Recursion) Write a recursive function that, given an array of integers, returns the sum of its elements. // input: array of integers // output: sum of its elements int sigma (list L){ if (L.isEmpty()) return 0; return L.head() + sigma(L.tail()); }

  6. (a) Implement the shallow and deep copy methods for the class, ALine, given below. You can add methods to APoint if you need to. Before you write this code, you should look at part (b) of this question on the next page. In fact, it may be helpful to do part (b) first. public class APoint { int x,y; public APoint (int initX, int initY) { x = initX; y = initY; } // add new methods here } public class ALine { APoint startPoint, endPoint; public ALine (APoint initStartPoint, APoint initEndPoint) { startPoint = initStartPoint; endPoint = initEndPoint; } public ALine shallowCopy () { // fill in } public ALine deepCopy() { // fill in } } Question 2 (shallow/deep copy)

  7. (a) Implement the shallow and deep copy methods for the class, ALine, given below. You can add methods to APoint if you need to. Before you write this code, you should look at part (b) of this question on the next page. In fact, it may be helpful to do part (b) first. public class APoint { int x,y; public APoint (int initX, int initY) { x = initX; y = initY; } public APoint shallowCopy () { return new APoint (x, y); } public APoint deepCopy () { return shallowCopy(); } } public class ALine { APoint startPoint, endPoint; public ALine (APoint initStartPoint, APoint initEndPoint) { startPoint = initStartPoint; endPoint = initEndPoint; } public ALine shallowCopy () { return new ALine (startPoint, endPoint); } public ALine deepCopy() { return new ALine (startPoint.deepCopy(), endPoint.deepCopy()); } } Question 2 (shallow/deep copy)

  8. (b) Illustrate, with figures, the difference between shallow and deep copies of an instance of ALine using the following examples: ALine original = new ALine (new APoint(0,0), new APoint(10,10)); ALine shallowCopy = original.shallowCopy(); ALine deepCopy = original.deepCopy(); Your figures should show the HAS-A links from the three variables, original, shallowCopy, and deepCopy. You should not draw a shared subobject twice, that is, you should show that it is being shared. Question 2 (shallow/deep copy) original shallow deep point point point point

  9. package ex; import java.util.NoSuchElementException; public class AnEmptyEnumerationException extends NoSuchElementException {}; package ex; import java.util.Vector; import java.util.Enumeration; import java.util.NoSuchElementException; class AnExceptionThrower { public static void main (String args[]) { Vector v = new Vector (); // create empty vector try { printFirstAndSecondElements(v.elements()); } catch (NoSuchElementException nsee) { System.out.println("Missing first or second vector element."); } v.addElement(""); printFirstAndSecondElements(v.elements()); } public static void printFirstAndSecondElements(Enumeration elements) throws AnEmptyEnumerationException, NoSuchElementException { try { System.out.println(elements.nextElement().toString().substring(1)); } catch (NoSuchElementException nsee) { System.out.println("Enumeration is empty"); throw new AnEmptyEnumerationException(); } System.out.println (elements.nextElement().toString()); } } What output does this program produce? Question 3 (exceptions)

  10. package ex; import java.util.NoSuchElementException; public class AnEmptyEnumerationException extends NoSuchElementException {}; package ex; import java.util.Vector; import java.util.Enumeration; import java.util.NoSuchElementException; class AnExceptionThrower { public static void main (String args[]) { Vector v = new Vector (); // create empty vector try { printFirstAndSecondElements(v.elements()); } catch (NoSuchElementException nsee) { System.out.println("Missing first or second vector element."); } v.addElement(""); printFirstAndSecondElements(v.elements()); } public static void printFirstAndSecondElements(Enumeration elements) throws AnEmptyEnumerationException, NoSuchElementException { try { System.out.println(elements.nextElement().toString().substring(1)); } catch (NoSuchElementException nsee) { System.out.println("Enumeration is empty"); throw new AnEmptyEnumerationException(); } System.out.println (elements.nextElement().toString()); } } What output does this program produce? error message and program terminates Question 3 (exceptions)

  11. Object Editor Example private final double ABSOLUTE_ZERO = 273.15; private double _f = 32, _c = 0, _k = ABSOLUTE_ZERO; public double getFahrenheit() { return _f; } public double getCelsius() { return _c; } public double getKelvin() { return _k; } public void setFahrenheit(double nf) { _f = nf; _c = 5 * (_f - 32) / 9; _k = _c + ABSOLUTE_ZERO; } public void setCelsius(double nc) { _c = nc; _k = _c + ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } public void setKelvin(double nk) { _k = nk; _c = _k - ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } }

  12. Object Editor Example private final double ABSOLUTE_ZERO = 273.15; private double _f = 32, _c = 0, _k = ABSOLUTE_ZERO; public double getFahrenheit() { return _f; } public double getCelsius() { return _c; } public double getKelvin() { return _k; } public void setFahrenheit(double nf) { _f = nf; _c = 5 * (_f - 32) / 9; _k = _c + ABSOLUTE_ZERO; } public void setCelsius(double nc) { _c = nc; _k = _c + ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } public void setKelvin(double nk) { _k = nk; _c = _k - ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } } package converter; import bus.uigen.ObjectEditor; import converter.TemperatureConverter; public class ConverterMain { public static void main(String[] args) { ObjectEditor.edit(new TemperatureConverter()); } }

  13. Object Editor Example private final double ABSOLUTE_ZERO = 273.15; private double _f = 32, _c = 0, _k = ABSOLUTE_ZERO; public double getFahrenheit() { return _f; } public double getCelsius() { return _c; } public double getKelvin() { return _k; } public void setFahrenheit(double nf) { _f = nf; _c = 5 * (_f - 32) / 9; _k = _c + ABSOLUTE_ZERO; } public void setCelsius(double nc) { _c = nc; _k = _c + ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } public void setKelvin(double nk) { _k = nk; _c = _k - ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } } package converter; import bus.uigen.ObjectEditor; import converter.TemperatureConverter; public class ConverterMain { public static void main(String[] args) { ObjectEditor.edit(new TemperatureConverter()); } }

  14. Object Editor Example private final double ABSOLUTE_ZERO = 273.15; private double _f = 32, _c = 0, _k = ABSOLUTE_ZERO; public double getFahrenheit() { return _f; } public double getCelsius() { return _c; } public double getKelvin() { return _k; } public void setFahrenheit(double nf) { _f = nf; _c = 5 * (_f - 32) / 9; _k = _c + ABSOLUTE_ZERO; } public void setCelsius(double nc) { _c = nc; _k = _c + ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } public void setKelvin(double nk) { _k = nk; _c = _k - ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } } package converter; import bus.uigen.ObjectEditor; import converter.TemperatureConverter; public class ConverterMain { public static void main(String[] args) { ObjectEditor.edit(new TemperatureConverter()); } }

  15. Object Editor Example private final double ABSOLUTE_ZERO = 273.15; private double _f = 32, _c = 0, _k = ABSOLUTE_ZERO; public double getFahrenheit() { return _f; } public double getCelsius() { return _c; } public double getKelvin() { return _k; } public void setFahrenheit(double nf) { _f = nf; _c = 5 * (_f - 32) / 9; _k = _c + ABSOLUTE_ZERO; } public void setCelsius(double nc) { _c = nc; _k = _c + ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } public void setKelvin(double nk) { _k = nk; _c = _k - ABSOLUTE_ZERO; _f = 9 * _c / 5 + 32; } } package converter; import bus.uigen.ObjectEditor; import converter.TemperatureConverter; public class ConverterMain { public static void main(String[] args) { ObjectEditor.edit(new TemperatureConverter()); } }

More Related