Created
October 20, 2022 07:48
-
-
Save ProfAndreaPollini/ced2be90498b4d8609209360e0576ef1 to your computer and use it in GitHub Desktop.
Esercizio Mensola di libri
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package libreria; | |
| import java.util.Vector; | |
| public class AppLibreria { | |
| public static void main(String[] args) { | |
| // long i=0; | |
| // Vector<Long> vals = new Vector<>(); | |
| // while (true) { | |
| // vals.add(1L); | |
| // | |
| // System.out.println(i); | |
| // i++; | |
| // } | |
| var l = new Libro(); | |
| var mensola = new Mensola(10); | |
| mensola.add(l,2); | |
| System.out.println(mensola); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package libreria; | |
| public class Libro { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package libreria; | |
| import java.util.Vector; | |
| public class Mensola { | |
| private final Vector<Libro> libri; | |
| private final int capacita; | |
| public Mensola(int capacita) { | |
| this.capacita = capacita; | |
| libri = new Vector<>(); | |
| for (int i = 0; i < capacita; i++) { | |
| libri.add(null); | |
| } | |
| System.out.println(libri); | |
| } | |
| public boolean add(Libro l,int pos) { | |
| if (pos >=capacita || pos < 0 || !isEmpty(pos)) return false; | |
| libri.set(pos,l); | |
| return true; | |
| } | |
| public Libro remove(int pos) { | |
| var l = libri.get(pos); | |
| libri.set(pos,null); | |
| return l; | |
| } | |
| private boolean isEmpty(int pos) { | |
| return libri.get(pos) == null; | |
| } | |
| @Override | |
| public String toString() { | |
| return "Mensola{" + | |
| "libri=" + libri + | |
| '}'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment