Java - the bare basics 12-25-2012, 04:57 PM
#1
Hello everybody, this is Java- the bare basics, you will be learning the most basic of basic java today, as I progress in my tutorials they will get more and more difficult. Have fun!
--firstClass.java--
--Hello world!--
________________________________________________________________________________
--firstClass.java--
--Variables!--
_____________________________________________________________________________________________________
--firstClass.java--
--Input!--
________________________________________________________________________________
--firstClass.java--
--calculator--
Challenges
1. Make a calculator with addition and subtraction [very simple]
2. Make a calculator with addition, subtraction, multiplication,division [simple]
3. Make a simple input-menu [simple to intermediate]
::Questions::
1. What is the difference between an 'int' and a 'double'?
2. What is the difference between a 'char' and a 'string'
3. How do you start and end a multi-line comment?
4. How do you start a single line comment?
5. What does '/n' and '/t' do within an output stream?
--firstClass.java--
--Hello world!--
Code:
//this is a single line comment, all single line comments start with '//'
/*
* this is a multiline comment, all multine comments start with '/*' and end with with the * on the opposite side
* all new lines start with a '*'
*/
/*
* as you can see, this file is named firstClass.java
* all classed must have the same name as the file
*/
public class firstClass {//this is a class, a class is bassicly just the file your working in
public static void main(String[] args){//this is the main method, every java program starts out by looking for this method (keep it the same every time you write a program)
/*
* as you can see, when you make a new class or method you must open it with { and close it with }
* all code goes inside these {}
*/
System.out.print("Hello world, this is your first console output!");
/*
* System = code telling the computer's hard drive that you are using a new piece of code
* out = telling the compiler to output something
* print = prints the text to the screen
* as you can see, this line ends with ';' all single line pieces of code end with ;
*/
System.out.println("This is on a new line!");
/*
* the above line of code is almost the same as system.out.print(), the only difference is the 'ln'
* the 'ln' is telling the compiler that the following text should be on a new line
*/
System.out.println("\n");// anything with \ is a console output command
/*
* /n = new line
* /t = tab (5 spaces)
*/
System.out.println("you can use \n in a line too, oops im on a new line!");
System.out.println("you can also use \t oops im 5 spaces away!");
}//end of main method
}//end of class
________________________________________________________________________________
--firstClass.java--
--Variables!--
Code:
public class firstClass{
public static void main(String[] args) {
int Integer = 1;
/*
* an 'int' is any whole number (no decimals)
*/
double Doubles = 2.84;
/*
* a double is any number with decimals
*/
boolean TF = true;
/*
* a boolean is a true or false statement
*/
String TEXT = "Hello world";
/*
* a string is a line of text
*/
char A = 'a'
/*
*a char is a character, we call a character with '' not ""
*/
int A = 1;
int B = 2;
int sum = A + B;
/*
* you can add with +, subtract with -, divide with /, multiply with *, and get the remainder of with %
*/
System.out.println(sum);//variables are printed out by putting their name without ""
String Hello = "hello I'm a string!";
System.out.println(Hello);//prints out the variable Hello
}
}
--firstClass.java--
--Input!--
Code:
import java.util.Scanner;// this is an import, it is used when you call a function from a class within java
public class firstClass{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);//a Scanner is a function used to read input
/*
* we named our scanner 'scan' and we made 'scan' equal a new input stream
*
*/
System.out.print("What is your name: ");
String name = scan.next();
/*
* we made a new string called "name" and set it to 'scan.next'
* .next = the next string inputed
* scan = the scanner we made earlier
*/
System.out.println(name);//prints out what you inputed
}
}
--firstClass.java--
--calculator--
Code:
import java.util.Scanner;// this is an import, it is used when you call a function from a class within java
public class firstClass{
public static void main(String[] args) {
Scanner fnum = new Scanner(System.in);//makes a new scanner named fnum
System.out.print("Enter your first number: ");
int fn= fnum.nextInt();//nextInt() reads the next whole number inputed
System.out.println("\n");//new line
Scanner snum = new Scanner(System.in);//makes a new scanner named snum
System.out.print("Enter your second number: ");
int sn= snum.nextInt();
System.out.println("\n");
int sum = fn + sn;//adds both of the numbers together
System.out.println("Your sum is: " + sum);//prints out the sum;
}
}
Challenges
1. Make a calculator with addition and subtraction [very simple]
2. Make a calculator with addition, subtraction, multiplication,division [simple]
3. Make a simple input-menu [simple to intermediate]
::Questions::
1. What is the difference between an 'int' and a 'double'?
2. What is the difference between a 'char' and a 'string'
3. How do you start and end a multi-line comment?
4. How do you start a single line comment?
5. What does '/n' and '/t' do within an output stream?