Notices
Results 1 to 2 of 2

Thread: Java Methods Help!

  1. #1 Java Methods Help! 
    New Member
    Join Date
    Mar 2013
    Posts
    2
    My program is supposed to calculate the squares of the numbers from 1 to twelve, then display them in a chart for showing the number and their square. I am supposed to use two methods but i'm having trouble how. Please help and tell me what is wrong

    CODE:
    import java.io.*;

    public class findingSquares
    {
    public static void main (String [] args) throws IOException
    {

    System.out.println("Number " + "Square");

    findSquare root = new findSquare();

    int numSquare = root.square();

    int num = 0;

    while (num >= 0 && num <= 11)
    {
    num++;
    System.out.println(num + "\t" + numSquare);
    }
    }


    public static int findSquare (String square)
    {
    int num = 1;
    int numSquare = num * num;


    return numSquare;
    }
    }


    Reply With Quote  
     

  2.  
     

  3. #2  
    WYSIWYG Moderator marnixR's Avatar
    Join Date
    Apr 2007
    Location
    Cardiff, Wales
    Posts
    5,760
    Quote Originally Posted by samanthaxc View Post
    import java.io.*;

    public class findingSquares {
    public static void main (String [] args) throws IOException {
    System.out.println("Number " + "Square");
    findSquare root = new findSquare();
    int numSquare = root.square();

    int num = 0;
    while (num >= 0 && num <= 11) {
    num++;
    System.out.println(num + "\t" + numSquare);
    }
    }


    public static int findSquare (String square) {
    int num = 1
    int numSquare = num * num;
    return numSquare;
    }
    }
    hard to know where to start, there's so many things that don't fit together

    (1) what's findSquare root supposed to refer to, since you have not created an object called findSquare
    (2) what's your method findSquare() supposed to do, since the only vaguely similar method carries a string argument
    (3) i assume root.square() is supposed to square your number, but afaik the proper ways to square a number are
    a * a, a ^ 2 or Math.pow(a,2)
    (4) also you seem to square your number outside your loop, so how do you expect numSquare to change its value
    (5) in your findSquare method it would appear that all you can return is a value of 1, since that's the value you assign to num, and which you then square

    try something like this instead - haven't tested it so can't be sure if it's the correct answer, but it should be a lot closer than your attempt

    import java.io.*;

    public class findingSquares {
    public static void main (String [] args) throws IOException {
    // 1st method
    System.out.println("Number " + "Square");

    for (int i = 0; i < 12; i++) {
    System.out.println((i + 1) + "\t" + ((i + 1) ^ 2));
    }

    // 2nd method
    System.out.println("Number " + "Square");

    for (int i = 0; i < 12; i++) {
    System.out.println((i + 1) + "\t" + findSquare(i + 1));
    }

    }


    private int findSquare (int num) {
    return num * num;
    }
    }


    "Reality is that which, when you stop believing in it, doesn't go away." (Philip K. Dick)
    Reply With Quote  
     

Similar Threads

  1. Methods for Archaeological Dating
    By Arcane_Mathematician in forum History
    Replies: 17
    Last Post: May 23rd, 2009, 09:43 PM
  2. Replies: 2
    Last Post: September 14th, 2008, 11:18 PM
  3. Methods for EPS extraction
    By mia-kaimonadiki in forum Chemistry
    Replies: 2
    Last Post: September 5th, 2008, 04:25 AM
  4. Evolutionary Science Methods
    By Ajain in forum Biology
    Replies: 1
    Last Post: July 12th, 2007, 10:58 PM
  5. Methods: How to reduce a categorization
    By Jacq in forum Behavior and Psychology
    Replies: 0
    Last Post: September 7th, 2006, 08:04 AM
Tags for this Thread

View Tag Cloud

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •