Skip to content
Snippets Groups Projects
Commit 36a56d87 authored by Martin Vatshelle's avatar Martin Vatshelle
Browse files

Code after lecture

parent 1f00a600
No related branches found
No related tags found
No related merge requests found
package lecture5.objects;
public class Book {
//feltvariabler
String title;
String author;
public Book(String title, String author){
this.title = title;
this.author = author;
}
@Override
public String toString() {
return title+" by "+author;
}
}
package lecture5.objects;
import java.util.ArrayList;
public class Library {
ArrayList<Book> books;
public Library() {
books = new ArrayList<Book>();
}
void add(Book book) {
books.add(book);
}
boolean hasBook(Book book) {
return books.contains(book);
}
public static void main(String[] args) {
Library myBooks = new Library();
System.out.println(myBooks.books);
Book myBook = new Book("Objects","Martin");
myBooks.add(myBook);
System.out.println(myBooks.books);
Book myFavorite = new Book("INF101", "Sondre");
System.out.println(myBooks.hasBook(myFavorite));
myBooks.add(myFavorite);
System.out.println(myBooks.hasBook(myFavorite));
System.out.println(myBooks.books);
Book wanted = new Book("Objects","Martin");
System.out.println(myBooks.hasBook(wanted));
}
}
......@@ -60,7 +60,8 @@ public class PersonsObjects {
public static void printAll(ArrayList<Person> persons) {
for(int i=0; i<persons.size(); i++) {
Person p = persons.get(i);
System.out.println(p.name+" is "+ p.age+" years.");
//System.out.println(p.name+" is "+ p.age+" years.");
System.out.println(p);
}
}
}
......
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