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 8 of 8

Thread: Making Methods

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making Methods

    I have to write a program that will define a number of public static methods:



    add
    takes two doubles, returns their sum
    max
    takes two doubles, returns the larger value (3.0 is larger than -10.0)
    rev
    takes a String, returns the reversed String ("Hi, Bob" reversed is "boB ,iH")
    pi
    takes nothing, returns exactly 3.14159
    store
    takes a double, remembers it, returns nothing
    recall
    takes nothing, returns the last double from store
    even
    takes an int, returns true or false if it’s even or not
    iota
    takes an int (let’s call it n), returns an array of n ints containing 1001, 2002, 3003, ..., n*1001.
    Assume that n > 0.
    sum
    takes an array of doubles, returns their sum
    mean
    takes an array of doubles, returns their mean (total divided by how many). It must call the sum method to calculate the total. Assume that the array is non-empty.


    I start with this. It seems really easy. But I'm not sure where to go. Can someone help me or do the first few methods so I know how to start? Thanks


    package Methods;
     
    import java.util.Scanner;
     
    public class Methods {
     
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int[] a;
            boolean b;
            int n;
            double d;
            String s;
     
            for (;;) {
                System.out.println("Enter command and arguments");
                String op = scan.next();
                if (op.equals("quit"))
                    break;
                else if (op.equals("add")) {
                    d = add(scan.nextDouble(), scan.nextDouble());
                    System.out.println(d);
                }
                else if (op.equals("max")) {
                    d = max(scan.nextDouble(), scan.nextDouble());
                    System.out.println(d);
                }
                else if (op.equals("rev")) {
                    s = rev(scan.nextLine());
                    System.out.println(s);
                }
                else if (op.equals("pi")) {
                    d = pi();
                    System.out.println(d);
                }
                else if (op.equals("store")) {
                    store(scan.nextDouble());
                }
                else if (op.equals("recall")) {
                    d = recall();
                    System.out.println(d);
                }
                else if (op.equals("even")) {
                    b = even(scan.nextInt());
                    System.out.println(b);
                }
                else if (op.equals("iota")) {
                    a = iota(scan.nextInt());
                    for (int i=0; i<a.length; i++)
                            System.out.print(a[i] + " ");
                    System.out.println();
                }
                else if (op.equals("sum")) {
                    scan.skip(" ");
                    s = scan.nextLine();
                    int count = s.split(" ").length;
                    Scanner linescan = new Scanner(s);
                    double[] data = new double[count];
                    for (int i=0; i<data.length; i++)
                            data[i] = linescan.nextDouble();
                    d = sum(data);
                    System.out.println(d);
                }
                else if (op.equals("mean")) {
                    scan.skip(" ");
                    s = scan.nextLine();
                    int count = s.split(" ").length;
                    Scanner linescan = new Scanner(s);
                    double[] data = new double[count];
                    for (int i=0; i<data.length; i++)
                            data[i] = linescan.nextDouble();
                    d = mean(data);
                    System.out.println(d);
                }
                else {
                    System.out.println("Invalid command: " + op);
                }
            }
        }
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Making Methods

    This is not a code service. It is your homework not ours. You write the code or at least make an attempt. When you get stuck post your code, error messages and ask a specific question. "It doesn't work" is neither a question nor specific.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making Methods

    Okay i've done some of them. Im not sure how to do the methods:

    rev
    store
    recall
    iota
    sum
    mean

    This is what I have so far:

    package Methods;
     
    import java.util.Scanner;
     
    public class Methods {
     
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int[] a;
            boolean b;
            int n;
            double d;
            String s;
     
            for (;;) {
                System.out.println("Enter command and arguments");
                String op = scan.next();
                if (op.equals("quit"))
                    break;
                else if (op.equals("add")) {
                    d = add(scan.nextDouble(), scan.nextDouble());
                    System.out.println(d);
                }
                else if (op.equals("max")) {
                    d = max(scan.nextDouble(), scan.nextDouble());
                    System.out.println(d);
                }
                else if (op.equals("rev")) {
                    s = rev(scan.nextLine());
                    System.out.println(s);
                }
                else if (op.equals("pi")) {
                    d = pi();
                    System.out.println(d);
                }
                else if (op.equals("store")) {
                    store(scan.nextDouble());
                }
                else if (op.equals("recall")) {
                    d = recall();
                    System.out.println(d);
                }
                else if (op.equals("even")) {
                    b = even(scan.nextInt());
                    System.out.println(b);
                }
                else if (op.equals("iota")) {
                    a = iota(scan.nextInt());
                    for (int i=0; i<a.length; i++)
                            System.out.print(a[i] + " ");
                    System.out.println();
                }
                else if (op.equals("sum")) {
                    scan.skip(" ");
                    s = scan.nextLine();
                    int count = s.split(" ").length;
                    Scanner linescan = new Scanner(s);
                    double[] data = new double[count];
                    for (int i=0; i<data.length; i++)
                            data[i] = linescan.nextDouble();
                    d = sum(data);
                    System.out.println(d);
                }
                else if (op.equals("mean")) {
                    scan.skip(" ");
                    s = scan.nextLine();
                    int count = s.split(" ").length;
                    Scanner linescan = new Scanner(s);
                    double[] data = new double[count];
                    for (int i=0; i<data.length; i++)
                            data[i] = linescan.nextDouble();
                    d = mean(data);
                    System.out.println(d);
                }
                else {
                    System.out.println("Invalid command: " + op);
                }
            }
        }
     
    	private static double recall() {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    	public static double mean(double[] data) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    	public static double sum(double[] data) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    	public static int[] iota(int nextInt) {
    		int n = nextInt;
    		int[] a = new int[n];
    		return null;
    	}
     
    	public static boolean even(int nextInt) {
    		if (nextInt % 2 == 0)
    			return true;
    		else
    			return false;
    	}
     
    	public static double store(double nextDouble) {
    		double d = nextDouble;
    		return d;
     
    	}
     
    	public static double add(double nextDouble, double nextDouble2) {
    		double d = nextDouble + nextDouble2;
    		return d;
    	}
     
    	public static double max(double nextDouble, double nextDouble2) {
    		if (nextDouble > nextDouble2)
    			return nextDouble;
    		else	
    			return nextDouble2;
    	}
     
    	public static String rev(String nextLine) {
    		Scanner scan = new Scanner(System.in);
    		String rev = scan.nextLine();
    		for(int i=rev.length()-1; i > 0; i--)
    			System.out.print(i);
    		String s = i;
    		return s;
    	}
     
    	public static double pi() {
     
    		return 3.14159;
    	}
     
    }

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Making Methods

    What aren't you sure about? Where are you stuck? What exactly are you trying to do with each method? Write out, in non-code, what you're trying to do. Be as specific as possible. When you have that written out, you'll have an algorithm that should be pretty easy to translate to code.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making Methods

    rev: I know i have to take a string and loop through each character starting from the end and remembering each character so i can place it in another string which is the reverse, but i am unsure how to do the loop and how to convert it to a string.

    store: I think i have correctly stored what i am supposed to, the problem is in the recall.

    recall: I cant figure out how to recall the variable stored in the store method, extended the braces from the store method to the recall method wont work so i dont know what else to do.

    iota: I am unfamiliar with arrays. I think i correctly took the number and made an array with a

    sum: Once i learn more how to work with arrays i can do this part. I just need to remember each number in each part of the array and add it to the next number in the array.

    mean: This is the same as the sum method, except divide the answer by the number of objects in the array.


    Now can someone help me?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Making Methods

    Take these methods one at a time. Focus on one method, and test only one method at a time. Write a small program (an SSCCE) that tests just one method, so you can control the input and view the output in more detail.

    If you get stuck, post the SSCCE for that method, along with a specific description of what you expected to happen and what happened instead.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making Methods

    ive tried that, hence why im stuck

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Making Methods

    Quote Originally Posted by kilroyjr View Post
    ive tried that, hence why im stuck
    Sorry, but you've already been told what you need to do to get help. You aren't really asking any questions, nor does it look like you've really tried to write any code. If you don't follow the advice you've been given, people aren't very likely to offer you any more.

    I recommend you read the link in my profile on asking smart questions, in addition to this: Starting Writing a Program
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Help making a loop please
    By CheekySpoon in forum Loops & Control Statements
    Replies: 10
    Last Post: February 5th, 2011, 08:23 PM
  2. Making a clock
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2011, 04:36 PM
  3. about making .class into .jar
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: November 13th, 2010, 01:50 PM
  4. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM
  5. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM