Looking for answers? Westonci.ca is your go-to Q&A platform, offering quick, trustworthy responses from a community of experts. Get immediate and reliable answers to your questions from a community of experienced experts on our platform. Our platform offers a seamless experience for finding reliable answers from a network of knowledgeable professionals.

implement a class address. an address has a house number, a street name, an optional apartment number, a city, a state, and a postal code. define the constructor such that an object can be created in one of two ways: with an apartment number or without. supply a method that prints the complete address of a person with the street on one line and the city, state, and postal code on the next line. Supply a method public boolean comesBefore (Address other) that tests whether this address comes before another when the addresses are compared by postal code.
I have written the program but am stuck on this part. Supply a method public boolean comesBefore (Address other) that tests whether this address comes before another when the addresses are compared by postal code
public class Address
{
private int HouseNumber;
private String Street;
private int AptNumber;
private String City;
private String State;
private int PostalCode;
public Address(int HouseNum,String street,int aptNum, String city,String state, int postalCode){
HouseNumber=HouseNum; Street=street; AptNumber=aptNum;
City=city; State=state; PostalCode=postalCode;
}
public Address(int HouseNum,String street, String city,String state, int postalCode){
HouseNumber=HouseNum; Street=street; AptNumber=-999;
City=city; State=state; PostalCode=postalCode;
}
public void PrintAddress(){
String str=HouseNumber+" "+Street;
if(AptNumber!=-999)
str+=" Apt #"+AptNumber;
str+="\n"+City+", "+State+" "+PostalCode;
System.out.println(str);
}
}

Sagot :

Java program that demonstrates the use of classes and constructors. Below is an example that creates an "Address class" with and without an apartment number. An image of the code and output of the program is attached.

Java code

import java.io.*;

public class Main{

public static void main(String args[]) throws IOException {

 BufferedReader bufEntrada = new BufferedReader(new InputStreamReader(System.in));

  • //Variable Define

 String r;

 int hn;

 String s;

 int apt;

 String c;

 String st;

 int pc;

 hn = 0;

 s = "";

 apt = 0;

 c = "";

 st = "";

 pc = 0;

    Address address = new Address(0, "", 0, "", "", 0);

 do {

  do {

  • //Data entry

   System.out.println("With an apartment number or without? ");

   System.out.println("(1) With ");

   System.out.println("(2) without ");

   r = bufEntrada.readLine();

   r = r.toLowerCase();}

  while (!(r.equals("1") || r.equals("2")));

  • // Function call

       hn=address.HouseNumber(hn);

       s=address.Street(s);

       apt=address.AptNumber(apt);

       c=address.City(c);

       st=address.State(st);

       pc=address.PostalCode(pc);

       // Output

  if (r.equals("1")) {

                System.out.println("House number: "+hn);

                System.out.println("Street name: "+s);

                System.out.println("Apartment number: "+apt);

                System.out.println("City: "+c);

                System.out.println("State: "+st);

                System.out.println("Postal code: "+pc);

  }

  else {

          System.out.println("House number: "+hn);

                System.out.println("Street name: "+s);

                System.out.println("City: "+c);

                System.out.println("State: "+st);

                System.out.println("Postal code: "+pc);

  }

 

   }

 while (!r.equals("n"));

}

}

  • // class address

class Address {

   int HouseNumber;

   String Street;

   int AptNumber;

   String City;

   String State;

   int PostalCode;

  •   //Constructor address with and without apt number

   Address (int HouseNum,String street,int aptNum, String city,String state, int postalCode) {

   HouseNumber=HouseNum;

   Street=street;

   AptNumber=aptNum;

   City=city;

   State=state;

   PostalCode=postalCode;

}

  • //Value returning

int HouseNumber(int hn){

return 594;

}

String Street(String s){

   return "Wilmington";

}

int AptNumber(int apt){

return 1000;

}

String City(String c){

   return "NEW YORK";

}

String State(String st){

   return "NY";

}

int PostalCode(int pc){

return 2000;

}

}

To learn more about classes and constructors in java see: https://brainly.com/question/18323161

#SPJ4

View image megatokay
View image megatokay
We appreciate your time. Please come back anytime for the latest information and answers to your questions. We appreciate your visit. Our platform is always here to offer accurate and reliable answers. Return anytime. Get the answers you need at Westonci.ca. Stay informed with our latest expert advice.