Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Linking class's

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Linking class's

    Hi I have the following three class's:

    public class Book {
     
     
    	String bookTitle;    //attributes
    	String author;
    	double price;
    	int isbn;
     
    	public Book(){
     
     
    	}       //empty constructor
     
    	public Book(String bookTitle, String author, double price, int isbn){ //constructor that takes parameters
    		this.bookTitle=bookTitle;
    		this.author=author;
    		this.price=price;
    		this.isbn=isbn;
     
    	}
     
    	public String getBookTitle(){   //get method that returns the book title
    		return bookTitle;
    	}
    	public String getAuthor(){     //get method that returns the author
    		return author;
    	}
    	public double getPrice(){  //set method to alter the attribute of price
    		return price;
    	}
    	public int getIsbn(){             //get method that returns the isbn number
    		return isbn;
    	}
    	public void setBookTitle(String bookTitle){    //set method that alters the book attribute
    		this.bookTitle=bookTitle;
    		}
    	public void setPrice(double price){          //set method that allows the price attribute to be changed
    		this.price=price;
    	}
    }


     
     
    public class TestBook {
     
     
    	public static void main(String[] args) {
    		Book book1= new Book("how to blar","tad blar",50,456789);
    		Book book2= new Book("how to pass programming","David Rayner",20,567824); //method to create objects
    		Book book3= new Book("How to fail programming","ivan blar",30,577437);
     
    		book1.setBookTitle("blar allot");     //code to alter name of the book
    		book1.setPrice(70);                   //code to alter price of book
     
    		System.out.println(book1.getBookTitle()+" "+
    							book1.getAuthor()+" "+     //code to display each attributes of each object
    							book1.getPrice()+" "+
    							book1.getIsbn());
     
     
     
     
    		System.out.println(book2.getBookTitle()+" "+
    				book2.getAuthor()+" "+
    				book2.getPrice()+" "+
    				book2.getIsbn());
     
    		System.out.println(book3.getBookTitle()+" "+
    				book3.getAuthor()+" "+
    				book3.getPrice()+" "+
    				book3.getIsbn());
     
     
    	}
     
    }

     
    public class author {
     
    	String firstName;
    	String lastName;
    	String nationality;
     
    	author(){}
     
    	author(String firstName, String lastName, String nationality){
    		this.firstName=firstName;
    		this.lastName=lastName;
    		this.nationality=nationality;
     
    	}
     
    	public String getFistName(){
    		return firstName;
    	}
     
    	public String getLastName(){
    		return lastName;
    	}
     
    	public String getNationality(){
    		return nationality;
    	}
     
    	public void setFirstname(String firstName){
    		this.firstName=firstName;
    	}
    	public void setLastName(String lastName){
    		this.lastName=lastName;
    	}
    	public void setNationality(String nationality){
    		this.nationality=nationality;
    	}
     
    }


    Now my task is the following:

    Now design (using a class diagram) and build an Author class to contain information about an Author, for example the firstname, lastname and nationality. Then modify the Book class so that a book contains a single author object.
    Modify your original book test class to make sure book and author now work together properly.


    The problem i'm having is modifying the Book class so it contains a single author object, I just can't seem to figure out what i need to put and where I need to put it in the book class, i'm sure its probably something simple as it always turns out to be, but if someone could give me a piont in the right direction, i would greatly appriciate any help.

    Thankyou in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Linking class's

    Book class so it contains a single author object
    Classnames should start with capital letters!
    Have you tried defining one instance of an Author in the Book class?
    You have a String variable: name author defined there,
    what is the problem with adding an Author variable there?
    Same syntax: <datatype> <variable name>

  3. The Following User Says Thank You to Norm For This Useful Post:

    Tate (February 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Location
    Outside Chicago, IL
    Posts
    5
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Linking class's

    I'm just learning Java, so this may be entirely wrong. What I'd try is to make your string author declarations as Author author (in your book class -- assuming your Author class name is capitalized as Norm suggests). Then, in the constructor of your book class, I would show the parameters as
    public Book(String bookTitle, Author author, double price, int isbn) {...
    And the getAuthor of your BookClass: public Author getAuthor(){

    What do you think?

    Regards,

    grNadpa

  5. The Following User Says Thank You to grNadpa For This Useful Post:

    Tate (February 23rd, 2012)

  6. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Linking class's

    I figured it out it was just creating the instance varaible for the auther class that I was really missing once I got that it was straight forward enough and i've done it pritty much how you've suggested.

    Thankyou for the help and advice.

Similar Threads

  1. Replies: 1
    Last Post: October 11th, 2011, 09:55 AM
  2. Re: linking two or more java files using menus
    By simonphilip102 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2011, 06:08 AM
  3. Linking two JLists
    By mib1bee in forum AWT / Java Swing
    Replies: 2
    Last Post: December 30th, 2010, 01:19 PM
  4. Database connection using NetBeans
    By jcc285 in forum Java IDEs
    Replies: 6
    Last Post: June 9th, 2009, 03:23 AM
  5. [SOLVED] How to link two different class?
    By John in forum Object Oriented Programming
    Replies: 11
    Last Post: April 27th, 2009, 02:57 PM