You need to implement a validation plan to get a valid input from the user for s
ID: 3712239 • Letter: Y
Question
You need to implement a validation plan to get a valid input from the user for state/territory name. A valid state/territory name needs to satisfy ALL the following criteria: The input should be one of the states/territories OR abbreviations (New South Wales, Victoria, Queensland, South Australia, Western Australia, Tasmania, Northerm Territory, Australian Capital Territory, NSW, VIC, QLD, SA, WA, TAS, NT and ACT). - If user have inputted New South Wales, the NSW will be an invalid input or if user have inputted NSW, the New South Wales will be an invalid input, so do other The state/territory name should be case-insensitive START END Confirm to add a state No Yes Information Presenting Prompt to input staterteritory name Display an error message validate No name Yes Prompt to input population Display arn error message No validate Yes Calculate for Largest and smallestpopulation(state/territory) Add stateterritory and to corresponding arraysExplanation / Answer
package checking;
import java.awt.GridLayout;
import java.util.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class StatePolulation
{
final int MAX = 100;
String stateNames[];
int population[];
int counter;
int highestPopulation;
int lowestPopulation;
int totalPopulation;
String highPopulation;
String lowPopulation;
double averagePopulation;
JFrame jf;
static Scanner sc = new Scanner(System.in);
StatePolulation()
{
jf = new JFrame("Inserting Row in a JTable Mechanism");
jf.setSize(500,550);
jf.setVisible(true);
stateNames = new String[MAX];
population = new int[MAX];
}
boolean acceptData()
{
String tempName;
int tempPopulation;
try
{
tempName = JOptionPane.showInputDialog(jf,"Enter State/Territory name: ");
if(tempName.length() == 0)
throw new NullPointerException();
else
stateNames[counter] = tempName;
tempPopulation = Integer.parseInt(JOptionPane.showInputDialog(jf,"Enter Population: "));
if(tempPopulation > 200000 && tempPopulation < 9000000)
population[counter] = tempPopulation;
else
throw new IndexOutOfBoundsException();
}
catch(NullPointerException ne)
{
JOptionPane.showMessageDialog(jf, "State/Territory name cannot be null.",
"State Name Error", JOptionPane.WARNING_MESSAGE);
return false;
}
catch (NumberFormatException ex)
{
JOptionPane.showMessageDialog(jf, "Population must be integer.",
"Population Error", JOptionPane.WARNING_MESSAGE);
return false;
}
catch (IndexOutOfBoundsException ex)
{
JOptionPane.showMessageDialog(jf, "Population must be between 200,000 - 9000,000.",
"Population Error", JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
void calculatePopulation()
{
totalPopulation = population[0];
highestPopulation = population[0];
lowestPopulation = population[0];
for(int x = 1; x < counter; x++)
{
totalPopulation += population[x];
if(population[x] > highestPopulation)
{
highestPopulation = population[x];
highPopulation = stateNames[x];
}
if(population[x] < lowestPopulation)
{
lowestPopulation = population[x];
lowPopulation = stateNames[x];
}
}
try
{
if(counter == 0)
throw new ArithmeticException();
else
averagePopulation = (double)totalPopulation / counter;
}
catch (ArithmeticException ae)
{
JOptionPane.showMessageDialog(jf, "Divide by zero error!.",
"Divide by zero error!", JOptionPane.WARNING_MESSAGE);
}
}
void displayInformation()
{
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel mainP = new JPanel();
String head [] = {"State/Territory", "Population"};
Object data [][] = new Object[counter][2];
DefaultTableModel dtm = new DefaultTableModel(data, head);
JTable table = new JTable(dtm);
JScrollPane scrollPane = new JScrollPane(table);
for(int x = 0; x < counter; x++)
{
dtm.insertRow(x, new Object[] {stateNames[x], population[x]});
}
jp2.add(new JLabel("There are " + counter + " state / territory(s) on the list"));
jp2.add(new JLabel("State / Teritory with largest population: " + highPopulation));
jp2.add(new JLabel("State / Teritory with smallest population: " + lowPopulation));
jp2.add(new JLabel("Total population of entered state / teritory(s): " + totalPopulation));
jp2.add(new JLabel("Average population of entered state / teritory(s): " + averagePopulation));
jp2.setLayout(new GridLayout(5, 1));
jp1.add(new JScrollPane(table));
jp1.add(table);
mainP.add(jp1);
mainP.add(jp2);
mainP.setLayout(new GridLayout(2, 1));
jf.add(mainP);
}
public static void main(String[] args)
{
StatePolulation sp = new StatePolulation();
char choice;
do
{
if(sp.acceptData())
sp.counter++;
choice = JOptionPane.showInputDialog(sp.jf,"Would you like to add more data (Y / N).").charAt(0);
if(choice == 'N' || choice == 'n')
break;
}while(true);
sp.displayInformation();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.