[go: up one dir, main page]

0% found this document useful (0 votes)
11 views76 pages

Ebook QA QE 1 L2 Core Java Extended

Uploaded by

Priyanka Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views76 pages

Ebook QA QE 1 L2 Core Java Extended

Uploaded by

Priyanka Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Code Java Extended

You Already Know

Course(s):

Core Java
▪ Explain the fundamentals of Java language
- Classes and Objects
- Methods and Variables

▪ Explain data types and their declarations


- Primitive data types

▪ Use of conditional statements, loops, break statements, and


continue statements
- If, if-else, and if-else-if
- While, for, and do-while
▪ Explain classes and objects
- Object life cycle
- Operators and their precedence

▪ Explain the use of core keywords


- static
- final
- this
A Day in the Life of a Test Engineer

Due to company’s frequent change in goals and based on Joe’s skills,


he is added to another sprint. This sprint focuses majorly on
implementing the features to their website. Selling the products is one
of the decisions the stakeholders agreed to implement. In this sprint,
Joe is assigned a task on JIRA which is a project management tool.

Joe has to develop a feature where users can add multiple items to
the cart and delete items from the cart. However, a senior developer
will be assisting Joe to complete this task. During pair programming,
Joe was asked to write a program that does the basic operations
similar to a calculator to get started.

In this lesson, we will learn how to solve this real-world scenario to


help Joe complete his task effectively and quickly.
Learning Objectives

By the end of this lesson, you will be able to:

Demonstrate type casting

Use access modifiers

Demonstrate working of while loop, do while loop, and for


loop

Explain object-oriented concepts of Java

Describe how exceptions are handled using try-catch block,


finally block, throw keyword, and throws keyword
Type Casting
Type Casting

When two data types are compatible with each other, the value of one
data type that is assigned to the other is called type casting.

Implicit Type Casting:


byte 🡪 short 🡪 int 🡪 long 🡪 float 🡪 double

Type Casting

Explicit Type Casting:


double 🡪 float 🡪 long 🡪 int 🡪 short 🡪 byte
Type Casting: Types

Widening or Implicit Conversion Narrowing or Explicit Conversion

▪ Widening or implicit conversion takes place


when two data types are automatically
converted ▪ To assign the value of a large data type to a
▪ This is possible when: smaller data type, we have to perform
• The two data types are compatible narrowing or explicit conversion
• The value of a smaller data type is ▪ This is useful for incompatible data types
assigned to a bigger data type when automatic conversion is not possible
▪ Conversion of numeric data type to char or
boolean is not supported
Type Casting

Duration: 20 min.

Problem Statement:

Write a program which will take a string as input and will convert it into other primitive data
types.
Assisted Practice: Guidelines

Steps to demonstrate type casting:

1. Create a Java project in your IDE.

2. Write a program in Java to take an input from user and convert the input variable into other data types.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Access Modifiers
Access Modifiers

The access modifiers specify the accessibility of a data member, class, constructor, or method.

Modifier Class Package Subclass World

public

protected

No modifier

private
Types of Access Modifiers

Default Public

The default access It is accessible


modifiers are accessible everywhere. It has the
only within the package. widest scope.

Private Protected

The protected access


Private access modifiers modifiers are accessible
are accessible only only within and outside
within the class. the package but only
through inheritance.
Access Modifiers

Duration: 20 min.

Problem Statement:

Write a program to demonstrate how and when access modifiers are used.
Assisted Practice: Guidelines

Steps to demonstrate access modifiers:

1. Create a Java project in your IDE.

2. Write a program in Java to create variables with different access modifiers and

access these variables in main().

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Looping
Loops in Java

Start

▪ Enable you to execute blocks of statements multiple times Conditional Code

▪ Controlled by Boolean expressions


If condition
▪ Types of loops: is true
o While loop
Condition
o Do while loop
o For loop

If condition
is false

Exit
while Loop

while(condition)
Continually executes a block of statements as long {
conditional code ;
as a given statement is true }

Evaluates its expression at the top of the loop Condition

If condition
is true
Has the following syntax:
code block
If condition
while (expression) { is false
statement(s)
}
do…while Loop

Same as while loop, except that it is guaranteed to do{


execute at least one time conditional code ;
} while (condition)

code block
Evaluates its expression at the bottom of the loop,
instead of the top If condition
is true

Has the following syntax:

Condition
do {
statement(s)
} while (expression);
If condition
is false
for Loop

for( init; condition; increment)


{
Commonly used for simple iterations conditional code ;
}

Init

Allows you to repeat a set of statements a


certain number of times until a condition is
matched
Condition

Has the following syntax:


If condition
is true
Increment is invoked
Initialization is executed Code block
after each iteration If condition
once the loop begins
through the loop is false
Increment
for (initialization; termination; increment) {
statement(s)
}

If the condition is false, then the for loop


will be terminated
While Loop

Duration: 15 min.

Problem Statement:

Write a program to demonstrate while loop execution.


Assisted Practice: Guidelines

Steps to demonstrate while loop:

1. Create a Java project in your IDE.

2. Write a program in Java to demonstrate while loop execution.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Do While Loop

Duration: 15 min.

Problem Statement:

Write a program to demonstrate do while loop execution.


Assisted Practice: Guidelines

Steps to demonstrate do while loop:

1. Create a Java project in your IDE.

2. Write a program in Java to demonstrate do while loop execution.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


For Loop

Duration: 20 min.

Problem Statement:

Write a program to demonstrate for loop execution.


Assisted Practice: Guidelines

Steps to demonstrate for loop:

1. Create a Java project in your IDE.

2. Write a program in Java to demonstrate for loop execution.

3. Add and commit the program files.

4. Push to code to your GitHub repository.


Class, Objects, and Constructors
What Is a Class?

A class is a user defined template or prototype that defines objects of the same type. All
objects in the class will exhibit the same properties.

Class Object Common Class Properties


Oak
Branches
Tree Willow Leaves
Elm Roots
What Is a Class?

A class declaration consists of the following segments:

Modifiers

Class Name
Class Declaration
Keywords

Body
Class Initialization

Class initialization consists of a block of code preceded by the “static” keyword. Every time a class is loaded, Java
invokes this code.

class Demo {
static {
// ...initialization code here...
}
}

What is class instantiation?


Creating a new instance (object) in a class is called class instantiation.

class Demo {
{
// ...initialization code here...
}
}
JVM Architecture

JVM Language Classes Class Loader

JVM Memory

JVM Native
Method PC
Heap Language Method
Area Registers
Stacks Stacks

Native Native
Execution
Method Method
Engine
Interface Libraries
Ways to Instantiate a Class

New Keyword

New Instance

Clone() method

Deserialization

newInstance() method of
constructor class
Difference between Class Referencing and Object Creation

▪ An object is an instance created in a class and a reference is the memory address to


navigate to that said object.

▪ For example: A mobile phone has several contacts, each contact representing an actual
person.
▪ Object: Person

▪ Reference: contact, which is a means of reaching the person.


class Person {
String name = “Smith";
int num = 9990000999;
public static void main(String[] args)
{
Person p = new Person();
}
}

p is a reference which points out the object to the person.


Static and Instance Blocks

Static Blocks:
• Used for static initializations of a class
• Executed when a static member is invoked in a class
• Executed before constructors

Instance Blocks:
• Executed when an object is created in a class

class student
{
static int rollno ;
static String name;
static{ rollno = 28; name = “John";
}
public static void main(String args[]){}
}
What Are Constructors?

Constructors are blocks of code executed while initializing an object. Constructors are used to
assign value to class variables when creating an object.

Must have the same name as the


class name

Must have no return type


Constructors
Cannot use abstract, static, final,
or synchronized

Can use access modifiers


Types of Constructors

There are three types of constructors such as Default, No-Arg, and Parameterized.

Default Constructor:
If user does not implement any constructor, then default constructor is inserted in the
class by Java compiler.

class Demo
Java {
class Demo{}
Compiler Demo(){}
}
Types of Constructors

No-Arg Constructor:
These constructors have no arguments.

class Demo
{
public Demo(){}
public static void main(String arg[]){
new Demo();
}
}
Types of Constructors

Parameterized Constructor:
These constructors have arguments or parameters.

public class Employee


{
int empId;
String empName; //parameterized constructor with two parameters
Employee(int id, String name){
this.empId = id;
this.empName = name;
}
}
Class, Objects, and Constructors

Duration: 20 min.

Problem Statement:
Explain Classes, Objects, and Constructors using an example.
Assisted Practice: Guidelines

Steps to demonstrate class, objects, and constructors:

1. Create a Java project in your IDE.

2. Write a program in Java to create a class and its objects in main().

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Inheritance
Inheritance

▪ A mechanism in which one class acquires all the properties and behaviors of the parent
class
▪ Used for method overriding and code reusability
IS-A Relationship

Inheritance represents IS-A relationship, also known as Parent-Child relationship.

Used in Java for

1 2

Method Overriding Code Reusability

Class SubClass extends SuperClass


{
Syntax of
//Variables
Inheritance
//Methods
}
Types of Inheritance
Inheritance

Duration: 15 min.

Problem Statement:
Explain types of inheritance using examples.
Assisted Practice: Guidelines

Steps to demonstrate inheritance:

1. Create a Java project in your IDE.

2. Write a program in Java to demonstrate inheritance.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Collections and Generics
What Are Collections?

A collection is a group or single unit of individual objects. Java Collection Framework defines
several classes and interfaces to represent objects as a single unit.

Search

Sort
Common operations
using Java collections Insert

Delete

Update
Collection Hierarchy
Interface
Iterable Class
Extends
Implements
Collection

List Queue Set

ArrayList PriorityQueue HashSet

LinkedList Deque
LinkedHashSet

Vector ArrayDeque SortedSet

Stack TreeSet
Legacy Classes vs. Collection Framework

Legacy Classes Collection Framework

Legacy classes comprises classes and Collections are used to store and
interfaces supported by older versions of manipulate group of objects.
Java.

Legacy classes are: Comes with interfaces like Collection, Set,


• Dictionary List, or Map. Classes include ArrayList,
• Hashtable LinkedList, Vector and Stack.
• Properties
• Stack
• Vector

Legacy classes are synchronized. Collections are non-synchronized.


List, Set, LinkedList, and Queue

List ● An interface that contains sequentially arranged elements


● ArrayList, LinkedList, and Vector subclasses implement List interface

Set ● Contains unique elements arranged in any order


● HashSet, LinkedHashSet, TreeSet classes implement Set interface

LinkedList ● Contains elements of a specified collection, arranged into an ordered list

● Contains an ordered list of homogeneous elements, where elements are added and
Queue removed at the rear and front end respectively
● LinkedList, Priority queue, ArrayQueue implement this interface
Collections Methods

Here are some collection methods:

size() clear()

add(Object o) isEmpty()

addAll(Collection c) iterator()

remove(Object o) equals(Object o)

removeAll(Collection c) toArray()

contains(Object o) toArray(Object[] a)

containsAll(Collection c) retainAll(Collection c)
What Are Generics?

Generics ensure type safety in Java by making bugs detectable during compile time. Type Erasure
feature in Java Generics erases extra information added to source code.

Types of Generics:

Generic type class or interface: A class Generic type method or Constructor:


is generic if it declares one or more type Generic methods have their own
variables parameters
Applications

The Java collection hierarchy has a number of uses, such as:


• ArrayList can be used for finding a specific object using array index.
• Linked list is useful in insertion and deletion operations.
• Set can be used to create unique elements and TreeSet is an implementation of the set.
Collections and Generics

Duration: 30 min.

Problem Statement:
Write a program to demonstrate working of Collections and Generics.
Assisted Practice: Guidelines

Steps to demonstrate Collections and Generics:

1. Create a Java project in your IDE.

2. Write a program to demonstrate functionality of Collections.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Exception Handling
What Is Exception Handling?

Exception Handling is a mechanism to handle run time errors that disrupt the normal flow of
an application.

Normal flow of application


Exception

Alternate way to continue flow of application

Several factors can cause an exception during runtime, such as an invalid input data, a
nonexistent file, or an interrupted network connection.
Keywords Used for Exception Handling

Following keywords are used for exception handling:

try A block that contains a set of statements where an exception may occur

catch Handles the uncertain exception in the try block

throw A keyword manually transfers control from try to catch

throws A keyword handles exceptions without try and catch

finally block contains a set of statements that will always execute whether or not there is an
exception
Need of Handling Errors and Exception

When an exception occurs midway through execution, the flow of the


statement breaks and the program terminates abruptly. Exception and
Why errors error handling ensures that all statements run and thereby maintains
and the flow of the program.
exceptions
should be The traditional error handling methods use multiple if else conditions in
the normal program flow, which makes the code less readable. Exception
handled? handling separates error handling code from the regular code, using try
catch blocks.
Exception Hierarchy

Throwable Throwable is the superclass of all


errors and exceptions.

Errors Exception

Errors are serious issues that


cannot be handled. Errors Checked exceptions can be
result in the abrupt foreseen during compile time
Checked
termination of the program. and can be handled before run
time.

Unchecked exceptions occur


Unchecked during runtime and need to be
handled by the developer.
Types of Exception

ArithmeticException Thrown when an exception has occurred in an arithmetic operation

ArrayIndexOutOfBoundsException Thrown to notify that an array has been accessed with an illegal index

ClassNotFoundException Thrown when a class whose definition is not found, is accessed

FileNotFoundException Thrown when a file is not found or is inaccessible

NullPointerException Thrown when accessing members of a null object

RuntimeException Represents any exception that occurs during runtime

IOException Thrown when an input-output operation fails or is interrupted

NumberFormatException Thrown when the conversion from string to numeric format fails

NoSuchFieldException Thrown when the class does not contain the specified field or variable
Basic Try-Catch Block

Duration: 15 min.

Problem Statement:
Demonstrate how a basic try-catch block is used for Exception Handling.
Assisted Practice: Guidelines

Steps to demonstrate basic try-catch block:

1. Create a Java project in your IDE.

2. Write a program to demonstrate functionality of try-catch block.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Throw and Throws

Duration: 20 min.

Problem Statement:
Explain throw and throws using an example.
Assisted Practice: Guidelines

Steps to demonstrate throw and throws keywords:

1. Create a Java project in your IDE.

2. Write a program to demonstrate functionality of throw and throws keywords.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Try with Parameters

Duration: 30 min.

Problem Statement:
Implement try with parameters in a program.
Assisted Practice: Guidelines

Steps to demonstrate try with parameters:

1. Create a Java project in your IDE.

2. Write a program to demonstrate working of try block.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Multiple Catch with Exception Class Hierarchy

Duration: 30 min.

Problem Statement:
Implement multiple catch with exception class hierarchy in a program.
Assisted Practice: Guidelines

Steps to demonstrate multiple catch with exception class hierarchy:

1. Create a Java project in your IDE.

2. Write a program to demonstrate working of multiple catch blocks..

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Finally Block

Duration: 30 min.

Problem Statement:
Demonstrate the use of finally block in a program.
Assisted Practice: Guidelines

Steps to demonstrate finally block:

1. Create a Java project in your IDE.

2. Write a program to demonstrate working of finally block.

3. Initialize the .git file.

4. Add and commit the program files.

5. Push to code to your GitHub repository.


Key Takeaways

Java is a general-purpose, object-oriented, concurrent, and


the run time environment (JRE) programming language

Java can be used in web development, Big Data, continuous


testing, and frameworks

Type casting is used to assign value of one data type to the


other, only if the two data types are compatible

The access modifiers in java are used to specify the scope of


a field, method, constructor, or a class

We can change the scope of the field, method, constructor,


or a class by applying the access modifiers on it
Create a program to find areas of Multiple Dimensions

Problem Statement: Duration: 90 min.


You are asked to develop a functionality where user will enter the
dimensions and you need to calculate and print the area of selected
shape.
Before the Next Class

Course: SQL Training

You should be able to:

▪ Explain what is database

▪ Demonstrate SQL queries

▪ Filter results using queries

▪ Group data using queries

You might also like