This discussion is archived
3 Replies Latest reply: Nov 21, 2012 4:38 AM by TPD-Opitz-Consulting-com RSS

Loops, while and If

968909 Newbie
Currently Being Moderated
Yes this is a school assignment. It is an online class and its hard to get answers in a timely fashion from my teacher so here is my homework and question.
And no - I don't want anyone to write it for me. :-)

Below is the code. Partially provided by the teacher. We had to manipulate the code to include two integers and find the gcf of the two. He suggests a loop.
I decided on an if loop... then a while loop.
I think I need to add an increment condition but not sure where.

Can I use both in the format below?
This line is from the text, but it is not working for me - *     gcf = (val2 (val1 % val2));* Any suggestions?

Thanks :-)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;

public class App extends JApplet implements ActionListener {
JTextField text1 = new JTextField(30);
JTextField text2 = new JTextField(30);

JTextArea textArea;
public void init () {

Container p = getContentPane();;
p.setLayout(new FlowLayout());
textArea = new JTextArea (30,20);
text1.addActionListener(this);
text2.addActionListener(this);

p.add(text1);
p.add(text2);

p.add(textArea);
text1.setText("Clear the field and enter an integer labeled a");
text2.setText("Clear the field and enter an integer labeled b");
}

public void actionPerformed (ActionEvent e) {
          int val1 = Integer.parseInt(text1.getText());
int val2 = Integer.parseInt(text2.getText());
int gcf;

if (val1 == 0)
     val2 = gcf;
if (val2 == 0)
     val1 = gcf;

while (val1 > 0 && val2 > 0){
*     gcf = (val2 (val1 % val2));*
     textArea.append(""+(gcf));
     

}
}
}
  • 1. Re: Loops, while and If
    rp0428 Guru
    Currently Being Moderated
    >
    Below is the code. Partially provided by the teacher. We had to manipulate the code to include two integers and find the gcf of the two. He suggests a loop.
    I decided on an if loop... then a while loop.
    I think I need to add an increment condition but not sure where.

    Can I use both in the format below?
    This line is from the text, but it is not working for me - * gcf = (val2 (val1 % val2));* Any suggestions?
    >
    1. There is no definition of what 'gcf' is supposed to mean.
    2. There is no such thing as an 'if loop' - so no, you can't use one
    3. If 'val2' is an int then what is 'val2 (. . . anything . . .) supposed to represent? That is the same as saying (if val1 = 7 and val2 = 13):
    gcf = (13 (7 % 13));
    How can that possibly mean anything?

    If you want help you need to explain, in English, what you are trying to do and how you are trying to do it. Just posting a bunch of code isn't enough. Please edit your post and add \
     on the line before and on the line after your code to preserve formatting. See the FAQ for other formatting options. Use the Preview tab to see what the result will look like. If you don't add the 'code' tags you are on your own.
    
    You can't automate what you can't do manually. So the first step is to perform your process MANUALLY
    1. select a sample value for 'val1'
    2. select a sample value for 'val2'
    3. show, using pseudo-code or a formula, how the result is to be computed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 2. Re: Loops, while and If
    968909 Newbie
    Currently Being Moderated
    Thank you - So in English - get two integers and find the GCF.
    Below is my updated code. It seems to work but I still have to run it through more tests.



    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.String.*;

    public class App extends JApplet implements ActionListener {
    JTextField text1 = new JTextField(30);
    JTextField text2 = new JTextField(30);

    JTextArea textArea;
    public void init () {

    Container p = getContentPane();;
    p.setLayout(new FlowLayout());
    textArea = new JTextArea (30,20);
    text1.addActionListener(this);
    text2.addActionListener(this);

    p.add(text1);
    p.add(text2);

    p.add(textArea);
    text1.setText("Clear the field and enter an integer labeled a");
    text2.setText("Clear the field and enter an integer labeled b");

    }

    public void actionPerformed (ActionEvent e) {
              int val1 = Integer.parseInt(text1.getText());
    int val2 = Integer.parseInt(text2.getText());
    GCF (val1, val2);

    textArea.append(""+(GCF (val1, val2)));
         
    }

    public int GCF(int a, int b) {
         if (b == 0) return a;
         else return (GCF (b, a % b));
    }

    }
  • 3. Re: Loops, while and If
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Please read this to make your code more readable to the forum members:
    https://wikis.oracle.com/display/Forums/Forums+FAQ

    and also read this for the same reason (as already suggested in another thread):
    http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points