<< Back Computer Science Club

November 13 2019 - Java 5 (methods 1, user input)

At this meeting, we went over the basics of methods and how to get user input into a program!

I'm doing the meeting logs for both 11/13 and 11/20 at the same time (along with like 3 other ones lol I'm so behind), so we may have done the stuff in the two logs in a different order.




Intro to Methods

Methods are blocks of code that you can call in your program. All of your functional code has to be inside a method (only variables and classes can be outside).
(btw, that public static void main thing that we always have, that's a method!)


Writing and calling a method
Most of the methods we create will look something like this:

public void hello() {

}


This is a basic method named hello, and within the curly brackets goes your code.


To call a method, simply use

hello();

in another part of your code, and the hello method will run. Obviously, if your method isn't named hello, then you'd call it with whatever you named it.



Method modifiers
This is an intro to methods, so for now, you'll know about two of the method modifiers: public and private.

When a method is defined as public, it means that any other class (you'll learn about classes later) can access it.

When a method is defined as private, it means that this method can only be called within your class.

Check out this example:

public class HelloWorld {

 public void myMethod() {

  System.out.println("dilan!");

 }

 private void myOtherMethod() {

  System.out.println("ranai!");

 }

}


public class ClassyClass {

 public static void main(String[] args) {

  HelloWorld h = new HelloWorld(); //<-- you'll learn about this later

  h.myMethod();
  //this will print dilan! because it's a public method in the other class.

  h.myOtherMethod();
  //you'll get a thick red underline here because you can't call this private method from this other class.

 }

}


Don't understand the example? There is some stuff we haven't learned, but if you have questions on the modifiers, let me know.



Method return type
You're probably like: okay I kinda get the public private stuff but what the heck is that "void" thing in that line of code? Well, my friend, that's the return type.

In this spot, put whatever object or variable type that you'd want to return from this method. Void means it doesn't return anything and just runs.

Here's an example:

public void runThis() {

 System.out.println("dilan!");

}


public int getANumber() {

 int number = 0;

 //maybe do stuff with that number

 return number;

}


public String getAWord() {

 return "Computer";

}


public void run() {

 runThis(); //this will print out dilan!

 int i = getANumber(); //this will set whatever number that the getANumber method returns to a variable

 System.out.println(i); //this will print out that number

 System.out.println(getAWord()); //this will print out Computer

}


Remember that you can replace the "int" or "String" with any variable type or object in your code.




User Input (Scanner)

We're always printing stuff out to the console with System.out, but now let's try allowing the user to type something into the console and using it in the program.


Preparing the scanner
In your code, let's create a Scanner object. We'll go over how making an object works on a later date, but for now, follow as done below (you can name the variable whatever you want, though):

Scanner myscanner = new Scanner(System.in);



Using the scanner for numbers
Now that we have a scanner named myscanner, we can use it to ask for user input. Watch this:

System.out.println("Enter a number");

int thatNumber = myscanner.nextInt();


When the nextInt method from the scanner is called, the program will pause until the user enters a number into the console.

Now, we have that number stored in our thatNumber variable.

Watch me print out the sum of two user-inputted numbers:

System.out.println("Enter the first number");

int numberOne = myScanner.nextInt();

System.out.println("Enter the second number");

int numberTwo = myScanner.nextInt();

int total = numberOne + numberTwo;

System.out.println("The total is: " + total);


Magic? No, it's science!



Using the scanner for words
You can get a word from the console by using scanner's next method.

String wordInput = myscanner.next();




If you have any questions, ask them at Computer Science Club!