A constructor : Constructor

Similar documents
Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

INPUT AND OUTPUT STREAMS

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Chapter 2: Elements of Java

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Building a Multi-Threaded Web Server

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Part I. Multiple Choice Questions (2 points each):

Java Crash Course Part I

Introduction to Programming

Introduction to Java

Stream Classes and File I/O

Using Files as Input/Output in Java 5.0 Applications

public static void main(string[] args) { System.out.println("hello, world"); } }

Object-Oriented Programming in Java

Event-Driven Programming

AP Computer Science Static Methods, Strings, User Input

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Install Java Development Kit (JDK) 1.8

AP Computer Science Java Subset

Java CPD (I) Frans Coenen Department of Computer Science

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

C++ INTERVIEW QUESTIONS

Files and input/output streams

Creating a Simple, Multithreaded Chat System with Java

LAB4 Making Classes and Objects

Building Java Programs

Programming Languages CIS 443

Introduction to Object-Oriented Programming

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Introduction to Java. CS 3: Computer Programming in Java

Java Interview Questions and Answers

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Building Java Programs

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

WRITING DATA TO A BINARY FILE

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Comp 248 Introduction to Programming

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

History OOP languages Year Language 1967 Simula Smalltalk

Basics of Java Programming Input and the Scanner class

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

More on Objects and Classes

java Features Version April 19, 2013 by Thorsten Kracht

13 File Output and Input

Description of Class Mutation Mutation Operators for Java

Lesson: All About Sockets

Building Java Programs

B.Sc (Honours) - Software Development

6.1. Example: A Tip Calculator 6-1

Sample CSE8A midterm Multiple Choice (circle one)

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS

CS 1302 Ch 19, Binary I/O

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

JAVA ARRAY EXAMPLE PDF

CS506 Web Design and Development Solved Online Quiz No. 01

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Lecture 5: Java Fundamentals III

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Reading Input From A File

Moving from CS 61A Scheme to CS 61B Java

JAVA - FILES AND I/O

Today s Outline. Computer Communications. Java Communications uses Streams. Wrapping Streams. Stream Conventions 2/13/2016 CSE 132

Simple Java I/O. Streams

J a v a Quiz (Unit 3, Test 0 Practice)

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Word Count Code using MR2 Classes and API

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Chapter 2 Elementary Programming

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Chapter 3. Input and output. 3.1 The System class

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Chapter 2 Introduction to Java programming

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

CSE 8B Midterm Fall 2015

AP Computer Science Java Mr. Clausen Program 9A, 9B

D06 PROGRAMMING with JAVA

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

CS 111 Classes I 1. Software Organization View to this point:

Fundamentals of Java Programming

Input / Output Framework java.io Framework

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Java (12 Weeks) Introduction to Java Programming Language

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Inheritance, overloading and overriding

Programmierpraktikum

Transcription:

Java Constructors

Constructor A constructor : is used in the creation of an object. is a special method with no return type. must have the same name as the class it is in. is used to initialize the object. if not defined will initialize all instance variables to default value.

Constructor public class Class2 { public class Class1 { int x; int y; public static void main(string[] args) { Class2 ob = new Class2(); public Class2() { x = 10; y = 11; System.out.println("Value of x when constructor is called: " + ob.x); System.out.println("Value of y when constructor is called: " + ob.y);

This keyword this is a keyword used to reference the current object within an instance method or a constructor.

Constructor Overloading public class Class2 { int x, y; public Class2() { x = 10; y = 11; public Class2(int z) { x = z; y = z; public class Class1 { public static void main(string[] args) { Class2 ob = new Class2(); Class2 ob = new Class2(5);

Java Review (Packages)

What is Package? A Java package is a mechanism for organizing Java classes into namespaces Programmers use packages to organize classes belonging to the same category Classes in the same package can access each other's package-access members Java Programming: OOP 7

Advantage of a Package Programmers can easily determine that these classes are related Programmers know where to find files of similar types The names won't conflict You can have define access of the types within the package Java Programming: OOP 8

Naming Convention of a Package Package names are written in all lower case Companies use their reversed Internet domain name to begin their package names for example, com.example.mypackage for a package named mypackage created by a programmer at example.com Java Programming: OOP 9

Naming Convention of a Package If the domain name contains i. a hyphen or a special character ii. if the package name begins with a digit, illegal character reserved Java keyword such as "int In this event, the suggested convention is to add an underscore Legalizing Package Names Domain Name Package Name Prefix hyphenated-name.example.org org.example.hyphenated_name example.int int_.example 123name.example.com com.example._123name Java Programming: OOP 10

Package package com.myproject; Project src com classes myproject com MyClass.java myproject MyClass.class Java Programming: OOP 11

Inheritance

Inheritance The child classes inherits all the attributes of the parent class They also have their distinctive attributes Java Programming: OOP 13

Class Animal { // Type : Not Human // Lives : On Land // Gives Birth : Class Aquatic extends Animal { // Lives : In Water // Gives Birth : Java Programming: OOP 14

package com.edureka.animal; public class Animal { public String Type = "Not Human"; public String Lives = "In Water"; public void fun(){ package com.edureka.animal; public class Aquatic extends Animal{ String Lives = "In Water"; public void fun(){ System.out.println("defined here"); Java Programming: OOP 15

package com.edureka.animal; public class Main { public static void main(string[] args) { Aquatic ob = new Aquatic(); System.out.println(ob.Type); System.out.println(ob.Lives); ob.fun(); Java Programming: OOP 16

Super keyword super is a keyword used to refer to the variable or method or constructor of the immediate parent class.

Method Overloading

Method Overloading The overloaded function must differ either by the number of arguments or operands or data types. The same function name is used for various instances of function call

Method Overloading package com.edureka.animal; public class Animal { public void fun(){ System.out.println("Without Parameters"); package com.edureka.animal; public class Aquatic extends Animal{ public void fun(int num){ System.out.println ("The number passed is : " + num);

Method Overloading package com.edureka.animal; public class Main { public static void main(string[] args) { Aquatic ob = new Aquatic(); //without Parameters, defined in class Animal ob.fun(); //with Parameter, overloaded function in class Aquatic ob.fun(10);

Method Overriding

Method Overriding The implementation in the subclass overrides (replaces) the implementation in the superclass It is done by providing a method that has same 1. name, same 2. parameters, and same 3. return type as the method in the parent class

Method Overriding package com.edureka.animal; public class Animal { int fun(int a, int b){ int c = a + b; return c; package com.edureka.animal; public class Aquatic extends Animal{ int fun(int a, int b){ System.out.println u int c = a * b; return c; y super lass: " + super.fun(a, b));

Method Overriding package com.edureka.animal; public class Main { public static void main(string[] args) { Aquatic ob = new Aquatic(); System.out.println Produ t y derived lass : + o.fu, ;

Abstract Class

Abstract Class abstract class Shape class Rectangle extends Shape class Circle extends Shape class Triangle extends Shape

What is an Abstract Class? A class that is declared abstract» Ex - abstract class Demo» It may or may not use abstract methods

What is an Abstract Method? A method that is declared abstract A method that is declared without an implementation Ex - abstract void add(int x, int y); Java Programming: OOP

Example public abstract class Shape { //Abstract method i.e no implementation abstract void Area(); public class Rectangle extends Shape { public class Circle extends Shape { @Override void Area() { Double area = length * width; @Override void Area() { Double area = 3.14 * radius*radius;

Interface

Interface Interfaces are declared using the interface keyword Interface can contain :» method signature (no implementation)» constant declarations (variable declarations that are declared to be both static and final)

Interface A class that implements an interface must implement all of the methods described in the interface Interface implements Class abstract Method1(); abstract Method1(); abstract Method2(); abstract Method2(); abstract Method3(); abstract Method3();

How to create an Interface public interface Demo_interface { int add(int value1, int value2); void print(int sum); Demo_interface int add(int value1, intvalue2); void print(int sum);

How to implement the Interface public class Class1 implements Demo_interface { @Override public int add(int value1, int value2) { int sum = value1 + value2; return sum; @Override public void print(int sum) { System.out.println("The sum is : " + sum); Demo_interface int add(int value1, intvalue2); void print(int sum); Class1 public int add(int value1, int value2); public void print(int sum);

Using it in the main class public class Execute { public static void main(string[] args) { Execute Class1 ob = new Class1(); ob int sum = ob.add(10, 10); ob.print(sum); sum = ob.add(10,10); ob.print(sum);

Input And Output

Input and Output Input is the data given by the user to the program. Output is the data what we receive from the program in the form of result. Stream represents flow of data i.e. sequence of data. To give input we use InputStream and to receive output we use OutputStream.

How input is read from Keyboard? connected to System.in It represents keyboard. To read data from keyboard it should be connected to InputStreamReader InputStream Reader send data to It reads data from keyboard and send that data to BufferedReader. BufferedReader It reads data from InputStreamReader and stores data in buffer. It has got methods so that data can be easily accessed.

Reading Input from console Input can be given either from file or keyboard. Input can be read from console in 3 ways. BufferedReader StringTokenizer Scanner

BufferedReader BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); int age = bufferedreader.read(); String name = bufferedreader.readline(); Methods int read() String readline()

StringTokenizer It can be used to accept multiple inputs from console in single line where as BufferedReader accepts only one input from a line. It uses delimiter(space, comma) to make the input into tokens.

StringTokenizer BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); String input = bufferedreader.readline(); StringTokenizer tokenizer = new StringTokenizer(input,, ); String name = tokenizer.nexttoken(); delimiter int age=integer.parseint(tokenizer.nexttoken());

Scanner It accepts multiple inputs from file or keyboard and divides into tokens. It has methods to different types of input( int, float, string, long, double, byte) where tokenizer does not have. Scanner scanner = new Scanner(System.in); int rollno = scanner.nextint(); String name = scanner.next();

Writing output to console The output can be written to console in 2 ways: print(string)system.out.print( hello ); write(int)int input= i ; System.out.write(input); System.out.write( / );

Q& A..?

Thanks..!