Design the CellPhone class so that the main method of tester class can produce the…
- User
Design the CellPhone class so that the main method of tester class can produce the following output:
Tester Class public class Tester9{ public static void main(String[]args){ CellPhone phone1 = new CellPhone(); phone1.printDetails(); phone1.model ="Nokia 1100"; System.out.println("1##################"); phone1.storeContact("Joy - 01834"); phone1.printDetails(); System.out.println("2##################"); phone1.storeContact("Toya - 01334"); phone1.storeContact("Aayan - 01135"); phone1.printDetails(); System.out.println("3##################"); phone1.storeContact("Sani - 01441"); phone1.printDetails();
} }Output Phone Model unknown Contacts Stored 0 1################## Contact Stored Phone Model Nokia 1100 Contacts Stored 1 Stored Contacts: Joy - 01834 2################## Contact Stored Contact Stored Phone Model Nokia 1100 Contacts Stored 3 Stored Contacts: Joy - 01834 Toya - 01334 Aayan - 01135 3################## Memory full. New contact can't be stored. Phone Model Nokia 1100 Contacts Stored 3 Stored Contacts: Joy - 01834 Toya - 01334 Aayan - 01135
- Assistant
Here's the
CellPhoneclass implementation that matches the expected behavior as described in yourTester9class and required output:public class CellPhone { String model; String[] contacts; int contactsStored; // Constructor public CellPhone() { this.model = "unknown"; this.contacts = new String[3]; this.contactsStored = 0; } // Method to store contacts public void storeContact(String contact) { if (contactsStored < contacts.length) { contacts[contactsStored] = contact; contactsStored++; System.out.println("Contact Stored"); } else { System.out.println("Memory full. New contact can't be stored."); } } // Method to print phone details public void printDetails() { System.out.println("Phone Model " + model); System.out.println("Contacts Stored " + contactsStored); if (contactsStored > 0) { System.out.println("Stored Contacts:"); for (int i = 0; i < contactsStored; i++) { System.out.println(contacts[i]); } } } // Main method in a separate class `Tester9` public static void main(String[] args) { CellPhone phone1 = new CellPhone(); phone1.printDetails(); phone1.model = "Nokia 1100"; System.out.println("1##################"); phone1.storeContact("Joy - 01834"); phone1.printDetails(); System.out.println("2##################"); phone1.storeContact("Toya - 01334"); phone1.storeContact("Aayan - 01135"); phone1.printDetails(); System.out.println("3##################"); phone1.storeContact("Sani - 01441"); phone1.printDetails(); } }In this implementation:
- The
CellPhoneclass has fieldsmodel,contacts, andcontactsStored. - The constructor initializes
modelto "unknown",contactsas an array that can hold a maximum of 3 contacts, andcontactsStoredto 0. - The
storeContactmethod adds a contact to thecontactsarray if there's space available; otherwise, it prints a message indicating the memory is full. - The
printDetailsmethod prints the current model, the number of contacts stored, and if there are any contacts, it lists them out.
The provided implementation will produce the desired output when running the
mainmethod in theTester9class. - The
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.