Question

In Java You are going to create a PhoneRecord class containing the following properties: firstName -...

In Java

You are going to create a PhoneRecord class containing the following properties:

  • firstName - which is a String
  • lastName - which is a String
  • number - which should also be a String

the properties will be public, and it should have two constructors: one which simply constructs the string and another that accepts values for first name, lastname and phone number.

You should also create a class called Phone Book. It should contain an array of Phone Records and a count of howmany records there actually are. The number of records is passed to it by the contructor call, i.e.,:
PhoneBook pb = new PhoneBook(3);
will construct a phone book that can contain up to 3 listings.

The methods are:

  • readRecord() - which reads in a new phone record.
  • toString(int i) - which returns a string containing record #i
  • writeRecord(int i) - which writes record #i.
  • writeBook() - which writes the entire phone book.

Write another class that uses the phone book, obtaining its size as a command line argument. Read in several records, write one or two of them and write the whole phone book.

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

PHONE RECODE CLASS

//Phone record Class
public class PhoneRecord {

   //Variables in Phone record class
   public String firstName;
   public String lastName;
   public String number;
  
   //Constructor which simply constructs the string
   public PhoneRecord()
   {
       this.firstName=null;
       this.lastName=null;
       this.number=null;
   }
  
  
   //Constructor accepts values for first name, last name and phone number.
   public PhoneRecord(String firstName,String lastName,String number)
   {
       this.firstName=firstName;
       this.lastName=lastName;
       this.number=number;
      
   }
}


PHONEBOOK CLASS

import java.util.Scanner;

//Phone Book Class
public class PhoneBook {
  
   static Scanner sc=new Scanner(System.in);
   PhoneRecord array[];//contains the array of phone records
   public int count=0;//to count of how many records there actually are
   int total_records; // total records that array can contain
  
  
   //Constructor that takes how many records should store maximum
   public PhoneBook(int total_records)
   {
       this.total_records=total_records;
       array=new PhoneRecord[total_records];//initializing array with max records
   }
  
   //Method to read the record
   public void readRecord()
   {
       if(count==total_records) //if Book is full
       {
           System.out.println("Phone Book is Full ");
           return;
       }
      
       else //if Book not full take the phone records and store in array
       {
           System.out.println("- - - Reading Phone Record - - - ");
           System.out.print("First name: ");
           String firstName=sc.nextLine();
           System.out.print("Last name: ");
           String lastName=sc.nextLine();
           System.out.print("Number: ");
           String number=sc.nextLine();
           array[count]=new PhoneRecord(firstName,lastName,number);
           count++;
          
       }
   }
  
   //Method to return Phone records details at specific index number
   public String toString(int i)
   {
       //ASSUMING RECORD NUMBERS START FROM 1
       if(i>=count)//if try to take non existing record
       {
           return null;
       }
       //if record found of specific record then return
       return array[i-1].firstName+" "+array[i-1].lastName+" "+array[i-1].number;
   }
  
   //Method to write phone records details at specific index number
   public void writeRecord(int i)
   {
       //ASSUMING RECORD NUMBERS START FROM 1
       if(i>=count)//if try to take non existing record
       {
           System.out.println("No record found..");
       }
       //if record found at specific record then print records
       System.out.println("Phone record "+i);
       PhoneRecord record=array[i-1];
       System.out.println(record.firstName+" "+record.lastName+" "+record.number);
   }

  
   //Method to Write the complete phoneBook
   public void writeBook()
   {
      
       if(count==0) //if no records in book
       {
           System.out.println("No Records in PhoneBook ");
          
       }
       else//if records in phone book
       {
           System.out.println("Phone Book: ");
           for(int i=0;i<count;i++)
           {
               PhoneRecord record=array[i];
               System.out.println(record.firstName+" "+record.lastName+" "+record.number);
           }
       }
       System.out.println();
   }

}

PHONEBOOKUTIL CLASS

import java.util.Scanner;

public class PhoneBookUtil {

   public static void main(String[] args) {
      
       Scanner sc=new Scanner(System.in);
       System.out.print("Size of Phone Book: ");
       int size=sc.nextInt();//obtaining its size as a command line argument
       PhoneBook phonebook=new PhoneBook(size);//Passing maximum size of phone book
       phonebook.readRecord();//read the record
       phonebook.readRecord();//read the record
       phonebook.readRecord();//read the record
       phonebook.readRecord();//read the record
       phonebook.writeBook();//write complete phone book
       phonebook.writeRecord(3);//write record 3
       System.out.println("Record 1 :\n"+phonebook.toString(1));//return and print the record1
       sc.close();
      

   }

}

OUTPUT OF ABOVE CODE:

Size of Phone Book: 4
- - - Reading Phone Record - - -
First name: Sudeepthi
Last name: Maruvada
Number: 97584637638
- - - Reading Phone Record - - -
First name: Kowlutla
Last name: Mangali
Number: 948794748
- - - Reading Phone Record - - -
First name: Roopasri
Last name: Mangali
Number: 9489584947
- - - Reading Phone Record - - -
First name: HinduSri
Last name: Mangali
Number: 9574836733
Phone Book:
Sudeepthi Maruvada 97584637638
Kowlutla Mangali 948794748
Roopasri Mangali 9489584947
HinduSri Mangali 9574836733

Phone record 3
Roopasri Mangali 9489584947
Record 1 :
Sudeepthi Maruvada 97584637638

Add a comment
Know the answer?
Add Answer to:
In Java You are going to create a PhoneRecord class containing the following properties: firstName -...
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
  • Create a Python class named Phonebook with a single attribute called entries. Begin by including a...

    Create a Python class named Phonebook with a single attribute called entries. Begin by including a constructor that initializes entries to be an empty dictionary. Next add a method called add_entry that takes a string representing a person’s name and an integer representing the person’s phone number and that adds an entry to the Phonebook object’s dictionary in which the key is the name and the value is the number. For example: >>> book = Phonebook() >>> book.add_entry('Turing', 6173538919) Add...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Java Project In Brief... For this Java project, you will create a Java program for a...

    Java Project In Brief... For this Java project, you will create a Java program for a school. The purpose is to create a report containing one or more classrooms. For each classroom, the report will contain: I need a code that works, runs and the packages are working as well The room number of the classroom. The teacher and the subject assigned to the classroom. A list of students assigned to the classroom including their student id and final grade....

  • C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

    C++ program Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int). Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor. Create a static function getStudentCount() that returns the value of studentCount. studentCount is used to track the number of student object has been instantiated. Initialize it to 0...

  • Requirements Create an Address Book class in Java for general use with the following behaviors: 1....

    Requirements Create an Address Book class in Java for general use with the following behaviors: 1. Constructor: public Address Book Construct a new address book object. • A contact has four fields: first name, last name, email and phone. (There could be more information for a real contact. But these are sufficient for the assignment.) . The constructor reads from the disk to retrieve previously entered contacts. If previous contacts exist, the address book will be populated with those contacts...

  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • I need the following written in Java please, thank you ' We want you to implement...

    I need the following written in Java please, thank you ' We want you to implement a java class that will show details on users and throw exceptions where needed. The following requirements specify what fields you are expected to implement in your User class: - A private String firstName that specifies the first name of the user - A private String lastName that specifies the last name of the user - A private int age that specifies user's age...

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