Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • adil.obol/lecture_code
  • ii/inf101/24v/students/lecture_code
  • Anders.Tokje/lecture_code
  • antoine.hureau/lecture_code_300124
  • Rein.Landmark/lecture-code-1-30-24
  • Simon.Alvsaker/lecture_code
  • stine.kristoffersen/lecture_code
  • Tyra.Eide/lecture_code
8 results
Show changes
Commits on Source (2)
  • Anders's avatar
    fixed · 192a8794
    Anders authored
    192a8794
  • Anders's avatar
    jeh · 753bab5c
    Anders authored
    753bab5c
package lecture5.objects;
public class Book {
String title;
String author;
public Book(String title, String author){
this.title = title;
this.author = author;
}
public String toString(){
return String.format("%s - %s", this.title, this.author);
}
public boolean equals(Object obj){
}
}
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);
myBooks.add(new Book("Hvordan være tøff", "Anders"));
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("Hvordan være tøff", "Anders");
System.out.println(myBooks.hasBook(wanted));
}
}
......@@ -33,7 +33,7 @@ public class Persons {
*/
private static void removeLast(ArrayList<String> names, ArrayList<Integer> ages) {
names.remove(names.size()-1);
ages.remove(names.size()-1);
ages.remove(ages.size()-1);
}
/**
......
package lecture5.objects;
import java.util.ArrayList;
/**
* This program keeps track of a list of persons names and ages.
*
* @author Martin Vatshelle
*/
public class PersonsObjects {
public static void main(String[] args) {
ArrayList<Person> persons = new ArrayList<Person>();
fill(persons);
printAll(persons);
removeLast(persons);
System.out.println();
printAll(persons);
}
/**
* Persons are stored in 2 lists,
* index i in names gives the name of person i and
* index i in ages gives the age of person i
*
* This method removes the last person from the list
*
* @param names a list of names
* @param ages a list of ages
*/
private static void removeLast(ArrayList<Person> persons) {
persons.remove(persons.size()-1);
}
/**
* Fills two lists with the same number of elements.
* names.get(i) and ages.get(i) are the name and age of person i
*
* @param names The List of names to be filled
* @param ages The List of ages to be filled
*/
public static void fill(ArrayList<Person> persons) {
persons.add(new Person("Anna", 12));
persons.add(new Person("Per", 3));
persons.add(new Person("Hans", 7));
persons.add(new Person("Lise", 9));
}
/**
* Prints out the names and ages of a list of Persons
*
* @param names
* @param ages
*/
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);
}
}
}