The Artima Developer Community
Sponsored Link

Java Answers Forum
Trying to learn this stuff

4 replies on 1 page. Most recent reply: Jun 8, 2003 5:53 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Julie

Posts: 3
Nickname: avernus
Registered: Jun, 2003

Trying to learn this stuff Posted: Jun 8, 2003 2:50 PM
Reply to this message Reply
Advertisement
Hey everyone, I have a question which is probably simple to most, but here it goes.
I have this question in a class of mine and im trying to get put in the right direction.
Here is the question:
Create a class Student that represents student objects. A student has a last name, a first name, a home address, and a school address. The addresses are objects of a class Address. An address has a street address, a city, a state, and a zip code. Write a program StudentTest that creates and prints all information for two different students.
So far I have this:
public class Student
{
/*
@param lname
@param fname
@param HomeAddress
@param SchoolAddress
*/
public Student(String alname, String afname, String aHome, String aSchool)
{
lname = alname;
fname = afname;

}

/**
Add first name to list.
@param Fname is the first name of the student */

public String getFname(String Fname)
{
fname = Fname;
return fname;
}
/**
Get last name from student
@param Lname is the last name of the student */

public String getLname(String Lname)
{
lname = Lname;
return lname;
}
private String lname;
private String fname;


}

My personal question is how do I get the address information into my current class from a different class?
Am I in the right direction?
Thanks


John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: Trying to learn this stuff Posted: Jun 8, 2003 3:06 PM
Reply to this message Reply
Hi Julie,
Your class should look something like this. Note the constructor.
John

public class Student
{
private String firstName;
private String lastName;
private Address homeAddress;
private Address schoolAddress;

public Student( String firstName, String lastName, Address homeAddress, Address schoolAddress )
{
this.firstName = firstName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.schoolAddress = schoolAddress;
}
}

Julie

Posts: 3
Nickname: avernus
Registered: Jun, 2003

Re: Trying to learn this stuff Posted: Jun 8, 2003 3:06 PM
Reply to this message Reply
Hmm interesting, thank you very much :)

John Channing

Posts: 17
Nickname: drc
Registered: Jun, 2003

Re: Trying to learn this stuff Posted: Jun 8, 2003 3:15 PM
Reply to this message Reply
Obviously I haven't included the methods to get the data out again. I would simply implement an appropriate toString() method.

public class Student
{
private String firstName;
private String lastName;
private Address homeAddress;
private Address schoolAddress;

public Student( String firstName, String lastName, Address homeAddress, Address schoolAddress )
{
this.firstName = firstName;
this.lastName = lastName;
this.homeAddress = homeAddress;
this.schoolAddress = schoolAddress;
}

/* Assumes implementation of toString() method on Address class */
public String toString()
{
StringBuffer str = new StringBuffer( firstName );
str.append( " " );
str.append( lastName );
str.append( "\n" );
str.append( homeAddress.toString() );
str.append( "\n" );
str.append( schoolAddress.toString() );
return str.toString();
}
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Trying to learn this stuff Posted: Jun 8, 2003 5:53 PM
Reply to this message Reply
Its fairly common to use get and set methods for retrieving and changing the internal data of an object. Some other class may need particular pieces of the data. The data parts can change.

A student object is basically then a bean or a java data object.

There are some very well written techniques on field and method design at:

http://www.artima.com/designtechniques/coupling.html


public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public Address getHomeAddress(){
return homeAddress;
}
public Address getSchoolAddress(){
return schoolAddress;
}

public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
return lastName;
}
public void setHomeAddress(Address homeAddress){
this.homeAddress = homeAddress;
}
public void setSchoolAddress(Address schoolAddress){
this.schoolAddress = schoolAddress;
}

Flat View: This topic has 4 replies on 1 page
Topic: Error: java.security.AccessControlException: access denied Previous Topic   Next Topic Topic: Java WebStart

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use