Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help with correcting errors in Java program. I am having a problem with the iden

ID: 3825074 • Letter: H

Question

Help with correcting errors in Java program. I am having a problem with the idenifiers. The program below keeps giving me these errors:


----jGRASP exec: javac -g IrregularPolyPanel.java

IrregularPolyPanel.java:16: error: cannot find symbol
private ArbitraryPolygon shape = new ArbitraryPolygon();
^
symbol: class ArbitraryPolygon
location: class IrregularPolyPanel
IrregularPolyPanel.java:28: error: cannot find symbol
private BoxesPanel drawPanel;
^
symbol: class BoxesPanel
location: class IrregularPolyPanel
IrregularPolyPanel.java:16: error: cannot find symbol
private ArbitraryPolygon shape = new ArbitraryPolygon();
^
symbol: class ArbitraryPolygon
location: class IrregularPolyPanel
IrregularPolyPanel.java:160: error: cannot find symbol
drawPanel = new BoxesPanel(npoints, xpoints, ypoints, 100);
^
symbol: class BoxesPanel
location: class IrregularPolyPanel.ButtonListener
4 errors

-------------------------------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.text.DecimalFormat;
import java.awt.Polygon;
import java.awt.Graphics;

public class IrregularPolyPanel extends JPanel
{
private JLabel Intro, numPointsLabel, area, perimeter;
private ArbitraryPolygon shape = new ArbitraryPolygon();
private int npoints;
private int[] xpoints;
private int[] ypoints;
private String[] numbers;
private JLabel Xpoints;
private JTextField[] XPoints;
private JLabel Ypoints;
private JTextField[] YPoints;
private JTextField numPoints;
private JButton enter;
private JPanel pane;
private BoxesPanel drawPanel;
private double thetaInc;
private double theta;

public IrregularPolyPanel()
{

setLayout(new BorderLayout());
  
numPointsLabel = new JLabel ("Number of Points:");
area = new JLabel("Area:");
perimeter = new JLabel ("Perimeter:");
  
  
enter = new JButton("Enter All Parameters");
enter.addActionListener(new ButtonListener());
  
numPoints = new JTextField(5);
numPoints.addActionListener(new ButtonListener());
  
pane = new JPanel (new GridLayout(0,1,1,1));
pane.setBorder(new TitledBorder("Arbitrary Polygon Parameters"));
  
pane.add(numPointsLabel);
pane.add(numPoints);
pane.add(area);
pane.add(perimeter);
pane.add(enter);
  
add(pane, BorderLayout.WEST);
  
}
  


private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String NUM_POINTS;
int result;
  
if(event.getSource() == enter)
{
NUM_POINTS = numPoints.getText();
if(NUM_POINTS.equals(""))
npoints = 0;
else
npoints = Integer.parseInt(NUM_POINTS);
if(npoints < 3)
JOptionPane.showMessageDialog(null, "Invalid entry: Number of points must be more than 2.", "Error", JOptionPane.ERROR_MESSAGE);
else
{
xpoints = new int[npoints];
ypoints = new int[npoints];
numbers = new String[npoints];
XPoints = new JTextField[npoints];
YPoints = new JTextField[npoints];

JPanel numPanel = new JPanel(new GridLayout((npoints+1), 2));
Xpoints = new JLabel(" Xpoints");
Ypoints = new JLabel(" Ypoints");
numPanel.add(Xpoints);
numPanel.add(Ypoints);
for(int i = 0; i < npoints; i++)
{
  
XPoints[i] = new JTextField(5);
YPoints[i] = new JTextField(5);
numPanel.add(XPoints[i]);
numPanel.add(YPoints[i]);
}
result = JOptionPane.showConfirmDialog(null, numPanel, "Enter Points", JOptionPane.YES_NO_OPTION);

boolean check = false;
int counter = 0;
while((result != JOptionPane.NO_OPTION) && (counter < npoints))
{
System.out.println(counter);
if(((XPoints[counter].getText()).equals("")) || ((YPoints[counter].getText()).equals("")))
xpoints[counter] = ypoints[counter] = 0;
else
{
xpoints[counter] = Integer.parseInt(XPoints[counter].getText());
ypoints[counter] = Integer.parseInt(YPoints[counter].getText());
}
if((xpoints[counter] < 1) || (ypoints[counter] < 1))
{
check = false;
JOptionPane.showMessageDialog(null, "Invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
result = JOptionPane.showConfirmDialog(null, numPanel, "Enter Points", JOptionPane.YES_NO_OPTION);   
}
else
{
xpoints[counter] = Integer.parseInt(XPoints[counter].getText());
ypoints[counter] = Integer.parseInt(YPoints[counter].getText());
counter++;
check = true;   
}
}

if(check == true)
{
shape.setNumPoints(npoints);
shape.addPoints(xpoints, ypoints, npoints);
  
thetaInc = 2 * Math.PI / npoints;
theta = (npoints % 2 == 0) ? thetaInc : -Math.PI/2;

for(int i = 0; i < npoints; i++)
{
  
int x1 = xpoints[i] - 100;
int y1 = ypoints[i] - 100;

xpoints[i] = 100+ (int) ((x1 * Math.cos(theta)) - (y1 * Math.sin(theta))) + 200;
ypoints[i] = 100+ (int) ((x1 * Math.sin(theta)) + (y1 * Math.cos(theta))) + 300;

theta += thetaInc;
}
  
double Perimeter = shape.getPerimeterLength();
double Area = shape.getArea();
  
DecimalFormat df = new DecimalFormat("#.##");
  
area.setText("Area: " + df.format(Area));
perimeter.setText("Perimeter: " + df.format(Perimeter));
  
//-----------------------------------------------------------------
// Draw the Arbitrary Polygon
//-----------------------------------------------------------------
drawPanel = new BoxesPanel(npoints, xpoints, ypoints, 100);
add(drawPanel, BorderLayout.CENTER);
}
}
}
}
}
}

Explanation / Answer


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.text.DecimalFormat;
import java.awt.Polygon;
import java.awt.Graphics;

public class IrregularPolyPanel extends JPanel {

private JLabel Intro, numPointsLabel, area, perimeter;
//private ArbitraryPolygon shape = new ArbitraryPolygon();
private ArbitraryPolygon shape = new ArbitraryPolygon();
private int npoints;
private int[] xpoints;
private int[] ypoints;
private String[] numbers;
private JLabel Xpoints;
private JTextField[] XPoints;
private JLabel Ypoints;
private JTextField[] YPoints;
private JTextField numPoints;
private JButton enter;
private JPanel pane;
private BoxesPanel drawPanel;
private double thetaInc;
private double theta;

public IrregularPolyPanel() {

setLayout(new BorderLayout());

numPointsLabel = new JLabel("Number of Points:");
area = new JLabel("Area:");
perimeter = new JLabel("Perimeter:");

enter = new JButton("Enter All Parameters");
enter.addActionListener(new ButtonListener());

numPoints = new JTextField(5);
numPoints.addActionListener(new ButtonListener());

pane = new JPanel(new GridLayout(0, 1, 1, 1));
pane.setBorder(new TitledBorder("Arbitrary Polygon Parameters"));

pane.add(numPointsLabel);
pane.add(numPoints);
pane.add(area);
pane.add(perimeter);
pane.add(enter);

add(pane, BorderLayout.WEST);

}

private class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent event) {
String NUM_POINTS;
int result;

if (event.getSource() == enter) {
NUM_POINTS = numPoints.getText();
if (NUM_POINTS.equals("")) {
npoints = 0;
} else {
npoints = Integer.parseInt(NUM_POINTS);
}
if (npoints < 3) {
JOptionPane.showMessageDialog(null, "Invalid entry: Number of points must be more than 2.", "Error", JOptionPane.ERROR_MESSAGE);
} else {
xpoints = new int[npoints];
ypoints = new int[npoints];
numbers = new String[npoints];
XPoints = new JTextField[npoints];
YPoints = new JTextField[npoints];

JPanel numPanel = new JPanel(new GridLayout((npoints + 1), 2));
Xpoints = new JLabel(" Xpoints");
Ypoints = new JLabel(" Ypoints");
numPanel.add(Xpoints);
numPanel.add(Ypoints);
for (int i = 0; i < npoints; i++) {

XPoints[i] = new JTextField(5);
YPoints[i] = new JTextField(5);
numPanel.add(XPoints[i]);
numPanel.add(YPoints[i]);
}
result = JOptionPane.showConfirmDialog(null, numPanel, "Enter Points", JOptionPane.YES_NO_OPTION);

boolean check = false;
int counter = 0;
while ((result != JOptionPane.NO_OPTION) && (counter < npoints)) {
System.out.println(counter);
if (((XPoints[counter].getText()).equals("")) || ((YPoints[counter].getText()).equals(""))) {
xpoints[counter] = ypoints[counter] = 0;
} else {
xpoints[counter] = Integer.parseInt(XPoints[counter].getText());
ypoints[counter] = Integer.parseInt(YPoints[counter].getText());
}
if ((xpoints[counter] < 1) || (ypoints[counter] < 1)) {
check = false;
JOptionPane.showMessageDialog(null, "Invalid entry", "Error", JOptionPane.ERROR_MESSAGE);
result = JOptionPane.showConfirmDialog(null, numPanel, "Enter Points", JOptionPane.YES_NO_OPTION);
} else {
xpoints[counter] = Integer.parseInt(XPoints[counter].getText());
ypoints[counter] = Integer.parseInt(YPoints[counter].getText());
counter++;
check = true;
}
}

if (check == true) {
shape.setNumPoints(npoints);
shape.addPoints(xpoints, ypoints, npoints);

thetaInc = 2 * Math.PI / npoints;
theta = (npoints % 2 == 0) ? thetaInc : -Math.PI / 2;

for (int i = 0; i < npoints; i++) {

int x1 = xpoints[i] - 100;
int y1 = ypoints[i] - 100;

xpoints[i] = 100 + (int) ((x1 * Math.cos(theta)) - (y1 * Math.sin(theta))) + 200;
ypoints[i] = 100 + (int) ((x1 * Math.sin(theta)) + (y1 * Math.cos(theta))) + 300;

theta += thetaInc;
}

double Perimeter = shape.getPerimeterLength();
double Area = shape.getArea();

DecimalFormat df = new DecimalFormat("#.##");

area.setText("Area: " + df.format(Area));
perimeter.setText("Perimeter: " + df.format(Perimeter));

//-----------------------------------------------------------------
// Draw the Arbitrary Polygon
//-----------------------------------------------------------------
drawPanel = new BoxesPanel(npoints, xpoints, ypoints, 100);
add(drawPanel, BorderLayout.CENTER);
}
}
}
}
}
}

this error is coming as the classes named as ArbitraryPolygon and BoxPanel are either not created or not rechable as they are not the previously defined class of awt package and their functions are also not available in the polygon class which is previously defined in the awt package

so either create these classes and then use them by creating their objects as per your requiremnt or you can use the predefined classes of awt pacage with their predefined methods and constructors.

Good Luck!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote