Java for beginners the complete guide to learning java for beginners

101 82 0
Java for beginners  the complete guide to learning java for beginners

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

JAVA FOR BEGINNERS THE COMPLETE GUIDE TO LEARNING JAVA FOR BEGINNERS Bruce Berke © 2018 Copyright 2018 by Bruce Berke - All rights reserved This document is geared towards providing exact and reliable information in regards to the topic and issue covered The publication is sold with the idea that the publisher is not required to render accounting, officially permitted, or otherwise, qualified services If advice is necessary, legal or professional, a practiced individual in the profession should be ordered - From a Declaration of Principles which was accepted and approved equally by a Committee of the American Bar Association and a Committee of Publishers and Associations In no way is it legal to reproduce, duplicate, or transmit any part of this document in either electronic means or in printed format Recording of this publication is strictly prohibited and any storage of this document is not allowed unless with written permission from the publisher All rights reserved The information provided herein is stated to be truthful and consistent, in that any liability, in terms of inattention or otherwise, by any usage or abuse of any policies, processes, or directions contained within is the solitary and utter responsibility of the recipient reader Under no circumstances will any legal responsibility or blame be held against the publisher for any reparation, damages, or monetary loss due to the information herein, either directly or indirectly Respective authors own all copyrights not held by the publisher The information herein is offered for informational purposes solely, and is universal as so The presentation of the information is without contract or any type of guarantee assurance The trademarks that are used are without any consent, and the publication of the trademark is without permission or backing by the trademark owner All trademarks and brands within this book are for clarifying purposes only and are the owned by the owners themselves, not affiliated with this document Table of Contents Introduction Chapter 1: Getting Started with java Chapter 2: Java Variables Chapter 3: Java operators Chapter 4: Java arrays Chapter 5: Decision making Chapter 6: looping Chapter 7: Inheritance Chapter 8: overriding Chapter 9: Polymorphism Chapter 10: Abstraction Chapter 11- encapsulation Chapter 12- Interfaces Chapter 13- Packages Chapter 14- Command-Line Arguments Chapter 15- Recursion Chapter 16- Method Calls Chapter 17- Java Applets Conclusion INTRODUCTION Java is a wide programming language, packed with lots of features good for application development It’s a good coding language for the design/development of desktop apps These are popular as standalone applications If you need a distributed application, Java is the best language to use Java applets can be embedded/added on web pages The many features offered by Java have made it a popular coding language Its wide features make it applicable in the development of apps applicable in various industries To code in Java, you should get the Java compiler Java is compiled, not interpreted You need an editor in which you can write your Java codes Notepad is good for this With these, you can start writing and running your Java codes This means it’s easy to get started with Java, making Java a good language for beginners The Java bytecode, normally generated after compiling Java source codes, is platform-independent, meaning it can be run on any platform including Windows, Solaris, Linux, etc Java apps are secured via public-key cryptography, making them safe/secure Java apps can be linked to various database management systems, for example, MySQL, Oracle, SQL Server, etc This book is an excellent guide on Java programming Get it and know every aspect of Java and transition from Java Beginner to Java Expert! CHAPTER 1: GETTING STARTED WITH JAVA Java is a platform as well as a coding language It is a robust, high-level, object-oriented and secured programming language It was released in 1995 by the Sun Microsystems Java treats everything as an object Its object model makes it easy to extend The language is compiled, meaning that we have a Java compiler Java is also platform independent After compiling your Java source code, you get the byte code which can be executed on any platform Java is also well known for being easy to learn If you know some basic concepts about object-oriented programming, it will be easy for you to learn Java It is packed with numerous features, making it widely functional Authentication in Java is done using a public-key encryption, making its systems very secure This is the reason we can develop temper-free and virus-free systems with Java In Java, errors are identified during the compile time, meaning that you cannot go to the run phase without first sorting out the error ENVIRONMENT SETUP For you to write and run Java programs, you need a text editor and the Java compiler The Java compiler comes with the JDK (Java Development Kit) which can be downloaded and installed for free You also need to have a text editor where you will be writing your Java programs You can even use a basic text editor like Notepad and it will be okay However, there are advanced text editors for Java such as Eclipse and Netbeans First, download and install the JDK on your computer After that, you will have installed Java on your machine Open your browser then type or paste the following URL: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html From there, you can get the latest version of the JDK You will then have to set the environment variables to know the right Java installation directories This means you should set the path variable Mostly, the installation is done in the c:\Program Files\java\jdk directory To set the path on Windows, correctly “My Computer”, choose “Properties”, open the “Advanced” tab then choose “Environment variables” Add the “c:\Program Files\java\jdk\bin” path For Linux users, read the documentation of your shell JAVA SYNTAX A Java program consists of objects communicating via invocation of methods from each other Below are the main constructs in a Java program: Object- Objects are characterized by state and behavior For example, a cat has states like color, breed name and behaviors like eating, meowing and wagging tail Class- This is a blueprint/template describing the state/behaviors supported by its objects Method- A method is a behavior Logic is written in methods, and they are usable in data manipulation FIRST PROGRAM This is our first Java program It should print “Hello World” once executed: public class OurFirstProgram{ public static void main(String args[]){ System.out.println("Hello World"); } } Open your text editor like Notepad and create a new file named “FirstProgram.java Save the file The java extension marks the file as a Java file Type the above program code in the file and save the changes Open the command prompt then navigate to the directory in which you have saved the file Run the commands given below from that directory: javac FirstProgram.java java FirstProgram In my case, I saved the file on the desktop I executed the commands as follows: The program prints “Hello World” as the result In the command “javac FirstProgram.java”, we are invoking the java compiler (javac) to compile the program The command returned nothing on the command prompt since the program has no error, and no warning was generated The command generates a class file (FirstProgram.class) in the directory; please confirm In the command “java FirstProgram”, we are simply running the program to give us the result Consider the following line extracted from the code: public class FirstProgram{ The line simply declares a class named FirstProgram Java is a case sensitive language, hence this class must always be referred to as it is written above The “public” keyword declares that class as public, meaning it is accessible from any other class within the same package The { declares the beginning of the class body, and it should be closed by a } Note: The filename in which we write the program should match the class-name; otherwise, you will experience trouble when running the program In our case, the class was named “FirstProgram”, hence the filename should be FirstProgram.java Consider the following line extracted from the code: public static void main(String args[]){ That’s the main method A java program cannot be executed or run without this method Consider the following line extracted from the code: System.out.println("Hello World"); We are simply invoking or calling the “println” method to print the text Hello World on the terminal of the command prompt Lastly, we have two closing curly braces, }} The first one closes the opening brace for the main method while the second one closes the opening brace for the class CHAPTER 17- JAVA APPLETS Applets are Java programs capable of running on web browsers It can act like a full functional app since it has full Java API The applet class extends java.applet.Applet Applets don’t have a main method, hence they don’t invoke main() They are embedded on HTML pages After an HTML page with an applet is opened, its code is downloaded to the local machine The JVM (Java Virtual Machine) is mandatory for you to view applets Applets are processed on the client’s side, hence they give users a quick response compared to other apps Applets offer good security APPLETS’ LIFECYCLE Applets go through these steps: Initialization- the init() method initializes applets Starting- the start() method starts applets Painting- done after calling paint() method Stopping- we invoke the stop() method to stop an applet Destroying- the applet is destroyed by invoking the destroy() method Applets are viewed by HTML Example: Create a new file in text editor with the name “MyApplet.java” Add this code to it: import java.awt.Graphics; import java.applet.Applet; public class OurApplet extends Applet{ public void paint(Graphics gc){ gc.drawString("First Applet!",150,150); } } Create a new file in editor named code1.html Add this code to it: First, we have Java code in which we added code regarding what our applet should The class is named MyApplet Consider this: gc.drawString("First Applet!",150,150); We are drawing the “First Applet! Text at points 150, 150 of the applet window Note the two libraries added via the import statement, that is, Graphics and Applet libraries The paint() method is defined in the Graphics library If we don’t import it, an error will be generated since Java compiler won’t understand what we mean We have HTML code in file code1.html In this, we created a window on which to draw text Note how we have linked HTML code and Java code: We linked to MyApplet.class rather than MyApplet.java This means Java code should compile first to give the class file, then HTML code will recognize it Open the OS terminal, navigate to the location of the files Run this command to compile Java code: javac MyApplet.java This will generate the MyApplet.class file We can use the appletviewer for our applet viewing Run this command on console: appletviewer Code1.html A window should popup with the text First Applet! That is the Applet Change the point where the text is drawn by altering the coordinates It will change the location on the window Try this and see the shapes generated: Create the file MyPaint1.java with this code: import java.applet.Applet; import java.awt.*; public class Mypaint1 extends Applet { public void paint(Graphics gc){ gc.drawOval(130, 40, 60, 50); gc.drawOval(120, 60, 10, 15); gc.drawOval(190, 60, 10, 15); gc.drawOval(160, 50, 10, 10); gc.drawOval(140, 50, 10, 10); gc.drawOval(150, 70, 15, 10); gc.drawRect(145, 90, 30, 30); gc.drawRect(120, 120, 80, 120); gc.drawRect(130, 240, 20, 120 ); gc.drawRect(170, 240, 20, 120); gc.drawRect(130, 360, 30, 20); gc.drawRect(170, 360, 30, 20); gc.drawLine(200,160,240,240); gc.drawLine(240,240, 280,160); gc.drawRect(280, 130, 120, 30); gc.setColor (Color.red); gc.fillRect (280, 130, 120, 30); gc.drawRect(280, 160, 120, 30); gc.setColor (Color.green); gc.fillRect (280, 160, 120, 30); gc.drawRect(280, 190, 120, 30); gc.setColor (Color.black); gc.fillRect (280, 190, 120, 30); } } Create the new file Code2.html with this code: Hellow World The Hellow Applet Compile MyPaint1.java, then run Code2.html Graphics library comes with functions/methods that can draw numerous shapes To draw an oval shape, call the drawOval() method Pass parameters to it, which are the points to be touched by the edges of the oval To draw a rectangle, call the drawRect() method and pass coordinates to it which specify its corners The drawLine() method helps one draw a line The setColor() method helps in setting object color Example 3: Write this code in the MyPaintClass.java file: import java.applet.Applet; import java.awt.*; public class MyPaintClass extends Applet { public void paint(Graphics gc){ gc.drawRect(120, 120, 160, 120); gc.drawLine(120, 120, 440, 365); } } Its HTML code: Hellow World The Hellow Applet Compile the MyPaintClass.java class then run MyPaint.html code with ap-pletviewer You will get a rectangle and line DISPLAYING IMAGES Images can be displayed on applets as applets are popular in animations and games The class java.awt.Graphics has the drawImage() method for image display The method getImage() returns an image object The method getCodeBase() returns base URL The getDocumentBase() image returns document URL in which the applet has been embedded Example: import java.awt.*; import java.applet.*; public class ShowImage extends Applet { Image img; public void init(){ img = getImage(getDocumentBase(),"house.png"); } public void paint(Graphics gc) { gc.drawImage(img, 30,30, this); } } The drawImage() method displays the image Its 4th parameter is an object for ImageObserver Save the code in the file named ShowImage.java Add this code to the file named imageshow.html: Compile Java class via the command javac ShowImage.java, then invoke the imageshow.html file via appletviewer If you specified a correct image in the same directory with these files, it will be shown on the window APPLET ANIMATIONS Since applets are common in games, images should be moved Example: Create the file AppletAnimationExample.java Add this code: import java.applet.*; import java.awt.*; public class AppletAnimationExample extends Applet { Image img; public void init() { img =getImage(getDocumentBase(),"car.gif"); } public void paint(Graphics gc) { for(int ix=0;ix

Ngày đăng: 05/03/2019, 08:49

Mục lục

  • Introduction

  • Chapter 1: Getting Started with java

  • Chapter 2: Java Variables

  • Chapter 3: Java operators

  • Chapter 4: Java arrays

  • Chapter 5: Decision making

  • Chapter 6: looping

  • Chapter 7: Inheritance

  • Chapter 8: overriding

  • Chapter 9: Polymorphism

  • Chapter 10: Abstraction

  • Chapter 11- encapsulation

  • Chapter 12- Interfaces

  • Chapter 13- Packages

  • Chapter 14- Command-Line Arguments

  • Chapter 15- Recursion

  • Chapter 16- Method Calls

  • Chapter 17- Java Applets

  • Conclusion

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan