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.
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
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.