javatutorialsbytravismix
javatutorialsbytravismix
Java Tutorials by Travis Mix
3 posts
Don't wanna be here? Send us removal request.
javatutorialsbytravismix · 7 years ago
Text
Flow Control
Today we are going to talk about if statements.  If statements let our programs make decisions which is really important.  Basically the way an if statement works is by comparing something and if the result is true then it will run the code that follows.  There is of course else if and else statements that allow you to add more to the check incase something turned out false.  Our first example will take a String and compare it with another string.  If they match then it will print a message.  Go ahead and enter the following code into your main statement.
String name = "Travis"; String test = "Bob";
if(name == test){     System.out.println("Hello, " + name); }
Something else that you have probably never seen before is the double equal sign == .  The double equal sign does is it compares or checks if something is equal.  An easy way that I use to remember the difference between an single = and a double == is that a single = means “gets”  as in name gets “Travis” and a double == means is equal to as in name is equal to test.  If you run this code you should get no output as name and test are not the same.  
Well what happens if we wanted to handle things in case things are not equal?  In that case we can use an else statement.  Go ahead and add the following code and run it.
String name = "Travis"; String test = "Bob";
if(name == test){     System.out.println("Hello, " + name); }else{      System.out.println("Your name is " + name + " not " + test); }
When you run the code now you will see that you get output that says Your name is Travis not Bob.  I hope that you are starting to see many different ways that this could be useful. 
I did mention that there was an else if statement as well so lets take a look at that real quick.  Enter the following code and run it to see what happens.
String name = "Travis"; String test = "Bob";
if(name == test){     System.out.println("Hello, " + name); }else if(name == "Sam"){      System.out.println("Hello, " + name); } else{      System.out.println("Your name is " + name + " not " + test);  }
At this point try changing the value of name to Sam and other things as well to see what happens.  Basically what an else if statement does is allows you to run another check while a plain else is just do this if nothing else works.
Your challenge today is to write a program that checks if you have enough money to purchase something.  For a hint create a double that stores a dollar and cents amount and check it with an if else statement to see if it is equal to or greater the value needed to make the purchase.  If the amount is equal print a message that says something like “You can buy this but it will cost all of your money” if you have more than enough money print a message that says “You can buy this and you will still have x money left over” where x == the amount of money starting with minus the amount of the purchase price.  And then have an else statement that prints a message like “Sorry you don’t have enough money to make the purchase”
Extra Reading
if statements in Java    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
0 notes
javatutorialsbytravismix · 7 years ago
Text
Variables
Variables are probably one of the most important concepts in programming.  Variables are probably the one thing in programming that you will use more than anything else.  So what are these variable things?  Variables are used to store values in memory so that you can access them later.  There are many different types of variables so lets get to them.  
The first variable type is int which lets store whole numbers.  Next we have float and double which let you store numbers with decimals.  A float is less precise than a double but also uses less memory however for most cases you can get by just fine with double as we have plenty of memory these days to work with.  The two other important variable types are String and boolean.  String lets you store strings of characters or words, but must be wrapped in double quotes ” “ The boolean type stores true or false. There is also the char type which stores a single character.
So lets take a look at how to use these variables.  Create a new Java project and find the main function.  As a reminder main is the code that looks like 
public static void main(String[] args) {
}
Inside of the main function place the following code.
int myInt = 5; // I am an integer and store whole numbers float myFloat = 3.2f; //I am a float and store numbers containing decimal points
double myDouble = 3.25; // I am a more precise version of float
 String myString = "Hello, World"; // I store strings of characters
boolean isBoolean = true; //I store true or false only
char myChar = 'c'; // I store single characters
You probably noticed that the float value has an f at the end before the semi colon.  You can use either lowercase or uppercase f but the f is required in Java to let it know that it is a float.  So now we have these variables and have initialized them (given them a starting value) what can we do with them?
Try adding the following code inside of main after your variables and running your code to see what happens.
System.out.println(myInt); System.out.println(myFloat); System.out.println(myDouble); System.out.println(myString); System.out.println(isBoolean); System.out.println(myChar);
Ok while that was interesting we can also change the value stored inside a variable at anytime by simply writing something like...
myFloat = 54.3f; 
To see what this does go ahead and have your program output myFloat after the previous code inside main using System.out.println  What you will notice is that the new output is 54.3  Another useful thing that you can do is with Strings.  Try the following code out.
System.out.println(myString + ", Travis");  What this code does is it concatenates ( adds together) the value stored in myString and “, Travis” so that you can output the whole thing.  Another useful thing you can do is
 myString = "Your number is: " + myInt;
now when you run System.out.println(myString); your output should be Your number is: 5 
You can even combine this all together inside the System.out.println function as follows
String myNewString = "Hello, "; String name = "Travis"; System.out.println(myNewString + name + " your number is: " + myInt);
Your output should be Hello, Travis your number is: 5
Your challenge for today is to keep messing with variables and coming up with your own things like what we have done above.  The best way to find out what works and what doesn’t work is to try things out.  The worst you will do is make a program that doesn’t work.  If something doesn’t work try reading the error output and seeing if you can figure it out and how to fix it.
Extra Reading
Java Variables https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
0 notes
javatutorialsbytravismix · 7 years ago
Text
Hello World And Basic Output
So you want to learn to program in Java.  Java is a good language to learn as it is used everywhere you look including to make websites and apps for the most used operating system on the planet.  
To get a few things out of the way first I will not be teaching specifics towards web development with Java or Many other topics for that matter, or at least at this time I have no plans.  My Java tutorials will be focused towards a final goal of Android tutorials.  If you want to learn how to use Java for Web development I have two things to say.  First off, what I am going to be covering here will get you started on that Journey but after the basics you will want to look into Spring Development.  Second if you really want to learn web development, I would instead recommend going with HTML, CSS, javascript, and then moving into the Node.JS world.
Moving on, the first thing you will want to get is a nice IDE such as intelliJ  community edition.  Other popular ones are Eclipse (personally I hate eclipse) or netbeans.  You will also need to install the Java SDK which can be found here. Grab the most recent one for your operating and get it installed.  
Now that we have a chosen a coding environment you will probably be asked to setup a workspace.  A workspace is basically a folder where you do your Java development.  Go ahead and pick a location on your hard drive. I recommend creating a directory in your root drive c:\ on windows for example and then create a new project.  If you don’t know how to create a new project this is the perfect time to do a Google search for how to create a new project in your chosen IDE.  If you are planning on going into Android development I would recommend intelliJ as Android Studio is built on intelliJ so you will already be familliar with it at that point.
Once your new project is created you will see something that looks like this...
package helloworldtutorial;
/** * * @author travismix1980 */ public class HelloWorldTutorial {
   /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic here    }
}
This looks like a lot but for now the only thing you really need to worry about is the section 
public static void main(String[] args) {        // TODO code application logic here    }
All programs will have a main function which is what public static void main(String[] args){} is.  Inside that function we have a comment right now which a comment is a useful way to leave yourself a note about what you did in that area.
Comments come in two forms first we have the single line comment which starts with // and continues to the end of the line.  We also have a multi line comment whic are comments surrounded by /* and */ .  Examples of a single line comment would be //add two numbers and for a multi line comment we could do 
/*
add 
two 
lines
*/
Alright so everyone starts with the hello world program to get started when learning a new language or learning to program in general so to do this in Java you need to find the main function which if you remember is the part that says
public static void main(String[] args) {        // TODO code application logic here    }
and inside of the main function we will add the following.
System.out.println(”Hello, World!”); 
when you have that done it should look like
   public static void main(String[] args) {        System.out.println("Hello, World!");    }
At this point you should have a green arrow at the top of your IDE that will let you run your program.  If you cannot find it once again do a search for how to run a program in your chosen IDE to get that program run.  You should see something similar in the output console in your IDE
run: Hello, World! BUILD SUCCESSFUL (total time: 0 seconds)
If you get that then you have successfully written your program.  If you don’t get this output and instead get a bunch of gibberish then you have a bug and should make sure that all your capitalization matches what I have here as well as make sure that you have the semi colon (;) at the end of your System.out.println(); section.  If you are still having problems search google with the error message you are getting or ask me here and I will do my best to help you out.
One important thing I have found when learning to program is to always try to find the answer yourself to a problem you are having and only to ask when you have looked everywhere, Also even more important is that you learn to use documentation.  
I will not give you the answer to every problem out there our you will become reliant on this and will not be able to solve problems with programming on your own.  I recommend keeping the following bookmarked as  they are probably your best resources for solving problems and getting help.
Stack Overflow
Java Documentation
Java Documentation part 2 This one is probably the most important and includes many tutorials and other useful information.
Ok now for your challenge for the day.  First get the hello world program working and then see if you can get other output of your choosing.  Make your computer repeat what you want it to.  Above all remember to have fun learning an important skill and if you have any questions just ask.
0 notes