Write a class TelephoneNumber that will hold a telephone number. An object of this class will have the attributes
- areaCode—a three-digit integer
- exchangeCode—a three-digit integer
- number—a four-digit integer
and the methods
- TelephoneNumber (aString)—a constructor that creates and returns a new instance of its class, given a string in the form xxx–xxx–xxxx or, if the area code is missing, xxx–xxxx. Throw an exception if the format is not valid. (Hint: To simplify the constructor, you can replace each hyphen in the telephone number with a blank. To accept a telephone number containing hyphens, you could process the string one character at a time or learn how to use Scanner to read words separated by a character—such as a hyphen—other than whitespace.)
- toString—returns a string in either of the two formats shown previously for the constructor.
Using a text editor, create a text file of several telephone numbers, using the two formats described previously. Write a program that reads this file, displays the data on the screen, and creates an array whose base type is TelephoneNumber. Allow the user to either add or delete one telephone number. Write the modified data on the text file, replacing its original contents. Then read and display the numbers in the modified file.
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Additional Engineering Textbook Solutions
Starting Out with C++: Early Objects (9th Edition)
Starting out with Visual C# (4th Edition)
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Concepts Of Programming Languages
- Define a class named CandyDistributor. A CandyDistributor object represents a hungry child who is going to divide a set of candies between herself and her hungry sibling. This will be done fairly by alternating who gets a candy. Each CandyDistributor object should have the following method: public void nextCandy() Each time nextCandy is called, the method prints a message about who gets a candy, and how many candies that person has. Each call to nextCandy produces a single line of output. The code below constructs a CandyDistributor object and calls its nextCandy 8 times. The output appears at right. public class TestCandy { public static void main(String[] args) { CandyDistributor nice = new CandyDistributor(); nice.nextCandy(); // 1 for you. nice.nextCandy(); // 1 for me. nice.nextCandy(); // 2 for you. nice.nextCandy(); // 2 for me. nice.nextCandy(); // 3 for you. nice.nextCandy();…arrow_forwardImplement a BankAccount class. BankAccount must have the following members: A member variable account_no of the type unsigned int.A member variable owner_name of type std :: string.A member variable balance of the double type.A parameterized constructor that takes account_no and owner_name as input parameters.A deposit feature to make a deposit.A withdraw function to make a withdrawal. A withdrawal may only be made if the balance is sufficient. The function must return the balance after withdrawal.The members must be encapsulated in accordance with the usual convention. Overlay the inflow operator (>>) for the BankAccount class Input follows the format:[account_no] [owner_name] [balance] and must be loaded with the inflow operator. Output should have the following format:Account [account_no], owned by [owner_name] has the balance [balance] Note that the square brackets refer to variables or special characters and are not included.You may add necessary designers, member…arrow_forwardDefine a class in java representing physical elements (like hydrogen, helium, lithium,etc.). Elements have a name, a symbol, an integer atomic number, and an atomicweight (which may be fractional). Your class should have a constructor that setsall the properties, and a toString method that prints each element as a nicelyformatted String. Write accessors and mutators for each field if you think it isappropriate to do so (ask yourself if the field should be changeable). Update yourmain method to create and print 10 unique elements.arrow_forward
- write the definition of a class ContestResult containing: an instance variable winner of type String, initialized to a empty string an instance variable second_place of type String, initialized to a empty string an instance variable third_place of type String, initialized to a empty string a method called set_winner that has one parameter, whose value it assigns to the instance variable winner a method called set_second_place that has one parameter, whose value it assigns to the instance variable second_place a method called set_third_place that has one parameter, whose value it assigns to the instance variable third_place a method called get_winner that has no parameter and returns the value of the instance variable winner a method called get_second_place that has no parameter and returns the value of the instance variable second_place a method called get_third_place that has no parameter and returns the value of the instance variable third_place no constructor needed to be defined.…arrow_forwardDesign a class named MyInteger. The class contains:■■ An int data field named value that stores the int value represented bythis object.■■ A constructor that creates a MyInteger object for the specified int value.■■ A getter method that returns the int value.■■ The methods isEven(), isOdd(), and isPrime() that return true if thevalue in this object is even, odd, or prime, respectively.■■ The static methods isEven(int), isOdd(int), and isPrime(int) thatreturn true if the specified value is even, odd, or prime, respectively.■■ The static methods isEven(MyInteger), isOdd(MyInteger), andisPrime(MyInteger) that return true if the specified value is even, odd,or prime, respectively.■■ The methods equals(int) and equals(MyInteger) that return true ifthe value in this object is equal to the specified value.■■ A static method parseInt(char[]) that converts an array of numericcharactersto an int value.■■ A static method parseInt(String) that converts a string into an int value.Draw the UML…arrow_forwardWrite the Circle class:• A Circle has a radius– Add instance variable– 2 constructors – one should be the default or no args– Add accessor and mutator method for each instance variable– Add methods to calculate Area, Perimeter, and Diameter– Write a printArea method in the main program which receives an object circleand “times” integer– printArea() will print the radius and area of objects “times” time by changingthe radius of object.public static void printAreas(Circle c, int times) {System.out.println("Radius \t\t Area");while (times>=1) {//print the radius and area here// create a new object by changing the radius heretimes--;}}//end of printAreasarrow_forward
- Java programming lanaguage A. Design and implement the class Circle that contains the double private data field radius and the following member methods: getRadius that returns the radius of Circle setRadius that sets the radius of Circle area that calculates and return the area of a Circle object (area = π*r²). toString that returns the radius of a Circle object as a string. Two constructors; the first one without arguments. The second constructor with one parameter r. It initializes the radius to r. B.Design and implement the class Cylinder that inherits the class Circle and contains one double private data field height and the following member methods: getHeight that returns the height of Cylinder setHeight that sets the height of Cylinder area that calculates and return the area of a Cylinder object (area = 2*π*r²+2*π*r*h). volume that calculates and return the volume of a Cylinder object (volume = π*r²*h). toString that returns the radius and height of a Cylinder object as…arrow_forwardA class may occasionally be dependent upon itself. In other words, an object of one class engages in communication with an object of the same class. To do this, a method of the class can take an object of the same class as a parameter.An illustration of this scenario is the concat method of the String class. One String object calls the method, while another String object is supplied as an argument. Here's an illustration:str2 = str1.concat;The String object (str1) that executes the method adds its characters to the String (str2) that was supplied as a parameter. As a result, a fresh String object is produced and saved as str3. Write Java code to put the circumstances given into effect.arrow_forwardIn Python, #Create a basket class, which has a class/static attribute pricelist (string/float dictionary - you can hardcode this) #The constructor should set the name to the name passed as a parameter and initialise the contents (empty) #You should provide an addItem method taking product,quantity which adds this to the contents #and a getQuantity method taking a product, which returns the relevant quantityarrow_forward
- Java Object Oriented Programming Create a Shape class that shall have color and filled instance variables of enum Color and boolean type. The filled boolean variable shall show, whether the shape is filled (colored). The enum Color shall be defined within the class having 4 color names (choose any four). Also define getArea and getPerimeter method in Shape class, both shall return 0. Create Rectangle class with length and width attributes and a Circle class with radius attribute, of type double. Properly encapsulate all fields. Circle and Rectangle shall extend Shape class and override getArea and getPerimters methods as per its type and specific formula. In Test class, create an object of Rectangle and Circle, by initializing all attributes in constructor and print both shapes' area and perimters. In Test class, define a static method with following signature, public static void printEachShapeCount(Shape... shapes) As you know, the parent type variable can hold the…arrow_forwardDefine the Circle2D class that contains: Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. A constructor that creates a circle with the specified x, y, and radius. A method getArea() that returns the area of the circle. A method getPerimeter() that returns the perimeter of the circle. A method contains(double x, double y) that returns true if the specified point (x, y) is inside this circle. See Figure 10.14(a). A method contains(Circle2D circle) that returns true if the specified circle is inside this circle. See Figure 10.14(b). A method overlaps(Circle2D circle) that returns true if the specified circle overlaps with this circle. See the figure below. Figure (a) A point is inside the circle. (b) A circle is inside another circle. (c) A circle overlaps another circle. Draw the UML diagram for the class.…arrow_forwardJava - Access Specifiers Create a class named Circle that has attributes radius, area, and circumference and make the attributes private. Make a public method that sets the radius and a method that prints all attributes. Ask the user input for radius. Note: Use the PI from the math functions Inputs A line containing an integer 10 Sample Output Radius: 20 Area: 1256.64 Circumference: 125.66arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,