O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Intro to programing with java-lecture 1

Próximos SlideShares
Carregando em…3

Confira estes a seguir

1 de 46 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (17)

Semelhante a Intro to programing with java-lecture 1 (20)


Mais de Mohamed Essam (20)

Mais recentes (20)


Intro to programing with java-lecture 1

  1. 1. Intro to structured Programing with java lecture 1
  2. 2. Hello! I am Mohamed Essam ! You can find me at mohamedessam.cs@gmail.com 2
  3. 3. Let’s make the world a better place 1
  4. 4. “ “Talk is cheap. Show me the code.” ― Linus Torvalds 4
  5. 5. What Java is ? ○ Java is a general-purpose computer-programming language that is concurrent, class-based, object-oriented and specifically designed to have as few implementation dependencies as possible. ○ Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. ○ As of 2016, Java is one of the most popular programming languages in use. ○ In Software engineering process the end one called maintaining which enable the software engineering maintain the software instead of build a new one from scratch and that is what make java developer still required to maintain the current app even after using another technologies for build android apps . ○ I can use it to build native Android Apps, Web Applications and desktop Applications. 5
  6. 6. Who many java developer around the world? European Union -27 Countries 6 Wikipedia say there are 10,000,000 and the majority lies in EU27 and about 5,900,000
  7. 7. How many java developer required in Egypt? According to Wazzuf app for offering job for software engineers i Egypt at my current date now at 01/03/2019 is 275 job. 7
  8. 8. Setting up Environment 8
  9. 9. Java Development Tools ○ You can use a text editor, such as the Windows Notepad or WordPad, to create Java programs and to compile and run the programs from the command window. ○ You can also use a Java development tool, such as NetBeans or Eclipse. These tools support an integrated development environment (IDE) for developing Java programs quickly. ○ Editing, compiling, building, executing, and debugging programs are integrated in one graphical user interface. 9
  10. 10. ○ Using these tools effectively can greatly increase your programming productivity. NetBeans and Eclipse are easy to use if you follow the tutorials. Tutorials on NetBeans and Eclipse can be found under Tutorials on the Student Companion Website at www.pearsonhighered.com/liang 10
  11. 11. Setting Up Java ○ You can get the latest, greatest versions of Java by visiting https://www.oracle.com/technetwork/java/javase/down loads/index.html Look for the newest available version of the JDK. Select a version that runs on your computer’s operating system. Figure 2-1 shows me clicking a Download JDK button (circa March 2014) at the Oracle website. 11
  12. 12. Setting Up Java 12
  13. 13. Setting Up the NetBeans Integrated Development Environment 13 Here’s how you download NetBeans: 1. Visit www.eclipse.org. 2. Look for a way to download NetBeans for your operating system https://netbeans.org/downloads/8.0.2/
  14. 14. The Way Java Works The goal is to write one application (in this example, an interactive party invitation) and have it work on whatever device your friends have. 14
  15. 15. 15 Source code in java Good Morning 1.Source Create a source document. Use an established protocol (in this case, the Java language). 2.Compilor Run your document through a source code compiler. The compiler checks for errors and won’t let you compile until it’s satisfied that everything will run correctly 3.Output(Code) The compiler creates a new document, coded into Java byte code. Any device capable of running Java will be able to interpret/translate this file into something it can run. The compiled byte code is platform independent. 4.Virtual Machine Your friends don’t have a physical Java Machine, but they all have a virtual Java machine (implemented in software) running inside their electronic gadgets. The virtual machine reads and runs the byte code.
  16. 16. Code structure in Java ○ Put a class in a source file. ○ Put methods in a class. ○ Put statements in a method. 16
  17. 17. What goes in a source file? A source code file (with the .java extension) holds one class definition. The class represents a piece of your program, A class is an extensible program- code-template for creating objects, although a very tiny application might need just a single class. The class must go within a pair of curly braces. 17
  18. 18. What goes in a class? A class has one or more methods. In the Dog class, the bark method will hold instructions for how the Dog should bark. Your methods must be declared inside a class (in other words, within the curly braces of the class) 18
  19. 19. What goes in a method? ○ Within the curly braces of a method, write your instructions for how that method should be performed. Method code is basically a set of statements, and for now you can think of a method kind of like a function or procedure. 19
  20. 20. What goes in a method? ○ Example : We can make a method for dog barking to help a user know why his dog is barking  Is that sign because of a medical problem?  Is that because of the protective?  Is he feel lonely or bored?  Is it because feeling hungry and need a food ? The method can give a detailed instruction and guide the user to know the reason of barking. 20
  21. 21. What method is ? ○ You can think of function as a black box take an input and produce an output and it’s not important to know what is the algorithm inside of it , just enough to know how you can use and what is their function 21 Hello,World
  22. 22. ○ It’s something similar to the coffee machine take input and produce output and we don't know what is the algorithm that written inside of it just we have to know how we can use it. 22
  23. 23. ○ Later we will learn how to define an algorithm and it will be something like CHORUS in songs lyrics. ○ The function make your code more organized. 23
  24. 24. Anatomy of a class  the JVM runs everything between the curly braces { } of your main method. Every Java application has to have at least one class, and at least one main method (not one main per class; just one main per application).  The main() method is where your program starts running like (When Green Flag clicked ) in Scratch. 24 public static void main (String[] args) { // your code goes here }  When the JVM starts running, it looks for the class you give it at the command line. Then it starts looking for a specially-written method that looks exactly like:
  25. 25. 25
  26. 26. 26 everything goes in a class.  You’ll type your source code file (with a .java extension),  then compile it into a new class file (with a .class extension). When you run your program, you’re really running a class. Running a program means telling the Java Virtual Machine (JVM) to “Load the MyFirstApp class,  then start executing its main() method. Keep running ‘til all the code in main is finished.”. Writing a class with a main In Java,
  27. 27. 27
  28. 28. Once you’re inside main (or any method), the fun begins. You can say all the normal things that you say in most programming languages to make the computer do something. Your code can tell the JVM to:  Do Something  Do Something Again and Again  Do something under a Condition What can you say in the main method? 28
  29. 29. A First Simple Program /* This is a simple Java program. Call this file "Example.java". */ class Example { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println(“First Line."); System.out.println(“Second Line."); System.out.println(“Third Line."); } } 29
  30. 30. A Closer Look at the First Simple Program ○ To compile the Example program Click in the green sign above, as shown. ○ When the program is run, the following output is displayed: ○ First Line. ○ Second Line. ○ Third Line. ○ The Compile runs the files line by line so they printed in this sequence /* This is a simple Java program. Call this file "Example.java". */ ○ This is a comment. Like most other programming languages, Java lets you enter a remark into a program’s source file. The contents of a comment are ignored by the compiler. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code. In this case, the comment describes the program and reminds you that the source file should be called Example.java. Of course, in real applications, comments generally explain how some part of the program works or what a specific feature does. 30
  31. 31. “ “Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson 31
  32. 32. A Closer Look at the First Simple Program ○ Java supports three styles of comments. The one shown at the top of the program is called a multiline comment. This type of comment must begin with /* and end with */. Anything between these two comment symbols is ignored by the compiler. As the name suggests, a multiline comment may be several lines long. ○ The next line of code in the program is shown here: ○ This line uses the keyword class to declare that a new class is being defined. Example is an identifier that is the name of the class. The entire class definition, including all of its members, will be between the opening curly brace ({) and the closing curly brace (}). 32
  33. 33. A Closer Look at the First Simple Program ○ The next line in the program is the single-line comment: ○ shown here: // Your program begins with a call to main(). ○ This is the second type of comment supported by Java. A single- line comment begins with a // and ends at the end of the line. As a general rule, programmers use multiline comments for longer remarks and single-line comments for brief, line-by-line descriptions. 33
  34. 34. A Closer Look at the First Simple Program ○ The next line of code is shown here: ○ public static void main(String args[]) { ○ This line begins the main( ) method. As the comment preceding it suggests, this is the line at which the program will begin executing. All Java applications begin execution by calling main( ). ○ The next line of code is shown here. Notice that it occurs inside main( ). System.out.println(“First Line."); This line outputs the string “This is a simple Java program.” followed by a newline on the screen. Output is actually accomplished by the built-in println( ) method. In this case, println( ) displays the string which is passed to it. ○ Notice that the println( ) statement ends with a semicolon. All statements in Java end with a semicolon. 34
  35. 35. Data types ○ Java defines eight primitive types of data: ○ byte, short, int, long, char, float, double, and boolean. ○ The primitive types are also commonly referred to as simple types, and both terms will be used in this book. These can be put in four groups: ○ Integers This group includes byte, short, int, and long, which are for whole- valued signed numbers. ○ Floating-point numbers This group includes float and double, which represent numbers with fractional precision. Characters This group includes char, which represents symbols in a character set, like letters and numbers. ○ Boolean This group includes boolean, which is a special type for representing true/false values. 35
  36. 36. A Second program ○ Further, you can perform mathematical computations and display the result on the console. Listing 1.3 gives an example of evaluating. public class ComputeExpression { public static void main(String[] args) { System.out.println((10.5 + 2 * 3) / (45 – 3.5)); } } ○ Output: 0.39759036144578314 36
  37. 37. Variable ○ You can think of variable as a little box or a container to store data in, in the memory of the computer and every data type has its own variable. 37
  38. 38. 38 But What would happen represented a 8 bytes number in a 4 bytes variable ?
  39. 39. 39 Arian 5 the most expensive software failures in history.
  40. 40. 40 ○ On June 4th, 1996, the very first Ariane 5 rocket ignited its engines and began speeding away from the coast of French Guiana. 37 seconds later, the rocket flipped 90 degrees in the wrong direction, and less than two seconds later, aerodynamic forces ripped the boosters apart from the main stage at a height of 4km. This caused the self-destruct mechanism to trigger, and the spacecraft was consumed in a gigantic fireball of liquid hydrogen. ○ The disastrous launch cost approximately $370m, led to a public inquiry, and through the destruction of the rocket’s payload, delayed scientific research into workings of the Earth’s magnetosphere for almost 4 years. The Ariane 5 launch is widely acknowledged as one of the most expensive software failures in history.
  41. 41. ○ The fault was quickly identified as a software bug in the rocket’s Inertial Reference System. The rocket used this system to determine whether it was pointing up or down, which is formally known as the horizontal bias, or informally as a BH value. This value was represented by a 64-bit floating variable, which was perfectly adequate. ○ However, problems began to occur when the software attempted to stuff this 64-bit variable, which can represent billions of potential values, into a 16-bit integer, which can only represent 65,535 potential values. For the first few seconds of flight, the rocket’s acceleration was low, so the conversion between these two values was successful. However, as the rocket’s velocity increased, the 64-bit variable exceeded 65k, and became too large to fit in a 16-bit variable. It was at this point that the processor encountered an operand error, and populated the BH variable with a diagnostic value. 41
  42. 42. A Third program ○ // Compute distance light travels using long variables. class Light { public static void main(String args[]) { int lightspeed; long days; long seconds; long distance; // approximate speed of light in miles per second lightspeed = 186000; days = 1000; 42
  43. 43. A Third program seconds = days * 24 * 60 * 60; // convert to seconds distance = lightspeed * seconds; // compute distance System.out.print("In " + days); System.out.print(" days light will travel about "); System.out.println(distance + " miles."); } } 43
  44. 44. Let’s write a simple traffic light code ○ First we will write a code for a traffic light system to prompt the user for input and check if the input is red will print go , if yellow will print wait and if green it will print go! ○ Hint take care and use else statement to avoid any problem in the traffic light ○ After that we can make it more difficult by using a counter instead of the user input . 44
  45. 45. references ○ Beginning Programming with Java For Dummies, 4th Edition. ○ Data Structures and Problem Solving Using Java4edWeiss. ○ fundamentals-of-computer-science-using-java. ○ Head First Java. ○ Java The Complete Reference, 7th Edition. ○ CS50 introduction to Computer Science. 45
  46. 46. 46 Thanks! Any questions?