I am working on a project for a class. It's due rather quickly and I don't know what needs to be fixed.
All we wanted to do was have the user press a button to allow a picture of a tree appear, and have three buttons for three trees. We have been messing around with the code and trying to figure out what's wrong, but we have no idea.
Here are the errors that appear when trying to compile:
E:\Comp. Sci\LandscapeApplet.java:56: invalid method declaration; return type required
repaint();
^
E:\Comp. Sci\LandscapeApplet.java:63: <identifier> expected
x.setColor(Color.darkGray);
^
E:\Comp. Sci\LandscapeApplet.java:64: <identifier> expected
x.fill(c);
^
E:\Comp. Sci\LandscapeApplet.java:67: <identifier> expected
x.setColor(Color.cyan);
^
E:\Comp. Sci\LandscapeApplet.java:68: <identifier> expected
x.fill(d);
^
E:\Comp. Sci\LandscapeApplet.java:70: invalid method declaration; return type required
repaint();
^
E:\Comp. Sci\LandscapeApplet.java:83: <identifier> expected
panel.add(Rectangle a,Double b);
^
E:\Comp. Sci\LandscapeApplet.java:84: <identifier> expected
panel.add(Rectangle b,Double c);
^
E:\Comp. Sci\LandscapeApplet.java:85: <identifier> expected
panel.add(Rectangle c,Double d);
^
E:\Comp. Sci\LandscapeApplet.java:88: <identifier> expected
frame.setContentPane(panel);
^
E:\Comp. Sci\LandscapeApplet.java:89: <identifier> expected
frame.pack();
^
E:\Comp. Sci\LandscapeApplet.java:90: <identifier> expected
frame.show();
^
E:\Comp. Sci\LandscapeApplet.java:93: 'class' or 'interface' expected
}
^
E:\Comp. Sci\LandscapeApplet.java:93: 'class' or 'interface' expected
}
^
14 errors
I really could use some help... The assignment is technically due tomorrow. If you're confused, just leave a post and I'll explain better.
It's asking for an identifier
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
The code has already changed a whole lot... but I'm still get the same type of error:
This new code creates a calculator of sorts, giving square roots as well as squaring the number put in (when the correct buttons are pressed).
There is a button below those ones that says "Final Project," and what we originally wanted for it to do was put a rectangle on the screen so that "Final Project by [our names]" could be displayed. We ended up vetoing that when it was simply screwing up the rest of the code. Now, all we want is a rectangle on the screen with our credit ("Final Project by..."), and that's not even working now, when it was earlier.
Here's our code:
And the errors that pop up when compiled:
E:\Comp. Sci\FinalProject.java:34: <identifier> expected
x.setColor(Color.darkGray);
^
E:\Comp. Sci\FinalProject.java:35: <identifier> expected
x.fill(r);
^
E:\Comp. Sci\FinalProject.java:37: <identifier> expected
x.setColor(Color.lightGray);
^
E:\Comp. Sci\FinalProject.java:38: <identifier> expected
x.drawString("Final Project by Ethan P. Schwartz and Jenna L. Forcey",55,191);
^
E:\Comp. Sci\FinalProject.java:75: 'class' or 'interface' expected
}
^
E:\Comp. Sci\FinalProject.java:75: 'class' or 'interface' expected
}
^
6 errors
We have no idea what this means.
Thank you so much for looking at this!
This new code creates a calculator of sorts, giving square roots as well as squaring the number put in (when the correct buttons are pressed).
There is a button below those ones that says "Final Project," and what we originally wanted for it to do was put a rectangle on the screen so that "Final Project by [our names]" could be displayed. We ended up vetoing that when it was simply screwing up the rest of the code. Now, all we want is a rectangle on the screen with our credit ("Final Project by..."), and that's not even working now, when it was earlier.
Here's our code:
Code: Select all
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
public class FinalProject extends JApplet implements ActionListener
{
JLabel value = new JLabel();
JButton square = new JButton("Square");
JButton squareRoot = new JButton("Square Root");
JTextField numField = new JTextField(10);
Container con = getContentPane();
JButton project = new JButton("Final Project");
public void init();
{
con.setLayout(new FlowLayout());
con.add(value);
con.add(square);
con.add(squareRoot);
con.add(project);
con.add(numField);
square.addActionListener(this);
squareRoot.addActionListener(this);
}
Graphics2D x = (Graphics2D)g;
Rectangle r = new Rectangle(50,175,312,25);
x.setColor(Color.darkGray);
x.fill(r);
x.setColor(Color.lightGray);
x.drawString("Final Project by Ethan P. Schwartz and Jenna L. Forcey",55,191);
public void paint(Graphics g);
{
this.setBackground(Color.orange);
value.repaint();
square.repaint();
squareRoot.repaint();
project.repaint();
numField.repaint();
}
public void actionPerformed(ActionEvent e);
{
double ans;
Object source = e.getSource();
if(source == square)
{
String s = numField.getText();
int k = Integer.parseInt(s);
k = k*k;
String result = "" + k;
value.setText(result);
repaint();
}
else
if(source == squareRoot)
{
String s = numField.getText();
int k = Integer.parseInt(s);
ans = Math.sqrt(k);
String result = "" + ans;
value.setText(result);
repaint();
}
}
}
}
E:\Comp. Sci\FinalProject.java:34: <identifier> expected
x.setColor(Color.darkGray);
^
E:\Comp. Sci\FinalProject.java:35: <identifier> expected
x.fill(r);
^
E:\Comp. Sci\FinalProject.java:37: <identifier> expected
x.setColor(Color.lightGray);
^
E:\Comp. Sci\FinalProject.java:38: <identifier> expected
x.drawString("Final Project by Ethan P. Schwartz and Jenna L. Forcey",55,191);
^
E:\Comp. Sci\FinalProject.java:75: 'class' or 'interface' expected
}
^
E:\Comp. Sci\FinalProject.java:75: 'class' or 'interface' expected
}
^
6 errors
We have no idea what this means.
Thank you so much for looking at this!
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
* Removed the semi-colons from the method declarations
* Removed the final brace
* Moved the code into the paint method where it belongs
I'm trying to remember how to make the html for applets but I'm a touch foggy but at the very least, it compiles so now you have logic errors to focus on and not syntactical errors.
* Removed the final brace
* Moved the code into the paint method where it belongs
I'm trying to remember how to make the html for applets but I'm a touch foggy but at the very least, it compiles so now you have logic errors to focus on and not syntactical errors.
Code: Select all
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
public class FinalProject extends JApplet implements ActionListener
{
JLabel value = new JLabel();
JButton square = new JButton("Square");
JButton squareRoot = new JButton("Square Root");
JTextField numField = new JTextField(10);
Container con = getContentPane();
JButton project = new JButton("Final Project");
public void init()
{
con.setLayout(new FlowLayout());
con.add(value);
con.add(square);
con.add(squareRoot);
con.add(project);
con.add(numField);
square.addActionListener(this);
squareRoot.addActionListener(this);
}
public void paint(Graphics g)
{
Graphics2D x = (Graphics2D)g;
Rectangle r = new Rectangle(50,175,312,25);
x.setColor(Color.darkGray);
x.fill(r);
x.setColor(Color.lightGray);
x.drawString("Final Project by Ethan P. Schwartz and Jenna L. Forcey",55,191);
this.setBackground(Color.orange);
value.repaint();
square.repaint();
squareRoot.repaint();
project.repaint();
numField.repaint();
}
public void actionPerformed(ActionEvent e)
{
double ans;
Object source = e.getSource();
if(source == square)
{
String s = numField.getText();
int k = Integer.parseInt(s);
k = k*k;
String result = "" + k;
value.setText(result);
repaint();
}
else if(source == squareRoot)
{
String s = numField.getText();
int k = Integer.parseInt(s);
ans = Math.sqrt(k);
String result = "" + ans;
value.setText(result);
repaint();
}
}
}
[/quote]
I choose to fight with a sack of angry cats.
Thank you for your prompt responses. I'm slightly laughing at myself for not seeing the semi-colons. This is probably because we had been staring at a computer screen for hours on end.
Now the code compiles, which you said it shouldn't, but it also won't run. I'll keep looking at it. And thank you so much.
::edit::
Okay, it runs now. And we found out that he pushed the deadline to Friday, so we'll have time for improvement.
Thanks again!
Now the code compiles, which you said it shouldn't, but it also won't run. I'll keep looking at it. And thank you so much.
::edit::
Okay, it runs now. And we found out that he pushed the deadline to Friday, so we'll have time for improvement.
Thanks again!
- talleyrand
- Posts: 624
- Joined: Mon Jul 21, 2003 6:56 pm
- Location: Kansas City, MO, USA
- Contact:
Nah, I said it compiles which implies a lack of syntactical errors.
I then said you can evaluate it for the presence of logical errors. For example, I don't know if the presence of the block of code that presents your name should be presented always or only when the 'Final Project' button is clicked. Should there be any plumbing behind the scenes for that button? As it stands, there is none. Is it logical for the applet to complain when I try to square 2.5? As you are the authors of this code, only you can evaluate whether the current behaviour is the intended behaviour. Thus, now that it runs you can kick the tires and attempt to evaluate if it is performing to specifications.
I then said you can evaluate it for the presence of logical errors. For example, I don't know if the presence of the block of code that presents your name should be presented always or only when the 'Final Project' button is clicked. Should there be any plumbing behind the scenes for that button? As it stands, there is none. Is it logical for the applet to complain when I try to square 2.5? As you are the authors of this code, only you can evaluate whether the current behaviour is the intended behaviour. Thus, now that it runs you can kick the tires and attempt to evaluate if it is performing to specifications.
I choose to fight with a sack of angry cats.
Could you help us with the construction of making the Final Project button a button that would allow the gray rectangle to appear once pressed? We've been working on that now, and for the life of us cannot figure out how to do it... It would be highly appreciated.
And thank you so much for the help you've already offered.
And thank you so much for the help you've already offered.
- jesusfr3ak4evr
- Posts: 2
- Joined: Fri Apr 21, 2006 8:33 pm
- Location: southern Maryland
- Contact: