Skip to content

Instantly share code, notes, and snippets.

@J-Curragh
Last active September 28, 2021 23:30
Show Gist options
  • Save J-Curragh/c815586112b3ae83f40cd7a990cfa0e3 to your computer and use it in GitHub Desktop.
Save J-Curragh/c815586112b3ae83f40cd7a990cfa0e3 to your computer and use it in GitHub Desktop.
Java tutorial 1
package selection;
import java.util.Scanner;
public class App {
/**
* Bonus rating from 1 to 3. Each rating provides a percentage bonus
* amount based on their pay.
* 1: 5%
* 2: 10%
* 3: 15%
*/
/**
* We declare these as constants using the "final" keyword since we don't need to change or "mutate" them.
* This lets the Java know exactly how much memory to allocate.
*
* We also declare them outside of the main() function, this means any
* other methods in this class can see and read them. We don't have any other methods yet, but we might add some later.
* These are called "class variables".
*/
static final String EMPLOYEE_NAME = "George";
static final double EMPLOYEE_SALARY = 25000.00d;
/**
* It would take me ages to explain this properly. I'm sure we'll go over it anyway.
* Basically, this defines this class' main function. Which means, when the code is compiled,
* this is always run.
* 'void' tells the compiler that the method does not "return" anything. It doesn't produce anything, it just
* prints to the console.
* We pass in String[] args as an argument. This isn't important for now. Just know it means an array of Strings, called "args".
*/
public static void main( String[] args ) {
/**
* We create a new instance of the Scanner object. You can read more
* about it here:
* https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
*
* It's actually a little complicated. The scanner basically just takes what you type in (System.in)
* word-by-word, and reads them as any data type we want.
*/
Scanner stdin = new Scanner(System.in);
System.out.println("Enter your employee bonus rating: ");
// Tells the scanner to read the next thing we type into our terminal (System.in) as an integer.
// It will try to do this even if you type in words, or a binary number, or even a file. So be careful!
int bonusRating = stdin.nextInt();
/**
* We declare the variable first, before modifying the value in the If statements.
* If we were to create variables within the If/Else/Elseif blocks,
* They would only exist within those blocks, and would then be destroyed when the blocks have finished executing..
* We need to use totalSalaryWithBonus later, so we need them to stay in memory.
*/
double totalSalaryWithBonus;
/**
* We are comparing what the user typed in (stored in bonusRating variable)
* to 1 and 2 to see if they are equal. And in any other case (maybe they type in 1000, or 0) we can just assume
* they meant to say 3. :D
* When you learn a bit more Java, you can raise errors if the user types something in wrong, or try asking for input again.
*/
if (bonusRating == 1) {
totalSalaryWithBonus = EMPLOYEE_SALARY * 1.05;
}
else if (bonusRating == 2) {
totalSalaryWithBonus = EMPLOYEE_SALARY * 1.10;
}
else {
totalSalaryWithBonus = EMPLOYEE_SALARY * 1.15;
}
/**
* Output should look like this:
*
* Employee Name: George
* Employee Salary: 25000.00
* Employee Salary with Bonus: 26250.00
*/
System.out.printf("Employee Name: %s\nEmployee Salary: %.2f\nEmployee Salary with Bonus: %.2f", EMPLOYEE_NAME, EMPLOYEE_SALARY, totalSalaryWithBonus);
/**
* It's generally good practice to close any object in code which reads from files or user input,
* for the sake of security but also efficient memory usage. This deletes the Scanner object from memory, if we try referring to it again after closing it,
* we will get an error.
* In this case, it doesnt matter, since this is the last line of code and the program terminates afterwards anyway :P
*/
stdin.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment