Sunteți pe pagina 1din 12

Unit-2

PART-B

1 Explain the various forms of if statement with examples.


2. Discuss the looping statements with correct syntax and examples.
3. Discuss the class and object concepts with suitable examples.
4. a. What is a constructor? Explain with example program. (8)
b. Explain how to overload a method with example program.(8)
5. Describe the concept of inheritance with example program.

1 Explain the various forms of if statement with examples.

Table of Contents

 Java if Statement
o How if (if...then) statement works?
o Example
 Java if...else Statement
o How if...else statement works?
o Example
 Java if...else...if Statement
o Example
 Nested if...else Statement
o Example

In programming, it's often desirable to execute a certain section of code based upon
whether the specified condition is true or false (which is known only during the run time).
For such cases, control flow statements are used.

Java if (if-then) Statement


The syntax of if-then statement in Java is:

if (expression)
{
// statements
}

Here expression is a boolean expression (returns either true or false).


If the expression is evaluated to true, statement(s) inside the body of if(statements
inside parenthesis) are executed.

If the expression is evaluated to false, statement(s) inside the body of if are skipped
from execution.

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

int number = 10;

if (number > 0)
{
System.out.println("Number is positive.");
}
System.out.println("This statement is always executed.");
}

Java if...else (if-then-else) Statement


The if statement executes a certain section of code if the test expression is evaluated to
true. The if statement can have optional else statement. Codes inside the body of else
statement are executed if the test expression is false.

The syntax of if-then-else statement is:

if (expression)
{
// codes
}
else
{
// some other code
}
class ex3
{
public static void main(String[] args)
{
int number = 10;
if (number > 0)
{
System.out.println("Number is positive.");
}
else
{
System.out.println("Number is not positive.");
}
System.out.println("This statement is always executed.");
}
}

Java if..else..if Statement


In Java, it's possible to execute one block of code among many. For that, you can use
if..else...if ladder.

if (expression1)
{
// codes
}
else if(expression2)
{
// codes
}
else if (expression3)
{
// codes
}
.
.
else
{
// codes
}
The if statements are executed from the top towards the bottom. As soon as the test
expression is true, codes inside the body of that if statement is executed. Then, the
control of program jumps outside if-else-if ladder.

If all test expressions are false, codes inside the body of else is executed.

Java Nested if..else Statement


It's possible to have if..else statements inside a if..else statement in Java. It's called
nested if...else statement.

Here's a program to find largest of 3 numbers:

Example 4: Nested if...else Statement


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

Double n1 = -1.0, n2 = 4.5, n3 = -5.3, largestNumber;

if (n1 >= n2) {


if (n1 >= n3) {
largestNumber = n1;
} else {
largestNumber = n3;
}
} else {
if (n2 >= n3) {
largestNumber = n2;
} else {
largestNumber = n3;
}
}

System.out.println("Largest number is " + largestNumber);


}
}
2. Discuss the looping statements with correct syntax and examples.

Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. In Java
we have three types of basic loops: for, while and do-while. In this tutorial we will learn how to use “for
loop” in Java.

Java for Loop


The syntax of for Loop in Java is:

for (initialization; testExpression; update)


{
// codes inside for loop's body
}

How for loop works?


1. The initialization expression is executed only once.
2. Then, the test expression is evaluated. Here, test expression is a boolean expression.
3. If the test expression is evaluated to true,
o Codes inside the body of for loop is executed.
o Then the update expression is executed.
o Again, the test expression is evaluated.
o If the test expression is true, codes inside the body of for loop is executed and
update expression is executed.
o This process goes on until the test expression is evaluated to false.
4. If the test expression is evaluated to false, for loop terminates.

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

for (int i = 1; i <= 10; ++i) {


System.out.println("Line " + i);
}
}

Java while Loop


The syntax of while loop is:

while (testExpression)
{
// codes inside body of while loop
}

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

int i = 1;

while (i <= 10) {


System.out.println("Line " + i);
++i;
}
}
}

Java do...while Loop


The do...while loop is similar to while loop with one key difference. The body
of do...while loop is executed for once before the test expression is checked.

The syntax of do..while loop is:

do {
// codes inside body of do while loop
} while (testExpression);

EX:

class DoWhileLoopExample
{
public static void main(String args[]){
int i=10;
do
{
System.out.println(i);
i--;
}
while(i>1);
}
}
Syntax of for loop:

for(initialization; condition ; increment/decrement)


{
statement(s);
}

EX:
class ForLoopExample {
public static void main(String args[]){
for(int i=10; i>1; i--){
System.out.println("The value of i is: "+i);
}
}
}

3. Discuss the class and object concepts with suitable examples

A class, in the context of Java, are templates that are used to create objects, and to define
object data types and methods. Core properties include the data types and methods that
may be used by the object.

A class is a user defined blueprint or prototype from which objects are created. It
represents the set of properties or methods that are common to all objects of one type. In
general, class declarations can include these components, in order:

 Modifiers : A class can be public or has default access (Refer this for details).

 Class name: The name should begin with a 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, { }.

Object

It is a basic unit of Object Oriented Programming and represents the real life entities. A
typical Java program creates many objects, which as you know, interact by invoking
methods. An object consists of :
 State : It is represented by attributes of an object. It also reflects the properties of an
object.
 Behavior : It is represented by methods of an object. It also reflects the response of
an object with other objects.
 Identity : It gives a unique name to an object and enables one object to interact with
other objects.

4. A What is a constructor? Explain with example program. (8)

Java constructor:

A constructor in Java is a method which is used used to initialize objects. Constructor


method of a class has the same name as that of the class, they are called or invoked when
an object of a class is created and can't be called explicitly. Attributes of an object may or
may not be available while creating objects, if no attribute is available then default
constructor is called, some of the attributes may be known initially. It is optional to write
constructor method(s) in a class but due to their utility they are used.

class Programming {
//constructor method
Programming() {
System.out.println("Constructor method called.");
}

public static void main(String[] args) {


Programming object = new Programming(); // Creating an object
}
}

b. Explain how to overload a method with example program.(8)

Method Overloading is a feature that allows a class to have more than one method having
the same name, if their argument lists are different. It is similar to constructor overloading
in Java, that allows a class to have more than one constructor having different argument
lists.

let’s get back to the point, when I say argument list it means the parameters that a method
has: For example the argument list of a method add(int a, int b) having two parameters is
different from the argument list of the method add(int a, int b, int c) having three
parameters.

Three ways to overload a method


In order to overload a method, the argument lists of the methods must differ in either of
these:

1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
3. Sequence of Data type of parameters.
For example:
add(int, float)
add(float, int)

EX:
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:

a
a 10

5. Describe the concept of inheritance with example program.

The process by which one class acquires the properties(data members) and
functionalities(methods) of another class is called inheritance. The aim of inheritance is to
provide the reusability of code so that a class has to write only the unique features and rest
of the common properties and functionalities can be extended from the another class.
Child Class:
The class that extends the features of another class is known as child class, sub class or
derived class.

Parent Class:
The class whose properties and functionalities are used(inherited) by another class is
known as parent class, super class or Base class.

Syntax:
Inheritance in Java
To inherit a class we use extends keyword. Here class XYZ is child class and class ABC is
parent class. The class XYZ is inheriting the properties and methods of ABC class.

class Teacher {
String designation = "Teacher";
String collegeName = "Beginnersbook";
void does(){
System.out.println("Teaching");
}
}

public class PhysicsTeacher extends Teacher{


String mainSubject = "Physics";
public static void main(String args[]){
PhysicsTeacher obj = new PhysicsTeacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}
Output:

Beginnersbook
Teacher
Physics
Teaching

UNIT-II

PART-A

1. What are the control statements in java?


2. Write syntax of if…else statement.
3. Write syntax of switch statement.
4. Define looping.
5. Write syntax of for loop.
6. State the difference between while and do-while statements.
7. Give the general form of a class.
8. State the purpose of new operator.
9. What is a constructor?
10. Define the term ‘overloading’.
11. State the uses of static keyword.
12. Write down the uses of final keyword.

1.What are the control statements in java?


Java Control statements control the order of execution in a java program, based on data
values and conditional logic. There are three main categories of control flow statements;
Selection statements: if, if-else and switch. Loop statements: while, do-while and for.

2. Write syntax of if…else statement.


if (expression)
{
// codes
}
else
{
// some other code
}

3. Write syntax of switch statement.

switch (variable/expression) {
case value1:
// statements
break;
case value2:
// statements
break;
.. .. ...
.. .. ...
default:
// statements
}
4. Define looping.

It repeats a statement or a group of statements while a given condition is true. It tests the
condition before executing the loop body. 2. for loop. It executes a sequence of statements
multiple times and abbreviates the code that manages the loop variable.

5. Write syntax of for loop.


Java for Loop
The syntax of for Loop in Java is:

for (initialization; testExpression; update)


{
// codes inside for loop's body
}

6. State the difference between while and do-while statements.


Difference between While and Do While in Java. ... So, While loop executes the code block
only if the condition is True. In Java Do While loop, condition is tested at the end of the loop
so Do While executes the statements in the code block at least once even if the condition
Fails.

7. Give the general form of a class.


Class is the means by which you define objects. A class may contain three types of items :
variables, methods and constructors.

Variables represent its state. Class can have static and instance variables. Methods provide
the logic that constitutes the behavior defined by a class. Class can have static and instance
methods. Constructors initialize the state of a new instance of a class.

8. State the purpose of new operator.


When you are declaring a class in java, you are just creating a new data type. A class
provides the blueprint for objects. ... This variable does not define an object. Instead, it is
simply a variable that can refer to an object.

9. What is a constructor?
A constructor in Java is a block of code similar to a method that's called when an instance of
an object is created. Here are the key differences between a constructor and a method: A
constructor doesn't have a return type. ... Unlike methods, constructors are not considered
members of a class.

S-ar putea să vă placă și