Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with an assignmen please.

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with an assignmen please.

    Question:Write a program that gets two numbers from the user, a and b, you may assume that a<b. It then prints the squares of all the numbers in the inclusive range [a, b].


    This is the question for my assignment and I am completely lost, I learned some of the concepts but I just don't know how to apply it to this.

    my code:
    import java.util.*;
    import javax.swing.*;
     
    public class Ch4Q11 {
    	public static void main (String[] args) {
    		int n;
    		int m;
    		int accumulator;
     
    		final int high = m, low = n;
     
     
    		String valuea, valueb;
     
     
    			valuea = JOptionPane.showInputDialog("Write first number here: ");
    			n = Integer.parseInt(valuea);
     
     
    			int amount = high - low + 1;
     
    			while (accumulator >= 0) {
     
    			valueb = JOptionPane.showInputDialog("Write second number here: ");
    			m = Integer.parseInt(valueb);
     
    				System.out.print(accumulator + " ");
    				System.out.println(Math.sqrt(accumulator) + " " );
    				n--;
    			}
    }
    }


    as you can see I'm not even close.. I would really appreciate it if someone could help me with this


    Thank you,


    Harris

  2. #2
    Moderators helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help with an assignmen please.

    accumulator? I haven't seen that since I was doing stuff with assembly

    Ok, the first issue I see is that you're declaring final integers high and low, and assigning them the values of m and n, which haven't been initialized yet. In Java (unlike other languages), the Java compiler will always complain if you don't initialize your variables.

    Now, the program logic should look something like this:

    1. Get the a and b.
    2. for all integers i starting at a and going to b (inclusive), print out i*i

    Here's how to get the for-loop part:
    for (int i = a; i <= b; i++)
    {
         System.out.println(i + " squared is " + i*i);
    }

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with an assignmen please.

    Thank you very much helloworld922, your post was really helpful

    I'm actually taking a high school course so it's nothing complicated but that is the way they want us to do it.


    Regards,


    Harris