Creating Address Attribute in Java - java

package week3;
import java.util.ArrayList;
public abstract class TaxPayer {
private final String TFN;
private String firstName;
private String lastName;
private double income;
private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);
public TaxPayer(String TFN, String firstName, String lastName, double income,
Address address) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public double getIncome() {
return income;
}
public Address getAddress() {
return address;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setIncome(double income) {
this.income = income;
}
public void setAddress(Address i) {
}
public String getFullName() {
return getFirstName() + " " + getLastName();
}
#Override
public String toString() {
return TFN + " " + firstName + " " + lastName + " "
+ income;
}
public static double calcTax(double income) {
return 1;
}
public double calcTax() {
return 1;
}
public static void printArrayListToConsole(ArrayList<TaxPayer> Array) {
for (TaxPayer d : Array) {
System.out.println(d);
}
}
}
package week3;
public class Address {
int streetNumber;
String streetName;
String suburb;
String city;
String state;
int postcode;
public Address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode) {
this.streetNumber = streetNumber;
this.streetName = streetName;
this.suburb = suburb;
this.city = city;
this.state = state;
this.postcode = postcode;
}
#Override
public String toString() {
return streetNumber + " " + streetName + " " + suburb + " " + city + " "
+ state + " " + postcode;
}
}
So, my problem is in creating the address attribute, which must be private. I think I've made the other class correctly yet it still isn't working. As well as this, the attribute isn't working with the fields I try to put in it (streetNumber, streetName, etc), it's coming up with the error 'missing method body, or declare abstract' but I'm unsure why. Any help is appreciated!

private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);// this is a method not a attribute
Make private attribute of Address Class.
public abstract class TaxPayer {
.
.
.
private Address address; // this is attribute.
.
.
.
//One way is passing the all address attributes in constructor
public TaxPayer(String TFN, String firstName, String lastName, double income,int streetNumber, String streetName, String suburb,
String city, String state, int postcode) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = new Address(streetNumber, streetName, String suburb,city, state, postcode);
}
}
second option is use same constructor and make a Address objectin main class and pass into that constructor like :
public TaxPayer(String TFN, String firstName, String lastName, double income,Address address) {
this.TFN = TFN;
this.firstName = firstName;
this.lastName = lastName;
this.income = income;
this.address = address;
}
And while creating Tax Payer Object first create Address Object and pass it.
Address address= new Address(streetNumber, streetName, String suburb,city, state, postcode);
TaxPayer tp= new TaxPayer(TFN, firstName, lastName, income,address);

private Address address(int streetNumber, String streetName, String suburb,
String city, String state, int postcode);
You have to declaring a variable of Address type but this is not the way to declare a variable.
Replace the above code of TaxPayer class with just this
private Address address;

Related

I am trying to print out the Address a1 of an object person p1 I created. The Person constructor takes in the Address object as well

public class Person {
enum Sex{Male, Female}
private String initials;
private String lastname;
private String firstname;
private Sex sex;
private int yearOfBirth;
private double netWorth;
private Address homeaddress;
Sex male = Sex.Male;
Sex female = Sex.Female;
public Person(){}
public Person(String firstname, String lastname, int yearOfBirth, Sex sex, double netWorth, Address homeaddress)
{
this.netWorth = netWorth;
this.firstname = firstname;
this.lastname = lastname;
this.sex = sex;
this.homeaddress = homeaddress;
this.male = Sex.Male;
this.female = Sex.Female;
}
public void updateNetworth(double x)
{
this.netWorth = netWorth + x;
}
public String toString(){
return("\nName: " + this.firstname + this.lastname + "\n" +
"Address: " + this.homeaddress + "\nNet Worth: " + this.netWorth);
}
public static void main(String[] args)
{
Address a1 = new Address (258, "Masachu","New York","USA");
Person p1 = new Person("John","Brown",1998,Sex.Male,1000.00,a1);
System.out.println(p1.toString());
p1.updateNetworth(1000.00);
System.out.println(p1.toString());
}
}
public class Address {
private int streetNumber;
private String streetName;
private String city;
private String country;
public Address (int streetNumber, String streetName, String city, String country)
{
this.streetNumber = streetNumber;
this.streetName = streetName;
this.city = city;
this.country = country;
}
}
Whenever I run this my Address field is being printed out as Address#3feba861. I am unclear as to why this is so. So my Person constructor takes in the Address object, but it seems that I cannot print out the person object with the address in this way. How do I go about fixing this.
#Override
public String toString() {
return "Address{" +
"streetNumber=" + streetNumber +
", streetName='" + streetName + '\'' +
", city='" + city + '\'' +
", country='" + country + '\'' +
'}';
}

Same Constructor Name Error

Okay so I'm trying to make more than one constructor with the same name. I'm getting an error and I am not sure why.I'm basically trying to make it so that when a player inputs their name, it will still work even if they don't input a specific field such as a suffix, etc. I'm aware I still have to add the setters and getters later and all that but I'm just trying to get the constructors set up right now. Any idea as to why it is giving me an error?
public class Name {
private String firstName;
private String middleInitial;
private String lastName;
private String suffix;
public String playerName(String firstName, String middleInitial, String lastName, String suffix){
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
public String playerName(String firstName, String lastName){
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
public String playerName(String firstName, String middleInitial, String lastName){ //this is where the error is. "Duplicate method playerName (String String String) in type Name"
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
public String playerName(String firstName, String lastName, String suffix){//same error here
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
this.suffix = suffix;
return suffix + firstName + middleInitial + lastName;
}
If you were to call
obj.playerName ("string1", "string2", "string3");
how would java know whether this was
public String playerName(String firstName, String middleInitial, String lastName)
or
public String playerName(String firstName, String lastName, String suffix)
Lets take the constructor where you have the error
public String playerName(String firstName, String middleInitial, String lastName) { /* error */ }
Now this is another one where you have the same error
public String playerName(String firstName, String lastName, String suffix){ /* same error */ }
That is because your are using same DATA TYPE for the arguments in both of them and same number of arguments. So here java won't be able to decide which one to call when you use one of these.
I hope this is clear for you now...
When overloading methods (or constructors), you can only have methods that differ in their type signatures. Thus, only one method (of the same name) that takes three String parameters. However, you currently have no constructors. What I think you really wanted was to create Name instances, perhaps to also have getters and setters and finally to override toString (so you can get the Name as a String). Something like,
public class Name {
private String firstName;
private String middleInitial = "";
private String lastName;
private String suffix = "";
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Name(String firstName, String middleInitial, String lastName) {
this(firstName, lastName);
this.middleInitial = middleInitial;
}
public Name(String firstName, String middleInitial, String lastName,
String suffix) {
this(firstName, lastName, middleInitial);
this.suffix = suffix;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleInitial() {
return middleInitial;
}
public void setMiddleInitial(String middleInitial) {
this.middleInitial = middleInitial;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(firstName);
if (sb.length() > 0) {
sb.append(' ');
}
if (middleInitial.length() > 0) {
sb.append(middleInitial).append(' ');
}
sb.append(lastName);
if (suffix.length() > 0) {
sb.append(' ');
sb.append(suffix);
}
return sb.toString();
}
}
The example toString is performing String concatenation, and you could do the less efficient
#Override
public String toString() {
String str = firstName;
if (str.length() > 0) {
str += " "; // <-- str = str + " ";
}
if (middleInitial.length() > 0) {
str += middleInitial + " ";
}
str += lastName;
if (suffix.length() > 0) {
str += " " + suffix;
}
return str;
}

Constructor requiring more than one for subclass super

Please help me find errors from this code. I'm still new and I don't know if this is correct or not.
I do have one error.
This is the error:
constructor Person in class Person cannot be applied to given types;
super();
^
required: String,String,String
found: no arguments
reason: actual and formal argument lists differ in length
This is my code:
import java.util.*;
public class Person {
//Data fields
private String lastName;
private String middleInitial;
private String firstName;
//Constructors
public Person(String lastName, String middleInitial, String firstName) {
this.lastName = lastName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
//Accessor methods
public String getlastName() {
return lastName;
}
public String getmiddleInitial() {
return middleInitial;
}
public String getfirstName() {
return firstName;
}
//Mutator methods
public void setlastName(String lastName) {
lastName = lastName;
}
public void setmiddleInitial(String middleInitial) {
middleInitial = middleInitial;
}
public void setfirstName(String firstName) {
firstName = firstName;
}
public String getName() {
String studentName = this.lastName + ", " + this.firstName +
this.middleInitial + ".";
return studentName;
}
} //end Person class
class Address {
//Data fields
private String streetName;
private int zipCode;
private String state;
private String country;
//Constructors
public Address(String streetName, int zipCode, String state,
String country) {
this.streetName = streetName;
this.zipCode = zipCode;
this.state = state;
this.country = country;
}
//Accessor methods
public String getstreetName() {
return streetName;
}
public int getzipCode() {
return zipCode;
}
public String getstate() {
return state;
}
public String getcountry() {
return country;
}
//Mutator methods
public void setstreetName(String streetName) {
streetName = streetName;
}
public void setzipCode(int zipCode) {
zipCode = zipCode;
//Integer.toString(zipCode);
}
public void setstate(String state) {
state = state;
}
public void setcountry(String country) {
country = country;
}
public String getAddress() {
String studentAddress = streetName + "\n" + state + ", " + country +
"\n" + zipCode;
return studentAddress;
}
} //end Address class
class Student extends Person {
private String dateOfBirth;
//Constructors
public Student (String studentName, String dateOfBirth) {
super();
dateOfBirth = dateOfBirth;
}
//Accessor methods
public String getdateOfBirth() {
return dateOfBirth;
}
//Mutator methods
public void setdateOfBirth() {
this.dateOfBirth = dateOfBirth;
}
public String toString() {
return ("Date of Birth: " + dateOfBirth);
}
} //end Student subclass
Edited: If I do so for both the Person and Address class. I can only have three-arg constructors. How can I call a one-arg constructor?
For example, I have
public Student (String firstName, String lastName, String middleInitial, String dateOfBirth) {
super(firstName, lastName, middleInitial); and
public Student (String streetName, String state, String country) {
super(streetName, state, country);
How can I get zipcode separately?
Class Person has a constructor, therefore the default no-arg constructor is not created for you. Therefore you can't call super() in Student's constructor, you have to call super(lastName, middleInitial, firstName);.
Or you could create a new Person no-arg constuctor.
Try this
In student class
public Student ( String lastName, String middleInitial, String firstName,String studentName, String dateOfBirth) {
super( lastName, middleInitial,firstName);
this.dateOfBirth = dateOfBirth;
}
Or
In Person class create no arg consructor. Eg:
public Person(){}
Person Class has a constructor with arguments. So default constructor will not be created. So you have to pass 3 String parameters in super(3 String parameters) or you have to create a constructor which does not take any parameter in person class.

Getters, Setters, Constructors and their parameters

Ye've been very helpful so far even though I havn't been great at wording my questions. I think I almost know what I'm doing but I'm trying to get my head around the relationship between getters, setters and constructors. I have two classes as follows
Student and Name and I'm confused about the relationship between the parameters in the getters and setters and constructor
If I remove the parameters from the Name constructor, i.e. this
public Name (){
this.firstName = firstName;
this.lastName = lastName;
and aslo edited the Student constructor to reflect this
public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course, int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(); //I have removed the parameters here
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage;
}
I'm not sure what impact it would have. I would be very greatful of someone could explain to me where I should/shouldn't have parameters and why? Understand this concept is what is holding me back from moving any further with coding at the moment.
public class Student {
private Name name; // This is calling from the Name class, giving it the name 'name'
private Address address; // This calls from Address, giving it the name 'address'
private char gender;
private String course, college;
private int gradePointAverage, id, age, level;
public Student(int id, String firstName, String lastName, String street, String area, String city, String country, int age, char gender, String college, String course, int level, int gradePointAverage){ //This is the list of variables called from the Student class
this.id = id;
this.name = new Name(firstName, lastName);
//this.name = new Name();
this.name.setFirstName(firstName);
this.name.setLastName(lastName);
this.address = new Address(street, area, city, country);
this.age = age;
this.gender = gender;
this.college = college;
this.course = course;
this.level = level;
this.gradePointAverage = gradePointAverage;
}
public int getId(){
return id;
}
public String getName(){
return name.toString();
}
public String getAddress(){
return address.toString();
}
public int getAge(){
return age;
}
public char getGender(){
return gender;
}
public String getCollege(){
return college;
}
public int getLevel() {
return level;
}
public String getCourse() {
return course;
}
public int getGradePointAverage() {
return gradePointAverage;
}
public void printStudent() {
System.out.println("The Student " + name.toString() + " is logged under the student ID number " + id + ".");
System.out.println("They live at " + address.toString() + " and their age is " + age + ".");
System.out.println("Their gender is " + gender + ".");
System.out.println("The student studies at " + college + " attending classes in " + course + ".");
System.out.println("Their level is " + level + " and the student grade average in points is " + gradePointAverage + ".");
System.out.println();
}
}
and Name
public class Name{
private String firstName, lastName;
public Name (String firstName, String lastName){
//public Name (){
this.firstName = firstName;
this.lastName = lastName;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
//public String getFirstName() {
// return firstName;
//}
//public String getLastName() {
// return lastName;
//}
///** Returns first name concatenated to last name */
//public String toString() {
// return firstName + " " + lastName;
//}
}
The constructors are for initialization of an instance of an Object to ensure that all the minimum amount of data required for a valid object state are provided at creation time.
In your case Name should have a constructor that requires firstName and lastName because those things are what make the Name object fully initialized. This Object should work just like your Address object you have shown as well.
Otherwise if you use setXXX methods, the Name object is incomplete with the two String objects initialized to null or some other undefined state.

How to make constructor that takes in 2 if there are 2 parameters, or 3 if there are 3

New to Java...
I have a name class that has:
private String firstName;
private String middleInitial;
private String lastName;
as its instance variables.
If I had certain data that had only firstName and lastName, no middleInitial, how would I make the constructor so that it took only 2 parameters instead of three?
You simply write a constructor with two parameters and a constructor with three
public YourClass(String firstName, String lastName) {
...
}
public YourClass(String firstName, String middleInitial, String lastName) {
...
}
Callers can then choose to use the appropriate constructor based on their needs.
Well, two options:
Just have a constructor with three parameters, and call it using null or the empty string for middleInitial
Overload the constructors, possibly calling one from the other.
As an example for the latter, using an empty string as the default middle initial:
public Person(String firstName, String middleInitial, String lastName)
{
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public Person(String firstName, String lastName)
{
this(firstName, "", lastName);
}
However, the compiler will need to know which one you're calling from the call site. So you can do:
new Person("Jon", "L", "Skeet");
or
new Person("Jon", "Skeet");
... but you can't do:
// Invalid
new Person(firstName, gotMiddleInitial ? middleInitial : ???, lastName);
and expect the compiler to decide to use the "two name" variant instead.
In Java, constructors can't have default arguments. Your only option here is to write two constructors. Fortunately, Java does allow you to call constructors from other constructors. You could do something like:
public class MyClass {
private String firstName;
private String middleInitial;
private String lastName;
public MyClass(String firstName, String middleInitial, String lastName) {
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public MyClass(String firstName, String lastName) {
this(firstName, "", lastName);
}
...
}
public Class Name{
private String first;
private String middle;
private String last;
public Name(String first, String middle, String last){
this.first = first;
this.middle = middle;
this.last = last;
}
public Name(String first, String last){
this.first = first;
this.last = last;
}
}
You could use two constructors:
public Person(String firstName, String lastName)
{
this(firstName, null, lastName);
}
public Person(String firstName, String middleInitial, String lastName)
{
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = = lastName;
}
Define 2 constructors, one with 2 parameters and one with 3 parameters.
You can write two constructors.
public Person( String firstName, String lastName, String middleName ) { ... }
public Person( String firstName, String lastName ) { ... }
public void myBike(String name, String color)
{
System.out.println("My bike's name is " + name + " and is " + color + ".");
}
public void myBike(String name, String color, float height)
{
System.out.println("My bike's name is " + name + " and is " + color + ".");
System.out.println("My bike is also " + height + " inches tall.");
}
Builder pattern...
class Name {
Builder builder;
public String getSurname() {
return builder.surname;
}
// getter...
public Name(Builder builder) {
this.builder = builder;
}
class Builder {
String surname = "";
String middleName = "";
String name = "";
Builder surname(String surname) {
this.surname = surname;
return this;
}
Builder middleName(String middleName) {
this.middleName = middleName;
return this;
}
Builder name(String name) {
this.name = name;
return this;
}
Name build() {
return new Name(this);
}
}
}

Categories

Resources