├── README.md ├── Week11 ├── Calculator │ ├── Calculator.java │ ├── CalculatorListener.java │ └── GraphicCalculator.java ├── ClickEffect │ ├── clicker.applicationlogic │ │ ├── Calculator.java │ │ └── PersonalCalculator.java │ └── clicker.ui │ │ ├── ClickListener.java │ │ └── UserInterface.java ├── DrawingBoard │ ├── DrawingBoard.java │ ├── Main.java │ └── UserInterface.java ├── FileManager │ └── FileManager.java ├── Greeter │ └── greeter.ui │ │ └── UserInterface.java ├── NoticeBoard │ └── noticeboard │ │ └── NoticeBoard.java ├── Survey │ └── survey │ │ └── UserInterface.java └── TwoDirectionDictionary │ └── dictionary │ └── MindfulDictionary.java ├── Week12 └── EnumAndIterator │ └── src │ └── personnel │ ├── Education.java │ ├── Employees.java │ ├── Main.java │ └── Person.java ├── Week7 ├── Airport │ ├── Flight.java │ ├── FlightDatabase.java │ ├── Main.java │ ├── Plane.java │ └── TextUserInterface.java ├── Calculator │ ├── Calculator.java │ └── Reader.java ├── CharacterStringChanger │ └── ChangeCharacterClass.java ├── Dictionary │ ├── Dictionary.java │ └── TextUserInterface.java ├── Nicknames │ └── Main.java ├── PromissoryNote │ ├── Main.java │ └── PromissoryNote.java ├── Smileys │ └── printWithSmileys.java └── ThingSuitcaseAndContainer │ ├── Container.java │ ├── Main.java │ ├── Suitcase.java │ └── Thing.java ├── Week8 ├── BoxesAndThings │ ├── Book.java │ ├── Box.java │ ├── CD.java │ └── ToBeStored.java ├── CarRegistrationCentre │ ├── Main.java │ ├── RegistrationPlate.java │ └── VehicleRegister.java ├── NationalService │ ├── CivilService.java │ ├── MilitaryService.java │ └── NationalService.java ├── OnlineShop │ ├── Purchase.java │ ├── Shop.java │ ├── ShoppingBasket.java │ └── Storehouse.java ├── RichFirstPoorLast │ └── Person.java ├── SkiJumping │ ├── Jumper.java │ ├── Participants.java │ └── TextUserInterface.java ├── SortedCards │ ├── Card.java │ ├── Hand.java │ └── SortAgainstSuitAndValue.java └── StudentsSortedByName │ └── Student.java ├── Week9 ├── DuplicateRemover │ └── tools │ │ ├── DuplicateRemover.java │ │ └── PersonalDuplicateRemover.java ├── FileAnalysis │ └── file │ │ └── Analysis.java ├── FirstPackages │ ├── DefaultPackage │ │ └── Main.java │ ├── mooc.logic │ │ └── ApplicationLogic.java │ └── mooc.ui │ │ ├── TextUserInterface.java │ │ └── UserInterface.java ├── MethodArgumentValidation │ └── Validation │ │ ├── Calculator.java │ │ └── Person.java ├── Moving │ ├── Moving │ │ └── Main.java │ ├── moving.domain │ │ ├── Box.java │ │ ├── Item.java │ │ └── Thing.java │ └── moving.logic │ │ └── Packer.java ├── MultipleEntryDictionary │ ├── MultipleEntryDictionary.java │ └── PersonalMultipleEntryDictionary.java ├── PhoneSearch │ ├── Contacts.java │ ├── Person.java │ └── TextUserInterface.java ├── Printer │ └── Printer.java ├── SensorAndTemperatureMeasurement │ └── application │ │ ├── AverageSensor.java │ │ ├── ConstantSensor.java │ │ ├── Sensor.java │ │ └── Thermometer.java └── WordInspection │ └── wordinspection │ └── WordInspection.java └── week10 ├── Container └── containers │ ├── Container.java │ ├── ContainerHistory.java │ ├── ProductContainer.java │ └── ProductContainerRecorder.java ├── DifferentBoxes └── boxes │ ├── BlackHoleBox.java │ ├── Box.java │ ├── MaxWeightBox.java │ ├── OneThingBox.java │ └── Thing.java ├── Dungeon └── dungeon │ ├── Character.java │ ├── Characters.java │ ├── Dungeon.java │ ├── GameGrid.java │ ├── Main.java │ ├── Player.java │ └── Vampire.java ├── FarmSimulator └── farmsimulator │ ├── Alive.java │ ├── BulkTank.java │ ├── Cow.java │ ├── CowHouse.java │ ├── Farm.java │ ├── Milkable.java │ └── MilkingRobot.java ├── Groups ├── Movable.java ├── Organism.java └── group.java ├── PersonAndTheirHeirs └── people │ ├── Person.java │ ├── Student.java │ └── Teacher.java └── TheFinnishRingingCentre ├── Bird.java └── RiningCentre.java /README.md: -------------------------------------------------------------------------------- 1 | oo-programming-java-part-II-Helsinki 2 | ==================================== 3 | 4 | Solutions to the exercises from the second part of the MOOC organized by the University of Helsinki 5 | http://mooc.fi/courses/2013/programming-part-2/ 6 | -------------------------------------------------------------------------------- /Week11/Calculator/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | public class Calculator { 7 | private int value; 8 | 9 | public Calculator(){ 10 | this.value = 0; 11 | } 12 | 13 | public int getValue(){ 14 | return this.value; 15 | } 16 | 17 | public void pressPlus(int n){ 18 | this.value += n; 19 | } 20 | 21 | public void pressMinus(int n){ 22 | this.value -= n; 23 | } 24 | 25 | public void pressZed(){ 26 | this.value = 0; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Week11/Calculator/CalculatorListener.java: -------------------------------------------------------------------------------- 1 | import java.awt.event.ActionEvent; 2 | import java.awt.event.ActionListener; 3 | import javax.swing.JButton; 4 | import javax.swing.JTextField; 5 | 6 | /** 7 | * 8 | * @author giuseppedesantis 9 | */ 10 | public class CalculatorListener implements ActionListener{ 11 | private Calculator calculator; 12 | private JTextField input; 13 | private JTextField output; 14 | private JButton plus; 15 | private JButton minus; 16 | private JButton zed; 17 | 18 | public CalculatorListener(Calculator calculator, JTextField input, JTextField output, 19 | JButton plus, JButton minus, JButton zed){ 20 | this.calculator = calculator; 21 | this.input = input; 22 | this.output = output; 23 | this.plus = plus; 24 | this.minus = minus; 25 | this.zed = zed; 26 | } 27 | 28 | @Override 29 | public void actionPerformed(ActionEvent e){ 30 | if(e.getSource() == plus){ 31 | pressPlus(); 32 | }else if(e.getSource() == minus){ 33 | pressMinus(); 34 | }else{ 35 | pressZed(); 36 | } 37 | } 38 | 39 | private void setOutput(){ 40 | output.setText("" + calculator.getValue()); 41 | zed.setEnabled(true); 42 | } 43 | 44 | private void pressPlus(){ 45 | try { 46 | calculator.pressPlus(Integer.parseInt(input.getText())); 47 | setOutput(); 48 | }catch (NumberFormatException exception){ 49 | } 50 | input.setText(""); 51 | } 52 | 53 | private void pressMinus(){ 54 | try { 55 | calculator.pressMinus(Integer.parseInt(input.getText())); 56 | setOutput(); 57 | }catch (NumberFormatException exception){ 58 | } 59 | input.setText(""); 60 | } 61 | 62 | private void pressZed(){ 63 | calculator.pressZed(); 64 | output.setText("0"); 65 | input.setText(""); 66 | zed.setEnabled(false); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Week11/Calculator/GraphicCalculator.java: -------------------------------------------------------------------------------- 1 | 2 | import java.awt.Container; 3 | import java.awt.Dimension; 4 | import java.awt.GridLayout; 5 | import javax.swing.JFrame; 6 | import javax.swing.JPanel; 7 | import javax.swing.*; 8 | import javax.swing.WindowConstants; 9 | 10 | public class GraphicCalculator implements Runnable { 11 | private JFrame frame; 12 | private Calculator calculator; 13 | 14 | @Override 15 | public void run() { 16 | this.calculator = new Calculator(); 17 | frame = new JFrame("Calculator"); 18 | frame.setPreferredSize(new Dimension(500, 200)); 19 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 20 | 21 | createComponents(frame.getContentPane()); 22 | 23 | frame.pack(); 24 | frame.setVisible(true); 25 | } 26 | 27 | private void createComponents(Container container) { 28 | GridLayout layout = new GridLayout(3,1); 29 | container.setLayout(layout); 30 | 31 | JTextField outputField = new JTextField("0"); 32 | outputField.setEnabled(false); 33 | 34 | 35 | JTextField inputField = new JTextField(""); 36 | 37 | container.add(outputField); 38 | container.add(inputField); 39 | container.add(createPanel(inputField, outputField)); 40 | 41 | } 42 | 43 | private JPanel createPanel(JTextField input, JTextField output){ 44 | JPanel panel = new JPanel(new GridLayout(1,3)); 45 | JButton plus = new JButton("+"); 46 | panel.add(plus); 47 | JButton minus = new JButton("-"); 48 | panel.add(minus); 49 | JButton zed = new JButton("Z"); 50 | panel.add(zed); 51 | 52 | CalculatorListener calculatorListener = new CalculatorListener(this.calculator, input, 53 | output, plus, minus, zed); 54 | 55 | plus.addActionListener(calculatorListener); 56 | minus.addActionListener(calculatorListener); 57 | zed.addActionListener(calculatorListener); 58 | 59 | zed.setEnabled(false); 60 | 61 | return panel; 62 | } 63 | 64 | public JFrame getFrame() { 65 | return frame; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Week11/ClickEffect/clicker.applicationlogic/Calculator.java: -------------------------------------------------------------------------------- 1 | package clicker.applicationlogic; 2 | 3 | public interface Calculator { 4 | int giveValue(); 5 | void increase(); 6 | } 7 | -------------------------------------------------------------------------------- /Week11/ClickEffect/clicker.applicationlogic/PersonalCalculator.java: -------------------------------------------------------------------------------- 1 | 2 | package clicker.applicationlogic; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class PersonalCalculator implements Calculator{ 9 | private int value; 10 | 11 | public PersonalCalculator(){ 12 | this.value = 0; 13 | } 14 | 15 | @Override 16 | public int giveValue(){ 17 | return this.value; 18 | } 19 | 20 | @Override 21 | public void increase(){ 22 | this.value++; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week11/ClickEffect/clicker.ui/ClickListener.java: -------------------------------------------------------------------------------- 1 | 2 | package clicker.ui; 3 | 4 | import clicker.applicationlogic.Calculator; 5 | import clicker.applicationlogic.PersonalCalculator; 6 | 7 | import java.awt.event.ActionListener; 8 | import java.awt.event.ActionEvent; 9 | import javax.swing.*; 10 | 11 | /** 12 | * 13 | * @author giuseppedesantis 14 | */ 15 | 16 | 17 | public class ClickListener implements ActionListener{ 18 | private Calculator calculator; 19 | private JLabel label; 20 | 21 | public ClickListener(Calculator calculator, JLabel label){ 22 | this.calculator = calculator; 23 | this.label = label; 24 | } 25 | 26 | @Override 27 | public void actionPerformed(ActionEvent ae){ 28 | this.calculator.increase(); 29 | this.label.setText(Integer.toString(this.calculator.giveValue())); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Week11/ClickEffect/clicker.ui/UserInterface.java: -------------------------------------------------------------------------------- 1 | 2 | import java.awt.Container; 3 | import java.awt.Dimension; 4 | import java.awt.GridLayout; 5 | import javax.swing.JFrame; 6 | import javax.swing.JPanel; 7 | import javax.swing.*; 8 | import javax.swing.WindowConstants; 9 | 10 | public class GraphicCalculator implements Runnable { 11 | private JFrame frame; 12 | 13 | @Override 14 | public void run() { 15 | frame = new JFrame("Calculator"); 16 | frame.setPreferredSize(new Dimension(500, 200)); 17 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 18 | 19 | createComponents(frame.getContentPane()); 20 | 21 | frame.pack(); 22 | frame.setVisible(true); 23 | } 24 | 25 | private void createComponents(Container container) { 26 | GridLayout layout = new GridLayout(3,1); 27 | container.setLayout(layout); 28 | 29 | JTextField outputField = new JTextField(); 30 | outputField.setEnabled(false); 31 | 32 | JTextField inputField = new JTextField("0"); 33 | 34 | container.add(outputField); 35 | container.add(inputField); 36 | container.add(createPanel()); 37 | } 38 | 39 | private JPanel createPanel(){ 40 | JPanel panel = new JPanel(new GridLayout(1,3)); 41 | panel.add(new JButton("+")); 42 | panel.add(new JButton("-")); 43 | panel.add(new JButton("Z")); 44 | return panel; 45 | } 46 | 47 | public JFrame getFrame() { 48 | return frame; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Week11/DrawingBoard/DrawingBoard.java: -------------------------------------------------------------------------------- 1 | package drawing.ui; 2 | 3 | import java.awt.Color; 4 | import java.awt.Graphics; 5 | import javax.swing.JPanel; 6 | 7 | public class DrawingBoard extends JPanel { 8 | 9 | public DrawingBoard() { 10 | super.setBackground(Color.WHITE); 11 | } 12 | 13 | @Override 14 | protected void paintComponent(Graphics graphics) { 15 | super.paintComponent(graphics); 16 | 17 | graphics.fillRect(100, 50, 50, 50); 18 | graphics.fillRect(250, 50, 50, 50); 19 | graphics.fillRect(50, 200, 50, 50); 20 | graphics.fillRect(300, 200, 50, 50); 21 | graphics.fillRect(100, 250, 200, 50); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week11/DrawingBoard/Main.java: -------------------------------------------------------------------------------- 1 | package drawing.ui; 2 | 3 | import javax.swing.SwingUtilities; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | UserInterface ui = new UserInterface(); 9 | SwingUtilities.invokeLater(ui); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Week11/DrawingBoard/UserInterface.java: -------------------------------------------------------------------------------- 1 | package drawing.ui; 2 | 3 | import java.awt.Container; 4 | import java.awt.Dimension; 5 | import javax.swing.JFrame; 6 | import javax.swing.WindowConstants; 7 | 8 | public class UserInterface implements Runnable { 9 | 10 | private JFrame frame; 11 | private DrawingBoard board; 12 | 13 | @Override 14 | public void run() { 15 | frame = new JFrame("Drawing Board"); 16 | frame.setPreferredSize(new Dimension(400, 400)); 17 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 18 | 19 | createComponents(frame.getContentPane()); 20 | 21 | frame.pack(); 22 | frame.setVisible(true); 23 | } 24 | 25 | private void createComponents(Container container) { 26 | board = new DrawingBoard(); 27 | container.add(board); 28 | } 29 | 30 | public JFrame getFrame() { 31 | return frame; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Week11/FileManager/FileManager.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.File; 3 | import java.io.FileNotFoundException; 4 | import java.io.FileWriter; 5 | import java.io.IOException; 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | import java.util.Scanner; 9 | 10 | public class FileManager { 11 | 12 | public List read(String file) throws FileNotFoundException { 13 | ArrayList readFile = new ArrayList(); 14 | Scanner reader = new Scanner(new File(file)); 15 | 16 | while(reader.hasNextLine()){ 17 | readFile.add(reader.nextLine()); 18 | } 19 | return readFile; 20 | } 21 | 22 | public void save(String file, String text) throws IOException { 23 | FileWriter writer = new FileWriter(file); 24 | writer.write(text); 25 | writer.close(); 26 | } 27 | 28 | public void save(String file, List texts) throws IOException { 29 | FileWriter writer = new FileWriter(file); 30 | for(String line : texts){ 31 | writer.write(line); 32 | writer.write("\n"); 33 | } 34 | writer.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Week11/Greeter/greeter.ui/UserInterface.java: -------------------------------------------------------------------------------- 1 | package greeter.ui; 2 | 3 | import java.awt.Container; 4 | import java.awt.Dimension; 5 | import javax.swing.JFrame; 6 | import javax.swing.JLabel; 7 | import javax.swing.WindowConstants; 8 | 9 | public class UserInterface implements Runnable { 10 | 11 | private JFrame frame; 12 | 13 | @Override 14 | public void run() { 15 | frame = new JFrame("Swing on"); 16 | frame.setPreferredSize(new Dimension(400, 200)); 17 | 18 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 19 | 20 | createComponents(frame.getContentPane()); 21 | 22 | frame.pack(); 23 | frame.setVisible(true); 24 | 25 | } 26 | 27 | private void createComponents(Container container) { 28 | JLabel text = new JLabel("Hi!"); 29 | container.add(text); 30 | } 31 | 32 | public JFrame getFrame() { 33 | return frame; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week11/NoticeBoard/noticeboard/NoticeBoard.java: -------------------------------------------------------------------------------- 1 | package noticeboard; 2 | 3 | import java.awt.Container; 4 | import java.awt.Dimension; 5 | import java.awt.GridLayout; 6 | import javax.swing.*; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | 10 | public class NoticeBoard implements Runnable { 11 | 12 | private JFrame frame; 13 | 14 | @Override 15 | public void run() { 16 | frame = new JFrame(); 17 | 18 | frame.setPreferredSize(new Dimension(500, 500)); 19 | 20 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 21 | 22 | createComponents(frame.getContentPane()); 23 | 24 | frame.pack(); 25 | frame.setVisible(true); 26 | } 27 | 28 | private void createComponents(Container container) { 29 | GridLayout layout = new GridLayout(3, 1); 30 | container.setLayout(layout); 31 | 32 | JTextField textFieldTop = new JTextField(); 33 | JButton copy = new JButton("Copy!"); 34 | JLabel jLabelBottom = new JLabel(); 35 | 36 | fieldCopier copier = new fieldCopier(textFieldTop, jLabelBottom); 37 | copy.addActionListener(copier); 38 | 39 | container.add(textFieldTop); 40 | container.add(copy); 41 | container.add(jLabelBottom); 42 | } 43 | 44 | public class fieldCopier implements ActionListener { 45 | private JTextField origin; 46 | private JLabel destination; 47 | 48 | public fieldCopier(JTextField origin, JLabel destination){ 49 | this.origin = origin; 50 | this.destination = destination; 51 | } 52 | 53 | @Override 54 | public void actionPerformed(ActionEvent ae){ 55 | this.destination.setText(this.origin.getText()); 56 | this.origin.setText(""); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Week11/Survey/survey/UserInterface.java: -------------------------------------------------------------------------------- 1 | package survey; 2 | 3 | import java.awt.Container; 4 | import java.awt.Dimension; 5 | import javax.swing.*; 6 | 7 | public class UserInterface implements Runnable { 8 | 9 | private JFrame frame; 10 | 11 | @Override 12 | public void run() { 13 | // Create your app here 14 | frame = new JFrame("Survey"); 15 | 16 | frame.setPreferredSize(new Dimension(200, 300)); 17 | 18 | frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 19 | 20 | createComponents(frame.getContentPane()); 21 | 22 | frame.pack(); 23 | frame.setVisible(true); 24 | } 25 | 26 | private void createComponents(Container container){ 27 | BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS); 28 | container.setLayout(layout); 29 | 30 | container.add(new JLabel("Are you?")); 31 | JCheckBox yes = new JCheckBox("Yes!"); 32 | JCheckBox no = new JCheckBox("No!"); 33 | container.add(yes); 34 | container.add(no); 35 | 36 | container.add(new JLabel("Why?")); 37 | JRadioButton nr = new JRadioButton("No reason."); 38 | JRadioButton fun = new JRadioButton("Because it is fun!"); 39 | ButtonGroup multipleChoice = new ButtonGroup(); 40 | multipleChoice.add(nr); 41 | multipleChoice.add(fun); 42 | container.add(nr); 43 | container.add(fun); 44 | JButton done = new JButton("Done!"); 45 | container.add(done); 46 | } 47 | 48 | 49 | public JFrame getFrame() { 50 | return frame; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Week11/TwoDirectionDictionary/dictionary/MindfulDictionary.java: -------------------------------------------------------------------------------- 1 | 2 | package dictionary; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | import java.util.Scanner; 12 | import java.io.File; 13 | import java.io.FileNotFoundException; 14 | import java.io.FileWriter; 15 | import java.io.IOException; 16 | 17 | public class MindfulDictionary { 18 | private HashMap dictionaryFromLangA; 19 | private HashMap dictionaryFromLangB; 20 | private Scanner reader; 21 | private FileWriter writer; 22 | private File file; 23 | 24 | public MindfulDictionary(){ 25 | this.dictionaryFromLangA = new HashMap(); 26 | this.dictionaryFromLangB = new HashMap(); 27 | } 28 | 29 | public MindfulDictionary(String file) throws FileNotFoundException, IOException{ 30 | this.dictionaryFromLangA = new HashMap(); 31 | this.dictionaryFromLangB = new HashMap(); 32 | this.file = new File(file); 33 | try{ 34 | this.reader = new Scanner(this.file); 35 | } 36 | catch (FileNotFoundException e){ 37 | System.out.println("file not found"); 38 | } 39 | } 40 | 41 | public boolean load(){ 42 | if(!this.reader.hasNextLine()){ 43 | return false; 44 | }else{ 45 | while(this.reader.hasNextLine()){ 46 | String line = reader.nextLine(); 47 | String [] parts = line.split(":"); 48 | this.dictionaryFromLangA.put(parts[0], parts[1]); 49 | this.dictionaryFromLangB.put(parts[1], parts[0]); 50 | } 51 | return true; 52 | } 53 | } 54 | 55 | public boolean save(){ 56 | try{ 57 | this.writer = new FileWriter(this.file); 58 | for(String a : this.dictionaryFromLangA.keySet()){ 59 | String line = a + ":" + this.dictionaryFromLangA.get(a) + "\n"; 60 | this.writer.write(line); 61 | } 62 | this.writer.close(); 63 | }catch (IOException e){ 64 | return false; 65 | } 66 | return true; 67 | } 68 | 69 | public void add(String word, String translation){ 70 | if(!this.dictionaryFromLangA.containsKey(word) && !this.dictionaryFromLangB.containsKey(translation)){ 71 | this.dictionaryFromLangA.put(word, translation); 72 | this.dictionaryFromLangB.put(translation, word); 73 | } 74 | } 75 | 76 | public String translate(String word){ 77 | if(this.dictionaryFromLangA.containsKey(word)){ 78 | return this.dictionaryFromLangA.get(word); 79 | }else if(this.dictionaryFromLangB.containsKey(word)){ 80 | return this.dictionaryFromLangB.get(word); 81 | }else{ 82 | return null; 83 | } 84 | } 85 | 86 | public void remove(String word){ 87 | if(this.dictionaryFromLangA.containsKey(word)){ 88 | this.dictionaryFromLangA.remove(word); 89 | String key = getKeyByValue(this.dictionaryFromLangB, word); 90 | this.dictionaryFromLangB.remove(key); 91 | }else if(this.dictionaryFromLangB.containsKey(word)){ 92 | this.dictionaryFromLangB.remove(word); 93 | String key = getKeyByValue(this.dictionaryFromLangA, word); 94 | this.dictionaryFromLangA.remove(key); 95 | } 96 | } 97 | 98 | public T getKeyByValue(Map map, E value) { 99 | for (Map.Entry entry : map.entrySet()) { 100 | if (value.equals(entry.getValue())) { 101 | return entry.getKey(); 102 | } 103 | } 104 | return null; 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /Week12/EnumAndIterator/src/personnel/Education.java: -------------------------------------------------------------------------------- 1 | package personnel; 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public enum Education { 8 | D, 9 | M, 10 | B, 11 | GRAD 12 | } 13 | -------------------------------------------------------------------------------- /Week12/EnumAndIterator/src/personnel/Employees.java: -------------------------------------------------------------------------------- 1 | package personnel; 2 | 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | import java.util.Iterator; 6 | 7 | /** 8 | * 9 | * @author giuseppedesantis 10 | */ 11 | public class Employees { 12 | private ArrayList persons; 13 | 14 | public Employees(){ 15 | persons = new ArrayList (); 16 | } 17 | 18 | public void add(Person person){ 19 | persons.add(person); 20 | } 21 | 22 | public void add(List persons){ 23 | for (Person p : persons){ 24 | this.persons.add(p); 25 | } 26 | } 27 | 28 | public void print(){ 29 | Iterator iterator = persons.iterator(); 30 | 31 | while(iterator.hasNext()){ 32 | Person nextPerson = iterator.next(); 33 | System.out.println(nextPerson); 34 | } 35 | } 36 | 37 | public void print(Education education){ 38 | Iterator iterator = persons.iterator(); 39 | 40 | while(iterator.hasNext()){ 41 | Person nextPerson = iterator.next(); 42 | if(nextPerson.getEducation() == education){ 43 | System.out.println(nextPerson); 44 | } 45 | } 46 | } 47 | 48 | public void fire(Education education){ 49 | Iterator iterator = persons.iterator(); 50 | 51 | while(iterator.hasNext()){ 52 | if(iterator.next().getEducation() == education){ 53 | iterator.remove(); 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Week12/EnumAndIterator/src/personnel/Main.java: -------------------------------------------------------------------------------- 1 | package personnel; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | // write test code here 9 | Employees university = new Employees(); 10 | university.add(new Person("Matti", Education.D)); 11 | university.add(new Person("Pekka", Education.GRAD)); 12 | university.add(new Person("Arto", Education.D)); 13 | 14 | university.print(); 15 | 16 | university.fire(Education.GRAD); 17 | 18 | System.out.println("=="); 19 | 20 | university.print(); 21 | 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week12/EnumAndIterator/src/personnel/Person.java: -------------------------------------------------------------------------------- 1 | package personnel; 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class Person { 8 | private String name; 9 | private Education education; 10 | 11 | public Person (String name, Education education){ 12 | this.name = name; 13 | this.education = education; 14 | } 15 | 16 | @Override 17 | public String toString() { 18 | return name + ", " + education; 19 | } 20 | 21 | public Education getEducation(){ 22 | return education; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Week7/Airport/Flight.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | 8 | public class Flight { 9 | private String depart; 10 | private String arriv; 11 | 12 | public Flight(String depart, String arriv){ 13 | this.depart = depart; 14 | this.arriv = arriv; 15 | } 16 | 17 | public String toString(){ 18 | return "(" + this.depart + "-" + this.arriv + ")"; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Week7/Airport/FlightDatabase.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.HashMap; 3 | import java.util.Map; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * 9 | * @author giuseppedesantis 10 | */ 11 | public class FlightDatabase { 12 | private ArrayList planes; 13 | private ArrayList flights; 14 | private Map> database; 15 | 16 | public FlightDatabase(){ 17 | this.planes = new ArrayList(); 18 | this.flights = new ArrayList(); 19 | this.database = new HashMap>(); 20 | } 21 | 22 | public void addPlane(String id, int capacity){ 23 | ArrayList init = new ArrayList(); 24 | Plane plane = new Plane(id, capacity); 25 | this.planes.add(plane); 26 | this.database.put(plane, init); 27 | } 28 | 29 | public void addFlight(String id, String depart, String arriv){ 30 | Plane plane = new Plane("",0); 31 | for(Plane p : this.planes){ 32 | if(p.getPlane().equals(id)){ 33 | plane = p; 34 | } 35 | } 36 | Flight flight = new Flight(depart, arriv); 37 | this.database.get(plane).add(flight); 38 | } 39 | 40 | public void printPlanes(){ 41 | for(Plane p : this.planes){ 42 | System.out.println(p); 43 | } 44 | } 45 | 46 | public void printFlights(){ 47 | for(Plane p : this.database.keySet()){ 48 | ArrayList flights = this.database.get(p); 49 | for(Flight f : flights){ 50 | System.out.println(p + " " + f); 51 | } 52 | } 53 | } 54 | 55 | public void printPlaneInfo(String id){ 56 | for(Plane p : this.planes){ 57 | if(p.getPlane().equals(id)){ 58 | System.out.println(p); 59 | } 60 | } 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /Week7/Airport/Main.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | import java.util.Scanner; 5 | import java.util.ArrayList; 6 | 7 | public class Main { 8 | 9 | public static void main(String[] args) { 10 | String input = "1\n" + "HA-LOL\n" + "42\n" + 11 | "1\n" + "G-OWAC\n" + "101\n" + 12 | "2\n" + "HA-LOL\n" + "HEL\n" + 13 | "BAL\n" + "2\n" + "G-OWAC\n" + 14 | "JFK\n" + "BAL\n" + "2\n" + 15 | "HA-LOL\n" + "BAL\n" + "HEL\n" + 16 | "x\n" + "1\n" + "2\n" + "3\n" + 17 | "G-OWAC\n" + "x"; 18 | 19 | Scanner reader = new Scanner(System.in); 20 | FlightDatabase flightDatabase = new FlightDatabase(); 21 | 22 | TextUserInterface ui = new TextUserInterface(reader, flightDatabase); 23 | ui.start(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Week7/Airport/Plane.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * @author giuseppedesantis 4 | */ 5 | 6 | 7 | public class Plane { 8 | private String id; 9 | private int capacity; 10 | 11 | public Plane(String id, int capacity){ 12 | this.id = id; 13 | this.capacity = capacity; 14 | } 15 | 16 | public String toString(){ 17 | return this.id + " (" + this.capacity + " ppl)"; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Week7/Airport/TextUserInterface.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class TextUserInterface { 8 | private Scanner reader; 9 | private FlightDatabase flightDatabase; 10 | 11 | public TextUserInterface(Scanner reader, FlightDatabase flightDatabase){ 12 | this.reader = reader; 13 | this.flightDatabase = flightDatabase; 14 | } 15 | 16 | public void start(){ 17 | System.out.println("Airport panel"); 18 | System.out.println("--------------------"); 19 | System.out.println(); 20 | while(true){ 21 | System.out.println("Choose operation:"); 22 | System.out.println("[1] Add airplane"); 23 | System.out.println("[2] Add flight"); 24 | System.out.println("[x] Exit"); 25 | System.out.println(">"); 26 | String input = reader.nextLine(); 27 | if(input.equals("x")){ 28 | break; 29 | }else if(input.equals("1")){ 30 | System.out.println("Give plane ID:"); 31 | String id = reader.nextLine(); 32 | System.out.println("Give plane capacity:"); 33 | int capacity = Integer.parseInt(reader.nextLine()); 34 | flightDatabase.addPlane(id, capacity); 35 | }else if(input.equals("2")){ 36 | System.out.println("Give plane ID:"); 37 | String id = reader.nextLine(); 38 | System.out.println("Give departure airport code:"); 39 | String depart = reader.nextLine(); 40 | System.out.println("Give destination airport code:"); 41 | String arriv = reader.nextLine(); 42 | this.flightDatabase.addFlight(id, depart, arriv); 43 | } 44 | } 45 | System.out.println(); 46 | System.out.println("Flight service"); 47 | System.out.println("------------"); 48 | System.out.println(); 49 | 50 | while(true){ 51 | System.out.println("Choose operation:"); 52 | System.out.println("[1] Print planes"); 53 | System.out.println("[2] Print flights"); 54 | System.out.println("[3] Print flight info"); 55 | System.out.println("[x] Quit"); 56 | System.out.println(">"); 57 | String input = reader.nextLine(); 58 | if(input.equals("x")){ 59 | break; 60 | }else if(input.equals("1")){ 61 | flightDatabase.printPlanes(); 62 | }else if(input.equals("2")){ 63 | flightDatabase.printFlights(); 64 | }else if(input.equals("3")){ 65 | System.out.println("Give plane ID:"); 66 | String id = reader.nextLine(); 67 | flightDatabase.printPlaneInfo(id); 68 | } 69 | } 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Week7/Calculator/Calculator.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class Calculator { 8 | private Reader reader; 9 | private int counter; 10 | 11 | public Calculator(){ 12 | this.reader = new Reader(); 13 | this.counter = 0; 14 | } 15 | 16 | public void start() { 17 | while (true) { 18 | System.out.print("command: "); 19 | String command = reader.readString(); 20 | if (command.equals("end")) { 21 | break; 22 | } 23 | 24 | if (command.equals("sum")) { 25 | sum(); 26 | } else if (command.equals("difference")) { 27 | difference(); 28 | } else if (command.equals("product")) { 29 | product(); 30 | } 31 | } 32 | 33 | statistics(); 34 | } 35 | 36 | private void sum(){ 37 | int values [] = this.readTwoValues(); 38 | int sum = values[0] + values[1]; 39 | System.out.println("sum of the values " + sum); 40 | } 41 | 42 | private void product(){ 43 | int values [] = this.readTwoValues(); 44 | int product = values[0] * values[1]; 45 | System.out.println("product of the values " + product); 46 | } 47 | 48 | private void difference(){ 49 | int values [] = this.readTwoValues(); 50 | int difference = values[0] - values[1]; 51 | System.out.println("difference of the values " + difference); 52 | } 53 | 54 | private int[] readTwoValues() { 55 | this.counter++; 56 | 57 | int[] values = new int[2]; 58 | System.out.print("value1: "); 59 | values[0] = reader.readInteger(); 60 | System.out.print("value2: "); 61 | values[1] = reader.readInteger(); 62 | 63 | return values; 64 | } 65 | 66 | private void statistics(){ 67 | System.out.println("Calculations done " + this.counter); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Week7/Calculator/Reader.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class Reader { 8 | private Scanner reader; 9 | 10 | public Reader(){ 11 | this.reader = new Scanner(System.in); 12 | } 13 | 14 | public String readString(){ 15 | String string = this.reader.nextLine(); 16 | return string; 17 | } 18 | 19 | public int readInteger(){ 20 | int input = Integer.parseInt(this.readString()); 21 | return input; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week7/CharacterStringChanger/ChangeCharacterClass.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | public class Change { 7 | private char fromCharacter; 8 | private char toCharacter; 9 | 10 | public Change(char fromCharacter, char toCharacter){ 11 | this.fromCharacter = fromCharacter; 12 | this.toCharacter = toCharacter; 13 | } 14 | 15 | public String change(String characterString){ 16 | String result = ""; 17 | for (int i = 0; i < characterString.length(); i++){ 18 | if(characterString.charAt(i) == this.fromCharacter){ 19 | result += this.toCharacter; 20 | }else{ 21 | result += characterString.charAt(i); 22 | } 23 | } 24 | return result; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Week7/Dictionary/Dictionary.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | /** 6 | * 7 | * @author giuseppedesantis 8 | */ 9 | public class Dictionary { 10 | private HashMap dictionary; 11 | 12 | public Dictionary(){ 13 | this.dictionary = new HashMap(); 14 | } 15 | 16 | public String translate(String word){ 17 | if(this.dictionary.containsKey(word)){ 18 | return this.dictionary.get(word); 19 | } 20 | return null; 21 | } 22 | 23 | public void add(String word, String translation){ 24 | this.dictionary.put(word, translation); 25 | } 26 | 27 | public int amountOfWords(){ 28 | return this.dictionary.size(); 29 | } 30 | 31 | public List translationList(){ 32 | List translationList = new ArrayList(); 33 | for(String key : this.dictionary.keySet()){ 34 | String translation = key + " = " + this.translate(key); 35 | translationList.add(translation); 36 | } 37 | return translationList; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Week7/Dictionary/TextUserInterface.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class TextUserInterface { 8 | private Scanner reader; 9 | private Dictionary dictionary; 10 | 11 | public TextUserInterface(Scanner reader, Dictionary dictionary){ 12 | this.reader = reader; 13 | this.dictionary = dictionary; 14 | } 15 | 16 | public void start(){ 17 | System.out.println("Statements:"); 18 | System.out.println(" add - adds a word pair to the dictionary"); 19 | System.out.println(" translate - asks a word and prints its translation"); 20 | System.out.println(" quit - quits the text user interface"); 21 | System.out.println(); 22 | while(true){ 23 | System.out.println("Statament:"); 24 | String command = this.reader.nextLine(); 25 | if(command.equals("quit")){ 26 | System.out.println("Cheers"); 27 | break; 28 | } 29 | else if(command.endsWith("add")){ 30 | System.out.println("In Finnish: "); 31 | String word = this.reader.nextLine(); 32 | System.out.println("Translation: "); 33 | String translation = this.reader.nextLine(); 34 | this.dictionary.add(word, translation); 35 | System.out.println(); 36 | }else if(command.equals("translate")){ 37 | System.out.println("Give a word: "); 38 | String word = this.reader.nextLine(); 39 | System.out.println("Translation: " + this.dictionary.translate(word)); 40 | System.out.println(); 41 | }else{ 42 | System.out.println("Unknown statement"); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Week7/Nicknames/Main.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.HashMap; 3 | 4 | public class Nicknames { 5 | 6 | public static void main(String[] args) { 7 | HashMap nicknames = new HashMap(); 8 | nicknames.put("matti", "mage"); 9 | nicknames.put("mikael", "mixu"); 10 | nicknames.put("arto", "arppa"); 11 | 12 | String nick = nicknames.get("mikael"); 13 | System.out.println(nick); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Week7/PromissoryNote/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | // Test your program here 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /Week7/PromissoryNote/PromissoryNote.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.HashMap; 3 | import java.util.Map; 4 | 5 | /** 6 | * 7 | * @author giuseppedesantis 8 | */ 9 | public class PromissoryNote { 10 | 11 | private HashMap loans; 12 | 13 | 14 | public PromissoryNote(){ 15 | this.loans = new HashMap(); 16 | } 17 | 18 | public void setLoan(String toWhom, double value){ 19 | this.loans.put(toWhom, value); 20 | } 21 | 22 | public double howMuchIsTheDebt(String whose){ 23 | if(this.loans.containsKey(whose)){ 24 | return this.loans.get(whose); 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Week7/Smileys/printWithSmileys.java: -------------------------------------------------------------------------------- 1 | 2 | public class Smileys { 3 | 4 | public static void main(String[] args) { 5 | 6 | // Test your method at least with the following 7 | printWithSmileys("Method"); 8 | printWithSmileys("Beerbottle"); 9 | printWithSmileys("Interface"); 10 | } 11 | 12 | private static void printWithSmileys(String characterString){ 13 | int strlen = characterString.length(); 14 | String smiley = ":)"; 15 | if(strlen % 2 == 0){ 16 | int topBottomRow = (strlen + 6)/2; 17 | printTopBottomRow(topBottomRow, smiley); 18 | System.out.println(smiley+" "+characterString+" "+smiley); 19 | printTopBottomRow(topBottomRow, smiley); 20 | }else{ 21 | int topBottomRow = (strlen + 7)/2; 22 | printTopBottomRow(topBottomRow, smiley); 23 | System.out.println(smiley+" "+characterString+" "+smiley); 24 | printTopBottomRow(topBottomRow, smiley); 25 | } 26 | } 27 | 28 | private static void printTopBottomRow(int rowLength, String smiley){ 29 | for(int i = 0; i < rowLength; i++){ 30 | System.out.print(smiley); 31 | } 32 | System.out.println(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Week7/ThingSuitcaseAndContainer/Container.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class Container { 8 | private int maxWeight; 9 | private ArrayList suitcases; 10 | 11 | public Container(int maxWeight){ 12 | this.maxWeight = maxWeight; 13 | this.suitcases = new ArrayList(); 14 | } 15 | 16 | public void addSuitcase(Suitcase suitcase){ 17 | if((suitcase.totalWeight() + this.totalWeight()) <= this.maxWeight){ 18 | this.suitcases.add(suitcase); 19 | } 20 | } 21 | 22 | public int totalWeight(){ 23 | int totalWeight = 0; 24 | for(Suitcase s : this.suitcases){ 25 | totalWeight += s.totalWeight(); 26 | } 27 | return totalWeight; 28 | } 29 | 30 | public String toString(){ 31 | int numberOfSuitcases = 0; 32 | for(Suitcase s : this.suitcases){ 33 | numberOfSuitcases++; 34 | } 35 | return numberOfSuitcases + " suitcases (" + this.totalWeight() + " kg)"; 36 | } 37 | 38 | public void printThings(){ 39 | for(Suitcase s : this.suitcases){ 40 | s.printThings(); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Week7/ThingSuitcaseAndContainer/Main.java: -------------------------------------------------------------------------------- 1 | 2 | public class Main { 3 | 4 | public static void main(String[] Container) { 5 | // use this main class to test your program! 6 | Container container = new Container(1000); 7 | addSuitcasesFullOfBricks(container); 8 | System.out.println(container); 9 | } 10 | 11 | public static void addSuitcasesFullOfBricks(Container container) { 12 | // adding 100 suitcases with one brick in each 13 | for(int i=0; i<100; i++){ 14 | Thing brick = new Thing("brick", i+1); 15 | Suitcase suitcase = new Suitcase(101); 16 | suitcase.addThing(brick); 17 | container.addSuitcase(suitcase); 18 | } 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Week7/ThingSuitcaseAndContainer/Suitcase.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class Suitcase { 8 | private ArrayList things; 9 | private int maxWeight; 10 | 11 | public Suitcase(int maxWeight){ 12 | this.maxWeight = maxWeight; 13 | this.things = new ArrayList(); 14 | } 15 | 16 | public void addThing(Thing thing){ 17 | if((thing.getWeight() + this.totalWeight()) <= this.maxWeight){ 18 | this.things.add(thing); 19 | } 20 | } 21 | 22 | public String toString(){ 23 | int numberOfThings = 0; 24 | int totalWeight = 0; 25 | for(Thing t : this.things){ 26 | numberOfThings++; 27 | totalWeight += t.getWeight(); 28 | } 29 | if(numberOfThings == 0){ 30 | return "empty (0 kg)"; 31 | } 32 | else if(numberOfThings == 1){ 33 | return numberOfThings + " thing (" + totalWeight + " kg)"; 34 | }else{ 35 | return numberOfThings + " things (" + totalWeight + " kg)"; 36 | } 37 | } 38 | 39 | public void printThings(){ 40 | for(Thing t : this.things){ 41 | System.out.println(t); 42 | } 43 | } 44 | 45 | public int totalWeight(){ 46 | int totalWeight = 0; 47 | for(Thing t : this.things){ 48 | totalWeight += t.getWeight(); 49 | } 50 | return totalWeight; 51 | } 52 | 53 | public Thing heaviestThing(){ 54 | int heaviest = 0; 55 | Thing heaviestThing = new Thing("", 0); 56 | if(this.things.isEmpty()){ 57 | return null; 58 | }else{ 59 | for(Thing t : this.things){ 60 | if(t.getWeight() > heaviest){ 61 | heaviest = t.getWeight(); 62 | heaviestThing = t; 63 | } 64 | } 65 | return heaviestThing; 66 | } 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Week7/ThingSuitcaseAndContainer/Thing.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class Thing { 8 | private String name; 9 | private int weight; 10 | 11 | public Thing(String name, int weight){ 12 | this.name = name; 13 | this.weight = weight; 14 | } 15 | 16 | public String getName(){ 17 | return this.name; 18 | } 19 | 20 | public int getWeight(){ 21 | return this.weight; 22 | } 23 | 24 | public String toString(){ 25 | return this.name + " (" + this.weight + " kg)"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Week8/BoxesAndThings/Book.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | public class Book implements ToBeStored{ 8 | private String writer; 9 | private String name; 10 | private double weight; 11 | 12 | public Book(String writer, String name, double weight){ 13 | this.writer = writer; 14 | this.name = name; 15 | this.weight = weight; 16 | } 17 | 18 | public double weight(){ 19 | return this.weight; 20 | } 21 | 22 | @Override 23 | public String toString(){ 24 | return this.writer + ": " + this.name; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Week8/BoxesAndThings/Box.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | 5 | /** 6 | * 7 | * @author giuseppedesantis 8 | */ 9 | 10 | public class Box implements ToBeStored{ 11 | private ArrayList things; 12 | private double maximumWeight; 13 | 14 | public Box(double maximumWeight){ 15 | this.maximumWeight = maximumWeight; 16 | this.things = new ArrayList(); 17 | } 18 | 19 | public void add(ToBeStored t){ 20 | if(this.weight() + t.weight() < maximumWeight){ 21 | this.things.add(t); 22 | } 23 | } 24 | 25 | public double weight(){ 26 | double weight = 0; 27 | for(ToBeStored t : this.things){ 28 | weight += t.weight(); 29 | } 30 | return weight; 31 | } 32 | 33 | @Override 34 | public String toString(){ 35 | return "Box: " + this.things.size() + " things, total weight " + this.weight() + " kg"; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Week8/BoxesAndThings/CD.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | public class CD implements ToBeStored{ 8 | private String artist; 9 | private String title; 10 | private int publishingYear; 11 | private double weight; 12 | 13 | public CD(String artist, String title, int publishingYear){ 14 | this.artist = artist; 15 | this.title = title; 16 | this.publishingYear = publishingYear; 17 | this.weight = 0.1; 18 | } 19 | 20 | public double weight(){ 21 | return this.weight; 22 | } 23 | 24 | @Override 25 | public String toString(){ 26 | return this.artist + ": " + this.title + " (" + this.publishingYear + ")"; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Week8/BoxesAndThings/ToBeStored.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public interface ToBeStored { 8 | double weight(); 9 | } 10 | -------------------------------------------------------------------------------- /Week8/CarRegistrationCentre/Main.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | import java.util.HashMap; 4 | import java.util.List; 5 | import java.util.Map; 6 | 7 | public class Main { 8 | 9 | public static void main(String[] args) { 10 | 11 | RegistrationPlate reg1 = new RegistrationPlate("FI", "ABC-123"); 12 | RegistrationPlate reg2 = new RegistrationPlate("FI", "UXE-465"); 13 | RegistrationPlate reg3 = new RegistrationPlate("D", "B WQ-431"); 14 | 15 | List finnish = new ArrayList(); 16 | finnish.add(reg1); 17 | finnish.add(reg2); 18 | 19 | RegistrationPlate newPlate = new RegistrationPlate("FI", "ABC-123"); 20 | 21 | if (!finnish.contains(newPlate)) { 22 | finnish.add(newPlate); 23 | } 24 | 25 | System.out.println("Finnish: " + finnish); 26 | 27 | Map owners = new HashMap(); 28 | owners.put(reg1, "Arto"); 29 | owners.put(reg3, "Jürgen"); 30 | 31 | System.out.println("owners:"); 32 | System.out.println(owners.get(new RegistrationPlate("FI", "ABC-123"))); 33 | System.out.println(owners.get(new RegistrationPlate("D", "B WQ-431"))); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Week8/CarRegistrationCentre/RegistrationPlate.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | public class RegistrationPlate { 8 | 9 | private final String regCode; 10 | private final String country; 11 | 12 | public RegistrationPlate(String country, String regCode) { 13 | this.regCode = regCode; 14 | this.country = country; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return country + " " + regCode; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object object){ 24 | if(object == null){ 25 | return false; 26 | } 27 | if(this.getClass() != object.getClass()){ 28 | return false; 29 | } 30 | 31 | RegistrationPlate compared = (RegistrationPlate) object; 32 | 33 | if(this.regCode != compared.regCode){ 34 | return false; 35 | } 36 | 37 | if(this.country != compared.country){ 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | public int hashCode() { 44 | if(this.country == null){ 45 | return 7; 46 | } 47 | return this.country.hashCode() + this.regCode.hashCode(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Week8/CarRegistrationCentre/VehicleRegister.java: -------------------------------------------------------------------------------- 1 | import java.util.Map; 2 | import java.util.HashMap; 3 | import java.util.List; 4 | import java.util.ArrayList; 5 | /** 6 | * 7 | * @author giuseppedesantis 8 | */ 9 | public class VehicleRegister { 10 | private Map register = new HashMap(); 11 | 12 | public boolean add(RegistrationPlate plate, String owner){ 13 | if(this.register.get(plate) == null){ 14 | this.register.put(plate, owner); 15 | return true; 16 | }else{ 17 | return false; 18 | } 19 | } 20 | 21 | public String get(RegistrationPlate plate){ 22 | if(this.register.get(plate) == null){ 23 | return null; 24 | }else{ 25 | return this.register.get(plate); 26 | } 27 | } 28 | 29 | public boolean delete(RegistrationPlate plate){ 30 | if(this.register.get(plate) == null){ 31 | return false; 32 | }else{ 33 | this.register.remove(plate); 34 | return true; 35 | } 36 | } 37 | 38 | public void printRegistrationPlates(){ 39 | for(RegistrationPlate plate : this.register.keySet()){ 40 | System.out.println(plate); 41 | } 42 | } 43 | 44 | public void printOwners(){ 45 | ArrayList owners = new ArrayList(); 46 | for(RegistrationPlate plate : this.register.keySet()){ 47 | String owner = this.register.get(plate); 48 | if(owners.contains(owner) == false){ 49 | owners.add(owner); 50 | } 51 | } 52 | for(String owner : owners){ 53 | System.out.println(owner); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Week8/NationalService/CivilService.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | public class CivilService implements NationalService{ 8 | private int daysLeft; 9 | 10 | public CivilService(){ 11 | this.daysLeft = 362; 12 | } 13 | 14 | public int getDaysLeft(){ 15 | return this.daysLeft; 16 | } 17 | 18 | public void work(){ 19 | if(this.daysLeft >= 1){ 20 | this.daysLeft--; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week8/NationalService/MilitaryService.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | public class MilitaryService implements NationalService{ 8 | private int daysLeft; 9 | 10 | public MilitaryService(int daysLeft){ 11 | this.daysLeft = daysLeft; 12 | } 13 | 14 | public int getDaysLeft(){ 15 | return this.daysLeft; 16 | } 17 | 18 | public void work(){ 19 | if(this.daysLeft >= 1){ 20 | this.daysLeft--; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Week8/NationalService/NationalService.java: -------------------------------------------------------------------------------- 1 | public interface NationalService { 2 | int getDaysLeft(); 3 | void work(); 4 | } 5 | -------------------------------------------------------------------------------- /Week8/OnlineShop/Purchase.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | public class Purchase { 7 | private String product; 8 | private int amount; 9 | private int unitPrice; 10 | 11 | public Purchase(String product, int amount, int unitPrice){ 12 | this.product = product; 13 | this.amount = amount; 14 | this.unitPrice = unitPrice; 15 | } 16 | 17 | public int price(){ 18 | return this.amount * this.unitPrice; 19 | } 20 | 21 | public String product(){ 22 | return this.product; 23 | } 24 | 25 | public void increaseAmount(){ 26 | this.amount++; 27 | } 28 | 29 | @Override 30 | public String toString(){ 31 | return this.product + ": " + this.amount; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Week8/OnlineShop/Shop.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Shop { 4 | 5 | private Storehouse store; 6 | private Scanner reader; 7 | 8 | public Shop(Storehouse store, Scanner reader) { 9 | this.store = store; 10 | this.reader = reader; 11 | } 12 | 13 | // the method to deal with a customer in the shop 14 | public void manage(String customer) { 15 | ShoppingBasket basket = new ShoppingBasket(); 16 | System.out.println("Welcome to our shop " + customer); 17 | System.out.println("below is our sale offer:"); 18 | 19 | for (String product : store.products()) { 20 | System.out.println( product ); 21 | } 22 | 23 | while (true) { 24 | System.out.print("what do you want to buy (press enter to pay):"); 25 | String product = reader.nextLine(); 26 | if (product.isEmpty()) { 27 | break; 28 | } 29 | 30 | // here, you write the code to add a product to the shopping basket, if the storehouse is not empty 31 | // and decreases the storehouse stocks 32 | // do not touch the rest of the code! 33 | store.take(product); 34 | int price = store.price(product); 35 | for(String p : store.products()){ 36 | if(p.equals(product)){ 37 | basket.add(product, price); 38 | } 39 | } 40 | 41 | } 42 | 43 | System.out.println("your purchases are:"); 44 | basket.print(); 45 | System.out.println("basket price: " + basket.price()); 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Week8/OnlineShop/ShoppingBasket.java: -------------------------------------------------------------------------------- 1 | import java.util.Map; 2 | import java.util.HashMap; 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class ShoppingBasket { 8 | private Map purchases; 9 | 10 | public ShoppingBasket(){ 11 | this.purchases = new HashMap(); 12 | } 13 | 14 | public void add(String product, int price){ 15 | if(this.purchases.containsKey(product)){ 16 | this.purchases.get(product).increaseAmount(); 17 | }else{ 18 | Purchase newp = new Purchase(product, 1, price); 19 | this.purchases.put(product, newp); 20 | } 21 | } 22 | 23 | public int price(){ 24 | int totalPrice = 0; 25 | for(Purchase p : this.purchases.values()){ 26 | totalPrice += p.price(); 27 | } 28 | return totalPrice; 29 | } 30 | 31 | public void print(){ 32 | for(Purchase p : this.purchases.values()){ 33 | System.out.println(p.toString()); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Week8/OnlineShop/Storehouse.java: -------------------------------------------------------------------------------- 1 | import java.util.Map; 2 | import java.util.HashMap; 3 | import java.util.Set; 4 | import java.util.HashSet; 5 | /** 6 | * 7 | * @author giuseppedesantis 8 | */ 9 | 10 | public class Storehouse { 11 | private Map prices; 12 | private Map stocks; 13 | 14 | public Storehouse(){ 15 | this.prices = new HashMap(); 16 | this.stocks = new HashMap(); 17 | } 18 | 19 | public void addProduct(String product, int price, int stock){ 20 | this.prices.put(product, price); 21 | this.stocks.put(product, stock); 22 | } 23 | 24 | public int price(String product){ 25 | for(String p : this.prices.keySet()){ 26 | if(p.equals(product)){ 27 | return this.prices.get(p); 28 | } 29 | } 30 | return -99; 31 | } 32 | 33 | public int stock(String product){ 34 | for(String p : this.stocks.keySet()){ 35 | if(p.equals(product)){ 36 | return this.stocks.get(p); 37 | } 38 | } 39 | return 0; 40 | } 41 | 42 | public boolean take(String product){ 43 | for(String p : this.stocks.keySet()){ 44 | if(p.equals(product)){ 45 | if(this.stock(p) > 0){ 46 | this.stocks.put(p, this.stock(p) - 1); 47 | return true; 48 | } 49 | } 50 | } 51 | return false; 52 | } 53 | 54 | public Set products(){ 55 | Set products = new HashSet(); 56 | for(String s : this.prices.keySet()){ 57 | products.add(s); 58 | } 59 | return products; 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Week8/RichFirstPoorLast/Person.java: -------------------------------------------------------------------------------- 1 | public class Person implements Comparable{ 2 | 3 | private int salary; 4 | private String name; 5 | 6 | public Person(String name, int salary) { 7 | this.name = name; 8 | this.salary = salary; 9 | } 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | public int getSalary() { 16 | return salary; 17 | } 18 | 19 | 20 | @Override 21 | public String toString() { 22 | return name + " " + salary; 23 | } 24 | 25 | @Override 26 | public int compareTo(Person person){ 27 | return person.salary - this.salary; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Week8/SkiJumping/Jumper.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.ArrayList; 3 | /** 4 | * 5 | * @author giuseppedesantis 6 | */ 7 | public class Jumper implements Comparable{ 8 | private String name; 9 | private int totalPoints; 10 | private List jumps; 11 | 12 | 13 | public Jumper(String name){ 14 | this.name = name; 15 | this.totalPoints = 0; 16 | this.jumps = new ArrayList(); 17 | } 18 | 19 | public void addPoints(int points){ 20 | this.totalPoints += points; 21 | } 22 | 23 | public String getName(){ 24 | return this.name; 25 | } 26 | 27 | public void addJump(int jump){ 28 | this.jumps.add(jump); 29 | } 30 | 31 | @Override 32 | public int compareTo(Jumper jumper){ 33 | return jumper.totalPoints - this.totalPoints; 34 | } 35 | 36 | @Override 37 | public String toString(){ 38 | return this.name + " (" + this.totalPoints + " points)"; 39 | } 40 | 41 | public void printJumpLengths(){ 42 | String formattedString = this.jumps.toString().replace("[", "").replace(",", " m,").replace("]", " m"); 43 | System.out.print(" jump lengths: " + formattedString); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Week8/SkiJumping/Participants.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.ArrayList; 3 | import java.util.Collections; 4 | import java.util.Random; 5 | /** 6 | * 7 | * @author giuseppedesantis 8 | */ 9 | public class Participants { 10 | private List participants; 11 | private Random random = new Random(); 12 | 13 | public Participants(){ 14 | this.participants = new ArrayList(); 15 | } 16 | 17 | public void add(Jumper jumper){ 18 | this.participants.add(jumper); 19 | } 20 | 21 | public void play(){ 22 | for(Jumper j : this.participants){ 23 | int jumpLength = random.nextInt(120 - 60 + 1) + 60; 24 | j.addJump(jumpLength); 25 | List evaluations = new ArrayList(); 26 | for( int i = 0; i < 5; i++){ 27 | int eval = random.nextInt(20 - 10 + 1) + 10; 28 | evaluations.add(eval); 29 | } 30 | System.out.println(" " + j.getName()); 31 | System.out.println(" length: " + jumpLength); 32 | System.out.println(" judge votes: " + evaluations); 33 | Collections.sort(evaluations); 34 | evaluations.remove(0); 35 | evaluations.remove(evaluations.size() - 1); 36 | int sumOfEvaluations = 0; 37 | for(int i : evaluations){ 38 | sumOfEvaluations = sumOfEvaluations + i; 39 | } 40 | int points = jumpLength + sumOfEvaluations; 41 | j.addPoints(points); 42 | } 43 | } 44 | 45 | public void print(){ 46 | for(Jumper j : this.participants){ 47 | System.out.println(j); 48 | } 49 | } 50 | 51 | public void printInOrder(){ 52 | for(int i = 0; i < this.participants.size(); i++){ 53 | int counter = i + 1; 54 | System.out.println(" " + counter + ". " + this.participants.get(i)); 55 | } 56 | } 57 | 58 | public void printResults(){ 59 | System.out.println("Position Name"); 60 | int counter = 1; 61 | for(Jumper j : this.participants){ 62 | System.out.println(counter + " " + j); 63 | j.printJumpLengths(); 64 | counter++; 65 | System.out.println(); 66 | } 67 | } 68 | 69 | public void sort(){ 70 | Collections.sort(this.participants); 71 | } 72 | 73 | public void reverse(){ 74 | Collections.reverse(this.participants); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Week8/SkiJumping/TextUserInterface.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | public class TextUserInterface { 7 | private Scanner reader; 8 | private Participants participants; 9 | 10 | public TextUserInterface(){ 11 | this.reader = new Scanner(System.in); 12 | this.participants = new Participants(); 13 | } 14 | 15 | public void launch(){ 16 | this.getParticipants(); 17 | this.round(); 18 | this.results(); 19 | } 20 | 21 | public void getParticipants(){ 22 | System.out.println("Kumpula ski jumping week"); 23 | System.out.println(); 24 | System.out.println("Write the names of the participants one at a time; an empty string brings you to the jumping phase."); 25 | while(true){ 26 | System.out.println(" Participant name: "); 27 | String name = reader.nextLine(); 28 | if(name.isEmpty()){ 29 | break; 30 | } 31 | Jumper jumper = new Jumper(name); 32 | this.participants.add(jumper); 33 | } 34 | System.out.println(); 35 | System.out.println("The tournament begins!"); 36 | System.out.println(); 37 | } 38 | 39 | public void round(){ 40 | int roundNumber = 1; 41 | while(true){ 42 | System.out.println("Write \"jump\" to jump; otherwise you quit: "); 43 | String input = reader.nextLine(); 44 | if(input.equals("quit")){ 45 | break; 46 | }else if(input.equals("jump")){ 47 | System.out.println(); 48 | System.out.println("Round " + roundNumber); 49 | System.out.println(); 50 | participants.sort(); 51 | participants.reverse(); 52 | System.out.println("Jumping order:"); 53 | participants.printInOrder(); 54 | System.out.println(); 55 | System.out.println("Results of round " + roundNumber); 56 | participants.play(); 57 | System.out.println(); 58 | roundNumber++; 59 | } 60 | } 61 | System.out.println(); 62 | System.out.println("Thanks!"); 63 | System.out.println(); 64 | } 65 | 66 | public void results(){ 67 | participants.sort(); 68 | System.out.println("Tournament results: "); 69 | participants.printResults(); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Week8/SortedCards/Card.java: -------------------------------------------------------------------------------- 1 | public class Card implements Comparable{ 2 | 3 | /* 4 | * These are static constant variables. These variables can be used inside and outside 5 | * of this class like, for example, Card.CLUBS 6 | */ 7 | public static final int SPADES = 0; 8 | public static final int DIAMONDS = 1; 9 | public static final int HEARTS = 2; 10 | public static final int CLUBS = 3; 11 | /* 12 | * To make printing easier, Card-class also has string arrays for suits and values. 13 | * SUITS[suit] is a string representation of the suit (Clubs, Diamonds, Hearts, Spades) 14 | * VALUES[value] is an abbreviation of the card's value (A, J, Q, K, [2..10]). 15 | */ 16 | public static final String[] SUITS = {"Spades", "Diamonds", "Hearts", "Clubs"}; 17 | public static final String[] VALUES = {"-", "-", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}; 18 | private int value; 19 | private int suit; 20 | 21 | public Card(int value, int suit) { 22 | this.value = value; 23 | this.suit = suit; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return VALUES[value] + " of " + SUITS[suit]; 29 | } 30 | 31 | public int getValue() { 32 | return value; 33 | } 34 | 35 | public int getSuit() { 36 | return suit; 37 | } 38 | 39 | @Override 40 | public int compareTo(Card card){ 41 | if(this.value - card.value != 0){ 42 | return this.value - card.value; 43 | }else{ 44 | return this.suit - card.suit; 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Week8/SortedCards/Hand.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.ArrayList; 3 | import java.util.Collections; 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class Hand implements Comparable{ 9 | private ArrayList cards; 10 | 11 | public Hand(){ 12 | this.cards = new ArrayList(); 13 | } 14 | 15 | public void add(Card card){ 16 | this.cards.add(card); 17 | } 18 | 19 | public void print(){ 20 | for(Card c : this.cards){ 21 | System.out.println(c); 22 | } 23 | } 24 | 25 | public void sort(){ 26 | Collections.sort(this.cards); 27 | } 28 | 29 | public int totalValue(){ 30 | int totalValue = 0; 31 | for(Card c : this.cards){ 32 | totalValue += c.getValue(); 33 | } 34 | return totalValue; 35 | } 36 | 37 | @Override 38 | public int compareTo(Hand hand){ 39 | return this.totalValue() - hand.totalValue(); 40 | } 41 | 42 | public void sortAgainstSuit(){ 43 | Collections.sort(cards, new SortAgainstSuitAndValue()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Week8/SortedCards/SortAgainstSuitAndValue.java: -------------------------------------------------------------------------------- 1 | import java.util.Comparator; 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | public class SortAgainstSuitAndValue implements Comparator{ 7 | @Override 8 | public int compare(Card card1, Card card2){ 9 | if(card1.getSuit() - card2.getSuit() == 0){ 10 | return card1.getValue() - card2.getValue(); 11 | }else{ 12 | return card1.getSuit() - card2.getSuit(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Week8/StudentsSortedByName/Student.java: -------------------------------------------------------------------------------- 1 | public class Student implements Comparable{ 2 | 3 | private String name; 4 | 5 | public Student(String name) { 6 | this.name = name; 7 | } 8 | 9 | public String getName() { 10 | return name; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return name; 16 | } 17 | 18 | @Override 19 | public int compareTo(Student student){ 20 | return this.name.compareToIgnoreCase(student.name); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Week9/DuplicateRemover/tools/DuplicateRemover.java: -------------------------------------------------------------------------------- 1 | package tools; 2 | 3 | import java.util.Set; 4 | 5 | public interface DuplicateRemover { 6 | void add(String characterString); 7 | int getNumberOfDetectedDuplicates(); 8 | Set getUniqueCharacterStrings(); 9 | void empty(); 10 | } 11 | -------------------------------------------------------------------------------- /Week9/DuplicateRemover/tools/PersonalDuplicateRemover.java: -------------------------------------------------------------------------------- 1 | 2 | package tools; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Set; 10 | import java.util.HashSet; 11 | 12 | public class PersonalDuplicateRemover implements DuplicateRemover{ 13 | private Set entries; 14 | private int numberOfDuplicates; 15 | 16 | public PersonalDuplicateRemover(){ 17 | this.numberOfDuplicates = 0; 18 | this.entries = new HashSet(); 19 | } 20 | 21 | @Override 22 | public void add(String characterString){ 23 | if(!entries.contains(characterString)){ 24 | entries.add(characterString); 25 | }else{ 26 | numberOfDuplicates++; 27 | } 28 | } 29 | 30 | @Override 31 | public int getNumberOfDetectedDuplicates(){ 32 | return numberOfDuplicates; 33 | } 34 | 35 | @Override 36 | public Set getUniqueCharacterStrings(){ 37 | return entries; 38 | } 39 | 40 | @Override 41 | public void empty(){ 42 | entries.clear(); 43 | numberOfDuplicates = 0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Week9/FileAnalysis/file/Analysis.java: -------------------------------------------------------------------------------- 1 | 2 | package file; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.io.File; 10 | import java.io.FileNotFoundException; 11 | import java.util.List; 12 | import java.util.ArrayList; 13 | import java.util.Scanner; 14 | 15 | public class Analysis { 16 | private List lines; 17 | 18 | public Analysis(File file) throws Exception{ 19 | Scanner reader = null; 20 | try{ 21 | reader = new Scanner(file); 22 | }catch(Exception e){ 23 | System.out.println("we could not find the file"); 24 | return; 25 | } 26 | lines = new ArrayList(); 27 | 28 | while(reader.hasNextLine()){ 29 | String line = reader.nextLine(); 30 | lines.add(line); 31 | } 32 | } 33 | 34 | public int lines(){ 35 | int numberOfLines = 0; 36 | for(String l : lines){ 37 | numberOfLines++; 38 | } 39 | return numberOfLines; 40 | } 41 | 42 | public int characters(){ 43 | int numberOfCharacters = 0; 44 | for(String l : lines){ 45 | numberOfCharacters += l.length() + 1; 46 | 47 | } 48 | return numberOfCharacters; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Week9/FirstPackages/DefaultPackage/Main.java: -------------------------------------------------------------------------------- 1 | import mooc.logic.ApplicationLogic; 2 | import mooc.ui.UserInterface; 3 | import mooc.ui.TextUserInterface; 4 | 5 | public class Main { 6 | 7 | public static void main(String[] args) { 8 | UserInterface ui = new TextUserInterface(); 9 | new ApplicationLogic(ui).execute(3); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Week9/FirstPackages/mooc.logic/ApplicationLogic.java: -------------------------------------------------------------------------------- 1 | package mooc.logic; 2 | import mooc.ui.UserInterface; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class ApplicationLogic { 9 | private UserInterface ui; 10 | 11 | public ApplicationLogic(UserInterface ui){ 12 | this.ui = ui; 13 | } 14 | 15 | public void execute(int howManyTimes){ 16 | for(int i = 0; i < howManyTimes; i++){ 17 | System.out.println("The application logic works"); 18 | this.ui.update(); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Week9/FirstPackages/mooc.ui/TextUserInterface.java: -------------------------------------------------------------------------------- 1 | 2 | package mooc.ui; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class TextUserInterface implements UserInterface{ 9 | @Override 10 | public void update(){ 11 | System.out.println("Updating the user interface"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Week9/FirstPackages/mooc.ui/UserInterface.java: -------------------------------------------------------------------------------- 1 | 2 | package mooc.ui; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public interface UserInterface { 9 | void update(); 10 | } 11 | -------------------------------------------------------------------------------- /Week9/MethodArgumentValidation/Validation/Calculator.java: -------------------------------------------------------------------------------- 1 | package validation; 2 | 3 | public class Calculator { 4 | 5 | public int multiplication(int fromInteger) { 6 | if(fromInteger < 0){ 7 | throw new IllegalArgumentException("negative numbers are not accepted"); 8 | } 9 | int multiplication = 1; 10 | for (int i = 1; i <= fromInteger; i++) { 11 | multiplication *= i; 12 | } 13 | 14 | return multiplication; 15 | } 16 | 17 | public int binomialCoefficient(int setSize, int subsetSize) { 18 | if(setSize < 0 || subsetSize < 0 || subsetSize > setSize){ 19 | throw new IllegalArgumentException("invalid entries"); 20 | } 21 | int numerator = multiplication(setSize); 22 | int denominator = multiplication(subsetSize) * multiplication(setSize - subsetSize); 23 | 24 | return numerator / denominator; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Week9/MethodArgumentValidation/Validation/Person.java: -------------------------------------------------------------------------------- 1 | package validation; 2 | 3 | public class Person { 4 | 5 | private String name; 6 | private int age; 7 | 8 | public Person(String name, int age) { 9 | if(name == null || name.isEmpty() || name.length() > 40){ 10 | throw new IllegalArgumentException("invalid name"); 11 | } 12 | this.name = name; 13 | if(age < 0 || age > 120){ 14 | throw new IllegalArgumentException("invalid age"); 15 | } 16 | this.age = age; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public int getAge() { 24 | return age; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Week9/Moving/Moving/Main.java: -------------------------------------------------------------------------------- 1 | package moving; 2 | 3 | import moving.domain.Item; 4 | import moving.domain.Thing; 5 | import moving.domain.Box; 6 | import moving.logic.Packer; 7 | import java.util.List; 8 | import java.util.ArrayList; 9 | import java.util.Collections; 10 | 11 | public class Main { 12 | 13 | public static void main(String[] args) { 14 | // test your program here 15 | List things = new ArrayList(); 16 | things.add(new Item("passport", 2)); 17 | things.add(new Item("toothbrash", 1)); 18 | things.add(new Item("book", 4)); 19 | things.add(new Item("circular saw", 8)); 20 | 21 | // we create a packer which uses boxes whose valume is 10 22 | Packer packer = new Packer(10); 23 | 24 | // we ask our packer to pack things into boxes 25 | List boxes = packer.packThings( things ); 26 | 27 | System.out.println("number of boxes: "+boxes.size()); 28 | 29 | for (Box box : boxes) { 30 | System.out.println(" things in the box: "+box.getVolume()+" dm^3"); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Week9/Moving/moving.domain/Box.java: -------------------------------------------------------------------------------- 1 | 2 | package moving.domain; 3 | 4 | import java.util.List; 5 | import java.util.ArrayList; 6 | 7 | /** 8 | * 9 | * @author giuseppedesantis 10 | */ 11 | public class Box implements Thing{ 12 | private int maximumCapacity; 13 | private List things; 14 | 15 | public Box(int maximumCapacity){ 16 | this.maximumCapacity = maximumCapacity; 17 | this.things = new ArrayList(); 18 | } 19 | 20 | public Box(Box aBox){ 21 | this.things = new ArrayList(); 22 | this.maximumCapacity = aBox.maximumCapacity; 23 | } 24 | 25 | public boolean addThing(Thing thing){ 26 | if((thing.getVolume() + this.getVolume()) <= this.maximumCapacity){ 27 | this.things.add(thing); 28 | return true; 29 | } 30 | return false; 31 | } 32 | 33 | @Override 34 | public int getVolume(){ 35 | int volume = 0; 36 | for(Thing t : this.things){ 37 | volume = volume + t.getVolume(); 38 | } 39 | return volume; 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Week9/Moving/moving.domain/Item.java: -------------------------------------------------------------------------------- 1 | 2 | package moving.domain; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class Item implements Thing, Comparable{ 9 | private String name; 10 | private int volume; 11 | 12 | public Item(String name, int volume){ 13 | this.name = name; 14 | this.volume = volume; 15 | } 16 | 17 | public String getName(){ 18 | return this.name; 19 | } 20 | 21 | @Override 22 | public int getVolume(){ 23 | return this.volume; 24 | } 25 | 26 | @Override 27 | public int compareTo(Item otherItem){ 28 | return this.volume - otherItem.volume; 29 | } 30 | 31 | @Override 32 | public String toString(){ 33 | return this.name + " (" + this.volume + " dm^3)"; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Week9/Moving/moving.domain/Thing.java: -------------------------------------------------------------------------------- 1 | 2 | package moving.domain; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public interface Thing { 9 | int getVolume(); 10 | } 11 | -------------------------------------------------------------------------------- /Week9/Moving/moving.logic/Packer.java: -------------------------------------------------------------------------------- 1 | 2 | package moving.logic; 3 | 4 | import moving.domain.Box; 5 | import moving.domain.Item; 6 | import moving.domain.Thing; 7 | import java.util.List; 8 | import java.util.ArrayList; 9 | 10 | /** 11 | * 12 | * @author giuseppedesantis 13 | */ 14 | public class Packer { 15 | private Box box; 16 | private List boxes; 17 | 18 | public Packer(int boxesVolume){ 19 | this.box = new Box(boxesVolume); 20 | this.boxes = new ArrayList(); 21 | } 22 | 23 | public List packThings(List things){ 24 | if(things.isEmpty() == false){ 25 | this.boxes.add(box); 26 | for(Thing t : things){ 27 | if(this.box.addThing(t) == true){ 28 | 29 | }else{ 30 | Box newBox = new Box(this.box); 31 | newBox.addThing(t); 32 | this.boxes.add(newBox); 33 | } 34 | } 35 | } 36 | return this.boxes; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Week9/MultipleEntryDictionary/MultipleEntryDictionary.java: -------------------------------------------------------------------------------- 1 | package dictionary; 2 | 3 | import java.util.Set; 4 | 5 | public interface MultipleEntryDictionary { 6 | void add(String word, String entry); 7 | Set translate(String word); 8 | void remove(String word); 9 | } 10 | -------------------------------------------------------------------------------- /Week9/MultipleEntryDictionary/PersonalMultipleEntryDictionary.java: -------------------------------------------------------------------------------- 1 | 2 | package dictionary; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Map; 10 | import java.util.HashMap; 11 | import java.util.Set; 12 | import java.util.HashSet; 13 | 14 | public class PersonalMultipleEntryDictionary implements MultipleEntryDictionary{ 15 | private Map> dict; 16 | 17 | public PersonalMultipleEntryDictionary(){ 18 | this.dict = new HashMap>(); 19 | } 20 | 21 | @Override 22 | public void add(String word, String entry){ 23 | if(!this.dict.containsKey(word)){ 24 | this.dict.put(word, new HashSet()); 25 | } 26 | 27 | Set translations = this.dict.get(word); 28 | translations.add(entry); 29 | } 30 | 31 | @Override 32 | public Set translate(String word){ 33 | Set translations = this.dict.get(word); 34 | return translations; 35 | } 36 | 37 | @Override 38 | public void remove(String word){ 39 | this.dict.remove(word); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Week9/PhoneSearch/Contacts.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | import java.util.Map; 8 | import java.util.HashMap; 9 | import java.util.Set; 10 | import java.util.List; 11 | import java.util.ArrayList; 12 | import java.util.Collection; 13 | import java.util.Collections; 14 | 15 | public class Contacts { 16 | private Map nameContacts; 17 | private Map, Person> phoneContacts; 18 | private Map addressContacts; 19 | private List contacts; 20 | 21 | public Contacts(){ 22 | this.contacts = new ArrayList(); 23 | this.nameContacts = new HashMap(); 24 | this.phoneContacts = new HashMap, Person>(); 25 | this.addressContacts = new HashMap(); 26 | } 27 | 28 | public void addContact(Person person){ 29 | this.contacts.add(person); 30 | this.nameContacts.put(person.getName(), person); 31 | this.phoneContacts.put(person.getNumbers(), person); 32 | this.addressContacts.put(person.getAddress(), person); 33 | } 34 | 35 | public boolean containsPersonByName(String name){ 36 | for(Person p : contacts){ 37 | if(p.getName().equals(name)){ 38 | return true; 39 | } 40 | } 41 | return false; 42 | } 43 | 44 | public void addNumber(String name, String number){ 45 | for(Person p : contacts){ 46 | if(p.getName().equals(name)){ 47 | p.addNumber(number); 48 | this.phoneContacts.put(p.getNumbers(), p); 49 | } 50 | } 51 | } 52 | 53 | public void addAddress(String name, String street, String city){ 54 | for(Person p : contacts){ 55 | if(p.getName().equals(name)){ 56 | p.addAddress(street, city); 57 | this.addressContacts.put(p.getAddress(), p); 58 | } 59 | } 60 | } 61 | 62 | public String getNameBasedOnNumber(String number){ 63 | for(Person p : contacts){ 64 | Set numbersOfContact = p.getNumbers(); 65 | for(String s : numbersOfContact){ 66 | if(s.equals(number)){ 67 | return " " + p.getName(); 68 | } 69 | } 70 | } 71 | return " not found"; 72 | } 73 | 74 | public String getNumberBasedOnName(String name){ 75 | return nameContacts.get(name).getNumbers().toString() 76 | .replace("[", "") 77 | .replace(", ", "\n") 78 | .replace("]", ""); 79 | } 80 | 81 | public Person getPersonalInformation(String name){ 82 | return nameContacts.get(name); 83 | } 84 | 85 | public void removePerson(String name){ 86 | Person toBeRemoved = new Person(""); 87 | for(Person p : this.contacts){ 88 | if(p.getName().equals(name)){ 89 | toBeRemoved = p; 90 | } 91 | } 92 | this.nameContacts.remove(name); 93 | this.phoneContacts.remove(toBeRemoved.getNumbers()); 94 | this.addressContacts.remove(name); 95 | this.contacts.remove(toBeRemoved); 96 | } 97 | 98 | public void filteredListing(String keyword){ 99 | Collection newList = new ArrayList(); 100 | Collections.sort(contacts); 101 | for(Person p : contacts){ 102 | if(keyword.isEmpty() || p.getAddress().contains(keyword) || p.getName().contains(keyword)){ 103 | newList.add(p); 104 | } 105 | } 106 | if(newList.isEmpty()){ 107 | System.out.println("not found"); 108 | }else{ 109 | for(Person p : newList){ 110 | System.out.println(p.getName()); 111 | System.out.println(p); 112 | } 113 | } 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Week9/PhoneSearch/Person.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | import java.util.Set; 8 | import java.util.HashSet; 9 | 10 | public class Person implements Comparable{ 11 | private String name; 12 | private Set numbers; 13 | private String address; 14 | 15 | public Person(String name){ 16 | this.name = name; 17 | this.numbers = new HashSet(); 18 | this.address = ""; 19 | } 20 | 21 | public void addNumber(String number){ 22 | this.numbers.add(number); 23 | } 24 | 25 | public void addAddress(String street, String city){ 26 | this.address = street + " " + city; 27 | } 28 | 29 | public String getName(){ 30 | return this.name; 31 | } 32 | 33 | public String getAddress(){ 34 | return this.address; 35 | } 36 | 37 | public Set getNumbers(){ 38 | return this.numbers; 39 | } 40 | 41 | @Override 42 | public String toString(){ 43 | if(!numbers.isEmpty() && !address.isEmpty()){ 44 | return "\naddress: " + address + "\nphone numbers: " + numbers; 45 | }else if(!numbers.isEmpty() && address.isEmpty()){ 46 | return "\naddress unknown" + "\nphone numbers: " + numbers; 47 | }else if(!address.isEmpty()){ 48 | return "\naddress: " + address + "\nphone number not found"; 49 | }else{ 50 | return null; 51 | } 52 | } 53 | 54 | @Override 55 | public int compareTo(Person compared){ 56 | if(this.name != null && compared.name != null){ 57 | return this.name.compareToIgnoreCase(compared.name); 58 | }else{ 59 | return 0; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Week9/PhoneSearch/TextUserInterface.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | import java.util.Scanner; 8 | 9 | public class TextUserInterface { 10 | private Scanner reader; 11 | private Contacts contacts; 12 | 13 | public TextUserInterface(){ 14 | this.reader = new Scanner(System.in); 15 | this.contacts = new Contacts(); 16 | } 17 | 18 | public void launch(){ 19 | initialCommands(); 20 | program(); 21 | } 22 | 23 | public void initialCommands(){ 24 | System.out.println("phone search"); 25 | System.out.println("available operations:"); 26 | System.out.println(" 1 add a number"); 27 | System.out.println(" 2 search for a number"); 28 | System.out.println(" 3 search for a person by phone number"); 29 | System.out.println(" 4 add an address"); 30 | System.out.println(" 5 search for personal information"); 31 | System.out.println(" 6 delete personal information"); 32 | System.out.println(" 7 filtered listing"); 33 | System.out.println(" x quit"); 34 | } 35 | 36 | public void program(){ 37 | while(true){ 38 | System.out.println(); 39 | System.out.println("command: "); 40 | String input = reader.nextLine(); 41 | if(input.equals("x")){ 42 | break; 43 | }else if(input.equals("1")){ 44 | addNumber(); 45 | }else if(input.equals("2")){ 46 | searchNumber(); 47 | }else if(input.equals("3")){ 48 | searchByNumber(); 49 | }else if(input.equals("4")){ 50 | addAddress(); 51 | }else if(input.equals("5")){ 52 | searchPersonalInfo(); 53 | }else if(input.equals("6")){ 54 | removePerson(); 55 | }else if(input.equals("7")){ 56 | filteredListing(); 57 | } 58 | } 59 | } 60 | 61 | public void addNumber(){ 62 | System.out.println("whose number: "); 63 | String name = reader.nextLine(); 64 | System.out.println("number: "); 65 | String number = reader.nextLine(); 66 | if(contacts.containsPersonByName(name)){ 67 | contacts.addNumber(name, number); 68 | }else{ 69 | Person toBeAdded = new Person(name); 70 | toBeAdded.addNumber(number); 71 | contacts.addContact(toBeAdded); 72 | } 73 | } 74 | 75 | public void searchNumber(){ 76 | System.out.println("whose number: "); 77 | String name = reader.nextLine(); 78 | if(contacts.containsPersonByName(name)){ 79 | System.out.println(contacts.getNumberBasedOnName(name)); 80 | }else{ 81 | System.out.println("not found"); 82 | } 83 | 84 | } 85 | 86 | public void searchByNumber(){ 87 | System.out.println("number: "); 88 | String number = reader.nextLine(); 89 | System.out.println(contacts.getNameBasedOnNumber(number)); 90 | } 91 | 92 | public void addAddress(){ 93 | System.out.println("whose address: "); 94 | String name = reader.nextLine(); 95 | if(contacts.containsPersonByName(name)){ 96 | System.out.println("street: "); 97 | String street = reader.nextLine(); 98 | System.out.println("city: "); 99 | String city = reader.nextLine(); 100 | contacts.addAddress(name, street, city); 101 | }else{ 102 | System.out.println("street: "); 103 | String street = reader.nextLine(); 104 | System.out.println("city: "); 105 | String city = reader.nextLine(); 106 | Person toBeAdded = new Person(name); 107 | toBeAdded.addAddress(street, city); 108 | contacts.addContact(toBeAdded); 109 | } 110 | } 111 | 112 | public void searchPersonalInfo(){ 113 | System.out.println("whose information: "); 114 | String name = reader.nextLine(); 115 | if(contacts.containsPersonByName(name)){ 116 | System.out.println(contacts.getPersonalInformation(name)); 117 | }else{ 118 | System.out.println("not found"); 119 | } 120 | } 121 | 122 | public void removePerson(){ 123 | System.out.println("whose information: "); 124 | String name = reader.nextLine(); 125 | if(contacts.containsPersonByName(name)){ 126 | contacts.removePerson(name); 127 | }else{ 128 | System.out.println("not found"); 129 | } 130 | } 131 | 132 | public void filteredListing(){ 133 | System.out.println("keyword (if empty, all listed) : "); 134 | String keyword = reader.nextLine(); 135 | contacts.filteredListing(keyword); 136 | } 137 | 138 | } 139 | -------------------------------------------------------------------------------- /Week9/Printer/Printer.java: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * 4 | * @author giuseppedesantis 5 | */ 6 | 7 | import java.util.Scanner; 8 | import java.io.File; 9 | 10 | public class Printer { 11 | private File textFile; 12 | private Scanner reader; 13 | 14 | public Printer(String fileName) throws Exception{ 15 | this.textFile = new File(fileName); 16 | this.reader = new Scanner(this.textFile, "UTF-8"); 17 | } 18 | 19 | public void printLinesWhichContain(String word){ 20 | if(word == ""){ 21 | System.out.println(this.textFile); 22 | }else{ 23 | while(reader.hasNextLine()){ 24 | String line = reader.nextLine(); 25 | if(line.contains(word)){ 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Week9/SensorAndTemperatureMeasurement/application/AverageSensor.java: -------------------------------------------------------------------------------- 1 | 2 | package application; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.List; 10 | import java.util.ArrayList; 11 | 12 | public class AverageSensor implements Sensor{ 13 | private List sensors; 14 | private List readings; 15 | 16 | public AverageSensor(){ 17 | this.sensors = new ArrayList(); 18 | this.readings = new ArrayList(); 19 | } 20 | 21 | public void addSensor(Sensor additional){ 22 | this.sensors.add(additional); 23 | } 24 | 25 | @Override 26 | public boolean isOn(){ 27 | for(Sensor s : this.sensors){ 28 | if(s.isOn()){ 29 | return true; 30 | } 31 | } 32 | return false; 33 | } 34 | 35 | @Override 36 | public void on(){ 37 | for(Sensor s : this.sensors){ 38 | s.on(); 39 | } 40 | } 41 | 42 | @Override 43 | public void off(){ 44 | for(Sensor s: this.sensors){ 45 | s.off(); 46 | } 47 | } 48 | 49 | @Override 50 | public int measure(){ 51 | if(this.sensors.isEmpty() || this.isOn() == false){ 52 | throw new IllegalStateException("No sensors or average sensor is off"); 53 | }else{ 54 | int sum = 0; 55 | for(Sensor s : this.sensors){ 56 | sum += s.measure(); 57 | } 58 | int average = sum/sensors.size(); 59 | this.readings.add(average); 60 | return average; 61 | } 62 | } 63 | 64 | public List readings(){ 65 | return readings; 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /Week9/SensorAndTemperatureMeasurement/application/ConstantSensor.java: -------------------------------------------------------------------------------- 1 | 2 | package application; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class ConstantSensor implements Sensor{ 9 | private int measure; 10 | 11 | public ConstantSensor(int measure){ 12 | this.measure = measure; 13 | } 14 | 15 | @Override 16 | public boolean isOn(){ 17 | return true; 18 | } 19 | 20 | @Override 21 | public void on(){ 22 | 23 | } 24 | 25 | @Override 26 | public void off(){ 27 | 28 | } 29 | 30 | @Override 31 | public int measure(){ 32 | return this.measure; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Week9/SensorAndTemperatureMeasurement/application/Sensor.java: -------------------------------------------------------------------------------- 1 | 2 | package application; 3 | 4 | public interface Sensor { 5 | boolean isOn(); // returns true if the sensor is on 6 | void on(); // switches the sensor on 7 | void off(); // switches the sensor off 8 | int measure(); // returns the sensor reading if the sensor is on 9 | // if the sensor is off, it throws an IllegalStateException 10 | } 11 | -------------------------------------------------------------------------------- /Week9/SensorAndTemperatureMeasurement/application/Thermometer.java: -------------------------------------------------------------------------------- 1 | 2 | package application; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | import java.util.Random; 9 | 10 | public class Thermometer implements Sensor{ 11 | private Random random; 12 | private boolean status; 13 | 14 | public Thermometer(){ 15 | this.random = new Random(); 16 | this.status = false; 17 | } 18 | 19 | @Override 20 | public boolean isOn(){ 21 | return this.status; 22 | } 23 | 24 | @Override 25 | public void on(){ 26 | this.status = true; 27 | } 28 | 29 | @Override 30 | public void off(){ 31 | this.status = false; 32 | } 33 | 34 | @Override 35 | public int measure(){ 36 | int measure = -30 + random.nextInt(30 - (-30) + 1); 37 | return measure; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Week9/WordInspection/wordinspection/WordInspection.java: -------------------------------------------------------------------------------- 1 | 2 | package wordinspection; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.io.File; 10 | import java.io.FileNotFoundException; 11 | import java.util.List; 12 | import java.util.ArrayList; 13 | import java.util.Scanner; 14 | 15 | public class WordInspection { 16 | private List words; 17 | private final String vowels = "aeiouyäö"; 18 | 19 | public WordInspection(File file) throws Exception{ 20 | Scanner reader = null; 21 | try{ 22 | reader = new Scanner(file, "UTF-8"); 23 | }catch(Exception e){ 24 | System.out.println("we could not find the file"); 25 | return; 26 | } 27 | words = new ArrayList(); 28 | 29 | while(reader.hasNextLine()){ 30 | String line = reader.nextLine(); 31 | words.add(line); 32 | } 33 | } 34 | 35 | public int wordCount(){ 36 | int wordCount = 0; 37 | for(String w : words){ 38 | wordCount++; 39 | } 40 | return wordCount; 41 | } 42 | 43 | public List wordsContainingZ(){ 44 | List wordsContainingZ = new ArrayList(); 45 | for(String w : words){ 46 | if(w.contains("z")){ 47 | wordsContainingZ.add(w); 48 | } 49 | } 50 | return wordsContainingZ; 51 | } 52 | 53 | public List wordsEndingInL(){ 54 | List wordsEndingInL = new ArrayList(); 55 | for(String w : words){ 56 | int lastIndex = w.length() - 1; 57 | if(w.substring(lastIndex).equals("l")){ 58 | wordsEndingInL.add(w); 59 | } 60 | } 61 | return wordsEndingInL; 62 | } 63 | 64 | public List palindromes(){ 65 | List palindromes = new ArrayList(); 66 | for(String word : words){ 67 | if(word.equals(reverse(word))){ 68 | palindromes.add(word); 69 | } 70 | } 71 | return palindromes; 72 | } 73 | 74 | private String reverse(String word){ 75 | String reverse = ""; 76 | int length = word.length() - 1; 77 | for(int i = length; i >= 0; i--){ 78 | reverse += word.charAt(i); 79 | } 80 | return reverse; 81 | } 82 | 83 | public List wordsWhichContainAllVowels(){ 84 | List returnedString = new ArrayList(); 85 | for(String word : words){ 86 | if(containsAllVowels(word)){ 87 | returnedString.add(word); 88 | } 89 | } 90 | return returnedString; 91 | } 92 | 93 | private boolean containsAllVowels(String word){ 94 | for(char vowel : this.vowels.toCharArray()){ 95 | if(!word.contains(""+vowel)){ 96 | return false; 97 | } 98 | } 99 | return true; 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /week10/Container/containers/Container.java: -------------------------------------------------------------------------------- 1 | package containers; 2 | 3 | public class Container { 4 | 5 | private double capacity; 6 | private double volume; 7 | 8 | public Container(double tilavuus) { 9 | if (tilavuus > 0.0) { 10 | this.capacity = tilavuus; 11 | } else { 12 | this.capacity = 0.0; 13 | } 14 | 15 | volume = 0.0; 16 | } 17 | 18 | 19 | public double getVolume() { 20 | return volume; 21 | } 22 | 23 | public double getOriginalCapacity() { 24 | return capacity; 25 | } 26 | 27 | public double getCurrentCapacity() { 28 | return capacity - volume; 29 | } 30 | 31 | public void addToTheContainer(double amount) { 32 | if (amount < 0) { 33 | return; 34 | } 35 | if (amount <= getCurrentCapacity()) { 36 | volume = volume + amount; 37 | } else { 38 | volume = capacity; 39 | } 40 | } 41 | 42 | public double takeFromTheContainer(double amount) { 43 | if (amount < 0) { 44 | return 0.0; 45 | } 46 | if (amount > volume) { 47 | double everything = volume; 48 | volume = 0.0; 49 | return everything; 50 | } 51 | 52 | volume = volume - amount; 53 | return amount; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "volume = " + volume + ", free space " + getCurrentCapacity(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /week10/Container/containers/ContainerHistory.java: -------------------------------------------------------------------------------- 1 | 2 | package containers; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.List; 10 | import java.util.ArrayList; 11 | 12 | public class ContainerHistory { 13 | private List values; 14 | 15 | public ContainerHistory(){ 16 | this.values = new ArrayList(); 17 | } 18 | 19 | public void add(double situation){ 20 | this.values.add(situation); 21 | } 22 | 23 | public void reset(){ 24 | this.values.clear(); 25 | } 26 | 27 | @Override 28 | public String toString(){ 29 | return this.values.toString(); 30 | } 31 | 32 | public double maxValue(){ 33 | double maxValue = 0; 34 | if(this.values.isEmpty()){ 35 | return maxValue; 36 | }else{ 37 | for(double v : this.values){ 38 | if(v >= maxValue){ 39 | maxValue = v; 40 | } 41 | } 42 | } 43 | return maxValue; 44 | } 45 | 46 | public double minValue(){ 47 | double minValue = 0; 48 | if(this.values.isEmpty()){ 49 | return minValue; 50 | }else{ 51 | minValue = this.maxValue(); 52 | for(double v : this.values){ 53 | if(v < minValue){ 54 | minValue = v; 55 | } 56 | } 57 | } 58 | return minValue; 59 | } 60 | 61 | public double average(){ 62 | double average = 0; 63 | for(double v : this.values){ 64 | average += v; 65 | } 66 | average = average/values.size(); 67 | return average; 68 | } 69 | 70 | public double greatestFluctuation(){ 71 | List fluctuations = new ArrayList(); 72 | double greatestFluctuation = 0; 73 | if(values.isEmpty() || values.size() == 1){ 74 | return greatestFluctuation; 75 | }else{ 76 | for(int i = values.size() - 1; i >= 1; i--){ 77 | double fluctuation = Math.abs(values.get(i) - values.get(i-1)); 78 | fluctuations.add(fluctuation); 79 | } 80 | } 81 | for(double f : fluctuations){ 82 | if(greatestFluctuation < f){ 83 | greatestFluctuation = f; 84 | } 85 | } 86 | return greatestFluctuation; 87 | } 88 | 89 | public double variance(){ 90 | double varianceSum = 0; 91 | if(values.isEmpty() || values.size() == 1){ 92 | return varianceSum; 93 | }else{ 94 | for(double v : values){ 95 | varianceSum += (v - this.average())*(v - this.average()); 96 | } 97 | } 98 | double variance = varianceSum/(values.size() - 1); 99 | return variance; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /week10/Container/containers/ProductContainer.java: -------------------------------------------------------------------------------- 1 | 2 | package containers; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class ProductContainer extends Container{ 9 | 10 | private String productName; 11 | 12 | public ProductContainer(String productName, double capacity){ 13 | super(capacity); 14 | this.productName = productName; 15 | } 16 | 17 | public String getName(){ 18 | return this.productName; 19 | } 20 | 21 | public void setName(String newName){ 22 | this.productName = newName; 23 | } 24 | 25 | @Override 26 | public String toString(){ 27 | return this.productName + ": " + super.toString(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /week10/Container/containers/ProductContainerRecorder.java: -------------------------------------------------------------------------------- 1 | 2 | package containers; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class ProductContainerRecorder extends ProductContainer{ 9 | private ContainerHistory containerHistory; 10 | 11 | public ProductContainerRecorder(String productName, double capacity, double initialVolume) { 12 | super(productName, capacity); 13 | super.addToTheContainer(initialVolume); 14 | containerHistory = new ContainerHistory(); 15 | containerHistory.add(initialVolume); 16 | } 17 | 18 | public String history(){ 19 | return containerHistory.toString(); 20 | } 21 | 22 | @Override 23 | public void addToTheContainer(double amount){ 24 | super.addToTheContainer(amount); 25 | containerHistory.add(super.getVolume()); 26 | } 27 | 28 | @Override 29 | public double takeFromTheContainer(double amount){ 30 | containerHistory.add(super.getVolume() - amount); 31 | return super.takeFromTheContainer(amount); 32 | } 33 | 34 | public void printAnalysis(){ 35 | System.out.println("Product: " + super.getName()); 36 | System.out.println("History: " + this.history()); 37 | System.out.println("Greatest product amount: " + containerHistory.maxValue()); 38 | System.out.println("Smallest product amount: " + containerHistory.minValue()); 39 | System.out.println("Average: " + containerHistory.average()); 40 | System.out.println("Greatest change: " + containerHistory.greatestFluctuation()); 41 | System.out.println("Variance: " + containerHistory.variance()); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /week10/DifferentBoxes/boxes/BlackHoleBox.java: -------------------------------------------------------------------------------- 1 | 2 | package boxes; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Collection; 10 | import java.util.LinkedList; 11 | 12 | public class BlackHoleBox extends Box{ 13 | private Collection things; 14 | 15 | public BlackHoleBox(){ 16 | this.things = new LinkedList(); 17 | } 18 | 19 | public void add(Thing thing){ 20 | this.things.add(thing); 21 | } 22 | 23 | @Override 24 | public boolean isInTheBox(Thing thing){ 25 | return false; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /week10/DifferentBoxes/boxes/Box.java: -------------------------------------------------------------------------------- 1 | package boxes; 2 | 3 | import java.util.Collection; 4 | 5 | public abstract class Box { 6 | 7 | public abstract void add(Thing thing); 8 | 9 | public void add(Collection things) { 10 | for (Thing thing : things) { 11 | add(thing); 12 | } 13 | } 14 | 15 | public abstract boolean isInTheBox(Thing thing); 16 | } 17 | -------------------------------------------------------------------------------- /week10/DifferentBoxes/boxes/MaxWeightBox.java: -------------------------------------------------------------------------------- 1 | 2 | package boxes; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Collection; 10 | import java.util.LinkedList; 11 | 12 | public class MaxWeightBox extends Box{ 13 | private int maxWeight; 14 | private Collection things; 15 | 16 | public MaxWeightBox(int maxWeight){ 17 | this.maxWeight = maxWeight; 18 | this.things = new LinkedList(); 19 | } 20 | 21 | @Override 22 | public void add(Thing thing){ 23 | int currentWeight = 0; 24 | for(Thing t : this.things){ 25 | currentWeight += t.getWeight(); 26 | } 27 | if(thing.getWeight() + currentWeight <= this.maxWeight){ 28 | this.things.add(thing); 29 | } 30 | } 31 | 32 | @Override 33 | public boolean isInTheBox(Thing thing){ 34 | for(Thing t : this.things){ 35 | if(t.equals(thing)){ 36 | return true; 37 | } 38 | } 39 | return false; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /week10/DifferentBoxes/boxes/OneThingBox.java: -------------------------------------------------------------------------------- 1 | 2 | package boxes; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | public class OneThingBox extends Box{ 10 | private Thing thing; 11 | 12 | public OneThingBox(){ 13 | } 14 | 15 | @Override 16 | public void add(Thing thing){ 17 | if(this.thing != null){ 18 | return; 19 | }else{ 20 | this.thing = thing; 21 | } 22 | } 23 | 24 | @Override 25 | public boolean isInTheBox(Thing thing){ 26 | if(this.thing!= null && this.thing.equals(thing)){ 27 | return true; 28 | } 29 | return false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /week10/DifferentBoxes/boxes/Thing.java: -------------------------------------------------------------------------------- 1 | package boxes; 2 | 3 | public class Thing { 4 | 5 | private String name; 6 | private int weight; 7 | 8 | public Thing(String name, int weight) { 9 | if(weight < 0){ 10 | throw new IllegalArgumentException("The weight cannot be negative"); 11 | } 12 | this.name = name; 13 | this.weight = weight; 14 | } 15 | 16 | public Thing(String name) { 17 | this(name, 0); 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public int getWeight() { 25 | return weight; 26 | } 27 | 28 | @Override 29 | public boolean equals(Object object){ 30 | if(object == null){ 31 | return false; 32 | } 33 | if(this.getClass() != object.getClass()){ 34 | return false; 35 | } 36 | Thing compared = (Thing) object; 37 | if(this.name == null || !this.name.equals(compared.name)){ 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | @Override 44 | public int hashCode(){ 45 | if(this.name == null){ 46 | return 7; 47 | } 48 | 49 | return this.name.hashCode(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /week10/Dungeon/dungeon/Character.java: -------------------------------------------------------------------------------- 1 | 2 | package dungeon; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.ArrayList; 10 | 11 | public abstract class Character { 12 | 13 | String name; 14 | int length; 15 | int heigth; 16 | int x; 17 | int y; 18 | 19 | public Character(String name, int length, int heigth){ 20 | this.name = name; 21 | this.length = length; 22 | this.heigth = heigth; 23 | } 24 | 25 | public String getName(){ 26 | return this.name; 27 | } 28 | 29 | public void move(ArrayList moves){ 30 | } 31 | 32 | public void move(int moves, boolean vampireMoves){ 33 | } 34 | 35 | public int getX(){ 36 | return x; 37 | } 38 | 39 | public int getY(){ 40 | return y; 41 | } 42 | 43 | @Override 44 | public String toString(){ 45 | return this.name + " " + this.x + " " + this.y; 46 | } 47 | 48 | @Override 49 | public boolean equals(Object object){ 50 | if(object == null){ 51 | return false; 52 | } 53 | Character compared = (Character) object; 54 | if(this.x == compared.x && this.y == compared.y){ 55 | return true; 56 | }else{ 57 | return false; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /week10/Dungeon/dungeon/Characters.java: -------------------------------------------------------------------------------- 1 | 2 | package dungeon; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.ArrayList; 10 | 11 | public class Characters { 12 | private ArrayList characters; 13 | 14 | public Characters(int length, int heigth, int vampires){ 15 | characters = new ArrayList(); 16 | Player player = new Player("@", length, heigth); 17 | characters.add(player); 18 | while(characters.size() <= vampires){ 19 | Vampire v = new Vampire("v", length, heigth); 20 | if(!characters.contains(v)){ 21 | characters.add(v); 22 | } 23 | } 24 | } 25 | 26 | public void PrintCharacters(){ 27 | for(Character c : characters){ 28 | System.out.println(c); 29 | } 30 | } 31 | 32 | public ArrayList returnCharacters(){ 33 | return characters; 34 | } 35 | 36 | public void moveAndRemoveCharacters(ArrayList moves, boolean vampiresMove){ 37 | ArrayList vampiresMoved = new ArrayList(); 38 | Character player = new Player("", 0, 0); 39 | for(Character c : characters){ 40 | if(c.getName().equals("@")){ 41 | c.move(moves); 42 | player = c; 43 | }else{ 44 | c.move(moves.size(), vampiresMove); 45 | if(!vampiresMoved.contains(c)){ 46 | vampiresMoved.add(c); 47 | } 48 | 49 | } 50 | } 51 | ArrayList toBeRemoved = new ArrayList(); 52 | for(Character vamp : vampiresMoved){ 53 | if(vamp.equals(player)){ 54 | toBeRemoved.add(vamp); 55 | } 56 | } 57 | vampiresMoved.removeAll(toBeRemoved); 58 | characters.clear(); 59 | characters.add(player); 60 | characters.addAll(vampiresMoved); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /week10/Dungeon/dungeon/Dungeon.java: -------------------------------------------------------------------------------- 1 | 2 | package dungeon; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Scanner; 10 | import java.util.ArrayList; 11 | 12 | public class Dungeon { 13 | private GameGrid game; 14 | private int moves; 15 | private Scanner reader; 16 | 17 | public Dungeon(int length, int height, int vampires, int moves, boolean vampiresMove){ 18 | this.game = new GameGrid(length, height, vampires, vampiresMove); 19 | this.moves = moves; 20 | this.reader = new Scanner(System.in); 21 | } 22 | 23 | public void run(){ 24 | int i = moves; 25 | System.out.println(i); 26 | game.printGameGrid(); 27 | while(i > 0){ 28 | if(this.game.returnCharacters().size() == 1){ 29 | break; 30 | } 31 | String input = reader.nextLine(); 32 | ArrayList commands = new ArrayList(); 33 | for(int j = 0; j < input.length(); j++){ 34 | commands.add(input.substring(j, j+1)); 35 | } 36 | game.moveAndRemoveCharacters(commands); 37 | i--; 38 | System.out.println(i); 39 | game.printGameGrid(); 40 | } 41 | if(game.returnCharacters().size() == 1){ 42 | System.out.println("YOU WIN"); 43 | }else{ 44 | System.out.println("YOU LOSE"); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /week10/Dungeon/dungeon/GameGrid.java: -------------------------------------------------------------------------------- 1 | 2 | package dungeon; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.ArrayList; 10 | 11 | public class GameGrid { 12 | private Characters characters; 13 | private ArrayList gameGrid; 14 | private int length; 15 | private int heigth; 16 | private boolean vampiresMove; 17 | 18 | public GameGrid(int length, int heigth, int Vampires, boolean vampiresMove){ 19 | characters = new Characters(length, heigth, Vampires); 20 | this.gameGrid = new ArrayList(); 21 | this.length = length; 22 | this.heigth = heigth; 23 | this.vampiresMove = vampiresMove; 24 | } 25 | 26 | public void createGrid(){ 27 | gameGrid.clear(); 28 | ArrayList players = characters.returnCharacters(); 29 | for(int y = 0; y < length; y++){ 30 | ArrayList playersOnY = new ArrayList(); 31 | for(Character c : players){ 32 | if(c.getY() == y){ 33 | playersOnY.add(c); 34 | } 35 | } 36 | String line = ""; 37 | for(int x = 0; x < length; x++){ 38 | boolean containsPlayer = false; 39 | for(Character c : playersOnY){ 40 | if(x == c.getX()){ 41 | line += c.getName(); 42 | containsPlayer = true; 43 | } 44 | } 45 | if(containsPlayer == false){ 46 | line += "."; 47 | } 48 | } 49 | gameGrid.add(line); 50 | } 51 | } 52 | 53 | public ArrayList returnCharacters(){ 54 | return characters.returnCharacters(); 55 | } 56 | 57 | public void printGameGrid(){ 58 | System.out.println(); 59 | this.characters.PrintCharacters(); 60 | System.out.println(); 61 | this.createGrid(); 62 | for(String g : this.gameGrid){ 63 | System.out.println(g); 64 | } 65 | System.out.println(); 66 | } 67 | 68 | public void moveAndRemoveCharacters(ArrayList moves){ 69 | characters.moveAndRemoveCharacters(moves, vampiresMove); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /week10/Dungeon/dungeon/Main.java: -------------------------------------------------------------------------------- 1 | package dungeon; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | new Dungeon(10,10,5,14,true).run(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /week10/Dungeon/dungeon/Player.java: -------------------------------------------------------------------------------- 1 | 2 | package dungeon; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.ArrayList; 10 | 11 | public class Player extends Character { 12 | 13 | public Player(String name, int length, int heigth){ 14 | super(name, length, heigth); 15 | this.x = 0; 16 | this.y = 0; 17 | } 18 | 19 | @Override 20 | public void move(ArrayList moves){ 21 | for(String m : moves){ 22 | if(m.equals("w") && y - 1 < heigth && y - 1 >= 0){ 23 | y -= 1; 24 | }else if(m.equals("s") && y + 1 < heigth && y + 1 >= 0){ 25 | y += 1; 26 | }else if(m.equals("a") && x - 1 < length && x - 1 >= 0){ 27 | x -= 1; 28 | }else if(m.equals("d") && x + 1 < length && x + 1 >= 0){ 29 | x += 1; 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /week10/Dungeon/dungeon/Vampire.java: -------------------------------------------------------------------------------- 1 | 2 | package dungeon; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Random; 10 | 11 | public class Vampire extends Character{ 12 | Random rand; 13 | 14 | public Vampire(String name, int length, int heigth){ 15 | super(name, length, heigth); 16 | rand = new Random(); 17 | this.x = rand.nextInt(length); 18 | this.y = rand.nextInt(heigth); 19 | } 20 | 21 | @Override 22 | public void move(int moves, boolean vampireMoves){ 23 | if(vampireMoves == true){ 24 | for(int i = 0; i < moves; i++){ 25 | int random = rand.nextInt(4); 26 | if(random == 0 && x + 1 < length && x + 1 >= 0){ 27 | x += 1; 28 | }else if(random == 1 && x - 1 < length && x - 1 >= 0){ 29 | x -= 1; 30 | }else if(random == 2 && y + 1 < heigth && y + 1 >= 0){ 31 | y += 1; 32 | }else if(random == 3 && y - 1 < heigth && y - 1 >= 0){ 33 | y -= 1; 34 | } 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /week10/FarmSimulator/farmsimulator/Alive.java: -------------------------------------------------------------------------------- 1 | package farmsimulator; 2 | 3 | public interface Alive { 4 | void liveHour(); 5 | } 6 | -------------------------------------------------------------------------------- /week10/FarmSimulator/farmsimulator/BulkTank.java: -------------------------------------------------------------------------------- 1 | 2 | package farmsimulator; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class BulkTank { 9 | private double capacity; 10 | private double volume; 11 | 12 | public BulkTank(){ 13 | this.capacity = 2000; 14 | this.volume = 0; 15 | } 16 | 17 | public BulkTank(double capacity){ 18 | this.capacity = capacity; 19 | this.volume = 0; 20 | } 21 | 22 | public double getCapacity(){ 23 | return this.capacity; 24 | } 25 | 26 | public double getVolume(){ 27 | return this.volume; 28 | } 29 | 30 | public double howMuchFreeSpace(){ 31 | return this.capacity - this.volume; 32 | } 33 | 34 | public void addToTank(double amount){ 35 | if(amount <= this.howMuchFreeSpace()){ 36 | this.volume += amount; 37 | }else{ 38 | this.volume = this.capacity; 39 | } 40 | } 41 | 42 | public double getFromTank(double amount){ 43 | if(amount >= this.volume){ 44 | this.volume = 0; 45 | return this.volume; 46 | }else{ 47 | this.volume -= amount; 48 | return this.volume; 49 | } 50 | } 51 | 52 | @Override 53 | public String toString(){ 54 | return Math.ceil(this.volume) + "/" + Math.ceil(this.capacity); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /week10/FarmSimulator/farmsimulator/Cow.java: -------------------------------------------------------------------------------- 1 | 2 | package farmsimulator; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Random; 10 | 11 | public class Cow implements Milkable, Alive{ 12 | private String name; 13 | private int udder; 14 | private double amountOfMilk; 15 | private Random rand = new Random(); 16 | private static final String[] NAMES = new String[]{ 17 | "Anu", "Arpa", "Essi", "Heluna", "Hely", 18 | "Hento", "Hilke", "Hilsu", "Hymy", "Ihq", "Ilme", "Ilo", 19 | "Jaana", "Jami", "Jatta", "Laku", "Liekki", 20 | "Mainikki", "Mella", "Mimmi", "Naatti", 21 | "Nina", "Nyytti", "Papu", "Pullukka", "Pulu", 22 | "Rima", "Soma", "Sylkki", "Valpu", "Virpi"}; 23 | 24 | public Cow(){ 25 | this.udder = 15 + rand.nextInt(40 - 15 + 1); 26 | int nameIndex = rand.nextInt(NAMES.length); 27 | this.name = NAMES[nameIndex]; 28 | } 29 | 30 | public Cow(String name){ 31 | this.name = name; 32 | this.udder = 15 + rand.nextInt(40 - 15 + 1); 33 | } 34 | 35 | public String getName(){ 36 | return this.name; 37 | } 38 | 39 | public double getCapacity(){ 40 | return this.udder; 41 | } 42 | 43 | public double getAmount(){ 44 | return this.amountOfMilk; 45 | } 46 | 47 | @Override 48 | public String toString(){ 49 | return this.name + " " + this.amountOfMilk + "/" + this.udder; 50 | } 51 | 52 | @Override 53 | public double milk(){ 54 | double tempAmountOfMilk = this.amountOfMilk; 55 | this.amountOfMilk = 0; 56 | return tempAmountOfMilk; 57 | } 58 | 59 | @Override 60 | public void liveHour(){ 61 | double milkProduced = Math.ceil(0.7 + (2 - 0.7) * rand.nextDouble()); 62 | if(this.amountOfMilk + milkProduced <= this.udder){ 63 | this.amountOfMilk += milkProduced; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /week10/FarmSimulator/farmsimulator/CowHouse.java: -------------------------------------------------------------------------------- 1 | 2 | package farmsimulator; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.Collection; 10 | 11 | public class CowHouse { 12 | private BulkTank tank; 13 | private MilkingRobot robot; 14 | 15 | public CowHouse(BulkTank tank){ 16 | this.tank = tank; 17 | } 18 | 19 | public BulkTank getBulkTank(){ 20 | return this.tank; 21 | } 22 | 23 | public void installMilkingRobot(MilkingRobot milkingRobot){ 24 | this.robot = milkingRobot; 25 | this.robot.setBulkTank(this.tank); 26 | } 27 | 28 | public void takeCareOf(Cow cow){ 29 | if(this.robot == null){ 30 | throw new IllegalStateException("The milking robot hasn't been installed"); 31 | }else{ 32 | this.robot.milk(cow); 33 | } 34 | } 35 | 36 | public void takeCareOf(Collection cows){ 37 | if(this.robot == null){ 38 | throw new IllegalStateException("The milking robot hasn't been installed"); 39 | }else{ 40 | for(Cow c : cows){ 41 | this.robot.milk(c); 42 | } 43 | } 44 | } 45 | 46 | @Override 47 | public String toString(){ 48 | return this.tank.toString(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /week10/FarmSimulator/farmsimulator/Farm.java: -------------------------------------------------------------------------------- 1 | 2 | package farmsimulator; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.LinkedList; 10 | 11 | public class Farm { 12 | private String owner; 13 | private CowHouse cowHouse; 14 | private LinkedList cowList; 15 | 16 | public Farm(String owner, CowHouse cowHouse){ 17 | this.owner = owner; 18 | this.cowHouse = cowHouse; 19 | this.cowList = new LinkedList(); 20 | } 21 | 22 | @Override 23 | public String toString(){ 24 | return "Farm owner: " + this.owner + 25 | "\nCowhouse bulk tank: " + this.cowHouse + 26 | "\n" + this.cowListString(); 27 | } 28 | 29 | public String getOwner(){ 30 | return this.owner; 31 | } 32 | 33 | public void addCow(Cow cow){ 34 | this.cowList.add(cow); 35 | } 36 | 37 | public String cowListString(){ 38 | if(this.cowList.isEmpty()){ 39 | return "No cows."; 40 | }else{ 41 | String cowListString = "Animals:"; 42 | for(Cow c : this.cowList){ 43 | cowListString += "\n " + c.toString(); 44 | } 45 | return cowListString; 46 | } 47 | } 48 | 49 | public void liveHour(){ 50 | for(Cow c : this.cowList){ 51 | c.liveHour(); 52 | } 53 | } 54 | 55 | public void manageCows(){ 56 | this.cowHouse.takeCareOf(this.cowList); 57 | } 58 | 59 | public void installMilkingRobot(MilkingRobot milkingRobot){ 60 | this.cowHouse.installMilkingRobot(milkingRobot); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /week10/FarmSimulator/farmsimulator/Milkable.java: -------------------------------------------------------------------------------- 1 | package farmsimulator; 2 | 3 | public interface Milkable { 4 | double milk(); 5 | } 6 | -------------------------------------------------------------------------------- /week10/FarmSimulator/farmsimulator/MilkingRobot.java: -------------------------------------------------------------------------------- 1 | 2 | package farmsimulator; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class MilkingRobot { 9 | private BulkTank bulkTank; 10 | 11 | public MilkingRobot(){ 12 | 13 | } 14 | 15 | public BulkTank getBulkTank(){ 16 | if(this.bulkTank == null){ 17 | return null; 18 | }else{ 19 | return this.bulkTank; 20 | } 21 | } 22 | 23 | void setBulkTank(BulkTank tank){ 24 | this.bulkTank = tank; 25 | } 26 | 27 | void milk(Milkable milkable){ 28 | if(this.getBulkTank() == null){ 29 | throw new IllegalStateException("The MilkingRobot hasn't been installed"); 30 | }else{ 31 | this.bulkTank.addToTank(milkable.milk()); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /week10/Groups/Movable.java: -------------------------------------------------------------------------------- 1 | package movable; 2 | 3 | 4 | public interface Movable { 5 | 6 | void move(int dx, int dy); 7 | } 8 | -------------------------------------------------------------------------------- /week10/Groups/Organism.java: -------------------------------------------------------------------------------- 1 | 2 | package movable; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class Organism implements Movable{ 9 | private int x; 10 | private int y; 11 | 12 | public Organism(int x, int y){ 13 | this.x = x; 14 | this.y = y; 15 | } 16 | 17 | @Override 18 | public String toString(){ 19 | return "x: " + this.x + "; y: " + this.y; 20 | } 21 | 22 | @Override 23 | public void move(int dx, int dy){ 24 | this.x += dx; 25 | this.y += dy; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /week10/Groups/group.java: -------------------------------------------------------------------------------- 1 | 2 | package movable; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | 9 | import java.util.List; 10 | import java.util.ArrayList; 11 | 12 | public class Group implements Movable{ 13 | private List movables; 14 | 15 | public Group(){ 16 | this.movables = new ArrayList(); 17 | } 18 | 19 | @Override 20 | public String toString(){ 21 | String listString = ""; 22 | for(Movable m : this.movables){ 23 | listString += m.toString() + "\n"; 24 | } 25 | return listString; 26 | } 27 | 28 | 29 | public void addToGroup(Movable movable){ 30 | this.movables.add(movable); 31 | } 32 | 33 | @Override 34 | public void move(int dx, int dy){ 35 | for(Movable m : this.movables){ 36 | m.move(dx, dy); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /week10/PersonAndTheirHeirs/people/Person.java: -------------------------------------------------------------------------------- 1 | 2 | package people; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class Person { 9 | private String name; 10 | private String address; 11 | 12 | public Person(String name, String address){ 13 | this.name = name; 14 | this.address = address; 15 | } 16 | 17 | @Override 18 | public String toString(){ 19 | return this.name + "\n " + this.address; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /week10/PersonAndTheirHeirs/people/Student.java: -------------------------------------------------------------------------------- 1 | 2 | package people; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class Student extends Person{ 9 | private int credits; 10 | 11 | public Student(String name, String address){ 12 | super(name, address); 13 | this.credits = 0; 14 | } 15 | 16 | public void study(){ 17 | this.credits++; 18 | } 19 | 20 | public int credits(){ 21 | return this.credits; 22 | } 23 | 24 | @Override 25 | public String toString(){ 26 | return super.toString() + "\n credits " + this.credits; 27 | } 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /week10/PersonAndTheirHeirs/people/Teacher.java: -------------------------------------------------------------------------------- 1 | 2 | package people; 3 | 4 | /** 5 | * 6 | * @author giuseppedesantis 7 | */ 8 | public class Teacher extends Person{ 9 | private int salary; 10 | 11 | public Teacher(String name, String address, int salary){ 12 | super(name, address); 13 | this.salary = salary; 14 | } 15 | 16 | @Override 17 | public String toString(){ 18 | return super.toString() + "\n salary " + this.salary + " euros/month"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /week10/TheFinnishRingingCentre/Bird.java: -------------------------------------------------------------------------------- 1 | 2 | public class Bird { 3 | 4 | private String name; 5 | private String latinName; 6 | private int ringingYear; 7 | 8 | public Bird(String name, String latinName, int ringingYear) { 9 | this.name = name; 10 | this.latinName = latinName; 11 | this.ringingYear = ringingYear; 12 | } 13 | 14 | 15 | @Override 16 | public String toString() { 17 | return this.latinName + " (" + this.ringingYear + ")"; 18 | } 19 | 20 | @Override 21 | public boolean equals(Object object){ 22 | if(object == null){ 23 | return false; 24 | } 25 | if(this.getClass() != object.getClass()){ 26 | return false; 27 | } 28 | Bird compared = (Bird) object; 29 | if(this.latinName == null || !this.latinName.equals(compared.latinName)){ 30 | return false; 31 | } 32 | if(this.ringingYear != compared.ringingYear){ 33 | return false; 34 | } 35 | return true; 36 | } 37 | 38 | @Override 39 | public int hashCode(){ 40 | if(this.latinName == null){ 41 | return 7; 42 | } 43 | 44 | return this.ringingYear + this.latinName.hashCode(); 45 | } 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /week10/TheFinnishRingingCentre/RiningCentre.java: -------------------------------------------------------------------------------- 1 | public class RingingCentre 2 | --------------------------------------------------------------------------------