Question

In: Computer Science

In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...

In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should return "unknown area code"). Write a client class to test all the methods in your class. Look up the getClass method in the Object class and the getName method in the Class class and use them in your client.

(The last time I posted it the answer was in regard to bills not area code, please make sure the answer is accurate)

Solutions

Expert Solution

class telephone_number
{
   String aString;
  
   telephone_number()
   {
       aString="00000";
   }

   void toString(int n)
   {
       aString= String.valueOf(n);

   }

   String equals()
   {
       String str;
       if(aString.length()<3)
       {
           str="Wrong Number";
       }
       else
       {
       str=   aString.substring(0,3);
       }
       return str;
   }
}

class get_substring
{
   public static void main(String args[])
   {
       telephone_number o=new

telephone_number();
       o.toString(123456);
       String s=o.equals();

       System.out.println("Area Code is " + s);
   }
}
              


Related Solutions

Write a class encapsulating the concept of the weather forecast, assuming that it has the following...
Write a class encapsulating the concept of the weather forecast, assuming that it has the following attributes: the temperature and the sky conditions (e.g. sunny, snowy, cloudy, rainy, etc.).  Include a default constructor, an overloaded constructor, accessors and mutators, and the methods, toString() and equals(). Temperature, in Fahrenheit, should be between -50 and +150; the default value is 70, if needed. The default sky condition is sunny. In Addition, include a method that converts Fahrenheit to Celsius. Celsius temperature = (Fahrenheit...
Write a class encapsulating the concept of a Student, assuming that the Student has the following...
Write a class encapsulating the concept of a Student, assuming that the Student has the following attributes: the name of the student, the average of the student, and the student’s GPA. Include a default constructor, an overloaded constructor, the accessors and mutators, and methods toString() and equals(). Also include a method returning the letter grade base on the following range for the average: Average Range Letter Grade 90-100 A 85-89   B+ 80-84 B 75-79 C+ 70-74 C 65-69 D+ 60-64...
Write a class encapsulating the concept of a Student, assuming that a student has the following...
Write a class encapsulating the concept of a Student, assuming that a student has the following attributes: last name, first name, id, array of grades. Include a constructor, the accessors and mutators, and method toString. Also code the following methods: one returning the GPA using the array of grades (assuming each grade represents a course grade and all courses have the same number of credit hours) and a method to add a course grade to the array of grades (this...
Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate...
Write a class encapsulating the concept of a corporate name (for example, IBM), assuming a corporate name has the following attribute: the corporate name. Include a constructor, the accessors and mutators, and methods toString() and equals(). Also include and method that returns a potential domain name by adding a www. at the beginning and .com at the end of the corporate name (for instance, if the corporate name is IBM, that method should return www.ibm.com). Write a client class to...
Java Programming language. Proof of concept class design based on the following ideas Look at your...
Java Programming language. Proof of concept class design based on the following ideas Look at your refrigerator and think about how you would model it as a class. Considerations include: A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables A refrigerator also has storage areas on the door for things like bottled...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept...
LANGUAGE: JAVA Create a New Project called YourLastNameDomainName. Write a DomainName class that encapsulates the concept of a domain name, assuming a domain name has a single attribute: the domain name itself. Include the following: - Constructor: accepts the domain name as an argument. - getDomain: an accessor method for the domain name field. - setDomain: a mutator method for the domain name field. - prefix: a method returning whether or not the domain name starts with www. - extension:...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Java programming language Write a Java application that simulates a test. The test contains at least...
Java programming language Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options. Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example: - create a method to simulate the questions – simulateQuestion - create a method to check the answer – checkAnswer - create a method to display a random message for the user – generateMessage - create a method to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT