Question: Using Methods In this lab you will create and use methods in the ZipCode class. Deliverable A zipped NetBeans project with 2 classes app ZipCode
Using Methods
In this lab you will create and use methods in the ZipCode class.
Deliverable
A zipped NetBeans project with 2 classes
- app
- ZipCode
Classes

Suggestion:
- Use Netbeans to copy your last lab (Lab 02) to a new project called Lab03.
- Close Lab02.
- Work on the new Lab03 project then.
The ZipCode Class
- Attributes
- String fiveDigit
- String plus4
- Constructors
- one constructor with no input parameters
- since it doesn't receive any input values, you need to use the default values below:
- fiveDigit - "00000"
- plus4 - ""
- since it doesn't receive any input values, you need to use the default values below:
- one constructor with one parameter
- one input parameter for fiveDigit
- use the default value from the no-parameter constructor to initialize plus4
- one constructor with all (two) parameters
- one input parameter for each attribute
- one constructor with no input parameters
- Methods (same as last lab)
- public String toString()
- returns this object as a String, i.e., make each attribute a String, concatenate all strings and return as one String.
- toString() is a special method, you will learn more about it in the next lessons
- it needs to be public
- it needs to have @override notation (on the line above the method itself). Netbeans will suggest you do it.
- the toString method will have a similar functionality as App had in the first lab.
- it returns all the data from each object as a String
- if the second attribute, plus4, is blank, display only the first attribute fivedigit, for instance, 16801
- if the second attribute, plus4, is not blank, display only the first attribute fivedigit followed by a "-" and then the plus4 attribute, for instance, 16802-1503
- it returns all the data from each object as a String
- display()
- this method gets the "toString()" value (whatever is returned by toString() ) and uses "System.out.println" to display it.
- you need to decide what is the type of this method
- displayPrefix(int p)
- this method receives an input parameter, an int number p
- based on p's value
- if p's value is 1
- uses "System.out.println" to display the zipcode's prefix, i.e., its 3 first digits.
- if the fiveDigit is "10022", displays "100"
- if p's value is2
- uses "System.out.println" to display the zipcode's area, i.e., its fourth and fifth digits.
- if the fiveDigit is "10022", displays "22"
- for any other value of p, it should not display anything
- if p's value is 1
- public String toString()
The App class
- create a ZipCode object called z1 using the no-parameter constructor
- create a ZipCode object called z2 using the one-parameter constructor with the value
- fiveDigit 90210
- create a ZipCode object called z3 using the two-parameter constructor with the values
- fiveDigit 16802
- plus4 1503
- display all the data from each object using the method display()
- display the prefix of z1 (using input parameter value 1) using the method displayPrefix(int p)
- display the area of z2 (using input parameter value 2) using the method displayPrefix(int p)
- display the area of z3 (using input parameter value 2) using the method displayPrefix(int p)
Output
The output should be similar to
00000 90210 16802-1503 Prefix = 000 Area = 10 Area = 02
App ZipCode Attributes String five Digit; String plus4; Creates a Zip Code object 21 using the no-parameter constructor. - Creates a Zip Code object z2 using the one-parameter constructor. - Creates a Zip Code object z3 using the two-parameter constructor. - Using the method display, display the data from each of the 3 objects - Using the method displayPrefix, display the data from the object z1 using the input parameter 1 display the data from the object z2 using the input parameter 2 display the data from the object z3 using the input parameter 2 Constructors No-parameter One-parameter Two-parameter Methods String toString() void display() void displayPrefix(int p)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
