Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleDel 4 - Objektstrukturer og delegering (20 %)

Oppgave a) Objektstrukturer (12 %)

Fyll inn vedlagte Temperature. Temperature har oversikt over en temperatur som enten er i Fahrenheit eller i Celsius.

Temperature har følgende metoder

  • Temperature(double degrees, char scale) - Initialiserer et Temperature-objekt med en skala (Fahrenheit/Celsius) og et antall grader
  • getScale() - Returnerer om temperaturen er i Fahrenheit eller Celsius.
  • getDegrees() - Returnerer temperaturen i antall grader.
  • toOther() - Gjør om dette temperatur-objektet til den andre skalaen og returnerer det.
  • inOther() - Lager et nytt temperatur-objekt i den den andre skalaen og returnerer det.
  • lower(double amount) - Senker temperaturen med et antall grader.
Expand
titleSkjelett til del 4Kodeskjelett 4a


Code Block
languagejava
package del4;

public class Temperature {

	// Add any needed fields
	/**
	 * 
	 * @param degrees an arbitrary number
	 * @param scale   a character declaring the scale
	 * 
	 * @throws IllegalArgumentException if scale is not 'C' or 'F'
	 */
	public Temperature(double degrees, char scale) {
		// TODO

	}

	/**
	 * 
	 * @return The current scale
	 */
	public char getScale() {
		// TODO
		return '0';
	}

	/**
	 * 
	 * @return the current degree of this object
	 */
	public double getDegrees() {
		// TODO
		return 0;
	}

	/**
	 * Converts this temperature object to be the opposite scale of what it
	 * currently is
	 * 
	 * @return this temperature object, converted with value in Celsius if the scale
	 *         of this temperature object is Fahrenheit, and value in Fahrenheit if
	 *         this the scale of this temperature object is Celsius
	 */
	public Temperature toOther() {
		// TODO
		return null;
	}

	/**
	 * Creates a new temperature object with values in the other scale of this
	 * temperature object
	 * 
	 * @return a new Temperature object, with value in Celsius if the scale of this
	 *         temperature object is Fahrenheit, and value in Fahrenheit if the
	 *         scale of this temperature object is Celsius
	 */
	public Temperature inOther() {
		// TODO
		return null;
	}

	/**
	 * Lowers the temperature
	 * 
	 * @param amount the amount to lower by
	 */
	public void lower(double amount) {
		// TODO
	}

	/**
	 * 
	 * @param celsius
	 * @return the temperature in Fahrenheit
	 */
	public static double convertCelsisusToFahrenheit(double celsius) {
		return (celsius * 1.8 + 32.0);
	}

	/**
	 * 
	 * @param fahrenheit
	 * @return the temperature in Celsius
	 */
	public static double convertFahrenheitToCelsius(double fahrenheit) {
		return (fahrenheit - 32.0) / 1.8;
	}

	public static void main(String[] args) {
		Temperature t = new Temperature(20, 'C');
		// Should print 20
		System.out.println(t.getDegrees());
		t.lower(10);
		// Should now print 10
		System.out.println(t.getDegrees());
		t.toOther();
		// Should now print 50
		System.out.println(t.getDegrees());
		t.toOther();

		Temperature t2 = t.inOther();
		// Should be 50;
		System.out.println(t2.getDegrees());

		// Should be 10
		System.out.println(t.getDegrees());
	}

}




Expand
titleLF


Code Block
languagejava
package del4;

public class Temperature {
	
	private char scale; // Only valid values are C or F
	private double degree;
	
	/**
	 * 
	 * @param degrees an arbitrary number
	 * @param scale a character declaring the scale
	 * 
	 * @throws IllegalArgumentException if scale is not 'C' or 'F'
	 */
	public Temperature(double degrees, char scale) {
		this.degree = degrees;
		if (scale != 'C' && scale != 'F') {
			throw new IllegalArgumentException();
		}
		this.scale = scale;
	}

	/**
	 * 
	 * @return The current scale
	 */
	public char getScale() {
		return scale;
	}

	
	/**
	 * 
	 * @return the current degree of this object
	 */
	public double getDegrees() {
		return degree;
	}

	/**
	 * Converts this temperature object to be the opposite scale of what it
	 * currently is
	 * 
	 * @return this temperature object, converted with value in Celsius if the value
	 *         of this temperature object is Fahrenheit, and value in Fahrenheit if
	 *         this temperature object is Celsius
	 */
	public Temperature toOther() {
		if (this.scale == 'C') {
			this.scale = 'F';
			this.degree = convertCelsisusToFahrenheit(this.degree);
		}
		else {
			this.scale = 'C';
			this.degree = convertFahrenheitToCelsius(this.degree);
		}
		return this;
	}

	/**
	 * Creates a new temperature object with values in the other scale of this
	 * temperature object.
	 * 
	 * @return a new Temperature object, with value in Celsius if the value of this
	 *         temperature object is Fahrenheit, and value in Fahrenheit if this
	 *         temperature object is Celsius
	 */
	public Temperature inOther() {
		if (this.scale == 'C') {
			return new Temperature(convertCelsisusToFahrenheit(this.degree), 'F');
	
		}
		else {
			return new Temperature(convertFahrenheitToCelsius(this.degree), 'C');

		}
	}


	
	/**
	 * Lowers the temperature
	 * @param amount the amount to lower by
	 */
	public void lower(double amount) {
		this.degree -= amount;
	}
	
	/**
	 * 
	 * @param celsius
	 * @return the temperature in Fahrenheit
	 */
	public static double convertCelsisusToFahrenheit(double celsius) {
		return (celsius * 1.8 + 32.0);
	}
	
	/**
	 * 
	 * @param fahrenheit
	 * @return the temperature in Celsius
	 */
	public static double convertFahrenheitToCelsius(double fahrenheit) {
		return (fahrenheit - 32.0) / 1.8;
	}
	
	public static void main(String[] args) {
		Temperature t = new Temperature(20, 'C');
		// Should print 20
		System.out.println(t.getDegrees());
		t.lower(10);
		// Should now print 10
		System.out.println(t.getDegrees());
		t.toOther();
		// Should now print 50
		System.out.println(t.getDegrees());
		t.toOther();

		Temperature t2 = t.inOther();
		// Should be 50;
		System.out.println(t2.getDegrees());

		// Should be 10
		System.out.println(t.getDegrees());
	}


	
	
}



Oppgave b) Delegering (8%)

Fyll inn DelegatingTemperature. DelegatingTemperature skal delegere til et underliggende Temperature-objekt og ha metodene beskrevet under. Du kan gå ut ifra at alle metodene til delegaten fungerer slik de skal.

  • DelegatingTemperature(Temperature delegate, char scale) - Oppretter et DelegatingTemperature-objekt med en skala (Fahrenheit/Celsisus) og et antall grader som er representert i delegaten.
  • getScale() - Returnerer om temperaturen er i Fahrenheit eller Celsius.
  • getDegrees() - Returnerer temperaturen i antall grader.
  • toOther() - Gjør om dette temperatur-objektet til den andre skalaen og returnerer det.
  • inOther() - Lager et nytt temperatur-objekt i den den andre skalaen og returnerer det.
Expand
titleKodeskjelett 4b


Code Block
languagejava
package del4;

public class DelegatingTemperature {

	// Add any needed fields
	/**
	 * 
	 * @param delegate - the Temperature to get degree from
	 * @param scale    a character declaring the scale of this temperature object
	 * 
	 * @throws IllegalArgumentException if scale is not 'C' or 'F'
	 */
	public DelegatingTemperature(Temperature delegate, char scale) {
		// TODO
	}

	/**
	 * 
	 * @return The current scale
	 */
	public char getScale() {
		// TODO
		return '0';
	}

	/**
	 * 
	 * @return the current degree from the delegate, converted to the correct scale
	 */
	public double getDegrees() {
		// TODO
		return 0;
	}

	/**
	 * 
	 * @return this temperature object, with scale converted to Celsius if the
	 *         current scale of this delegatingTemperature object is in Fahrenheit,
	 *         and value in Fahrenheit if this delegatingTemperature object is in
	 *         Celsius
	 */
	public DelegatingTemperature toOther() {
		// TODO
		return null;
	}

	/**
	 * 
	 * @return a new DelegatingTemperature object, with the same delegate, with
	 *         scale converted to Celsius if the current scale of this
	 *         delegatingTemperature object is Fahrenheit, and value in Fahrenheit
	 *         if this delegatingTemperature object is Celsius
	 */
	public DelegatingTemperature inOther() {
		// TODO
		return null;

	}

	public static void main(String[] args) {
		Temperature t = new Temperature(10, 'C');
		DelegatingTemperature dt = new DelegatingTemperature(t, 'F');
		// Should now print 10
		System.out.println(t.getDegrees());
		// Should now print 50
		System.out.println(dt.getDegrees());
		dt.toOther();
		// Should now print 10
		System.out.println(dt.getDegrees());
		// Should now print 10
		System.out.println(t.getDegrees());
	}
}



Expand
titleLF


Code Block
languagejava
package del4;

public class DelegatingTemperature {

	// Add any needed fields
	private Temperature delegate;
	private char scale;
	/**
	 * 
	 * @param delegate - the Temperature to get degree from
	 * @param scale    a character declaring the scale of this temperature object
	 * 
	 * @throws IllegalArgumentException if scale is not 'C' or 'F'
	 */
	public DelegatingTemperature(Temperature delegate, char scale) {
		this.delegate = delegate;
		this.setScale(scale);
	}

	/**
	 * 
	 * @return The current scale
	 */
	public char getScale() {
		return this.scale;
	}
	
	private void setScale(char scale) {
		if (scale != 'F' && scale != 'C') {
			throw new IllegalArgumentException() ;
		}
		this.scale = scale;
	}

	/**
	 * 
	 * @return the current degree from the delegate, converted to the correct scale
	 */
	public double getDegrees() {
		if (this.scale == this.delegate.getScale()) {
			return this.delegate.getDegrees();
		}
		return this.delegate.inOther().getDegrees();
	}

	/**
	 * 
	 * @return this temperature object, with scale converted to Celsius if the
	 *         current scale of this delegatingTemperature object is in Fahrenheit, and
	 *         value in Fahrenheit if this delegatingTemperature object is in Celsius
	 */
	public DelegatingTemperature toOther() {
		this.scale = this.scale == 'C' ? 'F' : 'C';
		return this;
	}

	/**
	 * 
	 * @return a new DelegatingTemperature object, with the same delegate, with
	 *         scale converted to Celsius if the current scale of this
	 *         delegatingTemperature object is Fahrenheit, and value in Fahrenheit
	 *         if this delegatingTemperature object is Celsius
	 */
	public DelegatingTemperature inOther() {
		return new DelegatingTemperature(this.delegate, this.scale == 'C' ? 'F' : 'C');

	}

	public static void main(String[] args) {
		Temperature t = new Temperature(10, 'C');
		DelegatingTemperature dt = new DelegatingTemperature(t, 'F');
		// Should now print 10
		System.out.println(t.getDegrees());
		// Should now print 50
		System.out.println(dt.getDegrees());
		dt.toOther();
		// Should now print 10
		System.out.println(dt.getDegrees());
		// Should now print 10
		System.out.println(t.getDegrees());
	}
}



...