Java Programming by Ramiro Rocha
Unit 1.1 Variables
Basic Variables Variables come in 3 basic types String int boolean class Test001{ public static void main(String[] args){ //this is an int int number = 10; //this is s String String text = “Hello, World!”; //this is a boolean Boolean state = true; //these are “print” statements System.out.println(number); System.out.println(text); System.out.println(state); } Variables come in 3 basic types String int boolean These variable types are not compatible! You cannot use a String in place of an int. *you can print all variables
A String Variables Question 1: Which is a String? A) “20” B) 20 String variables are just text. To use a String, you will use double quotes surrounding some plain text. A String is essentially a bunch of chars put together. Question 1: Which is a String? A) “20” B) 20 C) ‘20’ A
A int Variables Question 2: Which is an int? A) 13 B) 13.5 int variables are whole numbers. To use an int, you will type a number without any other type of characters. An int is essentially just a number. Question 2: Which is an int? A) 13 B) 13.5 C) “thirteen” A
A&B boolean Variables Question 3: Which is an boolean? A) true boolean variables are whole numbers. To use a boolean, you will type either true or false. A boolean is just a true or false statement. Another way of using a Boolean is by comparing two things. For example: 10 == 15 returns false. Question 3: Which is an boolean? A) true B) false C) “false” A&B
Unit 1.2 Comparing Variables
Comparing values There are a bunch of comparators to use. And these that I don’t have a name for The evaluators && - and || - or == - equal to ! – not != - not equal to You would use a ! To invert a value (aka, turn a true into a false or a false into a true) > - greater than < - less than You would use a special comparator for Strings! >= - greater than or equal to <= - less than or equal to
B Comparing Strings Question 4: To compare a String to another String you would use String.equals() to test if both Strings are the same. To test if two Strings are different, you would use !String.equals(). Less than and greater than and their variants are not applicable to String variables. Question 4: Which of these compares Strings properly? A) “Chocolate” != “Vanilla” B) “Chocolate”.equals(“Vanilla”) C) “Chocolate” < “Vanilla” B
Unit 1.3 If-else statements and for while/for loops
What are if statements? You use boolean variables to evaluate how different values compare to each other. if statements allow you to do something if a boolean evaluates to true. else if statements allow you test for something if, and only if the preceding if or else if is not testing for true. else statements execute if all preceding else ifs and ifs cannot execute. class Test002{ public static void main(String[] args){ boolean var = !(“text”.equals(“no text”)); if (var) System.out.println(“true”); else System.out.println(“false”); }
Syntax for if statments if statements will always start with if (/* **insert boolean evaluation here** */) After that, if statements can be written in two different ways. If you are only executing one line if the if/else-if is true, you do not have to surround the code with curly brackets. If you are executing more than one line of code, surround the code to execute if the if/else-if is true with curly brackets. else statements follow the same rules for bracket syntax, but do not need a boolean evaluator.
class Test003{ public static void main(String[] args){ if (/ class Test003{ public static void main(String[] args){ if (/* **insert boolean evaluation here** */) System.out.println("eval 1 is true"); else if (/* **insert boolean evaluation here** */){ System.out.println("eval 2 is true"); } else System.out.println("eval 1 and 2 are both false");
What are while loops and for loops? While loops are loops that do something while a boolean evaluator is true. When the evaluator is not true anymore, when the last line within the loop is executed, it will test to see if it is true or not. If it is not true, it will go to the next line after the loop. for loops are similar, but not quite the same. I will cover them soon. while (/* **insert boolean statement here** */){ //insert statements here } While (/* **insert boolean statement here** */) //insert ONE statement here
For loops and how they work for loops are like while loops, but they use a brand new variable to work. for (int i = 0;i < 10;i++) As you can see, the for loop defines its own variable within the parenthesis. This allow you to do something with a number such as math or print out a series of numbers. The outline for it goes for (/* **define an int variable** */;/* **boolean involving the variable you made** */;/* **what to do after each turn** */){} The variable that the for loop defines can only be used within the loop that defines it.
Questions True or false: you can use the variable created for the for loop outside of that loop.
Answers True
Unit 1.4 More variables
A list of variable types String use only if you plan to deal with extrememly large or extremely small numbers(2^64 being the largest value and 2^-63 being the smallest) int byte boolean 8-bit signed data float short a decimal number (ex. 10.3f) 16-bit signed two's complement integer double char a decimal number, but more exact and precise than a float(ex. 10.3) a single 16-bit Unicode character long
more info For more information go to http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.ht ml
Unit 1.5 Input
The Scanner class For the first time, you will be using a variable other than an int, boolean, or String. You will learn about the new type of variable, the Scanner. The Scanner allows you to take input from the user through the console. You can take an input of all different types of built –in variables. The Scanner is very useful in a lot of programs. Later we will move on to more advanced ways of taking an input.
Scanner Syntax Scanner sc = new Scanner(System.in); System.out.print(“What is your name? >”); String name = sc.nextString(); System.out.print(“What is your age? >”); int age = sc.nextInt(); System.out.print(name + “ is ” + age + “years old”);
Unit 2.1 Arrays
What is an array? An array is a listof data. An array can only contain one type of variable. But, it can contain multiple of that variable type. It can be of varying size. Arrays are used to store data of one type using only one variable. For example, if you had a bunch of names you wanted to keep safe, you would use an array called names. Each item in an array has an index. Think of it like an id for each item. the first index of the array will be 0. The reason for this is because the first number is not 1. It is 0.
int size = 10; int[] numList1 = new int[size]; for(int i = 0;i < size;i++){ numList1[i] = i + 1; System.out.println(numList1[i]); } int[] numList2 = new int[2]; numList2[0] = 23; System.out.println(numList2[0]); System.out.println(numList2[1]); //one of the above will give an error, which one?
Iterating over an array Just like you can iterate over a number, you can iterate over each item in an array. String[] names = {“John Smith”, “Mary Sue”, “Gary Stew”, “Mohammad Lee”}; for (String name:names) System.out.println(name); The reason for this is because it allows for more readable code. If you are not doing math, why would you need a number?
Exercises Make an integer array and give it a size of 10. Make a for loop that asks for an integer and sets each entry in that array to the integer that was taken in.
Unit 2.2 2d arrays
What makes it 2D? A 2D array is an array that has a y coordinate and an x coordinate. The y coordinate goes first 0,0 0,1 0,2 0,3 0,4 1,0 1,1 1,2 1,3 1,4 2,0 2,1 2,2 2,3 2,4 3,0 3,1 3,2 3,3 3,4 4,0 4,1 4,2 4,3 4,4 5,0 5,1 5,2 5,3 5,4 6,0 6,1 6,2 6,3 6,4 Just like a regular array, it can be of only one data type 2D arrays are like arrays full of arrays
//Example Code int[][] array1 = { {1,2,3}, {4,5,6}, {7,8,9} }; int[][] array2 = new int[3][3]; array2[0][0] = 2; System.out.println(array1[0][0]); System.out.println(array2[0][0]);
Unit 2.3 User defined methods
What is a 2-Dimensional array? A 2d array is used to store a table of data. 2d arrays are essentially an array within an array
Example public void println(Object arg0){ System.out.println(arg0); } /* **outline** modifiers return-type name (parameters){ **statements go here** return x; */
Why make a method? One reason you would make a method is because you’re doing this one task over and over again. Say that the only thing changing each time you do the task is one variable. So, one variable changes and the rest stays the same. Instead of copying and pasting the code and tweaking it, you can just make a method and type one line of code to do what would normally take 3 or 4. Put simply, we do it because programmers are lazy.
A note about methods if the return-type is void, you don’t need a return statement. This is because void is equivalent to null. If you ever need to stop a method in the middle of the program, put return; and the method will stop running.
Exercises Make a method called add() that takes two doubles, adds them together and returns that number. Make a method called print() that takes a String and prints it out.
Unit 2.4 modifiers
What are modifiers? Modifiers are the properties of a variable. They change how the variable acts. Modifiers go before the type declaration. Modifiers can be applied to both methods and variables
static Static variable: 1)Memory allocated before creation of object. 2)static variables are class variables and the values remains same fr the whole class and its value is same for all classes in a program. 3) There is only one copy of static variable and even when the class is instantiated, the value remains the same. 4)Static variables value is automatically show(there is no need to create an object.) Non Static variable: 1)Every time the class is instantiated, the object has their own copy of these variables. 2)Non static value is called by creating an object. 3)Non-Static Variables are loading only when an object is creating for the particular class
final A variable that has the final modifier cannot change from its initial value. It is a constant. Just as π is a constant so is a final variable. Variables with the final modifier should always have a name that is in all caps. For example: final double PI = 3.1415926;
private and public private variable can only be accessed within the class in which they are defined. public variables can be accessed outside of said class.
What is scope? Scope is the concept of where variables can be used Variables defined within a method can only be used within that method. Variables defined outside of a method can only be used within that class. But they can also be used within that class’s methods. Variables defined within a loop or if statement can only be used within that loop/if statement. The private and public modifiers change the scope of an object.
Unit 2.5 Custom Classes
What is a class? A class is an object. It is essentially a variable type. A class can have its own constructor, fields, and methods.
fields A field is a variable that acts like a property of an object. not all variables are fields, but all fields are variables. for example, age would be a field of the class human or animal.
Constructors A constructor is what allows you to specify the values of fields contained within the class. Say you have a field named radius that when the containing Circle class is instantiated, if no radius is given, is set to 1. Constructors work like methods, but don’t return anything. Thus they don’t need any return statements or a return- type.
public class Human{ private String name; private double age; public Human(String name, double age){ this.name = name; this.age = age; } public double getAge(){ return age; public String getName(){ return name; public void changeName(String newName){ this.name = newName;
Questions What is a constructor? A method The method of setting the class’s fields A variable True or false: A class is a variable type Why? What is a field? A property of an object A String
Answers B True A
Exercises Create a class called Car. Add the make, modelYear, color, and type fields to the class. Make them private. Make a getMake(),getModelYear(),getColor(), getType() methods to the class. Make them public.