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

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...
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...
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#?
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...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100...
Written in JAVA Code Write a program that inserts 25 random integers from 0 to 100 in order into a LinkedList object. The program should sort the elements, then calculate the sum of the elements and the floating-point average of the elements.
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
This program is to be written in Java Language. Thank you A College has conducted a...
This program is to be written in Java Language. Thank you A College has conducted a student survey. Students were asked to rate their satisfaction with remote learning courses. Students rated their satisfaction on a scale of 1 to 5 (1 = "I hate it", 5 = "I love it"). The student responses have been recorded in a file called "StudentResponses.txt". Each line of the file contains one student response. Program 1 You are to write a program that reads...
Must be written in Java: After completing this lab, you should be able to: Write a...
Must be written in Java: After completing this lab, you should be able to: Write a Java class to represent Time. Create default and parameterized constructors. Create accessor and mutator methods. Write a toString method. This project will represent time in hours, minutes, and seconds. Part 1: Create a new Java class named Time that has the following instance fields in the parameterized constructor: hours minutes seconds Create a default constructor that sets the time to that would represent the...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try...
Java Modify subclass HourlyEmployee11 and class HourlyExc (created to hold the exception) to add a "try and catch" statement to catch an exception if "empStatus == 1" hourly wage is not between $15.00/hr. and $25.00/hr. The keywords “throw” and “throws” MUST be used correctly and both keywords can be used either in a constructor or in a method. If an exception is thrown, the program code should prompt the user for the valid input and read it in. *NOTE*- I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT