Docsity

Object-Oriented Programming in Java: Classes, Objects, Constructors and Scanner, Lecture notes of Programming Languages

Partial preview of the text

Download Object-Oriented Programming in Java: Classes, Objects, Constructors and Scanner and more Lecture notes Programming Languages in PDF only on Docsity!

LAB 03 (I)

Introduction to OOP

Objectives

● To learn about classes, objects, constructors and scanner class.

Introduction to OOP:

Object-oriented programming System (OOPs) is a programming paradigm based on the concept of “objects” that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs. Object oriented programming brings together data and its behavior (methods) in a single location (object) and makes it easier to understand how a program works. In object-oriented programming technique, programs are designed using objects and classes. Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods.

Class

A class is the building block of OOP. It is the prototype that defines methods and variables that

are common to all specified types of objects. It is the way to bind the data and its logically related

functions together. It's a non-primitive data type that can be treated like any other built-in data type.

A class is a group of objects which have common properties. A Class is like an object constructor,

or a "blueprint" for creating objects. It is a template or blueprint from which objects are created.

It is a logical entity. It can't be physical. A class can be considered as a blueprint using which you

can create as many objects as you like. Data members are also known as fields, instance variables

and object states.

When you define a class, you declare its exact form and nature. You do this by specifying the instance variables that it contains and the methods that operate on them. Although very simple

classes might contain only methods or only instance variables, most real-world classes contain

both. A class is created by using the keyword class. In general, class declarations can include these

components, in order:

● Modifiers : A class can be public or has default access.

● Class keyword: class keyword is used to create a class.

● Class name: The name should begin with an initial letter (capitalized by convention).

● Superclass (if any): The name of the class’s parent (superclass), if any, preceded by

the keyword extends. A class can only extend (subclass) one parent.

● Interfaces (if any): A comma-separated list of interfaces implemented by the class, if

any, preceded by the keyword implements. A class can implement more than one interface.

● Body: The class body surrounded by braces, { }.

The general form of a class definition is shown below.

Syntax

//Syntax of a class public class classname { // declare instance variables

type var1; type var2; //… type varN;

// declare methods

type method1(parameters) { // body of method }

type method2(parameters) { // body of method } //…

type methodN(parameters) { // body of method } }

Example 3.

//FileName:cellPhone.java //Defining class public class CellPhone { //Declaring instance variables string name ; string processor; string memory; string os ;

//Declaring and Defining member function void display()

Calling member function through an object

Syntax

//syntax of calling member function of a class through an object objectName.member_function;

Calling display function of CellPhone class

Syntax

myObj.display();

Constructor:

When discussing classes, one of the most important sub topic would be constructors. Every class has a constructor. If we do not explicitly write a constructor for a class, the Java compiler builds a default constructor for that class. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes Each time a new object is created, at least one constructor will be invoked. The main rule of constructors is that they should have the same name as the class. A class can have more than one constructor.

Types of Java constructors

There are three types of constructors in Java:

  1. Default constructor
  2. Non-Parameterized (no-arg constructor)
  3. Parameterized constructor

Default constructor

If you do not implement any constructor in your class, Java compiler inserts a default

constructor into your code on your behalf. This constructor is known as the default constructor.

You would not find it in your source code (the java file) as it would be inserted into the code

during compilation and exists in the .class file.

Example 3.

Let’s see the example of the default constructor that displays the default values.

Here, we are creating a main() method inside the class.

Output:

In the above class, you are not creating any constructor so the compiler provides you a default constructor. Here 0 and null values are provided by default constructor.

Output:

Parameterized constructor

Constructor that take arguments are Parameterized constructors.

Syntax

class className { //Syntax of creating a non-parameterized constructor className( datatype ) { } };

Example 3. 4

Output:

Java Scanner Class

Java Scanner class allows the user to take input from the console. It belongs to the java.util package. It is used to read the input of primitive types like int, double, long, short, float, and byte. It is the easiest way to read input in a Java program.

Syntax

Scanner sc=new Scanner(System.in);

The above statement creates a constructor of the Scanner class having System.in as an argument. It means it is going to read from the standard input stream of the program. The java.util package should be imported while using Scanner class.

It also converts the Bytes (from the input stream) into characters using the platform's default charset.

Example 3.

Output:

The nextLine() method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end. The next is set to after the line separator. Since this method continues to search through the input looking for a line separator, it may search all of the input searching for the line to skip if no line separators are present. Below example shows how to solve this issue with nextLine() method:

Example 3.

Assignment # 3

Q1. Create a class BankAccount whose attributes are: account number, account holder’s name and balance. BankAccount constructor initializes the values of account number, account holder’s name and balance. Make one credit function which credits the amount either passed by arguments or the value entered by the user. Create three accounts for three persons and display the same on your screen.

Q2. Create a class Author whose attributes are: name, email, gender and books. Author constructor initializes the values of name, email, gender and books. Define a static variable, method and block , and use this keyword in your code.