Soluciones ejercicio Excepciones

Creo una clase nueva:

public class ExceptionMarcaIncorrecta extends Exception{

 public ExceptionMarcaIncorrecta() {
 // TODO Auto-generated constructor stub
 }
 public ExceptionMarcaIncorrecta(String mensaje) {
 super(mensaje);
 
 }
}

Añado esa excepción en setMarcas:

 public void setMarca(String marca) throws ExceptionMarcaIncorrecta {
 for (String m : VALID_BRANDS) {
 if (marca.equals(m)) {
 this.marca = marca;
 return;
 }
 }
 throw new ExceptionMarcaIncorrecta("Marca incorrecta");

 }

Modifico los constructores para que sean específicos:

public Television(String marca, int volumen, String proveedor) throws ExceptionMarcaIncorrecta {
 setMarca(marca);
 setVolume(volumen);
 setProveedor(proveedor);
 }

 public Television(String proveedor) throws ExceptionMarcaIncorrecta {

 this("LG", 6, proveedor);
 }

Ahora puedo usarla en la prueba:

try {
Television tv1 = new Television("ww", 4, "Miro");
 tv1.setVolume(-5);
 tv1.setMarca("Sony");
 tv1.setModelo("weweew");
 System.out.println(tv1);
} catch (ExceptionMarcaIncorrecta e) {
 System.out.println(e.getMessage());
 }
 catch (Exception e) {
 System.out.println(e.getMessage());
 }

 

Archivo Radio

radio

 

/*
 * This code is sample code, provided as-is, and we make no
 * warranties as to its correctness or suitability for
 * any purpose.
 *
 * We hope that it's useful to you. Enjoy.
 * Copyright 2004-13 LearningPatterns Inc.
 */

/*
 * Lab - Interfaces
 *
 * This class defines a Radio. It will implement Volume easily
 *
 *
 * This class performs no data validation; however, it
 * mutes/unmutes appropriately, i.e., can't mute a muted
 * Radio
 */

package com.entertainment;

// TODO add in declaration for implementing Volume
public class Radio

{
 // INSTANCE VARIABLES
 private int volume; // the volume

 // for muting behavior
 private int oldVolume;
 private boolean isMuted;

 // CONSTRUCTORS
 public Radio()
 {
 super();
 }

 public Radio(int volumeIn)
 {
 super();
 this.setVolume(volumeIn);
 }


 // ACCESSOR METHODS
 // from interface Volume
 public void setVolume(int volumeIn)
 {
 // delegate to contained VolumeControl object
 volume = volumeIn;
 }

/* TODO - Uncomment this method so Radio will compile after implementing the interface 
 // from interface IVolume
 public int getVolume()
 {
 return volume;
 }
*/

 // from interface IVolume
 // BEHAVIORAL METHODS
 public void mute()
 {
 if (!this.isMuted()) // not currently muted
 {
 // store current volume in oldVolume
 oldVolume = volume;
 
 // set volume to zero
 this.setVolume(0);
 }
 else // currently muted
 {
 // restore volume from oldVolume value
 this.setVolume(oldVolume);
 }
 
 // toggle muted flag
 isMuted = !isMuted;
 }
 

 // from interface IVolume
 public boolean isMuted()
 {
 // delegate to contained VolumeControl object
 return isMuted;
 }


 // returns a string representation of this class instance
 @Override
 public String toString()
 {
 // this method uses the "ternary" (three terms) operator
 // (expression ? true-value : false-value)
 // it's just a very compact if-else statement

 return "Radio: volume=" +
 (!this.isMuted() ? String.valueOf(volume) : "<muted>");
 }
}

Ejemplo clase abstracta

public class Puntos {
 private int x;
 private int y;
 
 /**
 * @return the x
 */
 public int getX() {
 return x;
 }

 /**
 * @param x the x to set
 */
 public void setX(int x) {
 this.x = x;
 }

 /**
 * @return the y
 */
 public int getY() {
 return y;
 }

 /**
 * @param y the y to set
 */
 public void setY(int y) {
 this.y = y;
 }

 public Puntos() {
 // TODO Auto-generated constructor stub
 }

}
public abstract class Poligono {
 private Puntos[] puntos; 
 public Poligono() {
 
 }
 public Poligono(int tam) {
 puntos = new Puntos[tam];
 }
 public abstract double area();

}

 

public class Cuadrado extends Poligono {

 public Cuadrado() {
 super(4);
 }
 public double area(){
 return 5;
 }

}

Clase ColorTelevision

package com.entertaiment;

public class ColorTelevision extends Television {

 public ColorTelevision(String proveedor) {
 super(proveedor);
 
 }
 public ColorTelevision(String proveedor, int color) {
 super(proveedor);
 colorTv=color;
 }
 private int colorTv;
 
 public ColorTelevision(String marca, int volumen, String proveedor) {
 super(marca, volumen, proveedor);
 // TODO Auto-generated constructor stub
 }

 public String getMarca(){
 return super.getMarca()+" TV!";
 }
 public String toString(){
 return super.toString()+" | "+colorTv;
 }

}

Clase equipo (Composición)

public class Equipo {
 private Persona jefe;
 private Persona coordinador;
 private Persona[] miembros = new Persona[3];

 public Equipo(String jefe, String coordinador, String emp1, String emp2, String emp3) {
 this.jefe = new Persona(jefe);
 this.coordinador = new Persona(coordinador);
 this.miembros[0] = new Persona(emp1);
 this.miembros[1] = new Persona(emp2);
 this.miembros[2] = new Persona(emp3);
 }

 /**
 * @return the jefe
 */
 public Persona getJefe() {
 return jefe;
 }

 /**
 * @param jefe
 * the jefe to set
 */
 public void setJefe(Persona jefe) {
 this.jefe = jefe;
 }

 public void setJefe(String jefe) {
 this.jefe = new Persona(jefe);
 }

 /**
 * @return the coordinador
 */
 public Persona getCoordinador() {
 return coordinador;
 }

 /**
 * @param coordinador
 * the coordinador to set
 */
 public void setCoordinador(Persona coordinador) {
 this.coordinador = coordinador;
 }

 /**
 * @return the miembros
 */
 public Persona[] getMiembros() {
 return miembros;
 }

 /**
 * @param miembros
 * the miembros to set
 */
 public void setMiembros(Persona[] miembros) {
 this.miembros = miembros;
 }

 public String toString() {
 String res = "";

 if (jefe != null) {
 res += "Jefe: "+jefe.toString()+ "\n";
 }
 if (coordinador != null) {
 res += "Coordinador:"+ coordinador.toString()+ "\n";
 }
 for (Persona m : miembros) {
 if (m != null) {
 res += m.toString()+ " | ";
 }
 }
 return res;

 }
}

Clase Television v 4.0

public class Television {
 private String marca = "";
 private int volumen = 6;
 private String proveedor;
 private String modelo;
 /**
 * @return the modelo
 */
 public String getModelo() {
 return modelo;
 }

 /**
 * @param modelo the modelo to set
 */
 public void setModelo(String modelo) {
 if (modelo.startsWith("TV") || modelo.indexOf("LG")>=0 || modelo.matches("[A-Za-z]{4}[0-9]{3}")){
 this.modelo = modelo; 
 }
 
 }

 public static final int MIN_VOLUME = 0;
 public static final int MAX_VOLUME = 100;
 public static final String[] VALID_BRANDS = { "Sony", "Zenith", "Hitachi", "RCA" };

 public Television(String marca, int volumen, String proveedor) {
 setMarca(marca);
 setVolumen(volumen);
 setProveedor(proveedor);
 }

 public Television(String proveedor) {

 this("LG", 6, proveedor);
 }

 /**
 * @return the proveedor
 */
 public String getProveedor() {
 return proveedor;
 }

 /**
 * @param proveedor
 * the proveedor to set
 */
 private void setProveedor(String proveedor) {
 this.proveedor = proveedor;
 }

 /**
 * @return the marca
 */
 public String getMarca() {
 return marca;
 }

 /**
 * @param marca
 * the marca to set
 */
 public void setMarca(String marca) {
 for (String m : VALID_BRANDS) {
 if (marca.equals(m)) {
 this.marca = marca;
 return;
 }
 }

 }

 /**
 * @return the volumen
 */
 public int getVolumen() {
 return volumen;
 }

 /**
 * @param volumen
 * the volumen to set
 */
 public void setVolumen(int volumen) {
 volumen = Math.min(volumen, Television.MAX_VOLUME);
 volumen = Math.max(volumen, Television.MIN_VOLUME);
 this.volumen = volumen;
 }
 public String toString(){
 return marca+"|"+modelo+"|"+volumen+"|"+proveedor;
 }

}