java: basics, user input, data type, constructor

20
BASIC STRUCTURE – Fundamentals to Understand Class Attributes Primitive data types in Java Arrays – How to declare them Methods in Java Main method - It provides the control of program flow. Simple methods – We need object to access such methods Static methods - We DON’T need object to access such methods Constructors Object – An instance of a class How to instantiate? Calling of method/attribute. //Creating an object of Student class with name as “ram” Student ram = new Student(); //Accessing the method/attribute of ram ram.course=“M.C.A”; System.out.println(“New course of ram is: ”+ ram.course)

Transcript of java: basics, user input, data type, constructor

Page 1: java:  basics, user input, data type, constructor

BASIC STRUCTURE – Fundamentals to Understand

ClassAttributes

Primitive data types in JavaArrays – How to declare them

Methods in JavaMain method - It provides the control of program flow. Simple methods – We need object to access such methodsStatic methods - We DON’T need object to access such methodsConstructors

Object – An instance of a class

How to instantiate? Calling of method/attribute.

//Creating an object of Student class with name as “ram”Student ram = new Student();

//Accessing the method/attribute of ramram.course=“M.C.A”;System.out.println(“New course of ram is: ”+ ram.course)

Page 2: java:  basics, user input, data type, constructor

BASIC STRUCTURE – Fundamentals to Understand

Single line comments: These comments start with //

e.g.: // this is comment line

Multi line comments: These comments start with /* and end with */

e.g.: /* this is comment line*/

Since Java is purely an Object Oriented Programming language –

• We must have at least one class.

• We must create an object of a class, to access SIMPLE methods/attributes of

a class.

Static methods are the methods, which can be called and executed without

creating objects. e.g. main() method.

Page 3: java:  basics, user input, data type, constructor

BASIC STRUCTURE – Flow of Execution

Page 4: java:  basics, user input, data type, constructor

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

PROGRAM EXECUTION – STEP I

Enter main method.

The main method provides the control of program flow. The Java interpreter

executes the application by invoking the main method.

Page 5: java:  basics, user input, data type, constructor

//This program prints Welcome to Java!

public class Welcome {

public static void main(String[] args) {

System.out.println("Welcome to Java!");

}

}

PROGRAM EXECUTION – STEP II

Execute Statement 1

Page 6: java:  basics, user input, data type, constructor

ESCAPE SEQUENCE

Java supports all escape sequence which is supported by C/ C++.

\t Insert a tab in the text at this point.

\b Insert a backspace in the text at this point.

\n Insert a newline in the text at this point.

\r Insert a carriage return in the text at this point.

\f Insert a form feed in the text at this point.

\' Insert a single quote character in the text at this point.

\" Insert a double quote character in the text at this point.

\\ Insert a backslash character in the text at this point.

Page 7: java:  basics, user input, data type, constructor

USER INPUT

There are three ways to take input from a user:

1. User input as Command Line Argument

2. User input using Scanner class object

3. User input using BufferedReader class object

Page 8: java:  basics, user input, data type, constructor

USER INPUT - Command Line Argument - Example

class Student{String course;String address;Integer semester;

}

import java.lang.*;

class Test{public static void main(String[] args){

Student ramesh = new Student();

//Set the values to attributesramesh.course="B.Tech";ramesh.address=“Delhi";

}}

Page 9: java:  basics, user input, data type, constructor

USER INPUT - Command Line Argument - Example

class Student{String course;String address;Integer semester;

}

import java.lang.*;

class Test{public static void main(String[] args){

Student ramesh = new Student();

//Set the values to attributesramesh.course=args[0];ramesh.address=args[1];

}}

javac Test.java

java Test B.Tech Delhi

Page 10: java:  basics, user input, data type, constructor

USER INPUT - Command Line Argument - Example

class Student{String course;String address;Integer semester;

}

import java.lang.*;

class Test{public static void main(String[] args){

Student ramesh = new Student();

//Set the values to attributesramesh.course=args[0];ramesh.address=args[1];

int i = Integer.parseInt(args[2]);ramesh.semester=i

}}

javac Test.java

java Test B.Tech Delhi 5

Page 11: java:  basics, user input, data type, constructor

USER INPUT - using Scanner class object

import java.util.Scanner;class Test{

public static void main(String[] args){Student ramesh = new Student();

Scanner ob=new Scanner(System.in);System.out.println("Please enter the course");String courseTemp=ob.nextLine();

System.out.println("Please enter sem no. ");int semesterTemp=ob.nextInt();

//Set the values to attributesramesh.course=courseTemp;ramesh.semester=semesterTemp;

}}

For more: http://www.tutorialspoint.com/java/util/java_util_scanner.htm

Page 12: java:  basics, user input, data type, constructor

USER INPUT - using BufferedReader class object

import java.io.BufferedReader;import java.io.Exception;import java.io.InputStreamReader;

public class Test{ public static void main(String[] args) throws Exception {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter String"); String s = br.readLine();

System.out.print("Enter Integer:"); try{ int i = Integer.parseInt(br.readLine()); }catch(Exception e){ System.err.println("Invalid Format!");

System.out.println(e); } }}

For more: http://stackoverflow.com/questions/2231369/scanner-vs-bufferedreader

Page 13: java:  basics, user input, data type, constructor

BASIC STRUCTURE – Fundamentals to Understand

ClassAttributes

Primitive data types in JavaArrays – How to declare them

Methods in JavaMain method - It provides the control of program flow. Simple methods – We need object to access such methodsStatic methods - We DON’T need object to access such methodsConstructors

Object – An instance of a class

How to instantiate? Calling of method/attribute.

Page 14: java:  basics, user input, data type, constructor

CONSTRUCTOR

A class contains constructors that are invoked to create objects.

There are basically two rules defined for the constructor:• Constructor name must be same as its class name• Constructor must have no explicit return type

Class Bicycle{

int gear, cadence, speed;

public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed;}}

To create a new Bicycle object called myBike, a constructor is called by the new operator:

Bicycle myBike = new Bicycle(30, 0, 8);

new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.

Page 15: java:  basics, user input, data type, constructor

CONSTRUCTOR

Although Bicycle only has one constructor, it could have others, including a no-argument constructor:

//Inside Bicycle classpublic Bicycle() {

gear = 1; cadence = 10; speed = 0;

}

//Outside Bicycyle class – e.g. Inside main methodBicycle yourBike = new Bicycle();

above statement invokes the no-argument constructor to create a new Bicycle object called yourBike.

• Both (or more) constructors could have been declared in same class - Bicycle.• Constructors are differentiated by Java Interpreter based on the SIGNATURE.

• SIGNATURE = No. of arguments + Data type of arguments + Order of arguments

Page 16: java:  basics, user input, data type, constructor

CONSTRUCTOR

class Student{String course;String address;int semester;

//Default Constructor – NO ARGUMENTpublic Student(){

//Creates space in memory for the object and initializes its fields to defualt.}

//Parameterized Constructor – ALL ARGUMENTSpublic Student(String s1, String s2, int i1){

this.course=s1;this.course=s2;this.semester=i1;

}

//Parameterized Constructor – FEW ARGUMENTSpublic Student(String s1){

this.course=s1;}

}

Rule: If there is no constructor in a class, compiler automatically creates a default constructor.

Page 17: java:  basics, user input, data type, constructor

CONSTRUCTOR

import java.lang.*;

class Test{public static void main(String[] args){

//Get the values in to TEMPORARY variablesString temp1 = args[0];String temp2 = args[1];int temp3 = args[2];

Student ramesh = new Student(temp1,temp2,temp3);

System.out.println(ramesh.course);System.out.println(ramesh.address);System.out.println(ramesh.semester);}

}

javac Test.java

java Test B.Tech Delhi 5

Page 18: java:  basics, user input, data type, constructor

PRIMITIVE DATA TYPE in JAVA

Type Contains Default Size Range

byte Signed integer 0 8 bits -128 to 127

short Signed integer 0 16 bits -32768 to 32767

int Signed integer 0 32 bits -2147483648 to 2147483647

float IEEE 754 floating point 0.0f 32 bits ±1.4E-45 to ±3.4028235E+38

long Signed integer 0L 64 bits -9223372036854775808 to 9223372036854775807

double IEEE 754 floating point 0.0d 64 bits ±4.9E-324 to ±1.7976931348623157E+308

boolean true or false FALSE 1 bit NA

char Unicode character '\u0000' 16 bits \u0000 to \uFFFF

Page 19: java:  basics, user input, data type, constructor

FEATURES of JAVA

• Simple: Learning and practicing java is easy because of resemblance with C and C++.

• Object Oriented Programming Language: Unlike C++, Java is purely OOP.

• Distributed: Java is designed for use on network; it has an extensive library which works

in agreement with TCP/IP.

• Secure: Java is designed for use on Internet. Java enables the construction of virus-free,

tamper free systems.

• Robust (Strong enough to withstand intellectual challenge): Java programs will not

crash because of its exception handling and its memory management features.

• Interpreted: Java programs are compiled to generate the byte code. This byte code can

be downloaded and interpreted by the interpreter. .class file will have byte code

instructions and JVM which contains an interpreter will execute the byte code.

Page 20: java:  basics, user input, data type, constructor

FEATURES of JAVA

• Portable: Java does not have implementation dependent aspects and it yields or gives

same result on any machine.

• Architectural Neutral Language: Java byte code is not machine dependent, it can run on

any machine with any processor and with any OS.

• High Performance: Along with interpreter there will be JIT (Just In Time) compiler which

enhances the speed of execution.

• Multithreaded: Executing different parts of program simultaneously is called

multithreading. This is an essential feature to design server side programs.

• Dynamic: We can develop programs in Java which dynamically change on Internet (e.g.:

Applets).

P l e a s e G o o g l e / R e a d m o r e f o r R E D h i g h l i g h t e d c o l o r k e y w o r d s

F O R M O R E I N F O / O F F I C I A L L I N K - J A V Ah tt p : / / d o c s . o r a c l e . c o m / j a v a s e / t u t o r i a l / j a v a / j a v a O O / i n d e x . h t m l