Question

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 the record is not found.

.

.

Individual.java

import java.util.Scanner;

public class Individual
{
   private String name;
   private String phonenum;

   public Individual()
   {
       name=" ";
       phonenum=" ";
   }
   public Individual(String n1, String p1)
   {
       this.name=n1;
       this.phonenum=p1;
   }
   public void setName(String n1)
   {
       this.name=n1;
   }
   public void setNumber(String p1)
   {
       this.phonenum=p1;
   }
   public String getName()
   {
       return name;
   }
   public String getNumber()
   {
       return phonenum;
   }
   public String toString()
   {
       return "Name: "+name+"\nPhone Number: "+phonenum;
   }

}

Phonebook.java

import java.util.ArrayList;
import java.util.List;

public class Phonebook
{
private List info;

public Phonebook()
{
info = new ArrayList();
}
public Individual findName(String s)
{
for (Individual c : info)
{
if (c.getName().equalsIgnoreCase(s))
{
return c;
}
}
return null;
}
}

Demo.java

import java.util.Scanner;

public class Demo
{
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);

       Phonebook contact1 = new Phonebook();
       contact1.setName("Alfred");
       contact1.setNumber("768-217-3945");

       String choice = "";

       while (!choice.equalsIgnoreCase("n"))
       {
           System.out.println("Would you like to search for a contact from the phone book?\nIf you would like to type y/Y or n/N to exit.");
           System.out.println("");

           choice = input.nextLine();

           if (choice.equalsIgnoreCase("y"))
           {
               System.out.print("Enter the name: ");
               String search = input.nextLine();

               Individual result = contact1.findName(search);
               if (result != null)
                   {
                       System.out.println("The name was found.");
                       System.out.println(result);
                   }
               else
                   {
                       System.out.println("Not found!");
                   }
           }
       }
   }
}

Errors:

inclass6_2.java:18: error: '(' expected
public void String setName(String n1)
^
inclass6_2.java:22: error: '(' expected
public void String setphonenum(String p1)
^
inclass6_2.java:30: error: '(' expected
public void String getphonenum()
^
3 errors

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Individual.java

import java.util.Scanner;

public class Individual
{
   //Declaring instance variables
private String name;
private String phonenum;

//Zero argumented constructor
public Individual()
{
name=" ";
phonenum=" ";
}
//Parameterized constructor
public Individual(String n1, String p1)
{
this.name=n1;
this.phonenum=p1;
}

// getters and setters
public void setName(String n1)
{
this.name=n1;
}
public void setNumber(String p1)
{
this.phonenum=p1;
}
public String getName()
{
return name;
}
public String getNumber()
{
return phonenum;
}

//toString method is used to display the contents of an object inside it
public String toString()
{
return "Name: "+name+"\nPhone Number: "+phonenum;
}

}

___________________________

// Phonebook.java

import java.util.ArrayList;
import java.util.List;

public class Phonebook {
   private List<Individual> info=null;

   public Phonebook() {
       info = new ArrayList<Individual>();
   }

   public int addContact(Individual contact)
   {
       Individual in=findName(contact.getName());
       if(in==null)
       {
           info.add(contact);
          
           return 1;
       }
       return -1;
   }
   public Individual findName(String s) {
       for (Individual c : info) {
           if (c.getName().equalsIgnoreCase(s)) {
               return c;
           }
       }
       return null;
   }
}

_______________________________

// Demo.java

import java.util.Scanner;

public class Demo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

Phonebook book = new Phonebook();
Individual in1=new Individual();
in1.setName("Alfred");
in1.setNumber("768-217-3945");
book.addContact(in1);

Individual in2=new Individual("Williams","343-233-3443");
book.addContact(in2);
  

String choice = "";

while (!choice.equalsIgnoreCase("n"))
{
System.out.println("Would you like to search for a contact from the phone book?\nIf you would like to type y/Y or n/N to exit.");
System.out.println("");

choice = input.nextLine();

if (choice.equalsIgnoreCase("y"))
{
System.out.print("Enter the name: ");
String search = input.nextLine();

Individual result = book.findName(search);
if (result != null)
{
System.out.println("The name was found.");
System.out.println(result);
}
else
{
System.out.println("Not found!");
}
}
}
}
}

____________________________

Output:

Would you like to search for a contact from the phone book?
If you would like to type y/Y or n/N to exit.

y
Enter the name: Williams
The name was found.
Name: Williams
Phone Number: 343-233-3443
Would you like to search for a contact from the phone book?
If you would like to type y/Y or n/N to exit.

y
Enter the name: James
Not found!
Would you like to search for a contact from the phone book?
If you would like to type y/Y or n/N to exit.

n

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
JAVA help Create a class Individual, which has: Instance variables for name and phone number of...
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
  • How to create a constructor that uses parameters from different classes? I have to create a...

    How to create a constructor that uses parameters from different classes? I have to create a constructor for the PrefferedCustomer class that takes parameters(name, address,phone number, customer id, mailing list status, purchase amount) but these parameters are in superclasses Person and Customer. I have to create an object like the example below....... PreferredCustomer preferredcustomer1 = new PreferredCustomer("John Adams", "Los Angeles, CA", "3235331234", 933, true, 400); System.out.println(preferredcustomer1.toString() + "\n"); public class Person { private String name; private String address; private long...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...

    GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...

  • I have a java class that i need to rewrite in python. this is what i...

    I have a java class that i need to rewrite in python. this is what i have so far: class Publisher: __publisherName='' __publisherAddress=''    def __init__(self,publisherName,publisherAddress): self.__publisherName=publisherName self.__publisherAddress=publisherAddress    def getName(self): return self.__publisherName    def setName(self,publisherName): self.__publisherName=publisherName    def getAddress(self): return self.__publisherAddress    def setAddress(self,publisherAddress): self.__publisherAddress=publisherAddress    def toString(self): and here is the Java class that i need in python: public class Publisher { //Todo: Publisher has a name and an address. private String name; private String address; public Publisher(String...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • I need help with my IM (instant messaging) java program, I created the Server, Client, and...

    I need help with my IM (instant messaging) java program, I created the Server, Client, and Message class. I somehow can't get both the server and client to message to each other. I am at a roadblock. Here is the question below. Create an IM (instant messaging) java program so that client and server communicate via serialized objects (e.g. of type Message). Each Message object encapsulates the name of the sender and the response typed by the sender. You may...

  • ble and instance variable, and then use clas 4. Please discuss the difference between class variable...

    ble and instance variable, and then use clas 4. Please discuss the difference between class variable and instant variable to complete the following code for the count of number of stud public class Student _//q1: fill this line to create a class variable num. public Student 1/ 22: fill this line to increase num. public static int getNum() return num; public static void main(String[] args) { Student stl=new Student O; Student st2-new Student ; Student st3-new Student ; //q3: fill...

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