Skip to content
Snippets Groups Projects
Commit 115f4b80 authored by antoine.hureau's avatar antoine.hureau
Browse files

Merge branch lecture_code:main into main

parents c84cbbd6 c866f179
No related branches found
No related tags found
No related merge requests found
Showing
with 1076 additions and 0 deletions
package lecture_18_repetition;
public class AirLock implements IAirLock{
private Door inner;
private Door outer;
public AirLock() {
inner = new Door();
outer = new Door();
}
Door getInner() {
return inner;
}
@Override
public void openInner() {
openDoor(inner);
}
@Override
public void openOuter() {
openDoor(outer);
}
private void openDoor(Door door) {
if(!inner.isOpen() && !outer.isOpen())
door.open();
else
System.out.println("Can not open both at the same time");
}
@Override
public void closeInner() {
inner.close();
}
@Override
public void closeOuter() {
outer.close();
}
@Override
public boolean innerIsOpen() {
return inner.isOpen();
}
@Override
public boolean outerIsOpen() {
return outer.isOpen();
}
@Override
public void lockInner() {
inner.lock();
}
@Override
public void unLockInner() {
inner.unLock();
}
@Override
public void lockOuter() {
outer.lock();
}
@Override
public void unLockOuter() {
outer.unLock();
}
public static void main(String[] args) {
AirLock doors = new AirLock();
doors.openOuter();
doors.getInner().open();
}
}
package lecture_18_repetition;
public class Door {
private boolean isOpen;
private boolean isLocked;
public void open() {
if(!isLocked)
isOpen = true;
}
public boolean isOpen() {
return isOpen;
}
public void close() {
isOpen=false;
}
public void lock() {
if(!isOpen)
isLocked=true;
}
public void unLock() {
isLocked = false;
}
}
package lecture_18_repetition;
/**
* An AirLock is a set of two doors where only one door be open at the time.
* We call the two doors inner and outer door.
*
* In addition each door has a lock, a locked door can not be opened before it is unlocked.
*
* @author Martin Vatshelle
*/
public interface IAirLock {
/** Opens the inner door if it is unlocked and it is allowed to open. */
public void openInner();
/** Opens the outer door if it is unlocked and it is allowed to open. */
public void openOuter();
/** Closes inner door if not already closed. */
public void closeInner();
/** Closes outer door if not already closed */
public void closeOuter();
/** @return true if innerDoor is open, false otherwise */
public boolean innerIsOpen();
/** @return true if outerDoor is open, false otherwise */
public boolean outerIsOpen();
/** locks the inner door if it is closed */
public void lockInner();
/** unlocks the inner door if it is locked */
public void unLockInner();
/** locks the outer door if it is closed */
public void lockOuter();
/** unlocks the outer door if it is locked */
public void unLockOuter();
}
package lecture_19_inheritance;
/**
* Class for storing various types of clothing;
* @author mva021
*
* This type of Object mainly stores information with brand, size and type being examples.
* Such variables are called field variables or instance variables.
* The collection of field variables is called the state of the Object
* These variables all should have get methods, and could have set methods if you want to be able to change the property.
* Brand typically don't change, unless you buy cheap clothing and replace the labels with proper brands in order to earn some extra bucks.
* The important part of a State is those field variables that can change.
*
* Which field variables can change in an object of this class?
*/
public class Clothing {
private String brand;
private int size;
private boolean isDirty;
private String type;
/**
* Constructor is called once for each time you want to make a new clothing item.
* Ideally all properties are set to reasonable default values in the constructor.
* In lecture we did not get the time to set all properties properly.
* @param brand
* @param size
* @param type
*/
public Clothing(String brand, int size, String type) {
this.brand = brand;
this.size = size;
//should probably check that input is a valid type.
//in a bigger application we should probably make type an Object on its own
//so we could have a better control that it was a proper clothingType rather than any string
this.type = type;
this.isDirty = false; //assume new clothing are clean?
}
public String getType() {
return this.type;
}
public String getBrand() {
return brand;
}
public int getSize() {
return this.size;
}
/**
* Method called by a "Person" object when jumpingInMuddyPuddle() method is activated
* Changes the state of this object, i.e. the field variable isDirty
*/
public void makeDirty(){
isDirty = true;
}
/**
* Method called by e.g. the "WashingMachine" object or "Maid" object
* Changes the state of this object, i.e. the field variable isDirty
*/
public void makeClean(){
isDirty = false;
}
/**
* Method used by the drawers, shelves e.g. to check if the item can be placed
* @return
*/
public boolean isDirty() {
return isDirty;
}
/**
* Method called by the "WashingMachine" object
* This is a method that changes the state of the Object by changing the field variable size.
*/
public void shrink() {
size = size-1;
}
}
package lecture_19_inheritance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class Drawer implements IStorage{
int height;
int width;
int depth;
Collection<Clothing> clothes;
public Drawer(int width, int depth, int height) {
clothes = new ArrayList<Clothing>();
}
public int getHeight(){
return this.height;
}
public int getWidth() {
return width;
}
public int getDepth() {
return depth;
}
public boolean add(Clothing clothing) {
if(canAdd(clothing)) {
clothes.add(clothing);
return true;
}else {
return false;
}
}
/**
* Drawers should only contain clean clothing, no shoes allowed in drawers.
*/
public boolean canAdd(Clothing item) {
if(item.isDirty())
return false;
if(item.getType().equals("Shoes"))
return false;
return true;
}
public boolean remove(Clothing item) {
if(clothes.contains(item))
{
clothes.remove(item);
return true;
}
else {
return false;
}
}
}
package lecture_19_inheritance;
/**
* IStorage space represents a place to put various clothing items.
* Shelf, Drawer and Bar was discussed in lecture.
* LaundryBin, PileOnFloor and Hook could be other classes to be added later
* @author mva021
*/
public interface IStorage {
/**
* Gets the height of the storage space, can be used to check if an item fits inside.
* @return
*/
public int getHeight();
/**
* Gets the width of the storage space, can be used to check if an item fits inside.
* @return
*/
public int getWidth();
/**
* Gets the depth of the storage space, can be used to check if an item fits inside.
* @return
*/
public int getDepth();
/**
* Places an Clothing object in this storage space
* @param clothing - The item to place in the Wardrobe
* @return true if the clothing can be added, false otherwise
*/
public boolean add(Clothing clothing);
/**
* Checks if a clothing fulfills the requirements for being placed.
* Dirty clothing should not end up in the shelves, but may end up in a LaundryBin
* Socks should not end up in shelves (unless they are in a Box which is placed in a Shelf)
* @param item an Clothing item
* @return true if the clothing can be added, false otherwise
*/
public boolean canAdd(Clothing item) ;
/**
* Tries to remove an item of clothing from the Wardrobe.
*
* @param item The clothing item to remove
* @return true if the item was found and removed, false otherwise.
*/
public boolean remove(Clothing item) ;
}
package lecture_19_inheritance;
import java.util.ArrayList;
import java.util.Collection;
public class Shelf implements IStorage{
int height;
int width;
int depth;
Collection<Clothing> clothes;
public Shelf(int width, int depth, int height) {
this.height = height;
this.width = width;
this.depth = depth;
clothes = new ArrayList<Clothing>();
}
public int getHeight(){
return this.height;
}
public int getWidth() {
return width;
}
public int getDepth() {
return depth;
}
public boolean add(Clothing clothing) {
if(canAdd(clothing)) {
clothes.add(clothing);
return true;
}else {
return false;
}
}
/**
* Shelves should only contain clean clothing.
* Socks should not be placed on shelves
*/
public boolean canAdd(Clothing item) {
if(item.isDirty())
return false;
if(item.getType().equals("Socks"))
return false;
return true;
}
public boolean remove(Clothing item) {
if(clothes.contains(item))
{
clothes.remove(item);
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
Shelf shelf = new Shelf(60,40,30);
if(shelf.add(new Clothing("Levis", 20,"Jeans")))
System.out.println("La den p� en hylle!");
else
System.out.println("Kunne ikke legge den p� hyllen");
}
}
package lecture_19_inheritance;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collections;
public class SortedListAbstract extends AbstractList<Integer>{
ArrayList<Integer> list;
public SortedListAbstract() {
list = new ArrayList<>();
}
@Override
public Integer get(int index) {
return list.get(index);
}
@Override
public int size() {
return list.size();
}
@Override
public Integer set(int index, Integer element) {
int num = list.set(index, element);
Collections.sort(list);
return num;
}
@Override
public void add(int index, Integer element) {
list.add(index, element);
Collections.sort(list);
}
@Override
public boolean remove(Object o) {
return list.remove(o);
}
}
package lecture_19_inheritance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class SortedListComposition{
//fields
ArrayList<Integer> list;
public SortedListComposition() {
list = new ArrayList<Integer>();
}
public Integer get(int index) {
return list.get(index);
}
public boolean add(Integer e) {
list.add(e);
Collections.sort(list);
return true;
}
//This code is already done
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains(Object o) {
return list.contains(o);
}
public Iterator<Integer> iterator() {
return list.iterator();
}
public Object[] toArray() {
return list.toArray();
}
public <T> T[] toArray(T[] a) {
return list.toArray(a);
}
public boolean remove(Object o) {
return list.remove(o);
}
public boolean containsAll(Collection<?> c) {
return list.containsAll(c);
}
public boolean addAll(Collection<? extends Integer> c) {
list.addAll(c);
Collections.sort(list);
return true;
}
public boolean addAll(int index, Collection<? extends Integer> c) {
list.addAll(index,c);
Collections.sort(list);
return true;
}
public boolean removeAll(Collection<?> c) {
return list.removeAll(c);
}
public boolean retainAll(Collection<?> c) {
return list.retainAll(c);
}
public void clear() {
list.clear();
}
public Integer set(int index, Integer element) {
return list.set(index, element);
}
public void add(int index, Integer element) {
list.add(index,element);
Collections.sort(list);
}
public Integer remove(int index) {
return list.remove(index);
}
public int indexOf(Object o) {
return list.indexOf(o);
}
public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}
public ListIterator<Integer> listIterator() {
return list.listIterator();
}
public ListIterator<Integer> listIterator(int index) {
return list.listIterator(index);
}
public List<Integer> subList(int fromIndex, int toIndex) {
return subList(fromIndex, toIndex);
}
public String toString() {
return list.toString();
}
}
package lecture_19_inheritance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class SortedListInheritance extends ArrayList<Integer>{
@Override
public boolean add(Integer e) {
super.add(e);
Collections.sort(this);
return true;
}
}
package lecture_19_inheritance;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class UseSortedList {
public static void main(String[] args) {
//List<Integer> numbers = new ArrayList<Integer>();
//List<Integer> numbers = new SortedListInheritance();
SortedListComposition numbers = new SortedListComposition();
//List<Integer> numbers = new SortedListAbstract();
Random rand = new Random();
for(int i=0; i<10; i++) {
numbers.add(rand.nextInt(100));
System.out.println(numbers);
}
}
}
package lecture_19_inheritance;
import java.util.ArrayList;
import java.util.List;
public class Wardrobe {
private List<IStorage> storage;
public Wardrobe() {
storage = new ArrayList<IStorage>();
}
/**
* Installs a new shelf in this Wardrobe
* @param width how wide the shelf is
* @param depth how deep the shelf is
* @param height how high the shelf is
*/
public void BuildShelf(int width, int depth, int height) {
storage.add(new Shelf(width,depth,height));
}
/**
* Installs a new drawer in this Wardrobe
* @param width how wide the drawer is
* @param depth how deep the drawer is
* @param height how high the drawer is
*/
public void BuildDrawer() {
storage.add(new Drawer(60,60,15));
}
/**
* Adds an item of clothing to this wardrobe
* @param item the clothing to add
* @return true if there was a space for this item and this item was successfully added, false otherwise
*/
public boolean put(Clothing item){
for(IStorage place : storage) {
if(place.add(item)) {
return true;
}
}
return false;
}
/**
* Takes out an item from the wardrobe
* @param item the item wanted
* @return true if item was found and removed, false otherwise
*/
public boolean remove(Clothing item) { //changed name to remove the name get was not very clear
for(IStorage place : storage) {
if(place.remove(item)) {
return true;
}
}
return false;
}
/**
* A method to build an sample Wardrobe (a bit modest to be a dream wardrobe...)
* A method similar to this one should probably be made such that the user can give user input
* @return A Wardrobe ready to store Clothing objects
*/
public static Wardrobe buildDreamWardrobe() {
Wardrobe myWardrobe = new Wardrobe();
for(int i=1;i<=10;i++) {
myWardrobe.BuildShelf(60,60,30);
}
return myWardrobe;
}
/**
* Just a test that this works, should probably make JUnit tests instead of main method
* @param args
*/
public static void main(String[] args) {
Wardrobe myWardrobe = Wardrobe.buildDreamWardrobe();
System.out.println(myWardrobe.storage.size());
}
}
package lecture_20_exams.corona;
import java.time.LocalDate;
import java.util.ArrayList;
/**
* This class represents the death tolls related to covid-19 reported by a country.
* Deaths are reported one day at the time.
*
* In order to use this data with an existing framework for generating plots
* we need to be able to output this data in ArrayLists
*
* @author Martin Vatshelle - martin.vatshelle@uib.no
*
*/
public interface ICoronaData {
/**
* Returns the dates on which coronaDeaths was reported for this data set(country).
* These dates shall be reported in order, the earliest day first and the most recent date last.
* @return An ArrayList of dates
*/
public ArrayList<LocalDate> getDates();
/**
* Each day a certain number of deaths is reported, this method returns all reported numbers so far.
* The order of the numbers will be the same order as the dates returned by getDates()
* @return an ArrayList of daily death count for each day
*/
public ArrayList<Integer> getDailyDeaths();
/**
* Each ICoronaData series represents a country
* @return name of the country
*/
public String getCountryName();
/**
* The population will be a positive number
* @return the population (number of people) of the country
*/
public int getPopulation();
/**
* For each day return the total number of deaths up until and including that day
* @return an ArrayList containing the cumulative sum of deaths
*/
public ArrayList<Integer> cumulativeDeaths();
/**
* As large countries are expected to have more deaths than small countries,
* a good measure of how hard hit a country is could be how many deaths per million inhabitants
* For each day compute how many deaths (up til and including this day) per million the country has.
*
* Example:
* If there is 2 million people and 5 deaths you should return 2.5
* If there is 3.5 million people and 7 deaths you should return 2
*
* @return a list containing for each day how many deaths there are per million people
*/
public ArrayList<Double> deathsPerMillion();
}
package lecture_20_exams.library;
public class Book {
String title;
String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
}
package lecture_20_exams.library;
import java.util.ArrayList;
public class Library{
ArrayList<Book> books = new ArrayList<Book>();
Library(){
//add books to the library
books.add(new Book("Effective Java", "Joshua Bloch"));
books.add(new Book("Clean Code", "Robert Martin"));
books.add(new Book("Head First Design Patterns", "Elisabeth Robson"));
}
/**
* Checks if the library has the book and prints a message
* @param book
*/
public void hasBook(Book book) {
if(books.contains(book))
System.out.println("Yes I can read this over summer!");
else
System.out.println("Oh no, I will not be an Java expert!");
}
public static void main(String[] args) {
Library UiBLib = new Library();
Book wanted = new Book("Clean Code", "Robert Martin");
UiBLib.hasBook(wanted);
}
}
package lecture_20_exams.music;
import java.util.List;
/**
* This class contains two different methods for sorting
*
* @author Martin Vatshelle
*/
public class MusicSorter {
/**
* Sorts the list according to the alphabetic order of Song::getArtistName()
* @param music - the list of songs to be sorted
*/
public static void sortByArtist(List<Song> music) {
//TODO: implement this
}
/**
* Sorts the list according to the Date of Song::getReleaseDate()
* @param music - the list of songs to be sorted
*/
public static void sortByReleaseDate(List<Song> music) {
//TODO: implement this
}
}
\ No newline at end of file
package lecture_20_exams.music;
import java.util.Date;
/**
* This class describes a song
* @author Martin Vatshelle
*/
public class Song {
private String artist;
private String title;
private String genre;
private Date releaseDate;
public Song(String artist, String title, String genre, Date releaseDate) {
this.artist = artist;
this.title = title;
this.genre = genre;
this.releaseDate = releaseDate;
}
public String getArtist() {
return artist;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public Date getReleaseDate() {
return releaseDate;
}
}
package lecture_20_exams.scrabble;
public class Scrabble {
/**
* This method is used by the game Scrabble where the goal is to rearrange
* some of the given letters into a word.
* A letter in Scrabble is always upper case, this method accepts both upper and lower case letters
* and considers e.g. a lower case 'a' equal to an upper case 'A'
* You may assume that all letters in input are valid letters in the English alphabet.
*
* @param letters - the letters you have to your disposal
* @param word - the word you are trying to form
* @return true if it is possible to form the word by rearranging the letters, false otherwise
*/
public static boolean canMake(String letters, String word) {
letters = letters.toUpperCase();
word = word.toUpperCase();
for(Character c:word.toCharArray()) {
if(!letters.contains(c.toString())) {
return false;
}
}
return true;
}
}
\ No newline at end of file
package lecture_20_exams.scrabble;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public class ScrabbleTest {
@Test
void testCanMakeEksamen(){
assertTrue(Scrabble.canMake("SKAMENE", "EKSAMEN"));
assertTrue(Scrabble.canMake("skaMENE", "EKSAMEN"));
assertTrue(Scrabble.canMake("SKAMENE", "eksAMEN"));
}
@Test
void testCanNotMakeFerie(){
assertFalse(Scrabble.canMake("SKAMENE", "FERIE"));
}
}
package lecture_18_repetition;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class AirLockTest {
IAirLock doors;
@BeforeEach
void setup() {
doors = new AirLock();
}
@Test
void testDoorsStartsAsClosed() {
assertFalse(doors.innerIsOpen(),"Doors should start as closed.");
assertFalse(doors.outerIsOpen(),"Doors should start as closed.");
}
@Test
void testDoorsCanInitiallyBeOpened() {
doors.openInner();
assertTrue(doors.innerIsOpen());
doors = new AirLock();
doors.openOuter();
assertTrue(doors.outerIsOpen());
}
@Test
void testDoorsCanBeOpenedAndClosed() {
doors.openInner();
assertTrue(doors.innerIsOpen());
doors.closeInner();
assertFalse(doors.innerIsOpen());
doors.openOuter();
assertTrue(doors.outerIsOpen());
doors.closeOuter();
assertFalse(doors.outerIsOpen());
}
@Test
void testDoorsCannotBothBeOpen() {
doors.openInner();
assertTrue(doors.innerIsOpen());
doors.openOuter();
assertFalse(doors.outerIsOpen());
doors = new AirLock();
doors.openOuter();
assertTrue(doors.outerIsOpen());
doors.openInner();
assertFalse(doors.innerIsOpen());
}
@Test
void testLookedDoorsCanNotOpen() {
doors.lockInner();
doors.openInner();
assertFalse(doors.innerIsOpen());
doors.lockOuter();
doors.openOuter();
assertFalse(doors.outerIsOpen());
doors.unLockInner();
doors.openInner();
assertTrue(doors.innerIsOpen());
}
@Test
void testUnLock() {
doors.lockInner();
doors.lockOuter();
doors.openInner();
assertFalse(doors.innerIsOpen());
doors.openOuter();
assertFalse(doors.outerIsOpen());
doors.unLockInner();
doors.openInner();
assertTrue(doors.innerIsOpen());
doors.closeInner();
doors.openOuter();
assertFalse(doors.outerIsOpen());
doors.lockInner();
doors.unLockOuter();
doors.openOuter();
assertTrue(doors.outerIsOpen());
doors.closeOuter();
doors.openInner();
assertFalse(doors.innerIsOpen());
}
@Test
void testCanNotLockOpenDors() {
doors.openInner();
doors.lockInner();
assertTrue(doors.innerIsOpen());
doors.closeInner();
assertFalse(doors.innerIsOpen());
doors.openInner();
assertTrue(doors.innerIsOpen());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment