i have a java error problem, can someone help me with it? the problem is the pub
ID: 3647280 • Letter: I
Question
i have a java error problem, can someone help me with it? the problem is the public Math(JButton buttons[]) {/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package math;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.JFrame;
/**
*
* @author Owner
*/
public class Math {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
JButton buttons[];
String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"};
public Math(JButton buttons[]) {
setTitle("Math using JOptionPane");
setSize(320,120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttons = new JButton[4];
Container pane = getContentPane();
setContentPane(pane);
GridLayout grid = new GridLayout(2,2);
pane.setLayout(grid);
String operation[] = {"Addition [+]","Subtraction [-]","Multiplication [x]","Division [/]"};
for(int count=0; count<buttons.length; count++) {
buttons[count] = new JButton(operation[count]);
pane.add(buttons[count]);
}
buttons[0].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 + num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
buttons[1].addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 - num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Implemeting Even-Listener on JButton button[2] which is multiplication
buttons[2].addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 * num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Implemeting Even-Listener on JButton button[3] which is division
buttons[3].addActionListener(
new ActionListener() {
//Handle JButton event if it is clicked
public void actionPerformed(ActionEvent event) {
try {
String input1, input2;
int num1, num2, result;
input1 = JOptionPane.showInputDialog("Please Input First Number: ");
input2 = JOptionPane.showInputDialog("Please Input Second Number: ");
num1 = Integer.parseInt(input1);
num2 = Integer.parseInt(input2);
result = num1 / num2;
JOptionPane.showMessageDialog(null,result,"Result:",JOptionPane.INFORMATION_MESSAGE);
} catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "Please Input a Integer","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
);
buttons[0].setMnemonic('A');
buttons[1].setMnemonic('S');
buttons[2].setMnemonic('M');
buttons[3].setMnemonic('D');
/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);
setResizable(false); //Fix window height and width
}
private Math() {
throw new UnsupportedOperationException("Not yet implemented");
}
private void setContentPane(Container pane) {
throw new UnsupportedOperationException("Not yet implemented");
}
Math pjtf = new Math();
private void setVisible(boolean b) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void setResizable(boolean b) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void setSize(int i, int i0) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void setDefaultCloseOperation(int EXIT_ON_CLOSE) {
throw new UnsupportedOperationException("Not yet implemented");
}
private Container getContentPane() {
throw new UnsupportedOperationException("Not yet implemented");
}
private void setTitle(String math_using_JOptionPane) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
Explanation / Answer
Main.java:1: class, interface, or enum expected { ^ Main.java:29: illegal start of expression public Math(JButton buttons[]) { ^ Main.java:29: ')' expected public Math(JButton buttons[]) { ^ Main.java:29: illegal start of expression public Math(JButton buttons[]) { ^ Main.java:29: ';' expected public Math(JButton buttons[]) { ^ Main.java:29: illegal start of expression public Math(JButton buttons[]) { ^ Main.java:29: ';' expected public Math(JButton buttons[]) { ^ 7 errors ______________________________________________ Watch this and get help from import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.lang.Math; public class Mandel extends JFrame implements ActionListener, ItemListener { JButton reset=new JButton("Reset"); JButton back=new JButton("Back"); JButton go=new JButton("Go!"); JButton cancel=new JButton("Cancel"); JButton clear=new JButton("Clear"); JButton up=new JButton("Up"); JButton down=new JButton("Down"); JButton left=new JButton("Left"); JButton right=new JButton("Right"); JComboBox col=new JComboBox(); JPanel buttons = new JPanel(); JPanel topbuttons = new JPanel(); PJPanel plot = new PJPanel(); JTextField cd=new JTextField(""+plot.depth, 4); JTextField th=new JTextField(""+plot.thresh, 2); JTextField zoom= new JTextField("1", 4); JTextField tbox=new JTextField("-1.5", 6); JTextField bbox=new JTextField("1.5", 6); JTextField lbox=new JTextField("-2.5", 6); JTextField rbox=new JTextField("1.5", 6); double zoomf; static Mandel mandel = new Mandel(); public static void main(String[] args) { mandel.setSize(800,700); mandel.setVisible(true); WindowListener wd = new WindowAdapter() { public void windowClosing(WindowEvent e) { e.getWindow().dispose(); System.exit(0); } }; mandel.addWindowListener(wd); } public Mandel() { this.setTitle("Phil's Mandelbrot Generator"); Container c=getContentPane(); c.setLayout(new BorderLayout()); buttons.setLayout(new FlowLayout()); buttons.add(left); buttons.add(right); // Colour base buttons.add(new Label("Colour base:")); col.addItem("Blue"); col.addItem("Red"); col.addItem("Green"); buttons.add(col); // Colour depth buttons.add(new Label("Colour depth:")); buttons.add(cd); // Threshold buttons.add(new Label("Threshold:")); buttons.add(th); // Buttons buttons.add(reset); buttons.add(back); buttons.add(go); buttons.add(cancel); // TOP PANEL topbuttons.add(up); topbuttons.add(down); topbuttons.add(new Label("Start:")); topbuttons.add(lbox); topbuttons.add(new Label("+")); topbuttons.add(tbox); topbuttons.add(new Label("i; End:")); topbuttons.add(rbox); topbuttons.add(new Label("+")); topbuttons.add(bbox); topbuttons.add(new Label("i")); topbuttons.add(new Label("Zoom")); topbuttons.add(zoom); topbuttons.add(clear); // Listeners reset.addActionListener(this); back.addActionListener(this); cd.addActionListener(this); th.addActionListener(this); col.addItemListener(this); go.addActionListener(this); cancel.addActionListener(this); clear.addActionListener(this); tbox.addActionListener(this); bbox.addActionListener(this); lbox.addActionListener(this); rbox.addActionListener(this); zoom.addActionListener(this); up.addActionListener(this); down.addActionListener(this); left.addActionListener(this); right.addActionListener(this); // Add the panels to the frame c.add(buttons,BorderLayout.SOUTH); c.add(topbuttons, BorderLayout.NORTH); c.add(plot,BorderLayout.CENTER); c.setSize(800,700); setSize(800,700); setContentPane(c); } public void itemStateChanged(ItemEvent e) { String fred=(String)e.getItem(); if(fred=="Red") { if(plot.isred==false) plot.isred=true; else plot.isred=false; } if(fred=="Green") { if(plot.isgreen==false) plot.isgreen=true; else plot.isgreen=false; } if(fred=="Blue") { if(plot.isblue==false) plot.isblue=true; else plot.isblue=false; } plot.initCols(); plot.repaint(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==reset) { tbox.setText("-1.5"); bbox.setText("1.5"); lbox.setText("-2.5"); rbox.setText("1.5"); zoom.setText("1"); plot.depth=Integer.parseInt(cd.getText()); if(plot.depth89440) { plot.depth=89440; cd.setText("89440"); } plot.reset(); } else if(e.getSource()==back) { plot.bottom=plot.obottom; plot.top=plot.otop; plot.left=plot.oleft; plot.right=plot.oright; tbox.setText(""+plot.ntop); bbox.setText(""+plot.nbot); lbox.setText(""+plot.nleft); rbox.setText(""+plot.nright); zoomf=4.0/(Math.abs(plot.nright-plot.nleft)); zoom.setText(""+zoomf); tbox.setCaretPosition(0); bbox.setCaretPosition(0); lbox.setCaretPosition(0); rbox.setCaretPosition(0); zoom.setCaretPosition(0); plot.dstart.x=0; plot.dnow.x=0; plot.dstart.y=0; plot.dnow.y=0; plot.cflag=0; plot.flag=0; plot.repaint(); } if(e.getSource()==go || e.getSource()==tbox || e.getSource()==bbox || e.getSource()==lbox || e.getSource()==rbox || e.getSource()== zoom || e.getSource()==th || e.getSource()==cd) { plot.thresh=Integer.parseInt(th.getText()); if(plot.thresh99) { plot.thresh=9; cd.setText("99"); } plot.depth=Integer.parseInt(cd.getText()); if(plot.depth89440) { plot.depth=89440; cd.setText("89440"); } // If there is no zoom box, get zoom points from text fields, if they're not empty if (plot.zflag==0 &&! (tbox.getText().length()==0 || lbox.getText().length()==0)) { plot.ntop=Double.parseDouble(tbox.getText()); plot.nleft=Double.parseDouble(lbox.getText()); // If both of the other boxes are empty, calculate values from zoom if (bbox.getText().length()==0 && rbox.getText().length()==0) { if(zoom.getText().length()!=0) zoomf=Double.parseDouble(zoom.getText()); plot.nright=plot.nleft+(4.0/zoomf); plot.nbot=plot.ntop+(3.0/zoomf); } else // If not, read the boxes and make sure we keep the aspect ratio { if(bbox.getText().length()!=0) plot.nbot=Double.parseDouble(bbox.getText()); else plot.nbot=plot.ntop; if(rbox.getText().length()!=0) plot.nright=Double.parseDouble(rbox.getText()); else plot.nright=plot.nleft; double yrange=Math.abs(plot.nbot-plot.ntop)/600.0f; double xrange=Math.abs(plot.nright-plot.nleft)/800.0f; if(xrange>yrange) plot.nbot=(plot.ntop+xrange*600.0f); else if (yrange>xrange) plot.nright=(plot.nleft+yrange*800.0f); } } // Put new values in text fields and plot tbox.setText(""+plot.ntop); bbox.setText(""+plot.nbot); lbox.setText(""+plot.nleft); rbox.setText(""+plot.nright); // Calculate zoom, i.e. width of screen / 4 zoomf=4.0/(Math.abs(plot.nright-plot.nleft)); zoom.setText(""+zoomf); tbox.setCaretPosition(0); bbox.setCaretPosition(0); lbox.setCaretPosition(0); rbox.setCaretPosition(0); zoom.setCaretPosition(0); plot.dogo(); plot.dstart.x=0; plot.dnow.x=0; plot.dstart.y=0; plot.dnow.y=0; } else if(e.getSource()==cancel) { plot.killLast(); plot.zflag=0; plot.ntop=plot.top; plot.nbot=plot.bottom; plot.nleft=plot.left; plot.nright=plot.right; } else if(e.getSource()==clear) { tbox.setText(""); bbox.setText(""); lbox.setText(""); rbox.setText(""); zoom.setText(""); } else if(e.getSource()==up) { double yrange=Math.abs(plot.bottom-plot.top); plot.ntop=plot.top-yrange/4.0; plot.nbot=plot.bottom-yrange/4.0; tbox.setText(""+plot.ntop); bbox.setText(""+plot.nbot); lbox.setText(""+plot.nleft); rbox.setText(""+plot.nright); // Calculate zoom, i.e. width of screen / 4 zoomf=4.0/(Math.abs(plot.nright-plot.nleft)); zoom.setText(""+zoomf); tbox.setCaretPosition(0); bbox.setCaretPosition(0); lbox.setCaretPosition(0); rbox.setCaretPosition(0); zoom.setCaretPosition(0); plot.dogo(); plot.dstart.x=0; plot.dnow.x=0; plot.dstart.y=0; plot.dnow.y=0; } else if(e.getSource()==down) { double yrange=Math.abs(plot.bottom-plot.top); plot.ntop=plot.top+yrange/4.0; plot.nbot=plot.bottom+yrange/4.0; tbox.setText(""+plot.ntop); bbox.setText(""+plot.nbot); lbox.setText(""+plot.nleft); rbox.setText(""+plot.nright); // Calculate zoom, i.e. width of screen / 4 zoomf=4.0/(Math.abs(plot.nright-plot.nleft)); zoom.setText(""+zoomf); tbox.setCaretPosition(0); bbox.setCaretPosition(0); lbox.setCaretPosition(0); rbox.setCaretPosition(0); zoom.setCaretPosition(0); plot.dogo(); plot.dstart.x=0; plot.dnow.x=0; plot.dstart.y=0; plot.dnow.y=0; } else if(e.getSource()==left) { double xrange=Math.abs(plot.right-plot.left); plot.nleft=plot.left-xrange/4.0; plot.nright=plot.right-xrange/4.0; tbox.setText(""+plot.ntop); bbox.setText(""+plot.nbot); lbox.setText(""+plot.nleft); rbox.setText(""+plot.nright); // Calculate zoom, i.e. width of screen / 4 zoomf=4.0/(Math.abs(plot.nright-plot.nleft)); zoom.setText(""+zoomf); tbox.setCaretPosition(0); bbox.setCaretPosition(0); lbox.setCaretPosition(0); rbox.setCaretPosition(0); zoom.setCaretPosition(0); plot.dogo(); plot.dstart.x=0; plot.dnow.x=0; plot.dstart.y=0; plot.dnow.y=0; } else if(e.getSource()==right) { double xrange=Math.abs(plot.right-plot.left); plot.nleft=plot.left+xrange/4.0; plot.nright=plot.right+xrange/4.0; tbox.setText(""+plot.ntop); bbox.setText(""+plot.nbot); lbox.setText(""+plot.nleft); rbox.setText(""+plot.nright); // Calculate zoom, i.e. width of screen / 4 zoomf=4.0/(Math.abs(plot.nright-plot.nleft)); zoom.setText(""+zoomf); tbox.setCaretPosition(0); bbox.setCaretPosition(0); lbox.setCaretPosition(0); rbox.setCaretPosition(0); zoom.setCaretPosition(0); plot.dogo(); plot.dstart.x=0; plot.dnow.x=0; plot.dstart.y=0; plot.dnow.y=0; } } } class PJPanel extends JPanel implements MouseListener, MouseMotionListener { Color[] carray=new Color[89441]; int[][] point = new int [800][600]; int ttop, tbot, tleft, tright; double top, bottom, left, right, otop, obottom, oleft, oright; double xrange, yrange, nxrange, nyrange, scale; int flag=0, cflag=0, thresh=3, depth=30, zflag=0; boolean isblue=true, isred=false, isgreen=false; boolean first=true; double ntop, nbot, nleft, nright; Point dstart=new Point(), dnow=new Point(); public PJPanel() { initCols(); setSize(800,600); top=-1.5; bottom=1.5; left=-2.5; right=1.5; ntop=top; nbot=bottom; nleft=left; nright=right; addMouseListener(this); addMouseMotionListener(this); dstart.x=0; dnow.x=0; dstart.y=0; dnow.y=0; } public void reset() { top=-1.5; bottom=1.5; left=-2.5; right=1.5; flag=0; cflag=0; dstart.x=0; dstart.y=0; dnow.x=0; dnow.y=0; repaint(); } public void paint (Graphics g2) { super.paint(g2); Graphics2D g = (Graphics2D) g2; if(cflag==0) calc(); else if(cflag==1) { g.setColor(Color.white); g.drawString("Working....", 50, 50); calc(); } else { for(int y=0; yRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.