Java Programming PDF
Document Details
Uploaded by Deleted User
Tags
Summary
These notes provide an outline of Java programming topics, including introduction to Java and OOP, writing your first program, variables and constants, and input and output statements. The document also covers the program development process, and discusses the benefits of using Java. This document can, therefore, be used as a tutorial or learning guide for Java programming.
Full Transcript
Topic Outline Introduction to Java and OOP Writing your first program Variables and Constants Input and Output Statements Program development Process 1. Planning – whats/ identify all the requirements input, process, output, given 2. Designing – hows Algorithm , flowchart 3. codi...
Topic Outline Introduction to Java and OOP Writing your first program Variables and Constants Input and Output Statements Program development Process 1. Planning – whats/ identify all the requirements input, process, output, given 2. Designing – hows Algorithm , flowchart 3. coding- writing program using Java language Courtship Rituals in the 19th Century England was that women of a certain class were expected to marry men within their social and financial means. Lifestyle-Check. Qualities were compared and contrasted, and similarities were highlighted while differences were discarded. Class and Objects What is Java? Started 1991 An Object-Oriented Programming developed by a group of engineers “Green Team” led by James Gossling from Sun Microsystems Netscape Navigator in 1995 “Write Once Run Anywhere” principle Acquisition of Sun Microsystems by Oracle Corporation 2009 – 2010,Oracle Corporation regarded themselves as the steward of Java Technology What is Java? Was once named Oak Green Java- they were consuming a lot of Java coffee during the course of their development What is Object-Oriented Programming? A way of writing programs using objects. An object is a data structure in memory that has attributes and methods. A class defines the common variables An object is an instance of a class The attributes of an object are the variables The methods of an object are the functions or procedures.an Benefits of Java Java is used in networking The Java language has many classes to facilitate Internet communications. Java classes can be loaded in most browsers to facilitate secure transactions over the Internet. Benefits of Java Java is dynamic Java makes it possible to reuse previously written programs to lessen the workload of a programmer. Benefits of Java Java is reliable Java is reliable because it excludes some components that are error-prone. It also provides a way for programmers to anticipate and handle errors in their program. Benefits of Java Java is simple The Java was designed to make OOP easier. It has the “Look and Feel of C++”. Once you understand how components in a program are treated as objects, you will find Java fairly simple. Benefits of Java Java is Secure Java ensures that a program can go only where it was designed to go. It eliminates the possibility of altering system data unintentionally. Benefits of Java Java is Architecture Neutral and portable Java codes have the filename extension.java. When compiled, a class file with.class extension is created. This file can then run on any machine which has the Java Virtual Machine, thus, making it portable. “Write Once Run Anywhere” principle Benefits of Java Java is free Java can be downloaded from the Internet for FREE. Just visit http://java.sun.com/. What you will need You will need the Java software : https://drive.google.com/file/d/1qCQ6TBxpknaeb gV-eXZx6GMJdpvJGzym/view?usp=sharing BlueJ (IDE) - Integrated Development Environment used to compile and run the Java applications Using classes and objects Before you can create an object you need to create a class. A class is the code for an object and an object is the instance of a class in memory. When you create an object from a class it is called instantiating the object. Using classes and objects ▪ Class Student ▪ Object SeniorStudent Name Yzabelle Age 15 Birthday May 14, 1999 YearLevel IV Section Faith Adviser Ms. Berlin Comments Written in a program to explain the code. Ignored by the compiler Multi-line comments must be written between Single-line comments must be written after // public class Hello // The Hello class { public static void main(String[] args) {System.out.println("Hello World!"); System.out.println(“Welcome to Java Programming!"); } } Writing your first Java program public class Hello { } This creates a class called Hello The First Letter of the Class Name should be written using Uppercase Letter Should be declared as public so that other classes may be able to access it. The main part of the program must be between the curly braces after the class declaration. Writing your first Java program public class Hello { public static void main(String[] args) { } } static – unique; resides in the memory until the program terminates void – the return value; but void means nothing which means that there will be no returned value main - the name of the method. (String[ ] args) - used for command line parameters; declares an array of type string Curly braces - used again to group the contents of main method or function Writing your first Java program public class Hello_Dinglasan { public static void main(String[ ] args) { System.out.println("Hello World!"); System.out.println(“Welcome to Java Programming!"); } } @Name (Cristy Dinglasan) * @Section (St. Lawrence) Variables, Constants, Arithmetic Operators Variables are held in specific locations in the computer's memory. Each variable is given a unique name. Types of variables: Numeric – integer (whole)and real (with decimal point) Character – character and string Boolean- true /false Types of variables Data type Type Values byte Numeric - Integer -128 to 127 short Numeric - Integer -32 768 to 32 767 int Numeric - Integer -2 147 483 648 to (10 digits) 372 036 854 775 808 to 9 long (L) Numeric - Integer 223 372 036 854 775 807 (19 digits) Float (F) Numeric - Real -3.4 * 1038 to 3.4 * 1038 double Numeric - Real -1.7 * 10308 to 1.7 * 10308 char Character one unicode characters String Character All unicode characters boolean Boolean True or False Declaring variables Format: datatype variablename; datatype variablename = value; A variable name can only include any letter of the alphabet, numbers and underscores. can start with a $ but may not start with a number Spaces are not allowed in variable names. usually starts with a lower case letter and every first letter of words that make up the name must be upper case Example: int myVariable; char ans; String studentName; float averageGrade, grade; Declaring constant Format: final vartype variablename = value; Variables with values that can't be changed. The value is assigned to a constant when it is declared. Use the word final in front of a normal variable declaration to make it a constant. Example: final double PI = 3.14; final String greet = “Hello”; Arithmetic Operators Operator Operation + Addition - Subtraction * Multiplication / Division % Remainder of Division Assignment Statement Syntax: VariableName = expression ; Example: ave = (a+b+c+d)/4; Discount = price *.50; X= a*b+c/4-2 + (a% 3) + 100*4; Output Statements To display information on screen, we can use System.out.print(stringExpression) - prints stringExpression and sets the cursor position right after the last character printed System.out.println(stringExpression) – prints stringExpression and sets the cursor position at the next line System.out.println()-prints a blank line Examples: System.out.print(“JAVA”); System.out.println(“JAVA PROGRAMMING”); System.out.println(); Input Statements Data can be accepted from users through the use of the ff: import java.io.*; public class InputData { public static void main(String[] args) throws Exception { BufferedReader data=new BufferedReader(new InputStreamReader(System.in)); String name; System.out.print(“Enter your name: “); name = data.readLine(); System.out.println(“Hi, “ + name + “!“); } } Input Statements However, only strings are accepted as input. Therefore, data should be converted to required data types. import java.io.*; public class InputDataInt { public static void main(String[] args) throws Exception { BufferedReader data=new BufferedReader(new InputStreamReader(System.in)); int age; System.out.print(“Enter your age: “); age = Integer.parseInt(data.readLine()); } } ASCII What are ASCII values? Why are they needed? We are all aware of the fact that all computers understand is 0 & 1 – and everything else is stored as patterns containing 0’s and 1’s. Some of us also know that numbers like 25, 12, etc can be represented as patterns of 0’s & 1’s. The computer interprets 25 as 11001 and 12 as 1100. But how does it interpret a sentence like “My name is K” or a word like “name” or a letter like “K”? Also, how does it know the difference between ‘k’ & ‘K’? ASCII This is where ASCII values come in. Each character (including a space) has a certain corresponding number associated to it. And there are different numbers assigned to the lower and upper case of the same alphabet. For instance, the letter ‘a’ corresponds to the number 97 while ‘A’ corresponds to the number 65. An ASCII table maps the characters to the numbers. Apart from the equivalent decimal representation, it also gives the hexadecimal and octal representations of the character. This is what the ASCII table