Advertisement
Avdhoot

Java Day-3 Practice set

Aug 19th, 2022
855
0
138 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | Source Code | 0 0
  1. // Question: Create an object to access these methods and invoke these methods with different type of numbers and display the result in the corresponding methods. Create an AddressBook class encapsulating name (String), tempAdd(Address), premAdd (Address) and phone number(long). Provide appropriate getter and setter methods. Address class encapsulates house address– name, street address, city, state. The Address class must be available only to AddressBook class. Make sure that name assigned to AddressBook object is same as name assigned to Address object. Test the application by creating AddressBook object and printing address and email id assigned to AddressBook object.
  2.  
  3. // My approach:
  4.  
  5. abstract class Address {
  6.     private String name;
  7.     private String streetAddress;
  8.     private String city;
  9.     private String state;
  10.  
  11.     public String getName() {
  12.         return name;
  13.     }
  14.  
  15.     public void setName(String name) {
  16.         this.name = name;
  17.     }
  18.  
  19.     public String getStreetAddress() {
  20.         return streetAddress;
  21.     }
  22.  
  23.     public void setStreetAddress(String streetAddress) {
  24.         this.streetAddress = streetAddress;
  25.     }
  26.  
  27.     public String getCity() {
  28.         return city;
  29.     }
  30.  
  31.     public void setCity(String city) {
  32.         this.city = city;
  33.     }
  34.  
  35.     public String getState() {
  36.         return state;
  37.     }
  38.  
  39.     public void setState(String state) {
  40.         this.state = state;
  41.     }
  42.  
  43.     @Override
  44.     public String toString() {
  45.         return "city: "+city + ", name: " + name + ", state: " + state + ", streetAddress: " + streetAddress;
  46.     }
  47. }
  48.  
  49. public class AddressBook extends Address {
  50.     private String name;
  51.     private Address tempAdd;
  52.     private Address permAdd;
  53.     private Long number;
  54.  
  55.     public AddressBook(String name, String streetAddress, String city, String state){
  56.         super.setName(name);
  57.         super.setStreetAddress(streetAddress);
  58.         super.setCity(city);
  59.         super.setState(state);
  60.     }
  61.  
  62.     public AddressBook(String name, Address tempAdd, Address permAdd, Long number) {
  63.         this.name = name;
  64.         this.tempAdd = tempAdd;
  65.         this.permAdd = permAdd;
  66.         this.number = number;
  67.     }
  68.  
  69.     public String getName() {
  70.         return name;
  71.     }
  72.  
  73.     public void setName(String name) {
  74.         this.name = name;
  75.     }
  76.  
  77.     public Address getTempAdd() {
  78.         return tempAdd;
  79.     }
  80.  
  81.     public void setTempAdd(Address tempAdd) {
  82.         this.tempAdd = tempAdd;
  83.     }
  84.  
  85.     public Address getPermAdd() {
  86.         return permAdd;
  87.     }
  88.  
  89.     public void setPermAdd(Address permAdd) {
  90.         this.permAdd = permAdd;
  91.     }
  92.  
  93.     public Long getNumber() {
  94.         return number;
  95.     }
  96.  
  97.     public void setNumber(Long number) {
  98.         this.number = number;
  99.     }
  100. }
  101.  
  102. public class Task3 {
  103.     public static void main(String[] args) {
  104.         Address address = new AddressBook("Amey", "Kanakia Road", "Mira Road", "Maharashtra");
  105.         Address address2 = new AddressBook("Amey", "Carter road", "Bandra", "Maharashtra");
  106.  
  107.         AddressBook book = new AddressBook(address.getName(), address, address2, 1234567890L);
  108.  
  109.         System.out.println(book.getTempAdd());
  110.         System.out.println(book.getPermAdd());
  111.     }
  112. }
  113.  
  114. // These are all in different .java files. Is this the right approach?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement