Customer Address Using String Builder
Strictly adhere to the object-oriented specifications given as a part of the problem statement.
Use the same class names and member variable names. ]
Data Type | Variable name |
String | line1 |
String | line2 |
String | city |
String | country |
Integer | zipCode |
Consider the class Main. In the main method read address details as input and display the details by printing the address objects.
The link to download the template code is given below
Code Template
Sample Input & Output:
Object Oriented Requirements:
public class Address {
private String line1;
private String line2;
private String city;
private String country;
private int zipCode;
public Address() {
super();
// TODO Auto-generated method stub
}
public Address(String line1,String line2,String city,String country,int zipCode) {
super();
this.line1 = line1;
this.line2 = line2;
this.city = city;
this.country = country;
this.zipCode = zipCode;
}
public String getLine1() {
return line1;
}
public void setLine1(String line1) {
this.line1 = line1;
}
public String getLine2() {
return line2;
}
public void setLine2(String line2) {
this.line2 = line2;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getZipCode() {
return zipCode;
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
@Override
public String toString() {
return "Address Details :\n"+new StringBuilder().append(this.getLine1())+",\n"+new StringBuilder().append(this.getLine2())+",\n"+new StringBuilder().append(this.getCity())+
"-"+new StringBuilder().append(this.getZipCode())+"\n"+new StringBuilder().append(this.getCountry());
}
}
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc =new Scanner(System.in);
System.out.println("Enter Address Details :\nEnter Line 1 :");
String line1=sc.nextLine();
System.out.println("Enter Line 2 :");
String line2=sc.nextLine();
System.out.println("Enter City :");
String city=sc.nextLine();
System.out.println("Enter Country :");
String country=sc.nextLine();
System.out.println("Enter Zip Code :");
int zipCode=sc.nextInt();
Address address =new Address(line1,line2,city,country,zipCode);
System.out.println(address.toString());
}
}
String API : split()
Write a program to split a string based on spaces (there may be multiple spaces too) and returns the string into separate word.
Input Format:
Input consists of a string.
Output Format:
Refer sample output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output.]
Sample Input and Output :
Enter the string
ABCD Technologies is a private organization
The words in the string are
ABCD
Technologies
is
a
private
organization
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String name = sc.nextLine();
String arr[] = name.split(" ");
System.out.println("The words in the string are");
for(String i:arr) {
System.out.println(i);
}
}
}
String API : startsWith() : Illustration
Write a program to illustrate the use of the method startsWith() defined in the string API.
Input and Output Format:
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.
Sample Input and Output 1:
Enter the string
Technology
Enter the start string
Logic
"Technology" does not start with "Logic"
Sample Input and Output 2:
Enter the string
Technology
Enter the start string
Tech
"Technology" starts with "Tech"
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string");
String name = sc.nextLine();
System.out.println("Enter the start string");
String start = sc.nextLine();
if(name.startsWith(start)) {
System.out.println("\""+name+"\""+" starts with "+"\""+start+"\"");
}
else {
System.out.println("\""+name+"\""+" does not start with "+"\""+start+"\"");
}
}
}
MOTIVATION :
“The way we spend our time defines who we are ”
Although we may not see each other as often as we’d like, distance is no match for the bond that we share. Thank you for coming to visit. It was fantastic to catch up.
VISITE MORE BLOGS