https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html
import java.util.ArrayList;
import java.util.Iterator;
public class Bolsa<T> implements Iterable<T>{
private ArrayList<T> objetos = new ArrayList<T>();
private int capacidad = 10;
public Bolsa() {
}
public Bolsa(int capacidad) {
this.capacidad=capacidad;
}
/**
* @return the objetos
*/
public ArrayList<T> getObjetos() {
return objetos;
}
/**
* @param objetos
* the objetos to set
*/
public void setObjetos(ArrayList<T> objetos) {
this.objetos = objetos;
}
public boolean meteObjeto(T objeto){
if (objetos.size()<capacidad){
objetos.add(objeto);
return true;
}
return false;
}
public boolean sacaObjeto(){
if (objetos.size()>0){
objetos.remove(objetos.size()-1);
return true;
}
return false;
}
public boolean sacaObjeto(T objeto){
return objetos.remove(objeto);
}
public boolean cambiaObjeto(T objeto1, T objeto2){
if (objetos.remove(objeto1)){
meteObjeto(objeto2);
return true;
}
return false;
}
public boolean cambiaObjetoR(T objeto1, T objeto2){
int pos=objetos.indexOf(objeto1);
if (pos>=0){
objetos.set(pos, objeto2);
return true;
}
return false;
}
/**
* @return the capacidad
*/
public int getCapacidad() {
return capacidad;
}
public String toString(){
String res=capacidad + " | ";
for(T s:objetos){
res+=s+" | ";
}
return res;
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
}
https://docs.oracle.com/javase/tutorial/collections/implementations/set.html
https://docs.oracle.com/javase/tutorial/collections/implementations/map.html
http://beginnersbook.com/2013/12/hashset-class-in-java-with-example/
http://beginnersbook.com/2013/12/hashmap-in-java-with-example/
import java.util.ArrayList;
public class Bolsa {
private ArrayList<String> objetos = new ArrayList<String>();
private int capacidad = 10;
public Bolsa() {
}
public Bolsa(int capacidad) {
this.capacidad=capacidad;
}
/**
* @return the objetos
*/
public ArrayList<String> getObjetos() {
return objetos;
}
/**
* @param objetos
* the objetos to set
*/
public void setObjetos(ArrayList<String> objetos) {
this.objetos = objetos;
}
public boolean meteObjeto(String objeto){
if (objetos.size()<capacidad){
objetos.add(objeto);
return true;
}
return false;
}
public boolean sacaObjeto(){
if (objetos.size()>0){
objetos.remove(objetos.size()-1);
return true;
}
return false;
}
public boolean sacaObjeto(String objeto){
return objetos.remove(objeto);
}
public boolean cambiaObjeto(String objeto1, String objeto2){
if (objetos.remove(objeto1)){
meteObjeto(objeto2);
return true;
}
return false;
}
public boolean cambiaObjetoR(String objeto1, String objeto2){
int pos=objetos.indexOf(objeto1);
if (pos>=0){
objetos.set(pos, objeto2);
return true;
}
return false;
}
/**
* @return the capacidad
*/
public int getCapacidad() {
return capacidad;
}
public String toString(){
String res=capacidad + " | ";
for(String s:objetos){
res+=s+" | ";
}
return res;
}
}
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());
}
http://dis.um.es/~bmoros/Tutorial/parte9/cap9-3.html
http://beginnersbook.com/2013/04/java-throws/
http://www.genbetadev.com/java-j2ee/java-8-metodos-default
http://codecriticon.com/default-static-interfaces-jdk8/
/*
* 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>");
}
}
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;
}
}