Using set/get methods and arrays
Hi guys, forgive me if breaking all sorts of laws in Java, I'm only a couple weeks into learning it and I've been struggling the whole way through. Right now what I was trying to do was create an array using the titles of five books and to test to see if my approach was working I tried to output the result. It seems like there's an error in my set/get methods. The code compiles, but my output is null.
So my real question is, can I do what I'm trying to achieve? That is, pull array variables from set/get methods? Is there a better approach or would this work (note: I'm supposed to have get/set methods so those have to stay, I'm struggling with my constructors and arrays at the moment) I'll be working with this some more while I wait for a response so if I figure it out first I'll update with a solved tag and the code I end up using to do so, thanks!
Code java:
import java.util.Arrays;
public class BookStore2 {
public static void main(String[] args) {
Book Book1 = new Book("978-0-7653-6264-3", "Wizard's First Rule", "Terry Goodkind", 1994, "Tom Doherty Associates, LLC", 7.99f);
Book Book2 = new Book("0-812-54809-4", "Stone of Tears", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
Book Book3 = new Book("0-812-55147-8", "Blood of the Fold", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
Book Book4 = new Book("0-439-15411-1", "Dracula", "Bram Stroker", 1897, "Scholastic Inc", 4.99f);
Book Book5 = new Book("0-440-94060-5", "I Am The Cheese", "Robert Cormier", 1977, "Dell Laurel-Leaf", 5.50f);
String[] titleArray = new String[] {Book1.gettitle, Book2.gettitle, Book3.gettitle, Book4.gettitle, Book5.gettitle};
System.out.println(Arrays.toString(titleArray));
}
}
class Book{
String ISBN;
String title;
String name;
int year;
String publisher;
float price;
String gettitle; // removed, this must have been part of the problem I was having
public Book(String ISBN, String title, String name, int year, String publisher, float price){
this.ISBN = ISBN;
this.title = title;
this.name = name;
this.year = year;
this.publisher = publisher;
this.price = price;
}
public void setISBN(String ISBN){
this.ISBN = ISBN;
}
String getISBN(){
return ISBN;
}
public void settitle(String title){
this.title = title;
}
String gettitle(){
return title;
}
public void setname(String name){
this.name = name;
}
String getname(){
return name;
}
public void setyear(int year){
this.year = year;
}
int getyear(){
return year;
}
public void setpublisher(String publisher){
this.publisher = publisher;
}
String getpublisher(){
return publisher;
}
public void setprice(float price){
this.price = price;
}
float getprice(){
return price;
}
// list of getters
} // end class book
Updated code:
Code java:
import java.util.Arrays;
public class BookStore2 {
public static void main(String[] args) {
Book book1 = new Book("978-0-7653-6264-3", "Wizard's First Rule", "Terry Goodkind", 1994, "Tom Doherty Associates, LLC", 7.99f);
Book book2 = new Book("0-812-54809-4", "Stone of Tears", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
Book book3 = new Book("0-812-55147-8", "Blood of the Fold", "Terry Goodkind", 1995, "Tom Doherty Associates, LLC", 7.99f);
Book book4 = new Book("0-439-15411-1", "Dracula", "Bram Stroker", 1897, "Scholastic Inc", 4.99f);
Book book5 = new Book("0-440-94060-5", "I Am The Cheese", "Robert Cormier", 1977, "Dell Laurel-Leaf", 5.50f);
String[] titleArray = new String[] {book1.gettitle(), book2.gettitle(), book3.gettitle(), book4.gettitle(), book5.gettitle()};
System.out.println(Arrays.toString(titleArray));
System.out.println(book1.gettitle());
}
}
class Book{
String ISBN;
String title;
String name;
int year;
String publisher;
float price;
public Book(String ISBN, String title, String name, int year, String publisher, float price){
this.ISBN = ISBN;
this.title = title;
this.name = name;
this.year = year;
this.publisher = publisher;
this.price = price;
}
public void setISBN(String ISBN){
this.ISBN = ISBN;
}
String getISBN(){
return ISBN;
}
String gettitle(){
return title;
}
public void settitle(String title){
this.title = title;
}
public void setname(String name){
this.name = name;
}
String getname(){
return name;
}
public void setyear(int year){
this.year = year;
}
int getyear(){
return year;
}
public void setpublisher(String publisher){
this.publisher = publisher;
}
String getpublisher(){
return publisher;
}
public void setprice(float price){
this.price = price;
}
float getprice(){
return price;
}
// list of getters
} // end class book
Re: Using set/get methods and arrays
Quote:
my output is null.
What variable are you printing that has a null value? Where do you give that variable a value so that it is not null? If you don't assign it a value it will keep the null default value.
It is confusing to name a variable and a method with the same exact name. Can you change one of the names to make them unique?
Re: Using set/get methods and arrays
Okay, I found part of my problem, instead of writing this code:
Code java:
String[] titleArray = new String[] {Book1.gettitle, Book2.gettitle, Book3.gettitle, Book4.gettitle, Book5.gettitle};
System.out.println(Arrays.toString(titleArray));
System.out.println(Book1.gettitle);
I changed it to:
Code java:
String[] titleArray = new String[] {Book1.title, Book2.title, Book3.title, Book4.title, Book5.title};
System.out.println(Arrays.toString(titleArray));
System.out.println(Book1.title);
To answer your questions Norm, the variable I was trying to get to print is the title variable from my Book class. The variables should be assigned using the constructors that you see for Book1 - Book5.
As for the second part of your question, I am uncertain what you mean by a variable and method with the same name maybe I am reading it wrong or understanding it wrong, but they appear to be all with different names?
With the exclusion of, I found a duplicate gettitle line in my code near the beginning of the Book class, I removed that as I assume it was part of the problem.
Edit: I see what you mean, you meant the variable gettitle and the method gettitle. Yeah, the variable for gettitle was not supposed to be there, it was removed once I found that it was there and then correcting the code to title instead of gettitle resulted in the correct output.
Re: Using set/get methods and arrays
Quote:
a variable and method with the same name
See gettitle: It's a String and the name of a method,
Where does the code use any of the get methods? It should use a method (book1.getTitle()) to access a class value and not access the class's variable directly like this: Book1.title
Note: variable names should start with lowercase letters: book1 vs Book1
Re: Using set/get methods and arrays
Quote:
Originally Posted by
Norm
See gettitle: It's a String and the name of a method
I removed the String already, as it was part of my confusion, that was the reason for producing a null value instead of a syntax error. I also made adjustments to the variable names to help readability and you pointed out the error that caused all of this to begin with. I wasn't understanding why it wasn't calling on the get methods, and it was because I left out the parenthesis (book1.gettitle()) at the end.
I'll probably be asking for more help soon, this stuff gives me one heck of a headache.
I will update the thread as solved, I don't think there is much that can be asked here and still remain on-topic.
Thank you, Norm.