1 answer

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

Question:

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.


Answers

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

.

Similar Solved Questions

1 answer
E-PHYS 1401 - X Home Row Review - EdClub Ch. 9 Problems & Exercises - Cox...
e-PHYS 1401 - X Home Row Review - EdClub Ch. 9 Problems & Exercises - Cox 36 HW: Statics and Tort Dates > HW: Statics and Torque Resources Apollo and Artemis are playing on the tecter-totter in their school's playground. They both have approximately the same mass. They are sitting on eith...
1 answer
The transverse displacement of a stretched string from equilibrium as a function of time and position...
The transverse displacement of a stretched string from equilibrium as a function of time and position is given by: y=0.13 cos(9 x + 45 t). x and y are in m; t is in s. The wavelength is ..... 1 m. The period is ..... 0.1 seconds. The wave travels in the negative x direction. The speed of the wave is...
1 answer
Roar Corp has a beta of 1.3. If the risk-free rate is 5% and the market...
Roar Corp has a beta of 1.3. If the risk-free rate is 5% and the market risk premium is 9%, what is the expected return of Roar Corp?...
1 answer
Question 11. During a review of competition video, you observe that Mr Q is unable to...
Question 11. During a review of competition video, you observe that Mr Q is unable to maintain the orientation of his trunk during the later stages of his run (excessive extension in his lumbar spine and excessive lateral tilting of his pelvis). It appears that he is unable to use his trunk and hip ...
1 answer
Answer all te a balanced chemical equation for each reaction listed below for the group IV/V...
answer all te a balanced chemical equation for each reaction listed below for the group IV/V known solution. Be sure to balance each equation and include physical state notations. These reactions will be included in your Lab Report for this experiment and should also be recorded in your Lab Noteboo...
1 answer
QUESTION 13 The wages of a timekeeper in the factory would be classified as a period...
QUESTION 13 The wages of a timekeeper in the factory would be classified as a period cost. direct labor. indirect labor. compliance costs....
1 answer
Find the equation of the regression line for the given data. Then construct a scatter plot...
Find the equation of the regression line for the given data. Then construct a scatter plot of the data and draw the regression line. The table shows the shoe size and heights (in.) for 6 men. Shoe size, x Height, y 6.0 66.5 10.0 67. 5 10.5 7 1.5 12.0 72.5 13.0 74.5 13.5 72.5 Find the regression equa...
1 answer
Which of these taxpayers has a casualty or theft gain? * Aaron's personal-use vehicle (FMV $9,000,...
Which of these taxpayers has a casualty or theft gain? * Aaron's personal-use vehicle (FMV $9,000, basis $15,000) was stolen and never recovered. His insurance reimbursed him $8,000 for the loss. Elaine's main home (FMV $125,000, basis $105,000) was damaged by wind in a hurricane that was de...
1 answer
Prelab Questions 1. Gastrulation begins at what end of the embryo? 2. What is the primitive strea...
Prelab Questions 1. Gastrulation begins at what end of the embryo? 2. What is the primitive streak? 3. What is Hensen's node? 4. What is Neurulation? What is the final outcome once neurulation is complete? Prelab Questions 1. Gastrulation begins at what end of the embryo? 2. What is the primiti...
1 answer
Grip is applied to produce normal surface forces that compress the object being gripped. Examples indlude...
Grip is applied to produce normal surface forces that compress the object being gripped. Examples indlude two people shaking hands, or a nurse squeezing a patient's forearm to stop bleeding, etc. The data on grip strength (in Newtons (N)) for a sample of 38 individuals is displayed in the box pl...
1 answer
Two cards are drawn without replacement from a well shuffled deck of 52 cards
Two cards are drawn without replacement from a well shuffled deck of 52 cards. What is the probability that the second card drawn is a heart, if the first card drawn was not a heart?...
1 answer
Part D What is the electron arrangement of silicon? Enter electron arrangement as integers separated by...
Part D What is the electron arrangement of silicon? Enter electron arrangement as integers separated by commas. VOAD ? Previous Answers Request Answer Submit...
1 answer
Large helium-filled balloons are used to lift scientific equipment to high altitudes. (a) What is the...
Large helium-filled balloons are used to lift scientific equipment to high altitudes. (a) What is the pressure inside such a balloon (in atm) if it starts out at sea level with a temperature of 16.8°C and rises to an altitude where its volume is sixteen times the original volume and its temperat...
1 answer
A wildlife conservationist specializing in wolf conservation read an article from 2008 that claimed that the...
A wildlife conservationist specializing in wolf conservation read an article from 2008 that claimed that the number of wolves born in a litter is normally distributed with a mean of 4.8 puppies and a standard deviation of 1.2 puppies. a. From the information above, what proportion of wolf litters wi...
1 answer
Problem 11-3A Payroll expenses, withholdings, and taxes LO P2, P3 Paloma Co. has four employees. FICA...
Problem 11-3A Payroll expenses, withholdings, and taxes LO P2, P3 Paloma Co. has four employees. FICA Social Security taxes are 6.2% of the first $118,500 paid to each employee, and FICA Medicare taxes are 1.45% of gross pay. Also, for the first $7,000 paid to each employee, the company's FUTA t...
1 answer
A small object P moves along a straight path. The position coordinate of P is known...
A small object P moves along a straight path. The position coordinate of P is known to be x = (t^2 - 2t + 10) meters, where t is time in seconds. The total distance traveled by P in 10s is? 082 m 80 m 90 m 100 m...