Phone Book Arraylist 1,Write a class named phoneBookEntry that has fields for a
ID: 3546018 • Letter: P
Question
Phone Book Arraylist
1,Write a class named phoneBookEntry that has fields for a person's name and phone number.The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creats at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList.
Rainfall Class
2, write a RainFall class that stores the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following :
* the total rainfall for the year
* the average monthly rainfall
* the month with the most rain
* the month with the least rain
Demonstrate the class in a complete program.
Input Validation: Do not accept negative numbers for the monthly rainfall figures.
Explanation / Answer
import java.util.ArrayList;
import java.util.Scanner;
public class PhoneBookEntry {
public PhoneBookEntry(String name, String phoneNo){
this.name = name;
this.phoneNo = phoneNo;
}
public PhoneBookEntry(){
}
private String name;
private String phoneNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
}
class MainClass{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the entries you want to make");
int n = in.nextInt();
if(n<5){
System.out.println("Enter again, must be more tha n 5");
n = in.nextInt();
}
ArrayList<PhoneBookEntry> list = new ArrayList<PhoneBookEntry>();
for(int i=0; i<n;i++){
PhoneBookEntry ob = new PhoneBookEntry();
System.out.println("Enter the name:");
ob.setName(in.next());
System.out.println("Enter the number");
ob.setPhoneNo(in.next());
list.add(ob);
}
for(PhoneBookEntry x: list){
System.out.println(x.getName()+" "+x.getPhoneNo());
}
}
}