Reputation: 347
I am trying to solve this problem:
A Book, which as a title, author, and year of publication. Include methods to get and set the private instance variables and a toString method to display the object. Also create a method moreRecent which takes two books as input parameters and returns the one that was published more recently. Create 3 JUnit tests for moreRecent.
However, I don't know how to access the other private instance variables author and yearofPub. I can only do one constructor per class right? So how do I get the other 2 variables? This is my code so far: Thanks!
class Book {
private String title;
private String author;
private String yearofPub;
Book(String input) {
title = input;
}
String moreRecent(int Book1, int Book2) {
String moreRecent;
if(Book1 > Book2) {
moreRecent = "Book 1";
}
if (Book2 > Book1) {
moreRecent = "Book 2";
}
else
moreRecent = "Both";
return moreRecent;
}
}
public class Chpt2930BookTitleAuthorYear {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter title of Book: ");
String nextLine = scan.next();
Book titleofBook = new Book(nextLine);
}
}
EDIT--------------------------
Ok thanks for all the responses! I improved my code further but am still not able to get a correct result. I'm especially having difficulty with the method moreRecent. How do I set the yearofPub for those books? Here is my code:
class Book {
private String title;
private String author;
private String yearofPub;
Book(String input) {
title = input.toString();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYearofPub() {
return yearofPub;
}
public void setYearofPub(String yearofPub) {
this.yearofPub = yearofPub;
}
public String toString() {
System.out.println("Book{" + "Title=" + title + " Author=" + author + " Year of
Publication=" + yearofPub + "}");
return " ";
}
//confused on this part :(
String moreRecent(Book Book1, Book Book2) {
if (Book1.getYearofPub>Book.getYearofPub) { //Is this correct? I'm not sure :/
}
}
}
public class Chpt2930BookTitleAuthorYear {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter title of Book: ");
String nextLineTitle = scan.nextLine();
Book titleofBook = new Book(nextLineTitle);
System.out.println("Enter author of the Book: ");
String nextLineAuthor = scan.nextLine();
titleofBook.setAuthor(nextLineAuthor);
System.out.println("Enter the date of publication of the Book: ");
String nextLineDate = scan.nextLine();
titleofBook.setYearofPub(nextLineDate);
titleofBook.toString();
}
}
Thanks!
EDIT 2-----------------
Nvm'd I found the problem :) Thanks for all the help though
Upvotes: 2
Views: 457
Reputation: 789
Do this:
class Book {
private String title;
private String author;
private String yearofPub;
Book(String input) {
title = input;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYearofPub() {
return yearofPub;
}
public void setYearofPub(String yearofPub) {
this.yearofPub = yearofPub;
}
}
And in your main method, you can:
Book titleofBook = new Book(nextLine);
titleofBook.setAuthor(...)
Upvotes: 2
Reputation: 62874
Provide the so called accessors for the variables. For example:
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
Same goes for the yeaofPub
field.
Upvotes: 1