Question

JAVA :The following are descriptions of classes that you will create. Think of these as service...

JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing the classes must be included as part of the assignment. As we get started with creating classes, it can be quite formulaic. Each class should include: - private instance variables, (declared one per line) - at least one constructor that has the same number of parameters as there are instance variables, accessor and mutator methods, - an accessor and a mutator for each instance variable (getters and setters) - a toString() method that returns a String object including the values of all of the instance variables. ==================== 1. Telephone.java Implement a class called Telephone that will be used to store telephone numbers. The class should store the area code, and the local number as separate instance variables of type String. It has only 2 instance variables. As well as the 2 argument constructor that accepts 2 String objects, create an overloaded constructor method that accepts an integer, and a String. You can convert an int to a String by concatenating an empty String to the int. String s = “” + 127; Look at the formula for creating a class above. Make sure you have included all of the parts. This class provides the ‘service’ of storing telephone numbers so you can pass them around as a single thing. The primitive data types allow us to store and pass around numbers, and characters, but the world we want to program in is the world the problems exist in. Your Telephone class is a new data type. An instance of the Telephone class is an object. 2. Address.java Implement a class called Address that will store a postal address. As well as a suitable constructor the class should have accessors, mutators, toString. What are the component parts (instance variables) of an Address? Street, apartment, country, postal code?? I expect these to be somewhat different – it is your design for how to provide the service of storing an address. 3. Contact.java Create a class called Contact that maintains the following information: name address telephone number email address The data type of the instance variable name will be String. Notice that String is the name of a class, not a primitive. What data type should you use to define the address? In the previous part of this assignment you created a very nice data type called Address. Now is the time to use it. private Address address; Your data types can use your other data types. This is called composition, or the HAS A relationship. If you say it out loud and it makes sense, then the design is correct. “A Contact HAS A Address” A Contact HAS A Telephone Follow the rules in the formula for creating classes. Constructor, accessors, mutators, toString. ======================================== This part is not a really useful thing to add to the Contact class. It’s here because I want you to see how a static variable declared in a class is different from an instance variable. The Contact class will have a Class variable (static) called contactsCreated, and a class (static) method called getContactsCreated. This variable will count how many instances of the Contact class have been created so far. Add one to this variable inside the constructor method. Each time you make an instance of the Contact class, the static variable is incremented.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

File Telephone.java

package service.provider.project;

public class Telephone {

private String AreaCode;

private String LocalNumber;

public Telephone()

{

AreaCode = "00";

LocalNumber = "00";

}

public Telephone(String areaCode, String localNumber) {

AreaCode = areaCode;

LocalNumber = localNumber;

}

public Telephone(int areaCode, String localNumber) {

AreaCode = ""+areaCode;

LocalNumber = localNumber;

}

public String getAreaCode() {

return AreaCode;

}

public void setAreaCode(String areaCode) {

AreaCode = areaCode;

}

public String getLocalNumber() {

return LocalNumber;

}

public void setLocalNumber(String localNumber) {

LocalNumber = localNumber;

}

@Override

public String toString() {

return "TELEPHONE NUMBER::"

+ "\n Area Code : " + AreaCode +

"\nLocal Number : " + LocalNumber;

}

}

File Address.java

package service.provider.project;

public class Address {

private String ApartmentNo;

private String StreetName;

private String State;

private String Country;

private long postalCode;

public Address()

{

ApartmentNo = "00";

StreetName = "NA";

State = "NA";

Country = "NA";

postalCode = 0;

}

public Address(String apartmentNo, String streetName, String state, String country, long postalCode)

{

ApartmentNo = apartmentNo;

StreetName = streetName;

State = state;

Country = country;

this.postalCode = postalCode;

}

public String getApartmentNo() {

return ApartmentNo;

}

public void setApartmentNo(String apartmentNo) {

ApartmentNo = apartmentNo;

}

public String getStreetName() {

return StreetName;

}

public void setStreetName(String streetName) {

StreetName = streetName;

}

public String getState() {

return State;

}

public void setState(String state) {

State = state;

}

public String getCountry() {

return Country;

}

public void setCountry(String country) {

Country = country;

}

public long getPostalCode() {

return postalCode;

}

public void setPostalCode(long postalCode) {

this.postalCode = postalCode;

}

@Override

public String toString() {

return "ADDRESS:: \nApartmentNumber : " + ApartmentNo + ", Street Name : " + StreetName +

"\nState : " + State +", Country : "

+ Country + "\nPostal Code : " + postalCode ;

}

}

File Contact.java

package service.provider.project;

public class Contact {

private String name;

private Address address;

private Telephone telephoneNo;

private String e_mail;

private static int contactsCreated = 0;

public Contact()

{

this.name = "NA";

this.address = new Address("00", "NA", "NA", "NA", 0);

this.telephoneNo = new Telephone("00", "00");

this.e_mail = "NA @ NA.com";

contactsCreated++;

}

public Contact(String name, Address address, Telephone telephoneNo, String e_mail)

{

this.name = name;

this.address = address;

this.telephoneNo = telephoneNo;

this.e_mail = e_mail;

contactsCreated++;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Address getAddress() {

return address;

}

public void setAddress(Address address) {

this.address = address;

}

public Telephone getTelephoneNo() {

return telephoneNo;

}

public void setTelephoneNo(Telephone telephoneNo) {

this.telephoneNo = telephoneNo;

}

public String getE_mail() {

return e_mail;

}

public void setE_mail(String e_mail) {

this.e_mail = e_mail;

}

public static int getContactsCreated()

{

return contactsCreated;

}

@Override

public String toString() {

return "CONTACT DETAILS:::"

+ "\n Name : " + name +

"\n\n"+ address + "\n\n" + telephoneNo +

"\n\nE - mail : " + e_mail;

}

}

Driver File Service.java

package service.provider.project;

import java.util.Scanner;

public class Service {

static Scanner input = new Scanner(System.in);

public static void main(String[] args)

{

Telephone[] telephone = new Telephone[20];

Address[] address = new Address[20];

Contact[] contact = new Contact[20];

int count = 0;

while(true)

{

String choice = "";

System.out.println("*** Welcome to Jacobs's Company! ***\n");

System.out.println("### MENU ###\n");

System.out.println("(A)dd a Service seeker: ");

System.out.println("(D)isplay Service seekers Contact Details: ");

System.out.println("(Q)uit");

System.out.print("Enter your choice: ");

choice = input.nextLine();

if(choice.equalsIgnoreCase("A"))

{

telephone[count] = new Telephone();

address[count] = new Address();

contact[count] = new Contact();

addContact(telephone[count], address[count], contact[count]);

addTelephone(telephone[count]);

addAddress(address[count]);

System.out.println("\nDetails saved successfully for customer name: "+contact[count].getName());

System.out.println("\n");

count++;

continue;

}

else if(choice.equalsIgnoreCase("D"))

{

String name;

System.out.print("Enter name of the customer: ");

name = input.nextLine();

int flag = 0;

for(int i = 0; i<count; i++)

{

if(contact[i].getName().equalsIgnoreCase(name.trim()))

{

System.out.println("\n*** Found "+(i+1)+" ***\n\n");

System.out.println(contact[i]);

System.out.println();

flag = 1;

}

}

if(flag == 0)

System.out.println("\n@ No such customer found @");

}

else if(choice.equalsIgnoreCase("Q"))

{

System.out.println("Thank you, Please visit again\n");

break;

}

else

{

System.out.println("Invalid Choice!");

continue;

}

}

}

public static void addTelephone(Telephone t1)

{

String AreaCode;

String LocalNumber;

System.out.print("Enter Area Code: ");

AreaCode = input.nextLine();

System.out.print("Enter the telephone number: ");

LocalNumber = input.nextLine();

t1.setAreaCode(AreaCode);

t1.setLocalNumber(LocalNumber);

}

public static void addAddress(Address adr)

{

String ApartmentNo;

String StreetName;

String State;

String Country;

long postalCode;

System.out.println("\nEnter Address Details.");

System.out.print("Enter Apartment number: ");

ApartmentNo = input.nextLine();

System.out.print("Enter street name: ");

StreetName = input.nextLine();

System.out.print("Enter State: ");

State = input.nextLine();

System.out.print("Enter country name: ");

Country = input.nextLine();

System.out.print("Enter postal code: ");

postalCode = Long.parseLong(input.nextLine());

adr.setApartmentNo(ApartmentNo);

adr.setStreetName(StreetName);

adr.setState(State);

adr.setCountry(Country);

adr.setPostalCode(postalCode);

}

public static void addContact( Telephone t1, Address adr, Contact c1)

{

String name;

Address address;

Telephone telephoneNo;

String e_mail;

System.out.println("\n|| Enter customer Details ||\n");

System.out.print("Enter the customer name: ");

name = input.nextLine();

System.out.print("Enter e-mail address: ");

e_mail = input.nextLine();

c1.setAddress(adr);

c1.setTelephoneNo(t1);

c1.setName(name);

c1.setE_mail(e_mail);

}

}

Directions: Open your ide create a new java project with package name service.provider.project and within this package create 4 new classes named Telephone, Address, Contact, and Service after then copy paste the above codes into their respective .java files and run the driver file Service.java

OUTPUT

Problems Javadoc DeclarationConsoleProgress <terminated> Service [Java Application] C:\Program FilesUavare1.8.0_45 binavaw.ex

8: Problems @Javadoc 다 Declaration貝Console X | Progress <terminated> Service [Java Application] C:\Program Filesavare1.8.0_45

Add a comment
Know the answer?
Add Answer to:
JAVA :The following are descriptions of classes that you will create. Think of these as service...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • Can you please help me with with problem. please follow all the specific insturctions, and try...

    Can you please help me with with problem. please follow all the specific insturctions, and try to make simple for a beginner in java. Write a class called Shape that contains instance data that represents the name and number of sides of the shape. Define a constructor to initialize these values. Include mutator(setter) methods – with the this reference – for the instance data, and a toString method that returns a the shape data. Create a static variable to keep...

  • Looking for some quick and useful notes on this: • Using Classes and Objects o Creating...

    Looking for some quick and useful notes on this: • Using Classes and Objects o Creating Objects: understand how to create objects, assign them, invoke methods on them and re-assign references. o The String Class: understanding and using strings and String methods o Packages: use import statement when needed o Wrapper Classes: use Wrapper classes to parse strings. • Writing classes o Anatomy of a Class: understand the concept of a class, define its instance variables, constructor and methods. o...

  • Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the...

    Create a class House with the private fields: numberOfUnits of the type int, yearBuilt of the type int, assessedPrice of the type double. A constructor for this class should have three arguments for initializing these fields. Add accessors and mutators for all fields except a mutator for yearBuilt, and the method toString. Create a class StreetHouse which extends House and has additional fields: streetNumber of the type int, homeowner of the type String, and two neighbors: leftNeighbor and rightNeighbor, both...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Java Question 3 Implement a program to store the applicant's information for a visa office. You...

    Java Question 3 Implement a program to store the applicant's information for a visa office. You need to implement the following: A super class called Applicant, which has the following instance variables: A variable to store first name of type String. A variable to store last name of type String. A variable to store date of birth, should be implemented as another class to store day, month and year. A variable to store number of years working of type integer...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance...

    Java code for the following inheritance hierarchy figure.. 1. Create class Point, with two private instance variables x and y that represented for the coordinates for a point. Provide constructor for initialising two instance variables. Provide set and get methods for each instance variable, Provide toString method to return formatted string for a point coordinates. 2. Create class Circle, its inheritance from Point. Provide a integer private radius instance variable. Provide constructor to initialise the center coordinates and radius for...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT