Oct 14, 2015 01:32 PM
(This post was last modified: Oct 14, 2015 01:33 PM by Paathurnax.)
I've got two classes set up, and I need to call three variables from one class into another class, but I'm not sure how to do it.
Here's the two classes.
What I need to do is call the variables postcode, town and street from the Address class into the Borrower class using one method. I'm trying to use Address address = new Address(); but that's not working and I'm not sure why.
I also need to initialise an attribute called Address in the Borrower class using the information given to the constructor in the Address class.
Would really appreciate some help here.
Here's the two classes.
Code
public class Borrower
{
//delcaring variables
private String firstName;
private String lastName;
private String libraryNumber;
private int noOfBooks;
//constructor for automatically assuming new borrower will borrow a single book
public Borrower(String name, String surname, String libraryID)
{
firstName = name;
lastName = surname;
libraryNumber = libraryID;
noOfBooks = 1;
postcode = new Address();
}
//constructor to allow user to manually input number of loaned books
public Borrower(String name, String surname, String libraryID, int booksLoaned)
{
firstName = name;
lastName = surname;
libraryNumber = libraryID;
noOfBooks = booksLoaned;
}
//simple accessors
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getLibraryNumber()
{
return libraryNumber;
}
public int getnoOfBooks()
{
return noOfBooks;
}
//methods for increasing/decreasing loaned books by 1 and manually incrementing numner of loaned books
public void borrowBook()
{
noOfBooks = noOfBooks + 1;
System.out.println(("Books now on loan: ") + noOfBooks);
}
public void returnBook()
{
noOfBooks = noOfBooks -1;
System.out.println(("Books now on loan: ") + noOfBooks);
}
public void borrowBooks(int loanedBooks)
{
noOfBooks = noOfBooks + loanedBooks;
System.out.println(("Books now on loan: ") + noOfBooks);
}
//method for printing borrower details
public void printBorrowerDetails()
{
System.out.println(firstName + (" ") + lastName);
System.out.println(("Library Number: ") + libraryNumber);
System.out.println(("Number of loans: ") + noOfBooks);
}
public String getFirstLastFirstName()
{
return firstName + lastName + firstName;
}
}Code
public class Address
{
// instance variables - replace the example below with your own
private String postcode;
private String town;
private String street;
public Address(String postcode, String town, String street)
{
this.postcode = postcode;
this.town = town;
this.street = street;
}
public String getFullAddress()
{
return(street + "\n" + town + "\n" + postcode);
}
public void printAddress()
{
System.out.println(street + "\n" + town + "\n" + postcode);
}
public String getPostcode()
{
return postcode;
}
public String getStreet()
{
return street;
}
public String getTown()
{
return town;
}
public void setFullAdress(String postcode, String street, String town)
{
this.postcode = postcode;
this.street = street;
this.town = town;
}
public void setPostcode(String postcode)
{
this.postcode = postcode;
}
public void setStreet(String street)
{
this.street = street;
}
public void setTown(String town)
{
this.town = town;
}
}What I need to do is call the variables postcode, town and street from the Address class into the Borrower class using one method. I'm trying to use Address address = new Address(); but that's not working and I'm not sure why.
I also need to initialise an attribute called Address in the Borrower class using the information given to the constructor in the Address class.
Would really appreciate some help here.
![[Image: abTrs9B.png]](https://i.imgur.com/abTrs9B.png)
16pVw8g1csPoa7BFJ6bTADa12yteiAvcst


![[Image: 999999112.png]](https://i.ibb.co/34HrNHt/999999112.png)
![[Image: vgx9T6b.gif]](https://i.imgur.com/vgx9T6b.gif)
