ihateaol
01-14-2007, 06:25 PM
Hey Everyone,
Does anyone know a website that explains how java methods work in a detailed, and easy to understand way for a beginner? I have a JAVA final exam on thursday and the one thing that's on it that I don't get are methods. I mean, I know what they are and why they're there, but I don't know how they work. I always get confused on what to put in the braces after "public static void(int/double) methodName (what do I put here?)" I mean, I know I put variables there, but which one's? One's that I use throughout the entire program? Or one's I'm just using in that method? And if it's just the one's I'm using in that method, how does that method read the one's that are used throughout the program? And returning stuff also baffles me. How does it know where to return to? I know you tell it where, but how? Basically, as you can see, I'm totally lost on how to convert a normal program into a program that uses methods.
This is the program I'm currently trying to convert: It allows the user to input as many numbers as he or she wants, then sorts them by negatives, evens, and odds. As you can see, I can make the actual program, I just can't put it into methods!
import java.util.Scanner;
public class ArrayProgram2{
public static void main(String [] args){
//Variables
Scanner reader = new Scanner(System.in);
int howManyNums;
int counter, j = 0, k = 0, h = 0;
int numOfEvens = 0, numOfOdds = 0, numOfNegs = 0;
System.out.print("How Many Numbers?: ");
howManyNums = reader.nextInt();
int[] list = new int[howManyNums];
System.out.println();
for(counter = 0; counter < howManyNums; counter++){
System.out.print("Number " + (counter + 1) + ": ");
list[counter] = reader.nextInt();
if ((list[counter] % 2 == 0) && (list[counter] > 0)){
numOfEvens++;
}
if ((list[counter] % 2 != 0) && (list[counter] > 0)){
numOfOdds++;
}
if (list[counter] * 2 < 0){
numOfNegs++;
}
}
int[] listOfEvens = new int[numOfEvens];
int[] listOfOdds = new int[numOfOdds];
int[] listOfNegs = new int[numOfNegs];
for(counter = 0; counter < howManyNums; counter++){
if ((list[counter] % 2 == 0) && (list[counter] > 0)){
listOfEvens[j] = list[counter];
j++;
}
if ((list[counter] % 2 != 0) && (list[counter] > 0)){
listOfOdds[k] = list[counter];
k++;
}
if (list[counter] * 2 < 0){
listOfNegs[h] = list[counter];
h++;
}
}
System.out.println();
System.out.println();
System.out.println("Results: ");
System.out.println();
System.out.println("Entire List: ");
for (counter = 0; counter < howManyNums; counter++){
System.out.println("Number " + (counter + 1) + ": " + list[counter]);
}
System.out.println();
System.out.println("Evens: ");
for (counter = 0; counter < numOfEvens; counter++){
System.out.println("Number " + (counter + 1) + ": " + listOfEvens[counter]);
}
System.out.println();
System.out.println("Odds: ");
for (counter = 0; counter < numOfOdds; counter++){
System.out.println("Number " + (counter + 1) + ": " + listOfOdds[counter]);
}
System.out.println();
System.out.println("Negatives: ");
for (counter = 0; counter < numOfNegs; counter++){
System.out.println("Number " + (counter + 1) + ": " + listOfNegs[counter]);
}
}
}
Any help at all would be good. A self explanation, or a web link would be amazing. (Also, sorry about the unneeded tab over after the variables).
Does anyone know a website that explains how java methods work in a detailed, and easy to understand way for a beginner? I have a JAVA final exam on thursday and the one thing that's on it that I don't get are methods. I mean, I know what they are and why they're there, but I don't know how they work. I always get confused on what to put in the braces after "public static void(int/double) methodName (what do I put here?)" I mean, I know I put variables there, but which one's? One's that I use throughout the entire program? Or one's I'm just using in that method? And if it's just the one's I'm using in that method, how does that method read the one's that are used throughout the program? And returning stuff also baffles me. How does it know where to return to? I know you tell it where, but how? Basically, as you can see, I'm totally lost on how to convert a normal program into a program that uses methods.
This is the program I'm currently trying to convert: It allows the user to input as many numbers as he or she wants, then sorts them by negatives, evens, and odds. As you can see, I can make the actual program, I just can't put it into methods!
import java.util.Scanner;
public class ArrayProgram2{
public static void main(String [] args){
//Variables
Scanner reader = new Scanner(System.in);
int howManyNums;
int counter, j = 0, k = 0, h = 0;
int numOfEvens = 0, numOfOdds = 0, numOfNegs = 0;
System.out.print("How Many Numbers?: ");
howManyNums = reader.nextInt();
int[] list = new int[howManyNums];
System.out.println();
for(counter = 0; counter < howManyNums; counter++){
System.out.print("Number " + (counter + 1) + ": ");
list[counter] = reader.nextInt();
if ((list[counter] % 2 == 0) && (list[counter] > 0)){
numOfEvens++;
}
if ((list[counter] % 2 != 0) && (list[counter] > 0)){
numOfOdds++;
}
if (list[counter] * 2 < 0){
numOfNegs++;
}
}
int[] listOfEvens = new int[numOfEvens];
int[] listOfOdds = new int[numOfOdds];
int[] listOfNegs = new int[numOfNegs];
for(counter = 0; counter < howManyNums; counter++){
if ((list[counter] % 2 == 0) && (list[counter] > 0)){
listOfEvens[j] = list[counter];
j++;
}
if ((list[counter] % 2 != 0) && (list[counter] > 0)){
listOfOdds[k] = list[counter];
k++;
}
if (list[counter] * 2 < 0){
listOfNegs[h] = list[counter];
h++;
}
}
System.out.println();
System.out.println();
System.out.println("Results: ");
System.out.println();
System.out.println("Entire List: ");
for (counter = 0; counter < howManyNums; counter++){
System.out.println("Number " + (counter + 1) + ": " + list[counter]);
}
System.out.println();
System.out.println("Evens: ");
for (counter = 0; counter < numOfEvens; counter++){
System.out.println("Number " + (counter + 1) + ": " + listOfEvens[counter]);
}
System.out.println();
System.out.println("Odds: ");
for (counter = 0; counter < numOfOdds; counter++){
System.out.println("Number " + (counter + 1) + ": " + listOfOdds[counter]);
}
System.out.println();
System.out.println("Negatives: ");
for (counter = 0; counter < numOfNegs; counter++){
System.out.println("Number " + (counter + 1) + ": " + listOfNegs[counter]);
}
}
}
Any help at all would be good. A self explanation, or a web link would be amazing. (Also, sorry about the unneeded tab over after the variables).