Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Size: px
Start display at page:

Download "Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani"

Transcription

1 Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces OOC 4 th Sem, B Div Prof. Mouna M. Naravani

2 Introducing Classes

3 A class defines a new data type (User defined data type). This new type can be used to create objects of that type. A class is a template for an object. An object is an instance of a class. Objects and instance are used interchangeably.

4 class is a keyword User defined valid class name instance variable, because each instance(object) of a class has its own copy of a data Members of class Methods, contains code which determines how the variables can be used.

5 Java classes do not need to have main() method. You only specify one if that class is the starting point for your program.

6 Access Specifiers/Modifiers Public - Members can be accessed by any other code. Private - Accessed only by other members of its class. Protected - Applies only when inheritance is involved. Default - When no access specifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package. That s why main() is always preceded by public specifier. It is called by code that is outside the program that is, by Java run time system.

7 Class Access Specifiers/Modifiers Public: If top level class within a package is declared as Public, then it is accessible both inside and outside of the package. Default: If no access modifier is specified in the declaration of the top level class, then it is accessible only within package level. It is not accessible in other packages or sub packages.

8 Access Modifier within class within package outside package by subclass only outside package Private Y N N N Default Y Y N N Protected Y Y Y N Public Y Y Y Y Ex: AccessTest.java

9 A Simple Class class Box { double width; double height; double depth; } A class defines a new data type, in this case it is called as Box. This name is used to create objects of type Box. Class declaration only creates a template; it does not create an actual object.

10 Declaring Objects Two step Process 1. Declare a variable of the class type. Ex: Box mybox; This variable does not define an object. Instead, it is simply a variable that can refer to an object. After this line executes, mybox contains the value null, which indicates that it does not yet point to an actual object. Any attempt to use mybox at this point will result in compile time error.

11 2. Having an actual, physical copy of the object and assign it to that variable. This is achieved using new operator. Ex: mybox = new Box(); The new operator dynamically allocates (i.e., allocates at run time) memory for an object and returns a reference to it. This reference is the address in memory of an object allocated by new. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated.

12 Combining both steps: Box mybox; mybox = new Box(); or Box mybox = new Box();

13 Coming back to Box class class Box { double width; double height; double depth; } class BoxDemo { public static void main(string args[]) { Box mybox = new Box(); double vol; // assign values to mybox's instance variables mybox.width = 10; mybox.height = 20; mybox.depth = 15; // compute volume of box vol = mybox.width * mybox.height * mybox.depth; System.out.println("Volume is " + vol); } } BoxDemo.java When this program is compiled, two.class files will be created, one for Box and one for BoxDemo. Each object has its own copy of instance variables. If there are two Box objects, each has its own copy of width, depth and height. Changes made to one object has no affect on the other object. BoxDemo2.java

14 Assigning Object Reference Variables Box b1 = new Box(); Box b2 = b1; What do you think about the above statement? You might think that b1 and b2 refer to separate and distinct objects. b1 b2

15 b1 and b2 will both refer to the same object. The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same object as does b1.

16 Thus, any changes made to object b2 will also affect object b1, since they are the same objects. Although, b1 and b2 both refer to the same object, they are not linked in any other way. Ex: Box b1 = new Box(); Box b2 = b1; b1 = null; b1 b2 Here, b1 has been set to null, but b2 still points to the original object.

17 Introducing Method General form of a method: return_type method_name(parameter_list) { // body of method } where, return_type: type of data returned by the method. It can be any valid data type, class or void. method_name: valid identifier parameter_list: sequence of type and identifier pairs separated by commas.

18 Adding a Method to the Box Class // This program includes a method inside the box class. class Box { double width; double height; double depth; // display volume of a box void volume() { System.out.print("Volume is "); System.out.println(width * height * depth); } } class BoxDemo3 { public static void main(string args[]) { Box mybox1 = new Box(); Box mybox2 = new Box(); // assign values to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; /* assign different values to mybox2's instance variables */ mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // display volume of first box mybox1.volume(); // display volume of second box mybox2.volume(); } }

19 When an instance variable is accessed by code that it not part of the class in which that instance variable is defined, it must be done through an object, by use of the dot operator. However, when an instance variable is accessed by code that is part of the same class as the instance variable, that variable can be referred to directly. The same thing applies to methods.

20 Returning a Value Two important things to understand about returning values: 1. The type of data returned by a method must be compatible with the return type specified by the method. 2. The variable receiving the value returned by a method must also be compatible with the return type specified for the method.

21 Adding a Method that takes Parameters int square() { return 10 * 10; } Parameter Use of such a method is very limited. int square( int i ) { return i * i; } int x, y; x = square(5); x = square(2); y = 2; x = square(y); Argument General purpose method that can compute the square of any integer value.

22 References Herbert Schildt, The Complete Reference, JAVA, 7 th ed

23

BoxDemo1.java jueves, 02 de diciembre de :31

BoxDemo1.java jueves, 02 de diciembre de :31 BoxDemo1.java jueves, 0 de diciembre de 0 13:31 1 package boxdemo1; 3 class Box { 4 double width; 5 double height; 6 double depth; 7 } 8 9 // This class declares an object of type Box. class BoxDemo1 {

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3 Class A class is a template that specifies the attributes and behavior of things or objects. A class is a blueprint or prototype from which objects are created. A class is the implementation of an abstract

More information

1SFWJFX FYDMVTJWF FYDFSQUT GSPN CSBOE OFX BOE GPSUIDPNJOH 0SBDMF 1SFTT +BWB +%, CPPLT

1SFWJFX FYDMVTJWF FYDFSQUT GSPN CSBOE OFX BOE GPSUIDPNJOH 0SBDMF 1SFTT +BWB +%, CPPLT TM 1SFWJFX FYDMVTJWF FYDFSQUT GSPN CSBOE OFX BOE GPSUIDPNJOH 0SBDMF 1SFTT +BWB +%, CPPLT 'FBUVSJOH BO JOUSPEVDUJPO CZ CFTUTFMMJOH QSPHSBNNJOH BVUIPS )FSC 4DIJMEU 8SJUUFO CZ MFBEJOH +BWB FYQFSUT 0SBDMF

More information

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Simple Java Programs OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani /* */ A First Simple Program This is a simple Java program. Call this file "Example.java". class Example { } // Your program begins

More information

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Friend Functions. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Friend Functions OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani It has been emphasized that the private members cannot be accessed from outside the class. That is, a non-member function cannot have

More information

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Static Members OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani Static Data Members Static data members hold global data that is common to all objects of the class. Ex: count of objects currently present,

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani The HTML APPLET Tag An applet viewer will execute each APPLET tag that it finds in a separate window, while web browsers

More information

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Constructors and Destructors OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani A constructor guarantees that an object created by the class will be initialized automatically. Ex: create an object integer

More information

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

Keyword this. Can be used by any object to refer to itself in any class method Typically used to Keyword this Can be used by any object to refer to itself in any class method Typically used to Avoid variable name collisions Pass the receiver as an argument Chain constructors Keyword this Keyword this

More information

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Data Types, Variables and Arrays OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani Identifiers in Java Identifiers are the names of variables, methods, classes, packages and interfaces. Identifiers must

More information

Java Lecture Note. Prepared By:

Java Lecture Note. Prepared By: Java Lecture Note Prepared By: Milan Vachhani Lecturer, MCA Department, B. H. Gardi College of Engineering and Technology, Rajkot M 9898626213 milan.vachhani@yahoo.com http://milanvachhani.wordpress.com

More information

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 4 Multi threaded Programming, Event Handling OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani Event Handling Any program that uses a graphical user interface, such as a Java application written

More information

JAVA. Lab-9 : Inheritance

JAVA. Lab-9 : Inheritance JAVA Prof. Navrati Saxena TA- Rochak Sachan Lab-9 : Inheritance Chapter Outline: 2 Inheritance Basic: Introduction Member Access and Inheritance Using super Creating a Multilevel Hierarchy Method Overriding

More information

ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI

ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI Venkatapuram(Village),Renigunta(Mandal),Tirupati,Chitoor District, Andhra Pradesh-517520. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING NAME

More information

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable? Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani The Applet Class Types of Applets (Abstract Window Toolkit) Offers richer and easy to use interface than AWT. An Applet

More information

2. Introducing Classes

2. Introducing Classes 1 2. Introducing Classes Class is a basis of OOP languages. It is a logical construct which defines shape and nature of an object. Entire Java is built upon classes. 2.1 Class Fundamentals Class can be

More information

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method

Unit 3 INFORMATION HIDING & REUSABILITY. -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method Unit 3 INFORMATION HIDING & REUSABILITY -Inheritance basics -Using super -Method Overriding -Constructor call -Dynamic method Inheritance Inheritance is one of the cornerstones of objectoriented programming

More information

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff and Arrays Comp Sci 1570 Introduction to C++ Outline and 1 2 Multi-dimensional and 3 4 5 Outline and 1 2 Multi-dimensional and 3 4 5 Array declaration and An array is a series of elements of the same type

More information

10 COMPUTER PROGRAMMING

10 COMPUTER PROGRAMMING 10 COMPUTER PROGRAMMING CLASS AND OBJECT CONTENTS GENERAL STRUCTURE OF THE CLASS DECLARATION OF THE CLASS OBJECT CREATION MEMBER VARIABLE, HOW TO ACCESS MEMBER VARIABLE CONSTRUCTOR KEYWORD METHOD 2 GENERAL

More information

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario The Story So Far... Classes as collections of fields and methods. Methods can access fields, and

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2017-18 Prof. Mouna M. Naravani The Applet Class Types of Applets (Abstract Window Toolkit) Offers richer and easy to use interface than AWT. An Applet

More information

Namespaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Namespaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Namespaces OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani using namespace std; We have been using the above statement in our programs. The using namespace statement specifies that the members defined

More information

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 5 The Applet Class, Swings. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 5 The Applet Class, Swings OOC 4 th Sem, B Div 2016-17 Prof. Mouna M. Naravani The layout manager helps lay out the components held by this container. When you set a layout to null, you tell the

More information

3.1 Class Declaration

3.1 Class Declaration Chapter 3 Classes and Objects OBJECTIVES To be able to declare classes To understand object references To understand the mechanism of parameter passing To be able to use static member and instance member

More information

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Module 4 Multi threaded Programming, Event Handling. OOC 4 th Sem, B Div Prof. Mouna M. Naravani Module 4 Multi threaded Programming, Event Handling OOC 4 th Sem, B Div 2017-18 Prof. Mouna M. Naravani Event Handling Complete Reference 7 th ed. Chapter No. 22 Event Handling Any program that uses a

More information

Methods. Bok, Jong Soon

Methods. Bok, Jong Soon Methods Bok, Jong Soon javaexpert@nate.com www.javaexpert.co.kr Methods Enable you to separate statements into code blocks. Can be called whenever appropriate. Can invoke each other. Can call themselves(recursion)

More information

PROGRAMMING LANGUAGE 2

PROGRAMMING LANGUAGE 2 31/10/2013 Ebtsam Abd elhakam 1 PROGRAMMING LANGUAGE 2 Java lecture (7) Inheritance 31/10/2013 Ebtsam Abd elhakam 2 Inheritance Inheritance is one of the cornerstones of object-oriented programming. It

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

1.00 Lecture 8. Using An Existing Class, cont.

1.00 Lecture 8. Using An Existing Class, cont. .00 Lecture 8 Classes, continued Reading for next time: Big Java: sections 7.9 Using An Existing Class, cont. From last time: is a Java class used by the BusTransfer class BusTransfer uses objects: First

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming Think Twice Code Once The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2005 Khaleel I. Shaheen Computer Programming, I Laboratory Manual Experiment #2

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7) Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants

More information

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

FAQ: Classes & Objects

FAQ: Classes & Objects Question 1: How do I define a class as a data type? Answer 1: Data types in Java can be simple data types such as integers and floating point numbers. Data types can also be complex, collecting many different

More information

Introduction to Java. Handout-1d. cs402 - Spring

Introduction to Java. Handout-1d. cs402 - Spring Introduction to Java Handout-1d cs402 - Spring 2003 1 Methods (i) Method is the OOP name for function Must be declared always within a class optaccessqualifier returntype methodname ( optargumentlist )

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 3

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 3 Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Exercise Session 3 Today s Exercise Session Assignment 2 Walkthrough the master solution (your solutions)

More information

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13 C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13 This chapter focuses on introducing the notion of classes in the C++ programming language. We describe how to create class and use an object of a

More information

Object Oriented Programming 2015/16. Final Exam June 28, 2016

Object Oriented Programming 2015/16. Final Exam June 28, 2016 Object Oriented Programming 2015/16 Final Exam June 28, 2016 Directions (read carefully): CLEARLY print your name and ID on every page. The exam contains 8 pages divided into 4 parts. Make sure you have

More information

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java 1 CS/ENGRD 2110 SPRING 2014 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 Java OO (Object Orientation) 2 Python and Matlab have objects and classes. Strong-typing nature of

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Give one example where you might wish to use a three dimensional array

Give one example where you might wish to use a three dimensional array CS 110: INTRODUCTION TO COMPUTER SCIENCE SAMPLE TEST 3 TIME ALLOWED: 60 MINUTES Student s Name: MAXIMUM MARK 100 NOTE: Unless otherwise stated, the questions are with reference to the Java Programming

More information

Chapter 12: How to Create and Use Classes

Chapter 12: How to Create and Use Classes CIS 260 C# Chapter 12: How to Create and Use Classes 1. An Introduction to Classes 1.1. How classes can be used to structure an application A class is a template to define objects with their properties

More information

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling Course Name: Advanced Java Lecture 5 Topics to be covered Exception Handling Exception HandlingHandlingIntroduction An exception is an abnormal condition that arises in a code sequence at run time A Java

More information

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 5 Names, Bindings, Type Checking, and Scopes Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Scope and Lifetime Type Checking Referencing Environments Named Constants Names Used for variables, subprograms

More information

CS 61B Discussion 5: Inheritance II Fall 2014

CS 61B Discussion 5: Inheritance II Fall 2014 CS 61B Discussion 5: Inheritance II Fall 2014 1 WeirdList Below is a partial solution to the WeirdList problem from homework 3 showing only the most important lines. Part A. Complete the implementation

More information

Preview from Notesale.co.uk Page 3 of 36

Preview from Notesale.co.uk Page 3 of 36 all people who know the language. Similarly, programming languages also have a vocabulary, which is referred to as the set of keywords of that language, and a grammar, which is referred to as the syntax.

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

C Programming for Engineers Functions

C Programming for Engineers Functions C Programming for Engineers Functions ICEN 360 Spring 2017 Prof. Dola Saha 1 Introduction Real world problems are larger, more complex Top down approach Modularize divide and control Easier to track smaller

More information

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012 CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012 Name: This exam consists of 6 problems on the following 7 pages. You may use your two-sided hand-written 8 ½ x 11 note sheet during the exam.

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). UNITII Classes Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT). It s a User Defined Data-type. The Data declared in a Class are called Data- Members

More information

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method Methods Objectives: 1. create a method with arguments 2. create a method with return value 3. use method arguments 4. use the return keyword 5. use the static keyword 6. write and invoke static methods

More information

5. PACKAGES AND INTERFACES

5. PACKAGES AND INTERFACES 5. PACKAGES AND INTERFACES JAVA PROGRAMMING(2350703) Packages: A is a group of classes and interfaces. Package can be categorized in two form: built-in such as java, lang, awt, javax, swing, net, io, util,

More information

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Introduction to Classes and Objects. David Greenstein Monta Vista High School Introduction to Classes and Objects David Greenstein Monta Vista High School Client Class A client class is one that constructs and uses objects of another class. B is a client of A public class A private

More information

Java and OOP. Part 5 More

Java and OOP. Part 5 More Java and OOP Part 5 More 1 More OOP More OOP concepts beyond the introduction: constants this clone and equals inheritance exceptions reflection interfaces 2 Constants final is used for classes which cannot

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

Objects and Iterators

Objects and Iterators Objects and Iterators Can We Have Data Structures With Generic Types? What s in a Bag? All our implementations of collections so far allowed for one data type for the entire collection To accommodate a

More information

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer Introduce Eclipse Create objects in Java Introduce variables as object references Aleksandar Stefanovski CSCI 053 Department of Computer Science The George Washington University Spring, 2010 Invoke methods

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program? Lexical Binding There are two ways a variable can be used in a program: As a declaration As a "reference" or use of the variable Scheme has two kinds of variable "declarations" -- the bindings of a let-expression

More information

Notes on Chapter Three

Notes on Chapter Three Notes on Chapter Three Methods 1. A Method is a named block of code that can be executed by using the method name. When the code in the method has completed it will return to the place it was called in

More information

Selected Questions from by Nageshwara Rao

Selected Questions from  by Nageshwara Rao Selected Questions from http://way2java.com by Nageshwara Rao Swaminathan J Amrita University swaminathanj@am.amrita.edu November 24, 2016 Swaminathan J (Amrita University) way2java.com (Nageshwara Rao)

More information

this keyword in java javatpoint

this keyword in java javatpoint this keyword in java javatpoint There can be a lot of usage of java this keyword. In java, this is a reference variable that refers to the current object. Usage of java this keyword Here is given the 6

More information

Session 04 - Object-Oriented Programming 1 Self-Assessment

Session 04 - Object-Oriented Programming 1 Self-Assessment UC3M Alberto Cortés Martín Systems Programming, 2014-2015 version: 2015-02-06 Session 04 - Object-Oriented Programming 1 Self-Assessment Exercise 1 Rectangles Part 1.A Write a class called Rectangle1 that

More information

Java Programming Tutorial 1

Java Programming Tutorial 1 Java Programming Tutorial 1 Every programming language has two defining characteristics: Syntax Semantics Programming Writing code with good style also provides the following benefits: It improves the

More information

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige CSC 308 2.0 System Development with Java Exception Handling Department of Statistics and Computer Science 1 2 Errors Errors can be categorized as several ways; Syntax Errors Logical Errors Runtime Errors

More information

JAVA PROGAMMING MATERIAL

JAVA PROGAMMING MATERIAL JAVA PROGAMMING MATERIAL (as per autonomous syllabus) Prepared by, P. Kiran, Assistant Professor, CSE Department, QISCET. :JAVA PROGRAMMING SYLLABUS: UNIT I: Introduction to OOP Need for Object Oriented

More information

Hierarchical abstractions & Concept of Inheritance:

Hierarchical abstractions & Concept of Inheritance: UNIT-II Inheritance Inheritance hierarchies- super and subclasses- member access rules- super keyword- preventing inheritance: final classes and methods- the object class and its methods Polymorphism dynamic

More information

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003 Arrays COMS W1007 Introduction to Computer Science Christopher Conway 10 June 2003 Arrays An array is a list of values. In Java, the components of an array can be of any type, basic or object. An array

More information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

Week 6: Review. Java is Case Sensitive

Week 6: Review. Java is Case Sensitive Week 6: Review Java Language Elements: special characters, reserved keywords, variables, operators & expressions, syntax, objects, scoping, Robot world 7 will be used on the midterm. Java is Case Sensitive

More information

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166 Lecture 13 & 14 Single Dimensional Arrays Dr. Martin O Connor CA166 www.computing.dcu.ie/~moconnor Table of Contents Declaring and Instantiating Arrays Accessing Array Elements Writing Methods that Process

More information

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Programming. Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 It is possible to declare as a friend A member function A global function A class All of the above What

More information

COMP 110/L Lecture 5. Kyle Dewey

COMP 110/L Lecture 5. Kyle Dewey COMP 110/L Lecture 5 Kyle Dewey Outlines Methods Defining methods Calling methods Methods Motivation Motivation Input Program Output -Start off with some high-level motivation -You write your program,

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

Nested Loops. A loop can be nested inside another loop.

Nested Loops. A loop can be nested inside another loop. Nested Loops A loop can be nested inside another loop. Nested loops consist of an outer loop and one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered, and started

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array Arrays Chapter 7 Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array as an instance variable Use an array not filled

More information

COMP 110/L Lecture 13. Kyle Dewey

COMP 110/L Lecture 13. Kyle Dewey COMP 110/L Lecture 13 Kyle Dewey Outline char, charat() Command-line arguments and arrays Array access Array length Array update Integer.parseInt char, charat() char Represents a single character char

More information

Fundamentals of Programming Data Types & Methods

Fundamentals of Programming Data Types & Methods Fundamentals of Programming Data Types & Methods By Budditha Hettige Overview Summary (Previous Lesson) Java Data types Default values Variables Input data from keyboard Display results Methods Operators

More information

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++ Preview Object Oriented Programming with C++ Class Members Inline Functions vs. Regular Functions Constructors & Destructors Initializing class Objects with Constructors C++ Programming Language C Programming

More information

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer STUDENT NAME: ID: The exam consists of five questions. There are a total of 10 points. You may use the back

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

Introduction to Computer Science I

Introduction to Computer Science I Introduction to Computer Science I Classes Janyl Jumadinova 5-7 March, 2018 Classes Most of our previous programs all just had a main() method in one file. 2/13 Classes Most of our previous programs all

More information

M.CS201 Programming language

M.CS201 Programming language Power Engineering School M.CS201 Programming language Lecture 4 Lecturer: Prof. Dr. T.Uranchimeg Agenda How a Function Works Function Prototype Structured Programming Local Variables Return value 2 Function

More information

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line A SIMPLE JAVA PROGRAM Class Declaration The Main Line INDEX The Line Contains Three Keywords The Output Line COMMENTS Single Line Comment Multiline Comment Documentation Comment TYPE CASTING Implicit Type

More information

Exam Duration: 2hrs and 30min Software Design

Exam Duration: 2hrs and 30min Software Design Exam Duration: 2hrs and 30min. 433-254 Software Design Section A Multiple Choice (This sample paper has less questions than the exam paper The exam paper will have 25 Multiple Choice questions.) 1. Which

More information