Question 2 (15 marks) As we have done for Question 1 of Assignment 4, reverse en
ID: 3710107 • Letter: Q
Question
Question 2 (15 marks) As we have done for Question 1 of Assignment 4, reverse engineer the memory diagram below: you need t provide the implementation of all the classes, instance variables, constructors and the main method that wil lead to that memory state. Hint: You need a class Book and a class Author, as well as a main method. args myLibrary Sz 0array ot Les miserables" title year 1$62 author title title Barney's Version author aut Author Author nane irth 1302Vietor Hogo death 1115 birth 191Mordeeal Kiehler death 2001Explanation / Answer
ANS:-
Given that,
Here is the code for Author.java:
class Author
{
String name;
int birth;
int death;
public Author(String nm, int b, int d)
{
name = nm;
birth = b;
death = d;
}
public String getName() { return name; }
public int getBirth() { return birth; }
public int getDeath() { return death; }
public void setName(String nm) { name = nm; }
public void setBirth(int b) { birth = b; }
public void setDeath(int d) { death = d; }
}
Book.java:
class Book
{
String title;
int year;
Author author;
public Book(String t, int y, Author a)
{
title = t;
year = y;
author = a;
}
public String getTitle() { return title; }
public int getYear() { return year; }
public void setTitle(String t) { title = t; }
public void setYear(int y) { year = y; }
}
And Library.java:
class Library
{
public static void main(String[] args)
{
Book[] myLibrary = new Book[Integer.parseInt(args[0])];
Author a1 = new Author("Victor Hugo", 1802, 1885);
myLibrary[0] = new Book("Les miserables", 1862, a1);
myLibrary[1] = new Book("Lane", 1880, a1);
Author a2 = new Author("Mordecai Richler", 1931, 2001);
myLibrary[2] = new Book("Barney's Version", 1997, a2);
}
}
And the execution should pass a command line argument 5 like this:
$java Library 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.