Java Basics M Taimoor Khan

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Programming Languages and Paradigms
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Understanding class definitions Looking inside classes.
Introduction to Methods
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 2 How to Compile and Execute a Simple Program.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Classes CS 21a: Introduction to Computing I First Semester,
Utilities (Part 2) Implementing static features 1.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
1  lecture slides online
OOP with Java, David J. Barnes/Eric Jul Defining Classes1 Object State and Complexity Objects maintain a state. State is represented by a set of attributes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
By Mr. Muhammad Pervez Akhtar
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Information and Computer Sciences University of Hawaii, Manoa
Objects as a programming concept
Java Programming: Guided Learning with Early Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Programming Language Concepts (CIS 635)
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Java Programming with BlueJ
Chapter 1: Computer Systems
Defining Classes and Methods
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Recap Week 2 and 3.
Anatomy of a Java Program
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Chap 2. Identifiers, Keywords, and Types
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Corresponds with Chapter 5
Presentation transcript:

Java Basics M Taimoor Khan

Content Java language syntax Structure of Java programs Creating a Java program Documentation, why it is important Reading and writing from the console 2

Important Java is cAsE sensitive A semicolon “;” is used to indicate the end of a statement ( VB is different) Braces, “{“ & “}” are often used to create blocks of code 3

All code is written in a class A program will have at least one class and may use many other classes Classes are written by programmers Many classes are supplied with Java The program will start running with a class that contains a special method public static void main (String args[]) 4

Hello World 5 Execution will start with this statement

Comments 6 Comments may appear in blocks /** indicates start of comment */ indicates end of comment All methods should have comments that describe what they do Comments may also appear at the end of a line of code These comments use two slashes // to indicate a start of comment.

Defining a class 7 A class is defined by the key word “class” A class has a name. By convention starts with a capital letter and each new word is also capitalised (Look up the syntax rules) The first brace indicates where the class begins The last “matching” brace indicates where the class ends

Methods 8 Methods have an opening brace “{“ A class will normally contain many methods. These are the same as functions and procedures. This class has one method Methods start with a method signature. A closing brace “}” A number of statements each terminated by a “;” semicolon

import java.awt.*; /** * A square that can be manipulated and that draws itself on a canvas. * Michael Kolling and David J. Barnes */ public class Square { private int size; private int xPosition; private int yPosition; private String color; private boolean isVisible; /** * Create a new square at default position with default color. */ public Square() { size = 30; xPosition = 60; yPosition = 50; color = "red"; isVisible = false; } 9 Knows about other classes Have a class header that describes the class Have instance variables that hold data for this class Have a constructor Have many methods, not shown

Structure within a class Attributes, (what the class knows about) o Defined as variables Data used within the class only (use private) Can be used externally (provide accessor or make public) Operations, (the things the class does) Defined in methods (Functions & Procedures) o Classified as accessor methods or mutator methods Constructors o Used when we create objects based on a class 10 Should be cohesive

A Program works with many classes 11 A class that has a public static void main method The place where the program starts Other classes written by you classes written by other programmers classes that come with Java Breaking a program into classes helps us understand our programs better. “Like behaviour” can be grouped into classes

Variables Most programs manipulate data. Data must be stored while the program is running We use variables to store the data Look at the tutorial o ariables.html ariables.html 12

String Class String str = "abc"; How do I find out more about String? Look at the API o ml ml Do a java tutorial o “Strings are constant; their values cannot be changed after they are created”

Declaring variables and assigning values Local variables – (located inside methods) boolean result = true; char capitalC = 'C'; byte b = 100; short s = 10000; int i = ; Instance variables - (inside a class, but not in a method) public char capitalC = 'C'; private byte b = 100; Parameters public void myProcedure(int i,String r) 14 Note use of public or private – Very Important. We will talk about scope often. This method has two parameters

Variables – Different types Instance Variables o Available when an object is created Class Variables o Declared with a static modifier Local Variables o Declared within methods Parameters o Declared as part of method signature 15 These are different from instance variables Arguments are sent to methods

Arrays 16 Source: Sun Java Tutorials

Functions & Parameters 17

for Loop 18

while Loop 19

do Loop 20

if 21

if …else 22

switch 23

if ….elseif …..else 24

Reading from the Console Independent research o o o java.util.scanner-3.html java.util.scanner-3.html o

The “.”, what does it mean? "the dot " connects classes and objects to members. o when you are connecting an object reference variable to a method. o when you are connecting a class name to one of its static fields. “An example of this is the dot between "System" and "out" in the statements we use to print stuff to the console window. System is the name of a class included in every Java implementation. It has an object reference variable that points to a PrintStream object for the console. So, "System.out.println( "text") invokes the println() method of the System.out object.” Source : Are there any other uses of the “.” operator?

Pass by value or by reference? on on o Pass-by-value “The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it) o Pass-by-reference “The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.” Java is strictly pass-by-value 27

Scope What does scope mean? o Restricting what code has access to variables, methods and classes Why do we care about it? o Poor scope means our programs may become corrupted or vulnerable May not run as expected How do we set scope? o Use an access modifier public, private o By being careful about where we declare variables and methods how we declare variables and methods More on this later 28

How can you document your code? Class Header Class Name Variables names Method names Method headers Comments in code ( Block and Line ) Comment to Javadoc standard Run Javadoc software 29

Why is documentation vital? Maintenance of code is very costly o Improve or modify o Fix bugs What is obvious now will not be in one months time Makes the code easier to reuse Makes the code easier to understand Gets you better marks 30

References ndex.html ndex.html s.html s.html ypes.html ypes.html Escape sequences o Java_Escape_Sequences.htm Java_Escape_Sequences.htm Code Conventions o Java 6 API o