need to add a Double Button to the code
Let's add another button to this version of the WordPlay program, a "Double" button. Notice that we've added new code, so that WordPlay now contains a JButton component called doubleButton. When the Double button is clicked, the displayed string should be doubled, or repeated, in the program.
They give me most of the code but it is incomplete, im going to post the code that they gave first, then my answers which are incorrect.
Code :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WordPlay extends JPanel implements ActionListener{
JLabel inst = new JLabel("Enter your text:");
JTextField textField = new JTextField(5);
JButton submit = new JButton("Submit");
JButton doubleButton = new JButton("Double");
JLabel display = new JLabel();
public WordPlay() {
this.add(inst);
this.add(textField);
this.add(submit);
submit.addActionListener(this);
CODE 1
this.add(display);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == submit){
display.setText(textField.getText());
}
CODE 2
}
}
MY answer for CODE 1
Code :
doubleButton = new JButton("Double");
this.add(doubleButton);
doubleButton.addActionListener(this);
My answer for CODE 2
Code :
if(source == doubleButton){
display.setText(textField.getText(Double));
}
Usually when I submit something, it gives me some sort of feedback or error but not in this problem, i get nothing back. it just says it is wrong.
Re: need to add a Double Button to the code
Quote:
it just says it is wrong.
Who/what is the 'it' that says it "it is wrong'?
If there are error messages please copy and paste the full text here.
Re: need to add a Double Button to the code
Quote:
Originally Posted by
Norm
Who/what is the 'it' that says it "it is wrong'?
If there are error messages please copy and paste the full text here.
well this is a homework assignment and usually, when something is wrong, there is a Feedback section that usually says some error message. When i enter the code I entered as my answer it just says Feedback:
then blank. there is no error message so i dont know what is wrong with my code.
Re: need to add a Double Button to the code
Can you compile the program using javac?
If you get errors, copy and paste them here.
Can you execute the program using the java.exe command?
If you get errors, copy and paste them here.
Quote:
there is a Feedback section
What program has the Feedback section? I don't recognize the word in the context of compiling and executing a java program.
Re: need to add a Double Button to the code
I would say it cant compile.
Code java:
if(source == doubleButton){
display.setText(textField.getText(Double));
}
textField.getText(Double) should be attempting to call the getText method of JTextField. JTextField inherits this from JTextComponent. There are two getText() methods in JTextComponent. Here are the API descriptions of them:
Quote:
getText
public String getText()Returns the text contained in this TextComponent. If the underlying document is null, will give a NullPointerException. Note that text is not a bound property, so no PropertyChangeEvent is fired when it changes. To listen for changes to the text, use DocumentListener.
Returns:
the text
Throws:
NullPointerException - if the document is null
See Also:
setText(java.lang.String)
Quote:
getText
public String getText(int offs,
int len)
throws BadLocationExceptionFetches a portion of the text represented by the component. Returns an empty string if length is 0.
Parameters:
offs - the offset >= 0
len - the length >= 0
Returns:
the text
Throws:
BadLocationException - if the offset or length are invalid
As you can see, there is no JTextField.getText(Variable) method. Also, keep in mind that what you have attempted to send getText() is just Double. The compiler will attempt to find a variable named Double. The Double Class is wrapper class for the primitive double, but you cant reference a class as a variable or whatever you tried to do.
Re: need to add a Double Button to the code
Ive also tried it without Double as the variable and just left is empty () and i still got the same no feedback.
Re: need to add a Double Button to the code
Quote:
i still got the same no feedback
Can you explain what "feedback" is?
Can you show the code for the statements you are talking about? We can't see what it is you are doing and would have to guess.
Re: need to add a Double Button to the code
Quote:
Originally Posted by
Norm
Can you explain what "feedback" is?
Can you show the code for the statements you are talking about? We can't see what it is you are doing and would have to guess.
Everything I know and was given is in my first post. The prompt followed by the incomplete Code. Then I gave you both of my codes.
"Feedback" is a section that, once i submit my answer, usually gives me some sort of feedback/errors to tell me how my answer is wrong. In this case there isnt any information in Feedback once I hit submit so I don't know how my code is wrong because no error messages are given.
Re: need to add a Double Button to the code
YOU must be working with some kind of IDE. There is no "Feedback" section involved in java programming.
If you are having problems with your IDE, you'll have to find a forum for that IDE to ask questions about the problems you are having with it.
Re: need to add a Double Button to the code
well it is a class for java programming
The feedback is just where the errors show up for the class via the website that the homework is on. we can rename Feedback "Errors" if it helps get my point across. REGARDLESS nothing shows up, no error messages, but it is still wrong
Re: need to add a Double Button to the code
I have no idea what the website does with your java program. I'm not sure how that relates to writing and debugging java programs.
What happens when you compile your program with the javac command?
Do you get errors?
If no errors from the javac compiler, then what happens when you execute the program using the java command?
Re: need to add a Double Button to the code
Ok, there are some fundamental issues here. This code does compile:
Code java:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WordPlay extends JPanel implements ActionListener{
JLabel inst = new JLabel("Enter your text:");
JTextField textField = new JTextField(5);
JButton submit = new JButton("Submit");
JButton doubleButton = new JButton("Double");
JLabel display = new JLabel();
public WordPlay() {
this.add(inst);
this.add(textField);
this.add(submit);
submit.addActionListener(this);
doubleButton = new JButton("Double");
this.add(doubleButton);
doubleButton.addActionListener(this);
this.add(display);
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source == submit){
display.setText(textField.getText());
}
if(source == doubleButton){
display.setText(textField.getText());
}
}
}
So, let me ask you a question about this code:
Do you have a main? If so, where is the code for that? Because I am having no issues with this. If you dont know what a main is, it would be the class in your other programs that contain the code public static void main(String args[]). Look at those to figure it out.
If you don't have a main, here is the steps for your main (I wont make the code for you).
1. Create a JFrame
2. Create a WordPlay object
3. Set your WordPlay object as the JFrame's Content Pane by using the method JFrame.setContentPane(WordPlay)
4. Set your JFrame's size by using the method JFrame.setSize(int,int)
5. Set your JFrame as visible by using the method JFrame.setVisible(boolean)
6. Set your JFrame's Default Close Operation by using the method JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE)
And presto, you have your main.
Now, if the doubleButton should double the text, do you mean that if the input is Test, the output should be TestTest? If so, you need to get the textField text twice and just concatenate them. That is extremely simple, and I'm sure you can figure it out.
Re: need to add a Double Button to the code
I think he's using an online submission of his code. So, he submits his source (either in a file or just text), then the webpage does some magic to compile and insert it into a testing framework. It then spits back if he got the answer right or wrong (usually with little to no feedback).
I've worked with something similar in the past on physics homework called "Web-assign", and it just doesn't provide any feedback except whether you got it right or wrong :(
However, since it is a Java program I would suggest you get the full code (or at least enough to test the section you are working on) and actually build/debug it yourself before submitting your answer to the web-page. Tell us what errors you get at compile time, what errors you get during runtime, and any problems with the actual output vs. the expected output.
Re: need to add a Double Button to the code
How does anyone learn programming by only coding a small subsection of a program?
I suppose for the first couple of weeks while learning loops or other simple constructs.
Re: need to add a Double Button to the code
Quote:
Originally Posted by
helloworld922
I think he's using an online submission of his code. So, he submits his source (either in a file or just text), then the webpage does some magic to compile and insert it into a testing framework. It then spits back if he got the answer right or wrong (usually with little to no feedback).
I've worked with something similar in the past on physics homework called "Web-assign", and it just doesn't provide any feedback except whether you got it right or wrong :(
However, since it is a Java program I would suggest you get the full code (or at least enough to test the section you are working on) and actually build/debug it yourself before submitting your answer to the web-page. Tell us what errors you get at compile time, what errors you get during runtime, and any problems with the actual output vs. the expected output.
YES this is what is going on, this online submission is making it difficult to learn and do well in this class, and the all online textbook isn't very in-depth also there arent any lectures or in-class lessons with a real professor. so the students of this class are basically teaching Java to themselves, which is probably why I find myself at this forum way too much...
Re: need to add a Double Button to the code
Best advice, play around with non-GUI related things in your free time. Try to grab a good understanding of the language prior to going really indepth with GUIs because there is alot of overhead in GUIs that will distract you from understanding the code that matters.
In a few weeks I'm starting college and I'll be starting a C++ class. I've done extremely little C++ (like 1 or 2 programs maybe), so I'm expecting to go to a forum and ask plenty of questions myself.