I\'m finishing up a Java Programming assignment and when I run the Tester it\'s
ID: 3857514 • Letter: I
Question
I'm finishing up a Java Programming assignment and when I run the Tester it's not working, I'm unsure of what I missed here. There's 4 classes to it:
==============================================
public class AddressParser
{
//Instance Variable
private final String address;
//Constructor
public AddressParser(String address)
{
validate(address);
this.address = address.trim();
}
//Accessor
public String getAddress()
{
return this.address;
}
//************************ Parsing Methods ************************
// number(), street(), city(), state(), zipCode()
// All return a String value
//(Internal use) Class Constants
private static final char SPACE = ' ';
private static final char COMMA = ',';
public String number(){
return this.address.substring(0, this.address.indexOf(SPACE));
}
public String street(){
return this.address.substring(this.address.indexOf(SPACE),
this.address.indexOf(COMMA)).trim();
}
public String city(){
return this.address.substring(this.address.indexOf(COMMA) + 1,
this.address.lastIndexOf(COMMA)).trim();
}
public String state(){
return this.address.substring(this.address.lastIndexOf(COMMA) + 1).trim().substring(0, 2);
}
public String zipCode(){
return this.address.substring(this.address.lastIndexOf(SPACE) + 1);
}
//********************* Constructor Helper ************************
// Partial validation/check for standard US address format
// AddressException thrown if an invalid format is detected
private static void validate(String address)
{
address = address.trim();
if (address.length() < 16)
throw new AddressException("Invalid Length");
//Indexes of first space and comma
int space1 = address.indexOf(SPACE);
int comma1 = address.indexOf(COMMA);
if (space1 < 0 || comma1 < 0) //No space or no comma
throw new AddressException("Invalid Address Format");
//Indexes of last space and comma
int space2 = address.lastIndexOf(SPACE);
int comma2 = address.lastIndexOf(COMMA);
if (comma2 == comma1 || space2 == space1) //Only 1 space or 1 comma
throw new AddressException("Invalid Address Format");
if (space1 > comma1 || comma2 > space2) //Incorrect comma placement
throw new AddressException("Invalid Address Format");
}
}
========================================================================
public class MiamiAddressParser extends AddressParser
{
//Constructor
public MiamiAddressParser(String address)
{
super(address);
if ( !this.isMiamiFLAddress() )
throw new AddressException("Not Miami, FL");
}
//Return the direction of the street of this Miami, FL address
public String direction()
{
return this.street().substring (0, this.street().indexOf(' '));
}
//Return the name of the street of this Miami, FL address
public String streetName()
{
return this.street().substring (this.street().indexOf(' ') + 1);
}
//Return the zone, NE, NW, SE or SW of this Miami, FL address
public String zone()
{
switch ( this.direction() )
{
case "N" :
case "S" : return this.miamiAvenueZone();
case "E" :
case "W" : return this.flaglerStreetZone();
case "NE":
case "NW":
case "SE":
case "SW": return this.direction();
default : throw new AddressException("Invalid Direction");
}
}
//Helper Method
//Returns the zone, NE, NW, SE, or SW, of this Flagler Street address
private String flaglerStreetZone()
{
if ( !this.streetName().toUpperCase().contains("FLAGLER") )
throw new AddressException("Flagler St Expected");
return this.direction();
}
//Helper Method
//Returns the zone, NE, NW, SE, or SW, of this Miami Avenue address
private String miamiAvenueZone()
{
if ( !this.streetName().toUpperCase().contains("MIAMI") )
throw new AddressException("Miami Ave Expected");
return this.direction();
}
//Helper Method
//Returns true if this address is in Miami, FL; returns false otherwise
private boolean isMiamiFLAddress()
{
return this.city().contains("Miami");
}
}
=========================================================================
public class AddressException extends RuntimeException
{
public AddressException()
{
super();
}
public AddressException(String message)
{
super(message);
}
}
=========================================================================
import javax.swing.JOptionPane;
public class MiamiAddressParserTester
{
public static void main(String[] args)
{
String prompt = "Enter a Miami, FL address in standard format" +
" number street , city , state zipcode " +
" (Click Cancel to quit)" ;
String input;
do
{
input = JOptionPane.showInputDialog(null, prompt);
if (input != null)
{
try
{
MiamiAddressParser parser = null;
JOptionPane.showMessageDialog(null, profile(parser));
}
catch (AddressException axe)
{
System.out.println(input + " " + axe.getMessage());
}
}
} while (input != null);
}
//Create an address profile of a Miami FL address
//For example:
/* **********************************************
ADDRESS PROFILE
11200 SW 8 St, Miami, FL 33199
Number: 11200
Street: SW 8 St
City: Miami
State: FL
ZipCode: 33199
Zone: SW
*************************************************/
//@Parameter map: A parser for a Miami, FL address
private static String profile(MiamiAddressParser map)
{
return "Address Profile" + " " + map.getAddress() + " Number: " +
map.number() + " Street: " + map.street() + " City: " +
map.city() + " State: " + map.state() + " ZipCode: " +
map.state() + " Zone: " + map.zone();
}
}
Explanation / Answer
Main issue is in the MiamiAddressParserTester class where null MiamiAddressParser instance reference is passing to frofile static method.
MiamiAddressParser parser = null;
JOptionPane.showMessageDialog(null, profile(parser));
In above code snipet parser value is null. Insated of that propoer MiamiAddressParser class reference should be passed.
Please find the updated line mentioned below
MiamiAddressParser parser = new MiamiAddressParser(input);
JOptionPane.showMessageDialog(null, profile(parser));
For the refernce please find the updated MiamiAddressParserTester class
package com.chegg;
import javax.swing.JOptionPane;
public class MiamiAddressParserTester {
public static void main(String[] args) {
String prompt = "Enter a Miami, FL address in standard format"
+ " number street , city , state zipcode "
+ " (Click Cancel to quit)";
String input;
do {
input = JOptionPane.showInputDialog(null, prompt);
if (input != null) {
try {
MiamiAddressParser parser = new MiamiAddressParser(input);
JOptionPane.showMessageDialog(null, profile(parser));
} catch (AddressException axe) {
System.out.println(input + " " + axe.getMessage());
}
}
} while (input != null);
}
// Create an address profile of a Miami FL address
// For example:
/* **********************************************
* ADDRESS PROFILE 11200 SW 8 St, Miami, FL 33199 Number: 11200 Street: SW 8
* St City: Miami State: FL ZipCode: 33199 Zone: SW
* ***********************************************
*/
// @Parameter map: A parser for a Miami, FL address
private static String profile(MiamiAddressParser map) {
return "Address Profile" + " " + map.getAddress() + " Number: "
+ map.number() + " Street: " + map.street() + " City: "
+ map.city() + " State: " + map.state() + " ZipCode: "
+ map.state() + " Zone: " + map.zone();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.