Java, average age, books, author, publication year, code snippet
This Java code calculates the average age of books written by a specific author, given a list of books and their publication years. The code iterates through the list, checks if the book's publication year matches the given year, and updates the author's book count and average age accordingly. If no books are found for the given year, the average age is set to -1.
[...] if( .getAuthor().getName() ==name I.getAuthor().getFirstName() ==first name) { nbBooks++; date= date+l.getPublication(); if (nbLivres==0) { averageAge=-1; else { averageAge=year-(date/nbLivres); return averageAge; 3.4 public HashMap createHashMap("int year) { HashMap< string > =new HashMap< string >(); //browse the list des books and retrieve all the books of the year data with their author Iterator iter = list of books.iterator(); while (iter.hasNext()) { Book = (Book) iterate. [...]
[...] Object-Oriented Programming and Java - Sample Partial Exam Exercise 1 - UML Modeling points, recommended time: 30 minutes) Exercise Program Analysis points, recommended time: 10 minutes) Exercise Java Programming points, recommended time: 30 minutes) Exercise 1 Exercise 2 : Voici le résultat affiché en console : Apple Juice 2.0 euros Sparkling Water 1.0 euros Bordeaux Wine 8.0 euros 19 degree Total price : 10.9 euros Exercice 3 : 3.1 import java.util.ArrayList; import java.util.Iterator; public class Library { private ArrayList< book > listBooks; public Bibliotheque() { listBooks = ArrayList(); public String toString() { String result=""; Iterator iter = list of Books.iterator(); while (iter.hasNext()) { result = result + iterate.next().toString() return result; 3.2 public void addition title,int year, String name,String first name) { //verification of existence you book boolean = false; Iterator iter = listLivres.iterator(); //des what we have found one book similar, on sort of the loop grace au boolean while (==false iter.hasNext()) { if iterate.next()).getTitre()==title) { =true; //addition you free only if it does not exist if (==false) { //creation of a author Author a = new Authorname,first name); //creation you book Book I = new Booktitle,a,year); //addition you book to the list list of Books.add(); 3.3 public int averageAge(int year,String name,String first name) { int average age; int nbLivres=0; int date=0; //journey de the list des books, yes the author is he/she/it sought, put on day the dates and the name of books of the author Iterator iter = listLivres.iterator(); while (iter.hasNext()) { Book = (Book) iterate. [...]
APA Style reference
For your bibliographyOnline reading
with our online readerContent validated
by our reading committee