-
Notifications
You must be signed in to change notification settings - Fork 6
Java Fundamentals #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,89 @@ | ||||||
| --- | ||||||
| title: Java Fundamentals | ||||||
| description: An Intro to print statements and variables | ||||||
| prev: intro-to-java/java-fundamentals | ||||||
| next: false | ||||||
| --- | ||||||
| import Aside from '../../../components/Aside.astro'; | ||||||
| import ContentFigure from '../../../components/ContentFigure.astro'; | ||||||
|
|
||||||
| ## Syntax | ||||||
| As you start programming, you might make a mistake or make a typo. When that happens, your code will have a red line under it. This is because Java has rules called syntax. Syntax is a set of rules that have to be followed so that the computer can understand and run your code. It’s important to pay attention to the syntax! | ||||||
| If your code has a red line under it, first check whether the syntax is correct. This can be done by researching online or by comparing your code with examples. | ||||||
|
|
||||||
| You can also learn what’s wrong with your code when you put the cursor over the section of the code with the red line and read what the error message is. | ||||||
| Error messages will not tell you exactly what's wrong with your code. If you’re unsure on what the error message means, you can look it up to get additional context. | ||||||
|
|
||||||
| <ContentFigure src="/java-fundamentals/ErrorExample1.png" /> | ||||||
| <ContentFigure src="/java-fundamentals/ErrorExample2.png" /> | ||||||
|
|
||||||
|
|
||||||
| ## Variables | ||||||
| Variables are containers that are used to store information in a program. This could be a variable that holds the temperature or a variable that holds the speed of the motor. | ||||||
|
|
||||||
| The syntax of a variable is has following: | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ```java | ||||||
| Datatype name = value; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be out of scope for this section (maybe better to specify it in the section on Objects?), but might be worth noting that any Class can be used as a datatype, not just primitives (int, double, etc) |
||||||
| ``` | ||||||
| Data types refer to the type of value that our variable has. It helps tell our program more information about our variables such as what type of information it holds and how it can be used. | ||||||
| Data types can include numbers, characters or a string of words. Some examples of data types that are commonly used in FRC programming are: | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
for clarity this might be better as "Some example data types" |
||||||
| * Int: integers or numbers that are positive or negative. Int only allows numbers without decimals. Example: 12 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
small rewrite for clarity: "Integer numbers" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| * Double: Double: numbers that are positive or negative. Unlike int, double allows numbers with or without decimals. Example: 34.1 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
double is repeated twice here |
||||||
| * Boolean: either True or False. | ||||||
| * String: holds a sequence of characters. Example: “Hello World” | ||||||
| * Char: holds a Single character. Example: 'A' | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
small capitalization thing also on the topic of capitalization, are we talking about the primitive types here, or the wrapper objects? This is a bit pedantic for a beginner level course, but |
||||||
|
|
||||||
| The name of your variable can be whatever you want. However, it should be easy to read and make sense to others who may be reading your code. | ||||||
| There are also some rules with variable names. The name of the variable can not include spaces. Instead you can write variables with camel case (frontLeftDrive) and snake case (front_Left_Drive). | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we program in java, |
||||||
| Variable names can not start with a number. However, they can have a number at the end of the name. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They can have a number at any point in the identifier other than the first character (not just the end). |
||||||
|
|
||||||
| <Aside type="note"> | ||||||
| Java is a case-sensitive programming language! This means that uppercase and lowercase letters are treated as being two different things. For example: MotorID is different from motorID. Even though it’s spelled the same, if your variable name is MotorID then you try to reference it again but spell it as motorID, Java will see the two as different, and your code will get an error. | ||||||
| </Aside> | ||||||
|
|
||||||
| In Java, semi-colons (;) are similar to a period in a sentence. It is what tells the compiler when a line of code ends. Semi-colons are used commonly in programming and aren’t only for creating variables. We will see more examples of this later in the course. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this makes it clearer that if you do something like int someVariable =
someVeryLongMethodNameThatDoesntFitOnTheOriginalLine(argument1, longArgument);you only need one |
||||||
|
|
||||||
| In FRC, variables can be used to hold information about the robot and its different mechanisms. The example below shows 3 variables that were used for a climber mechanism, CLIMBERID is an integer that holds the motor controller ID number which is 51. UP_POSITION is a double that holds the value -33.5 and DOWN_POSITION is a double that holds the value 0. | ||||||
| ```java | ||||||
| int CLIMBERID = 51; | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. once again on the case style pedantry, this should for consistency be |
||||||
| double UP_POSITION = -33.5; | ||||||
| double DOWN_POSITION = 0; | ||||||
| ``` | ||||||
|
|
||||||
|
|
||||||
| ## Print statements | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add a part here that talks about how print statements aren't really used in FRC, and that we generally use things like networktables/telemetry. It doesn't need to name specifics, just something like:
|
||||||
| When programming, it can be useful to display information. This can be helpful for making programs that display information to the user or trying to see what speed that a motor is running. | ||||||
| In Java, we can print information to a terminal using a print statement. A print statement in Java looks like: | ||||||
| ```java | ||||||
| System.out.print(“hello!”); | ||||||
| ``` | ||||||
| What the print statement does is take information inside the parentheses, in the above example, it’s “hello!”, and prints it out to the terminal screen. When using a print statement, the text that we want to print out goes inside the parentheses and is in quotes. However, if we are printing out the value of a variable, then we do not need quotes, as shown below. | ||||||
| ```java | ||||||
| int number = 4; | ||||||
| System.out.print(number); // prints out the value 4 | ||||||
| ``` | ||||||
|
|
||||||
| ## Comments | ||||||
| When programming, we use comments to write notes that explain what the code does. This helps make the code more readable for others because if they are unsure of what your code does, they can read your comments. Comments are not run by the compiler, which also means that you can use comments to prevent code from running. | ||||||
| In Java, there are two types of comments. Single-line Comments and Multi-line Comments. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some clarity things:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ### Single-line Comments | ||||||
| Single-line comments are // and you add them to the front of your text or line of code. For example, the code below leaves a note of “this prints out Hello World. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity, I'd probably rewrite this line to something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ```java | ||||||
| // This prints Hello World | ||||||
| System.out.print("Hello World"); | ||||||
| ``` | ||||||
| You will also see comments placed at the end of code like the following | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity pt2: I'd replace "at the end of code" with "at the end of a line" |
||||||
| ```java | ||||||
| System.out.print("Hello World"); // This prints Hello World | ||||||
| ``` | ||||||
| Both examples accomplish the same tasks and there is no difference. If you put your comments above or next to code is up to you and what makes the most sense for your code. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity pt3: in "If you put your comments above or next to code is up to you", the starting "if" should be replaced with "whether" |
||||||
|
|
||||||
| ### Multi-line Comments | ||||||
| Multi-line Comments start with /* and end with */ The text or code that is in between the two will turn into comments. Multi-line Comments are commonly used when you have many lines of text or need to turn a large amount of code into a comment. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd make sure to add codeblocks `` around any in-line code (`/*`, `*/`). Also make sure to escape * as this is markdown, and *phrase* means italics
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Although in codeblocks you don't need to escape * There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| For example, this is a comment with two lines of text | ||||||
| ```java | ||||||
| /* This prints Hello World | ||||||
| This is another line */ | ||||||
| System.out.print("Hello World"); | ||||||
| ``` | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| title: Stage Overview | ||
| description: An Intro to what Java is and the topics that will be covered in this stage | ||
| prev: false | ||
| next: intro-to-java/java-fundamentals | ||
| --- | ||
|
|
||
| Learning the basics of Java is the most important step in learning FRC programming because it builds the foundation. In Java, objects are used to create motor | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This paragraph makes little sense, especially because readers at this stage probably have very little understanding of words like |
||
| controllers that allow a robot to run a motor, variables are used to assign a speed to the motor, and much more. There is a lot to learn when learning Java. However, | ||
| FRCSoftware will go over the basics needed to program a robot. | ||
|
|
||
| Each section will have different activities and exercises to help build up a good understanding of the basics of Java. If you’re new to programming, it is highly | ||
| recommended to go through each step in order. This will help ensure that you don’t miss any important information. | ||
|
|
||
| This stage will cover the following topics | ||
| * <a href="/intro-to-java/java-fundamentals/"> Java Fundamentals </a> | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small typo here
IMO "is" is the correct word here