Results 1 to 3 of 3
- 11-04-2011, 11:11 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 7
- Rep Power
- 0
help with java code - new to java
Hi All
I am creating a class called Book which contains getters and setters for book attributes.
now when I create new objects in my other class, I doJava Code:class Book { private String title; private String ISBN; //etc I have a constructor used as the setter as follows: public Book (String myTitle, String myISBN, String ....){ this.title = myTitle; this.ISBN = myISBN; } within the class i created getters as follows: public String getTitle(){ return title; }//end method getTitle public String getISBN(){ return ISBN; }
Book Book1 = new Book {"title123","8888",etc}
All this is working well so far.
now I know i can get each attribute of the book by Book1.getTitle() but how do I get ALL the attributes of each book in one so that i can refer to Book1 as an object rather than referring to each attribute of Book1.
I need this to store all my books in an ArrayList, print them, sort them in different orders etc.
thanksLast edited by sunde887; 11-04-2011 at 11:48 PM. Reason: Added code tags, [code]...[/code]
- 11-04-2011, 11:28 PM #2
Member
- Join Date
- Aug 2011
- Posts
- 54
- Rep Power
- 0
Re: help with java code - new to java
Please post full code and your current output.
- 11-04-2011, 11:51 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: help with java code - new to java
One nitpick and then some help: When naming variables use camel case (first letter first word lowercase, first letter of 2nd+ words capitalized), so book1, not Book1. Classes have the first letter of each word capitalized.
If you want to do things with the variable, you would use the getters, however; if you simply want to neatly print a list of books, look into overriding the toString method.
Java Code:@Override public String toString(){ //do stuff here return someString; }
Similar Threads
-
Equivalent code in Java native access(JNA)? Type casting of Pointer to java
By Sanjeev in forum Advanced JavaReplies: 1Last Post: 07-14-2011, 01:14 AM -
Is it possible to compile Java-code when running a Java-program?
By Toll in forum Advanced JavaReplies: 13Last Post: 06-29-2011, 07:25 AM -
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Convert java code to midlet code
By coldvoice05 in forum New To JavaReplies: 1Last Post: 08-12-2009, 11:14 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks