Home > Archive > Java Help > August 2007 > How to call a class from another class
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to call a class from another class
|
|
| dlittlebear@gmail.com 2007-08-18, 7:12 pm |
| I do not know if I am saying that correctly. This is a school
project, I do not know how to get data from 2 different files called
Client.java and Attorney.java, both having data pulled in by
LawFirm.java
The actual project is
CASE PROJECT
HOWARD, FINE, and HOWARD
The law firm of Howard, Fine, and Howard want to develop two classes
-- a Client class that hows data about the firm's clients, and an
Attorney class that hows data about each of the attorneys who works
for the firm. Client data includes a client number, last name, first
name, primary attorney's ID number, and balance owed to the firm.
Attorney data includes an ID number, last name, first name, and annual
salary. Each class includes public get and set instance methods for
accessing the data. Create an application that instantiates five
Clients and two Attorneys, assigns appropriate values to their data
fields, and displays the values in an attractive format. Save the
files as Client.java, Attorney.java and LawFirm.java.
| |
| Andrew Thompson 2007-08-18, 7:12 pm |
| dlittlebear@gmail.com wrote:
>I do not know if I am saying that correctly. This is a school
>project, I do not know how to get data from 2 different files called
>Client.java and Attorney.java, both having data pulled in by
>LawFirm.java
It seems that what you need in this 'data' are
instances of the class. The spec. calls for
5 client objects, and 2 attorney object instances.
To be able to use or reference the two classes from
within the LawFirm class, they will need to be
'imported' into it.
Classes in the default package are *automatically*
available (or 'imported') into other classes in the
default package, but for other packages, it becomes
more complicated.
Are your classes in a package? Is 'package' a term
that has come up in the course, yet?
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via http://www.javakb.com
| |
|
| Andrew Thompson wrote:
> dlittlebear@gmail.com wrote:
>
> It seems that what you need in this 'data' are
> instances of the class. The spec. calls for
> 5 client objects, and 2 attorney object instances.
>
> To be able to use or reference the two classes from
> within the LawFirm class, they will need to be
> 'imported' into it.
>
> Classes in the default package are *automatically*
> available (or 'imported') into other classes in the
> default package, but for other packages, it becomes
> more complicated.
>
> Are your classes in a package? Is 'package' a term
> that has come up in the course, yet?
Once you've used "import" or "package" to make sure that each class /can/
refer to another, then you have to make sure the class /does/ refer to an
instance of that other class.
This is done inside a "method" of the class.
Presumably all this is ocvered in your lecture notes? Is there any material
in your class notes related to "package", "import", "instance", "method" or
"constructor"? (Please answer.)
Sun has some excellent tutorials that will nicely supplement your class notes
and textbook(s). It is likely that they will approach these issues from
enough of a different angle to help you triangulate on the concepts.
<http://java.sun.com/docs/books/tutorial/index.html>
Do your professor or their teaching assistants offer any advice, where you
could go to their office and ask for some additional clarification?
Have you tried googling on your own at all for these concepts?
--
Lew
| |
| rossum 2007-08-18, 7:12 pm |
| On Sat, 18 Aug 2007 13:50:34 -0000, dlittlebear@gmail.com wrote:
>I do not know if I am saying that correctly. This is a school
>project, I do not know how to get data from 2 different files called
>Client.java and Attorney.java, both having data pulled in by
>LawFirm.java
>
>The actual project is
>
>CASE PROJECT
>
>HOWARD, FINE, and HOWARD
>
>The law firm of Howard, Fine, and Howard want to develop two classes
>-- a Client class that hows data about the firm's clients, and an
>Attorney class that hows data about each of the attorneys who works
>for the firm. Client data includes a client number, last name, first
>name, primary attorney's ID number, and balance owed to the firm.
>Attorney data includes an ID number, last name, first name, and annual
>salary. Each class includes public get and set instance methods for
>accessing the data. Create an application that instantiates five
>Clients and two Attorneys, assigns appropriate values to their data
>fields, and displays the values in an attractive format. Save the
>files as Client.java, Attorney.java and LawFirm.java.
Your homework asks you to write two classes. Start by writing one of
them, the other one will be similar.
I will start with the client class:
public class Client { ... }
Looking at the question, it tells you that: "Client data includes a
client number, last name, first name, primary attorney's ID number,
and balance owed to the firm."
This tells you that five different data items that need to be in your
Client class. You will need to make these private data fields, for
instance:
private int clientNumber;
As well as the data, you need some methods in your Client class: "Each
class includes public get and set instance methods for accessing the
data."
So for each of your five private data fields you will need a public
get method and a public set method:
public int getClientNumber() { ... }
public void setClientNumber(int newClientNumber) { ... }
That will give you a total of ten public methods, five getters and
five setters.
In addition you might need a constructor:
public Client( ... ) { ... }
If you have problems then post what you have written here and we can
help you.
You should also write a small test program to test your Client class:
put data into the class and retrieve data from it. This will also be
useful later.
When you have written and tested your Client class, then write your
Attorney class in much the same way, with its own data, getters and
setters. Again write a small test program to add and retrieve
Attorney data.
Once you have the two classes set up and tested then you can start on
the last part, creating the LawFirm application to populate your data
classes and display the data nicely. You can use your two small test
programs here as the basis of the final program as they will cover
many of the same things.
Again, if you have any problems then post what you have written here
and we can help you.
rossum
| |
|
| On Aug 18, 8:50 am, dlittleb...@gmail.com wrote:
> I do not know if I am saying that correctly. This is a school
> project, I do not know how to get data from 2 different files called
> Client.java and Attorney.java, both having data pulled in by
> LawFirm.java
>
> The actual project is
>
> CASE PROJECT
>
> HOWARD, FINE, and HOWARD
>
> The law firm of Howard, Fine, and Howard want to develop two classes
> -- a Client class that hows data about the firm's clients, and an
> Attorney class that hows data about each of the attorneys who works
> for the firm. Client data includes a client number, last name, first
> name, primary attorney's ID number, and balance owed to the firm.
> Attorney data includes an ID number, last name, first name, and annual
> salary. Each class includes public get and set instance methods for
> accessing the data. Create an application that instantiates five
> Clients and two Attorneys, assigns appropriate values to their data
> fields, and displays the values in an attractive format. Save the
> files as Client.java, Attorney.java and LawFirm.java.
rossum gave the best overall explanation. I want to comment though on
the wonderful sense of humor your teacher has (Howard, Fine, and
Howard).
Another thought though regards the overall program design. Although
not specifically in the design, you could consider that a LawFirm HAS
Attorneys, and Attorneys HAS clients. The data structure could then
logically have Attorney objects that have a array of Client objects,
and the LawFirm object would contain an array of Attorney objects.
Just another approach.
| |
| Pseudo Silk Kimono 2007-08-21, 7:12 pm |
| On 2007-08-18, rossum <rossum48@coldmail.com> wrote:
>
> You should also write a small test program to test your Client class:
> put data into the class and retrieve data from it. This will also be
> useful later.
>
> When you have written and tested your Client class, then write your
> Attorney class in much the same way, with its own data, getters and
> setters. Again write a small test program to add and retrieve
> Attorney data.
>
> Once you have the two classes set up and tested then you can start on
> the last part, creating the LawFirm application to populate your data
> classes and display the data nicely. You can use your two small test
> programs here as the basis of the final program as they will cover
> many of the same things.
>
> Again, if you have any problems then post what you have written here
> and we can help you.
>
> rossum
>
Greetings,
Being somewhat new to Java, I decided to give this assignment a try. I
was surprised at how easy it was even for me. I would like some
pointers on how my solution could be improved.
As you can see, I created two arrays, one of clients and one of lawyers.
I thought that iterating through an array would be easier should there
ever be more than 5 lawyers and 2 clients.
I also added a method to my client class called "displayAttorney".
Unfortunantly, in order for it to work, I have to pass the entire
attorney array into it so that it can search for the attorney who's
number matches the client's primary attorney ID number. Such a
technique may not be necessary, how ever I do not know of any with my
limited knowledge. Again, suggestions for improving are welcome.
I did this to comply with the requirment that the values be displayed in "...an
attractive format". Of course a database would be ideal for this task,
but I think my solution is quite good. Any suggestions on how to
improve it would be welcome.
First, my client class
I should apologize in advance as I am not sure how to configure Eclipse
to prevent the word wrapping you see here.
package howardFineAndHoward;
public class Client {
private int clientNumber;
private String lastName;
private String firstName;
private int primaryAttorneyIDNumber;
private float balanceOwing;
public Client(int clientNumber, String lastName, String
firstName,
int primaryAttorneyIDNumber, float balanceOwing)
{
this.clientNumber = clientNumber;
this.lastName = lastName;
this.firstName = firstName;
this.primaryAttorneyIDNumber = primaryAttorneyIDNumber;
this.balanceOwing = balanceOwing;
}
public int getClientNumber() {
return clientNumber;
}
public void setClientNumber(int clientNumber) {
this.clientNumber = clientNumber;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;a
}
public int getPrimaryAttorneyIDNumber() {
return primaryAttorneyIDNumber;
}
public void setPrimaryAttorneyIDNumber(int
primaryAttorneyIDNumber) {
this.primaryAttorneyIDNumber = primaryAttorneyIDNumber;
}
public float getBalanceOwing() {a
return balanceOwing;
}
public void setBalanceOwing(float balanceOwing) {
this.balanceOwing = balanceOwing;
}
public void displayClient()
{
System.out.println("Client Number: " +
this.getClientNumber());
System.out.println("Client Name: " +
this.getFirstName() + " " + this.getLastName());
System.out.println("Primary Lawyer Id: " +
this.getPrimaryAttorneyIDNumber());
System.out.println("Balance owing: " +
this.getBalanceOwing());
}
public void displayAttorney(Attorney [] attorneyList)
{
int attorneyIndex =0;
boolean found = false;
while (!found) {
if ( attorneyList[attorneyIndex].getIdNumber() ==
this.getPrimaryAttorneyIDNumber())
{
found = true;
System.out.println("Primary Attorney " +
attorneyList[attorneyIndex].getFirstName() + " " +
attorneyList[attorneyIndex].getLastName());
}
attorneyIndex++;
}
}
}
Now for my Attorney class
package howardFineAndHoward;
public class Attorney {
private int idNumber;
private String lastName;
private String firstName;
private float annualSalary;
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public float getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(float annualSalary) {
this.annualSalary = annualSalary;
}
public Attorney(int idNumber, String lastName, String firstName,
float annualSalary) {
this.idNumber = idNumber;
this.lastName = lastName;
this.firstName = firstName;
this.annualSalary = annualSalary;
}
public void displayAttorney()
{
System.out.println("ID Number: " + this.getIdNumber());
System.out.println("Attorney Name: " +
this.getFirstName() + " " + this.getLastName());
System.out.println("Annual Salary: " +
this.getAnnualSalary());
}
}
And finally, the LawFirm class
package howardFineAndHoward;
public class LawFirm {
public static void main(String [] args)
{
Client [] clientList = new Client[2];
Attorney [] attorneyList = new Attorney[5];
clientList[0] = new Client(1, "Smith", "John", 5,
10221.11f);
clientList[1] = new Client(2, "Ford", "Harrison", 2,
4932.33f);
attorneyList[0] = new Attorney(1,"Johnson","Mike",
140000.00f);
attorneyList[1] = new Attorney(2,"Marshall","Larry",
150000.00f);
attorneyList[2] = new Attorney(3,"Howard","Curly",
160000.00f);
attorneyList[3] = new Attorney(4,"Fine","Joe", 170000.00f);
attorneyList[4] = new Attorney(5,"Howard","Sally",
180000.00f);
System.out.println("Printing Client List");
for (int i=0;i<2;i++) {
clientList[i].displayClient();
clientList[i].displayAttorney(attorneyList);
}
System.out.println("Printing Attorney List");
for (int i=0;i<5;i++) {
attorneyList[i].displayAttorney();
}
}
}
Here is the output in case you are interested
Printing Client List
Client Number: 1
Client Name: John Smith
Primary Lawyer Id: 5
Balance owing: 10221.11
Primary Attorney Sally Howard
Client Number: 2
Client Name: Harrison Ford
Primary Lawyer Id: 2
Balance owing: 4932.33
Primary Attorney Larry Marshall
Printing Attorney List
ID Number: 1
Attorney Name: Mike Johnson
Annual Salary: 140000.0
ID Number: 2
Attorney Name: Larry Marshall
Annual Salary: 150000.0
ID Number: 3
Attorney Name: Curly Howard
Annual Salary: 160000.0
ID Number: 4
Attorney Name: Joe Fine
Annual Salary: 170000.0
ID Number: 5
Attorney Name: Sally Howard
Annual Salary: 180000.0
Obviously the Annual Salary could be displayed in a better format and I
know that it's possible by using formatted output, but I have never used
it before so I will attempt this on my next iteration. What I would
really like to be able to do is have the Attorney List available without
having to pass it in. This would save time, considering there could be
hundreds of lawayers in a large firm.
--
PSK - GG = bad slrn = good
><>
http://www.the-company.com/journal.htm
Huddled in the safety of a Pseudo Silk Kimono
| |
| Joshua Cranmer 2007-08-21, 10:12 pm |
| Pseudo Silk Kimono wrote:
> I should apologize in advance as I am not sure how to configure Eclipse
> to prevent the word wrapping you see here.
>
> package howardFineAndHoward;
>
> public class Client {
> [...]
> private int clientNumber;
Two things:
1. Try to avoid using tabs in Usenet posts.
2. UIDs are best declared final.
> [...]
> private int primaryAttorneyIDNumber;
Why are storing the UID as opposed to the actual object? Assuming that
the related Attorney is in creation, there is no penalty in storing a
reference to that object as opposed to a UID referencing that. That
said, there are cases where storing UIDs is beneficial (where the space
and creation cost of the object is very high compared to the likelihood
of its being used), but for simple cases, using the actual objects
themselves is a far better idea.
I recommend you do not use the changes I detail below, but instead
change the private reference to the Attorney object itself. I include my
changes only for reference on how to deal with UIDs.
> private float balanceOwing;
Money should not be stored using a float or double; a BigDecimal or
Integer (number of cents) should be used. Also, "-ing"s tend to indicate
(in my mind at least) a boolean variable or bit flag representing a
state; "balanceOwed" is a better name for the variable.
> [...]
> public void displayAttorney(Attorney [] attorneyList)
Rewritten method:
public void displayAttorney() {
Attorney a = Attorney.getAttorney(primaryAttorneyIDNumber);
System.out.println("Primary Attorney "+a.getFirstName()+" "+
a.getLastName());
}
>
> public class Attorney {
> [...]
add:
private Map<Attorney> attorneys = new HashMap<Attorney>();
public Attorney(int idNumber, String lastName, String firstName,
float annualSalary) {
// Initialize variables
attorneys.put(idNumber,this);
}
public static Attorney getAttorney(int id) {
return attorneys.get(id);
}
> [...]
> }
>
> [...]
>
> Obviously the Annual Salary could be displayed in a better format and I
> know that it's possible by using formatted output, but I have never used
> it before so I will attempt this on my next iteration. What I would
> really like to be able to do is have the Attorney List available without
> having to pass it in. This would save time, considering there could be
> hundreds of lawayers in a large firm.
Look for the printf function of the PrintStream class for formatting.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
|
|
|
|
|