Question

In: Computer Science

Must be written in JAVA Code Modify the program you created in previous project to accomplish...

Must be written in JAVA Code

Modify the program you created in previous project to accomplish the following:

Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-'

Store the data in an ArrayList object

Program to Modify

import java.util.ArrayList;
import java.util.Scanner;

public class Address
{
String firstname;
String lastname;
int zipcode;
  
Address(String firstname,String lastname,int zipcode)
{
this.firstname = firstname;
this.lastname = lastname;
this.zipcode = zipcode;
}

public String toString()
{
return this.firstname + " " + this.lastname + " " + this.zipcode;
}

public static void main(String[] args)
{
Scanner aa = new Scanner(System.in);
int n;
System.out.print("Enter a number up to 25: ");
n = aa.nextInt();
String firstname,lastname;
int zipcode;
ArrayList<Address> addr = new ArrayList<Address>();
aa.nextLine();
for(int i=1;i<=n;i++)
{
System.out.print("Enter First Name:");
firstname = aa.nextLine();

System.out.print("Enter Last Name:");
lastname = aa.nextLine();

System.out.print("Enter Zip Code:");
zipcode = aa.nextInt();
aa.nextLine();

Address ad = new Address(firstname,lastname,zipcode);
addr.add(ad);
}

System.out.println("\nFirstName/LastName/Zipcode");
System.out.println();
for(Address a:addr)
{
System.out.println(a);
}
}

}

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Scanner;

public class Address {
   String firstname;
   String lastname;
   int zipcode;
   // added variables
   String address;
   String city;
   String state;
   long phone;

   // changed constructor according to new variables
   public Address(String aFirstname, String aLastname, int aZipcode, String aAddress, String aCity, String aState,
           long aPhone) {
       super();
       firstname = aFirstname;
       lastname = aLastname;
       zipcode = aZipcode;
       address = aAddress;
       city = aCity;
       state = aState;
       phone = aPhone;
   }

   // added toString to return new fields
   public String toString() {
       return this.firstname + " " + this.lastname + " " + this.zipcode + " " + this.address + " " + this.state + " "
               + this.city + " " + this.phone;
   }

   public static void main(String[] args) {
       Scanner aa = new Scanner(System.in);
       int n;
       System.out.print("Enter a number up to 25: ");
       n = aa.nextInt();
       String firstname, lastname, add, c, s;
       int zipcode;
       long ph;
       ArrayList<Address> addr = new ArrayList<Address>();
       aa.nextLine();
       for (int i = 1; i <= n; i++) {
           System.out.print("Enter First Name:");
           firstname = aa.nextLine();

           System.out.print("Enter Last Name:");
           lastname = aa.nextLine();

           System.out.print("Enter Zip Code:");
           zipcode = aa.nextInt();
           aa.nextLine();
           System.out.println("Enter address: ");
           add = aa.nextLine();
           System.out.println("Enter city: ");
           c = aa.nextLine();
           System.out.println("Enter state: ");
           s = aa.nextLine();
           System.out.println("Enter phone: ");
           ph = aa.nextLong();
           aa.nextLine();
           Address ad = new Address(firstname, lastname, zipcode, add, c, s, ph);
           addr.add(ad);
       }

       System.out.println("\nFirstName/LastName/Zipcode/Address/City/State/Phone");
       System.out.println();
       for (Address a : addr) {
           System.out.println(a);
       }
   }

}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
This code must be written in Java. This code also needs to include a package and...
This code must be written in Java. This code also needs to include a package and a public static void main(String[] args) Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week (Sunday..Saturday). The following example shows what the user will see on the screen: This program calculates the...
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java...
For this assignment, you will modify existing code to create a single Java™ program named BicycleDemo.java that incorporates the following: An abstract Bicycle class that contains private data relevant to all types of bicycles (cadence, speed, and gear) in addition to one new static variable: bicycleCount. The private data must be made visible via public getter and setter methods; the static variable must be set/manipulated in the Bicycle constructor and made visible via a public getter method. Two concrete classes...
Must be written in C++ in Visual studios community In this lab, you will modify the...
Must be written in C++ in Visual studios community In this lab, you will modify the Student class you created in a previous lab. You will modify one new data member which will be a static integer data member. Call that data member count. You also add a static method to the Student class that will display the value of count with a message indicating what the value represents, meaning I do not want to just see a value printed...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
How would you characterize the performance of Java-based code relative to the same program written (competently)...
How would you characterize the performance of Java-based code relative to the same program written (competently) in C#?
This assignment covers file I/O. Must design and code a program that can accomplish four things:...
This assignment covers file I/O. Must design and code a program that can accomplish four things: copy one file to another (i.e., place the contents of file A into file B), ‘compress’ a file by removing all vowels (a, e, i, o, u, and y) from the file, merge two files, and finally double each line of an input file, saving into an output file. Also make it so the input file's content can be changed from the example given....
In Java in previous question #1, you created a method called isDNAvalid() in the DNA class....
In Java in previous question #1, you created a method called isDNAvalid() in the DNA class. You probably looped through the entire sequence one character at a time (using charAt() method of the String class), and check if the character is 'A', 'T', 'G', or 'C'. If it is not one of these four characters, the sequence contains an invalid base. Please re-create the isDNAvalid() method using a character pattern and the regular expression to validate the DNA sequence. previous...
JAVA CODE Using the PurchaseDemo program and output from the previous problem as your guide, write...
JAVA CODE Using the PurchaseDemo program and output from the previous problem as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end. You may use integer, double, and String variables as needed but you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT