8 Replies - 435 Views - Last Post: 09 August 2011 - 02:22 PM Rate Topic: -----

#1 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

methods

Posted 08 August 2011 - 05:42 PM

I have 12 problems that need to be written as seperate methods and called in a main program. I have the main method but I'm not sure how to do this and make it work because some of the problems seem to need a seperate class to work, Example: 1)write a program that will capitalize the first letter of each word and 2)write a program that will reverse the string.

public static void seven(){}
	public static String reverseIt(String a){
		System.out.print("Enter string:");
		int length=a.length();
		StringBuilder reverse=new StringBuilder();
		for(int i=length; i>0;--i){
			char result=a.charAt(i-1);
			reverse.append(result);
		}
		return reverse.toString();
	}
	
	public static void eight(){}
	
	
	public static void nine(){}
	
	
	public static void ten(){}
	
	
	public static void eleven(){}
	
	
	public static void twelve(){}
	
	

	public static void main(String[] args) {
		Scanner kbd = new Scanner(System.in);
		int x = 0;
		boolean myFlag = true;
		while(myFlag){
			System.out.println("Enter -1 to quit\n or enter question number");
			x = kbd.nextInt();
			switch(x){
			case 1:
				one();
				break;
			case 2:
				two();
				break;
			case 3:
				three();
				break;
			case 7:
				seven();
			case 11:
				eleven();
				break;
			case -1:
				myFlag = false;
			default:
				System.out.println("Enter -1 to quit\n or enter question number");
			}
		}
	}

}


This is the output, I'm not really sure how this is suppose to work so maybe thats my issue.
Enter -1 to quit
or enter question number
7
Enter -1 to quit
or enter question number

Is This A Good Question/Topic? 0
  • +

Replies To: methods

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: methods

Posted 08 August 2011 - 05:51 PM

you have no method one(), two(), three() (or at least you dont show them to us)

when you input 7 then the seven() method will be call which contains nothing.
What exactly are you having difficulties with?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: methods

Posted 08 August 2011 - 06:03 PM

You have a good start.....
just don't add a method into the one to seven
and when you need a Scanner pass the one from your main method to the other so:

seven(kbd); // in the switch statement


public static void seven(){}
    public static String reverseIt(String a){
        System.out.print("Enter string:");
        int length=a.length();
        StringBuilder reverse=new StringBuilder();
        for(int i=length; i>0;--i){
            char result=a.charAt(i-1);
            reverse.append(result);
        }
        return reverse.toString();
    }


to (I am lazy I will use the reverse() method of the StringBuilder class :))
        public static void seven(Scanner scan){
		System.out.print("Enter string:");
		String str = scan.nextLine();
		StringBuilder reverse=new StringBuilder(str).reverse();
		System.out.println(reverse.toString();
	}



If ever you need another method, no problem

static void seven() {
     int x;
     ... code
     foo(x);
}

static void foo(int x) {
   ... code
}


Happy coding
Was This Post Helpful? 0
  • +
  • -

#4 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: methods

Posted 08 August 2011 - 06:07 PM

View PostCasiOo, on 08 August 2011 - 05:51 PM, said:

you have no method one(), two(), three() (or at least you dont show them to us)

when you input 7 then the seven() method will be call which contains nothing.
What exactly are you having difficulties with?


Figuring out how to use methods to start with and then how to use them in the way this assignment needs. Our book isn't very good and I feel like it doesnt clearly point out what these things are I thought by just writing "public static void eight(){}" I was creating a method. Maybe you can help start by helping me understand that.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: methods

Posted 08 August 2011 - 06:26 PM

View Postsweetpea56mw, on 08 August 2011 - 06:07 PM, said:

View PostCasiOo, on 08 August 2011 - 05:51 PM, said:

you have no method one(), two(), three() (or at least you dont show them to us)

when you input 7 then the seven() method will be call which contains nothing.
What exactly are you having difficulties with?


Figuring out how to use methods to start with and then how to use them in the way this assignment needs. Our book isn't very good and I feel like it doesnt clearly point out what these things are I thought by just writing "public static void eight(){}" I was creating a method. Maybe you can help start by helping me understand that.


To begin with you should not focus on what public and static means, there are other things more important for you to learn about methods right now.

What makes a method unique
Return type (void, int, double ...)
Name
Parameters - seven( int a, double b, String c )

Example of a method
public static int add( int value1, int value2 ) {
  return value1 + value2;
} 



Example of how to call a method
int result = add( 5, 10 );
System.out.println( result );




You want your methods to do some kind of work. Anywhere in your program you can call these methods and make them execute their algorithm. You will find methods helpful when you later find out you don't have to repeat the same kind of code over and over again.

This post has been edited by CasiOo: 08 August 2011 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: methods

Posted 08 August 2011 - 06:43 PM

View PostCasiOo, on 08 August 2011 - 09:26 PM, said:

[Example of a method
public static int add( int value1, int value2 ) {
  return value1 + value2;
} 



Example of how to call a method
int result = add( 5 + 10 );
System.out.println( result );


CasiOo hope you are talking of 2 different methods add() here :)
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: methods

Posted 08 August 2011 - 06:53 PM

View Postpbl, on 08 August 2011 - 06:43 PM, said:

View PostCasiOo, on 08 August 2011 - 09:26 PM, said:

[Example of a method
public static int add( int value1, int value2 ) {
  return value1 + value2;
} 



Example of how to call a method
int result = add( 5 + 10 );
System.out.println( result );


CasiOo hope you are talking of 2 different methods add() here :)


Thanks, got that edited hehe ^^
Was This Post Helpful? 0
  • +
  • -

#8 sweetpea56mw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 10-July 11

Re: methods

Posted 09 August 2011 - 01:29 PM

View PostCasiOo, on 08 August 2011 - 05:51 PM, said:

you have no method one(), two(), three() (or at least you dont show them to us)

when you input 7 then the seven() method will be call which contains nothing.
What exactly are you having difficulties with?

I do have methods one(), two(), all the way up to twelve but I decided to start at seven because that seemed the easiest. I'm not sure what you mean by seven() contains nothing.
Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1413
  • View blog
  • Posts: 3,133
  • Joined: 05-April 11

Re: methods

Posted 09 August 2011 - 02:22 PM

View Postsweetpea56mw, on 09 August 2011 - 01:29 PM, said:

View PostCasiOo, on 08 August 2011 - 05:51 PM, said:

you have no method one(), two(), three() (or at least you dont show them to us)

when you input 7 then the seven() method will be call which contains nothing.
What exactly are you having difficulties with?

I do have methods one(), two(), all the way up to twelve but I decided to start at seven because that seemed the easiest. I'm not sure what you mean by seven() contains nothing.


public static void seven(){
    //You need to write something in here
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1