Lesson 1 - frc2052/2023-KnightKrawlerJavaTraining GitHub Wiki
In this lesson we will begin understanding how to write code as well as some broad programming topics like printing to the console, setting variables, and instancing classes.
Open up the explorer view in visual studio and Navigate to the Main.java
file located within the src
folder. Double-click on Main.java to edit the file. Within the open and close brackets {...}
of the main method and under the comment labeled ASSIGNMENT A:
write out the following line of code.
System.out.println("Hello world!");
Press F5
to run your code and watch the terminal at the bottom of the screen.
Under the comment labeled "ASSIGNMENT B:" we first need to initialize or set the input variable to a new instance of the Scanner class. This sounds like a complicated task but is quite simple in practice using the following line of code.
input = new Scanner(System.in);
For now, don't worry about the System.in
portion of the code, this is called a parameter and is used for passing values to a newly initialized class. All we have done through this code is set our input variable input =
to a new
instance of the Scanner
class (System.in);
with a parameter at the end. An instance in programming is simply a specific occurrence of a class. In this way think of a class as a template and the instance of that class as the filled in template. For example, I might have a class called "Dog" that has specific properties like its color and weight, but without any instances of this class, it's just a blueprint without specific values. When I initialize that class as Dog bobby = new Dog();
and give it a color and weight the Dog class blueprint gets used to build a new instance of itself with those values. That new instance is then stored in the variable bobby
with the blueprint type Dog
. The concept of instancing can be difficult for many new and sometimes even experienced programmers to understand; be sure to keep an eye out for the new
keyword were instancing is happen and ask for help if you are confused.
For a more in-depth, but potentially more confusing, understanding of instancing check out this article.
Image Source: Visual Paradigm
Next, we will ask the user for their input (not required, but helpful for the user).
System.out.println("Please enter your name below:");
To actually get the user's input we need to use the Scanner class we instanced previously. To get user input in the form of a String we need to call the nextLine();
method within the instanced Scanner class by calling input.
followed by the name of the method.
input.nextLine();
However, simply calling this method isn't enough because we need somewhere to store the returned value of the user input. Luckily, we already have a String variable named name
in our Main class. So we just need to set name
equal to =
the value returned by input.nextLine();
.
name = input.nextLine();
Finally, we will print a customized welcome message for the user with their name.
System.out.println("Welcome, " + name + "!");
Now try and run your code by pressing F5
and provide input in the terminal at the bottom of the screen.
In Java when we refer to scope we are referring to where a certain variable or method can be accessed in a program. In these lessons we will focus on three types of scope: class level, method level, and block scope. A block level scope can only be created in a method level scope which in turn can only be created in a class level scope (class -> method -> block). The highest level of scope is the class level scope. This scope covers everything within a class's opening and closing curly brackets {...}
. Within the class level scope we can create class level variables and methods. When we create methods we also create a new scope because of the opening and closing curly brackets. Within the method scope we can implement the functionality and logic of our class. Part of this functionality and logic are block statements like if
statements, for
loops, and while
loops. All these statements have an opening and a closing curly bracket which create a block scope. Notice a pattern? Anytime we have an opening curly bracket we automatically enter a new scope, and anytime we have a closing curly bracket we automatically exit that scope. So what's the importance of scope? Scopes are what determine accessibility. For example a variable defined in a class level scope can only be accessed in that class level scope or a method level and block scope, but a variable defined in a method level scope can only be accessed in that method level scope or a block level scope. And finally, any variable defined in a block level scope can only be accessed in that block scope. This is why keeping track of curly brackets is so important!
public class ExampleClass { // CLASS LEVEL SCOPE // Variables here can be accessed by this scope and any scopes defined within it public void exampleMethod() { // METHOD LEVEL SCOPE // Variables here can be accessed by this scope and any block scopes defined within it, but not the class level scope that contains it if (...) { // BLOCK SCOPE // Variables here can be accessed by this scope and any block scopes defined within it, but not the method or class level scopes // that contain it } } }
For more information on scopes and instancing check out this article by Codecadamy.
Before getting starting working with source control in visual studio we need to setup a username and email that will be associated with our commits. You only need to do this on a personal computer. If you are on a team computer, skip this step. To do this open a command prompt and enter the following commands replacing [email protected]
with your email and first last
with your name.
git config --global user.email "[email protected]"
git config --global user.name "first last"
You only ever have to do this once to globally set your email and name for git.
Click on the source control icon on the left of your screen to open the source control panel of visual studio code. Within this panel we'll find any feature related to git we'll be using throughout this tutorial and throughout most of your programming experience on the team. To view all the options available to you in this panel click the three dots in the top right corner of the panel. Within this panel we will be using the Commit
tab and the Push
feature in this step, but in the future some other common features we'll use are Pull
, Fetch
, and sometimes Merge
(within the Branch
tab). First we need to type in a commit message consisting of the changes we made. To do this enter in a message overviewing your changes at the top field labeled message
in the source control panel. After that go to the Commit
tab and within it select Commit All
this prepares our files and commits them locally on our machine. To actually push our commit to the Github we need to select push (make sure you're connected to the internet for this step or else you will get an error).
Now your code is on Github! You can check to see if it's been properly pushed by going to the project repository and selected your branch instead of main. If everything worked you should see your latest code with your commit message.
For more information regarding source control in visual studio code check out this visual studio code documentation.
Image Source: Source Graph
If you get stuck at any point you can use the following code as a guideline to help you work through the assignments. This code will also be located in lessons\lesson#
in the project folder if you get stuck in any of the following lessons. This code is also already commented to help you quickly read through it and understand what each line does. If you still don't understand it be sure to ask for help.
import java.util.Scanner; public class Main { // Variable to store our instanced class for receiving input // NOTE: notice this variable has yet to be set equal to anything, this is done on line 13 private static Scanner input; // Variable to store the users name once they provide it private static String name; // This method is the singular starting point of our application public static void main(String[] args) throws Exception { // ASSIGNMENT A: System.out.println("Hello world!"); // ASSIGNMENT B: // Here we "initialize" or set our variable (input) equal to a new Scanner instance that receives input from 'System.in' input = new Scanner(System.in); // Asks the user to input their name through the console System.out.println("Please enter your name below:"); // Gets the next available user input and sets the name variable to that input name = input.nextLine(); // Prints a customized welcome message for the user System.out.println("Welcome, " + name + "!"); } }