of a research project to develop software for electronic devices like TV, VCRS, toasters, etc u Also wanted a programming language that was small, fast, efficient, and portable to a range of hardware devices
program will do, turns it into “machine code” (a format the computer can run really fast), then saves that to be executed later. u An interpreter steps through the source code line by line, figuring out what it’s doing as it goes. u Technically any language could be compiled or interpreted, but one or the other usually makes more sense for a specific language. Generally, interpreting tends to be more flexible, while compiling tends to have higher performance.
Java interpreter. u Compiler takes Java program and generate bytecodes from it u Bytecodes are set of instruction processed by a program called the virtual machine, rather than the “real” computer u Processor is the circuit in a computer that processes the basic instructions and make the computer work u Java interpreter translate .class file (contains java bytecodes) into a code (raw machine language code) that works on your computer
[] ) { System.out.println(“Hello World!”) ; } } TWO MAIN PARTS: - PROGRAM IS ENCLOSED IN A CLASS DEFINITION (HELLOWORLD) - THE BODY OF THE PROGRAM IS CONTAINED IN A ROUTINE CALLED MAIN() - MAIN() IS THE FIRST THING THAT RUNS WHEN A PROGRAM EXECUTES
After every method/function, you must end with ; a semicolon u Use { } u Indentation is not important, it’s only for making your code neat u To print, you need to type out u System.out.println( “ “ ); u For variables, it’s the same but you need to declare the variable type u Ex, string phrase = “hello world”; u int number = 4;
in Python u Run time error or logic error is when syntax is correct but the program does something it isn’t supposed to do u You made an logic error u like misspelling in a string, a wrong math formula, etc
problem u MUST BE: u Unambiguous : Precise instructions for what to do at each step and where to go next. No guess work. u Executable : It can be carried out, no syntax errors and etc u Terminating : the sequence of steps must eventually come to a stop u if it doesn’t your program will continue to run forever and forever and it most likely will break your program/computer u Will usually show up as an error
you use // (double slashes) u Or /* and */ // this is a comment for only one line /* this is also a comment but I can write multiple lines of comments */
are made up of many kinds of smaller object u Is an instance of a class u Objects have state and behaviors u Example: dogs has states: color, name, breed, and behaviors like barking, eating, and etc
and state of an object u Embody all the features of a particular set of objects u When you write a program in object-orientated language, you don’t define the objects. You define the classes of objects
that’s okay if you don’t 100% understand them now) u Every class has a constructor u If you don’t create one yourself, Java will make a default one for you that may not be correct or not u Each time a new object is created, one constructor will be made and used. u Can have more than one constructor for one class u Rule: constructors have the same name as the class.
a letter, or underscore, u No starting with numbers u No other symbols other than underscore u CASE SENSITIVE u milespergallon and milesPerGallon are two different variables u Never start with a capital letter for variables u Don’t use Java reserved worlds like class, double, and etc (you’ll learn more soon)
them functions) u Methods often require values that give them details about the work that the methods need to do u Like Println method, you must supply the string or value to be printed u Argument is the value that is supplied in a method call u System.out.println(“greetings”)
a value u Ex. length method returns a value string greeting = “Hello”; int numberOfCharacters = greeting.length(); System.out.println(numberOfCharacters);
the () u Reminder, you usually put arguments or parameters in () u Or the code will give you something (return a value) that you might need to store in a variable if you want to save it
code so that will print out the remainder of 2 public class Modulo { public static void main(String[] args) { int myRemainder = System.out.println(myRemainder); } }
not equal to public class EqualityOperators { public static void main(String[] args) { int myInt = -1; double myDouble = -1.0; System.out.println(myInt == myDouble); } }
u Returns true only if both sides of the equation are true u System.out.println ( true && false ); u || means or u Returns true if at least one expression is true u System.out.println ( true || false);
Library are listed in the API documentation u Application Programing Interface u If you need help finding a code or figuring out what a particular code does, you go there u You can also create your own methods (System programming) u Programmers who uses the already created standard Java classes are Application programmers
need more conditionals if (shoeSize > 12) Print “sorry, your shoe size is out of stock” else if (shoeSize >= 6) Print “you shoes size is in stock” else Print “We don’t carry sizes smaller than a size 6”
int round; if (round > 12) System.out.println("The match is over!"); } else if (round > 0) { System.out.println("The match is underway!"); } else { System.out.println("The boxing match hasn't started yet."); }
method without one (like bark) u Except you have to write variable type and variable name in the ( ) public void run (int feet) --------------------------------------------- Spike.run(30);
structure should function • - Constructor: instructs the class to set up the initial state of an object • - Object: instance of a class that stores the state of a class • - Method: set of instructions that can be called on an object • - Parameter: values that can be specified when creating an object or calling a method • - Return value: specifies the data type that a method will return after it runs • - Inheritance: allows one class to use functionality defined in another class
programming u Most professional code requires heavy use of different types of data structures u As long as you understand how data structure works, you’re in good shape
organizing data in a computer so that it can be used efficiently u The are the key to designing and programming efficient algorithms u All of these affects the quality of your program u Which affects your electronic products and user’s experiences with them
inside the loop body, the variable only exists for that iteration u As your code keeps going through different iteration, the variable is created again and again and then forgotten again and again
and forgotten u This loop will keep printing as long as num < 10 u ++ is a short cut for +1 (adding 1 to the variable value) int num = 5; while (num < 10) { equ= num + 2; num++; System.out.println(equ); }
that runs forever and can only be stopped by killing the program or restarting the computer. u You forgot to update the variable the controls the loop (in boolean expression) u Off by one error: You end the program maybe 1 times off after you wanted to. u Keep in mind when you want to us < or <=
diagrams to help you figure out what you need to write in real coding language. u When you’re having trouble writing a loop, always go back to pseudocode and hand trace the loop you want to write u Then go back to the programming language
runs from a starting point to an ending point with a constant increment or decrement. u In other words, it is controlled by a counter u There isn’t much difference between while and for loops except for loops are cleaner and neater to use
10 double (data type) double [] value = new double [10] u double[] values = we are creating the variable of the type array u [10] is the length of the array u new operator constructs the array
double [10] u So your array hold elements of the type double here u Position number of the elements (or index number) starts from 0 u To access an element and store a number in it value [0] = 1.0;
packages u Package is a collection of classes with a related purpose u Ex. Rectangle class belong to package java.awt which contains many classes for drawing window, graphics, and shapes u You have to import these classes u System and string classes don’t need to be imported by it’s already automatically imported