/* * Copyright (c) 2001 Matthew Feldt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided the copyright notice above is * retained. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR * IMPLIED WARRANTIES. */ /** * Address.java * * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan * Exercise 2-2: * Write a class that represents a person's mailing address. It should have * seperate fields for name, street address, city, state and ZIP code. Define * a toString() method that produces nicely formatted output. * * @author Matthew Feldt * @version 1.0, 01,28/2001 23:59 */ package com.feldt.examples.classes; class Address { private String firstName, lastName, address, city, state, zip; public Address(String f, String l, String a, String c, String s, String z) { firstName = f; lastName = l; address = a; city = c; state = s; zip = z; } public Address(Address o) { firstName = o.firstName; lastName = o.lastName; address = o.address; city = o.city; state = o.state; zip = o.zip; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getAddress() { return address; } public String getCity() { return city; } public String getState() { return state; } public String getZip() { return zip; } public String getFullName() { return lastName + ", " + firstName; } public String toString() { return firstName + " " + lastName + "\n" + address + "\n" + city + ", " + state + " " + zip; } public int hashCode() { return firstName.hashCode() + lastName.hashCode() + address.hashCode() + city.hashCode() + state.hashCode() + zip.hashCode(); } public boolean equals(Object o) { return ((o instanceof Address) && (firstName.equals(((Address)o).firstName)) && (lastName.equals(((Address)o).lastName)) && (address.equals(((Address)o).address)) && (city.equals(((Address)o).city)) && (state.equals(((Address)o).state)) && (zip.equals(((Address)o).zip))); } static class Test { public static void main (String args[]) { Address a1 = new Address("Bob", "Smith", "1 Main Street", "Springfield", "IL", "12345"); Address a2 = new Address("Mary", "Johnson", "5 Peach Tree", "Columbia", "IA", "23456"); Address a3 = new Address(a1); System.out.println(a1); System.out.println(a2); System.out.println(a3); System.out.println("hashcode(a1) = " + a1.hashCode()); System.out.println("hashcode(a2) = " + a2.hashCode()); System.out.println("hashcode(a3) = " + a3.hashCode()); System.out.println("a1 == a2 : " + a1.equals(a2)); System.out.println("a1 == a3 : " + a1.equals(a3)); } } }