Sunteți pe pagina 1din 18

Vidya Murugendrappa

Assistant Professor

UNIT 3 STUDY GUIDE

SL.NO QUESTIONS MARKS


A. Summarize basic features of java. 10 marks
B. List out and explain object-oriented principles of java. 10 marks
C. Define java? Explain structure of java program with example. 5 marks
D. List out and explain any two primitive data-types of java. 10 marks
E. Explain general form for declaring variable in java with 5 marks
example.
F. Discuss lexical issues of java. 10 marks
G. Summarize general form of declaring one-dimensional and 10 marks
multi-dimensional array with example. Or 5 marks
each
H. Design a java program to perform basic arithmetic operations. 5 marks
I. Classify and explain the following: 10 marks
i. Arithmetic operators in java. Or
ii. Relational operators in java. 5 marks each
iii. Boolean operators in java.
J. Design a java program to compare the values of three variables 10 marks
a=10, b=15, c=15 using relational operators and print the
returned values.
K. Design a java program to print values from 0 to 20 using two- 6 marks
dimensional array.
L. Design a java program to perform Boolean logical operations . 6 marks

Advanced Computer Programming UNIT 3 Page 1


Vidya Murugendrappa
Assistant Professor

A. Summarize basic features of java.


i. Simple
ii. Secure
iii. Portable
iv. Object-oriented
v. Robust
vi. Multithreaded
vii. Architecture-neutral
viii. Interpreted
ix. High performance
x. Distributed
xi. Dynamic
xii. Platform independent

Explanation of features:

i. Simple:
 Java is Easy to write and more readable and eye catching.
 Java has a concise, cohesive set of features that makes it easy to learn and use.
 Most of the concepts are drew from C++ thus making Java learning simpler.
ii. Security: Explained in Page No 2
iii. Portability: Explained in Page No 3
iv. Object-oriented
 Object-oriented means we organize our software as a combination of
different types of objects that incorporates both data and behaviour.
 Object-oriented programming(OOPs) is a methodology that simplify
software development and maintenance by providing some rules.
 Basic concepts of OOPs are:
 Objects
 Classes
 Inheritance
 Polymorphism
 Abstraction
 Encapsulation

Advanced Computer Programming UNIT 3 Page 2


Vidya Murugendrappa
Assistant Professor

v. Robust:
Robust simply means strong. Java uses strong memory management. There are
lack of pointers that avoids security problem. There is automatic garbage
collection in java. There is exception handling and type checking mechanism
in java. All these points makes java robust.
vi. Multithreaded
A thread is like a separate program, executing concurrently. We can write Java
programs that deal with many tasks at once by defining multiple threads. The
main advantage of multi-threading is that it shares the same memory. Threads
are important for multi-media, Web applications etc.
vii. Architecture-neutral
 Java is not tied to a specific machine or operating system architecture.
 Machine Independent i.e Java is independent of hardware.
viii. Interpreted
 Java supports cross-platform code through the use of Java bytecode.
 Bytecode can be interpreted on any platform by JVM.
ix. High performance

 Bytecodes are highly optimized.


 JVM can executed them much faster.
x. Distributed
 Java was designed with the distributed environment.
 Java can be transmit, run over internet.
xi. Dynamic
Java programs carry with them substantial amounts of run-time type information
that is used to verify and resolve accesses to objects at run time.
xii. Platform Independent:

Platform is the hardware or software environment in which a program runs. There are two
types of platforms software-based and hardware-based. Java provides software-based
platform. The java platform differs from most other platforms in the sense that it's a
software-based platform that runs on top of other hardware-based platforms.it has two
components:
1. Runtime environment

Advanced Computer Programming UNIT 3 Page 3


Vidya Murugendrappa
Assistant Professor

2. Api(application programming interface)

Java code can be run on multiple platforms e.g.Windows,Linux,Sun Solaris,Mac/OS etc.


Java code is compiled by the compiler and converted into bytecode.This bytecode is a
platform independent code because it can be run on multiple platforms i.e. Write Once and
Run Anywhere(WORA).
B. List out and explain object-oriented principles of java.
a. All object-oriented programming languages provide mechanisms that help you implement
the object-oriented model. The mechanisms are as follows:
i. Encapsulation.
ii. Inheritance.
iii. Polymorphism.
i. Encapsulation:
 Encapsulation is the mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and misuse.

Advanced Computer Programming UNIT 3 Page 4


Vidya Murugendrappa
Assistant Professor

 One way to think about encapsulation is as a protective wrapper that prevents the
code and data from being arbitrarily accessed by other code defined outside the
wrapper.
 Access to the code and data inside the wrapper is tightly controlled through a well-
defined interface.
 A class defines the structure and behavior (data and code) that will be shared by a
set of objects. Each object of a given class contains the structure and behavior
defined by the class, as if it were stamped out by a mold in the shape of the class.
For this reason, objects are sometimes referred to as instances of a class. Thus, a
class is a logical construct; an object has physical reality.
 When you create a class, you will specify the code and data that constitute that
class. Collectively, these elements are called members of the class. Specifically, the
data defined by the class are referred to as member variables or instance variables.
The code that operates on that data is referred to as member methods or just
methods.
 The public interface of a class represents everything that external users of the class
need to know, or may know. The private methods and data can only be accessed by
code that is a member of the class.

Figure A: Encapsulation: public methods can be used to protect private data

Advanced Computer Programming UNIT 3 Page 5


Vidya Murugendrappa
Assistant Professor

 If a field is declared private, it cannot be accessed by anyone outside the class,


thereby hiding the fields within the class. Hence, Encapsulation is also called as
data hiding mechanism.
ii. Inheritance:
Inheritance is the process by which one object acquires the properties of
another object. This is important because it supports the concept of
hierarchical classification.
Example:
A Dog Is An Animal
A Cat Is An Animal
The idea here is that both "Dog" and "Cat" have some common characteristics
which are subsumed under the general notion of "Animal". We can represent
this pictorially in an inheritance diagram:

 In Java terms, we would say that Animal, Dog, and Cat are all classes
as they have common characteristics.
 Animal is the base class, superclass, or parent class as it represents the
animals in an abstract way that they have some attributes, such as size,
intelligence, and type of skeletal system. Animals also have certain behavioural
aspects; they eat, breathe, and sleep. This description of attributes and behavior
is the class definition for animals. Dog and Cat are derived classes, subclasses,
or child classes as they all describe a more specific class of animals, they would
have more specific attributes, such as type of teeth, and eyes. This is known as a
subclass of animals.
iii. Polymorphism:

Advanced Computer Programming UNIT 3 Page 6


Vidya Murugendrappa
Assistant Professor

 Polymorphism is the ability of an object to take on many forms. The most common use
of polymorphism in OOP occurs when a parent class reference is used to refer to a
child class object.
 When one task is performed by different ways i.e. known as polymorphism. For
example: to convense the students regarding shapes it’s necessary to draw different
shapes.So in this example shape is an object which is described in multiple forms.

Figure 2: Polymorphism with shape in multiple forms.

C. Define java? Explain structure of java program with example.

Java program can be defined as a collection of objects that communicate via invoking
each other's methods.
public class MyFirstJavaProgram {
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}
 Basic Structure of java program :
 Class name:

public class MyFirstJavaProgram {

 The word class is a keyword to define a new class and MyFirstJavaProgram is a


name of the class. The class definition must begins with opening curly brace ({)
and ends with closing curly brace (}). The rest of the things defined inside these

Advanced Computer Programming UNIT 3 Page 7


Vidya Murugendrappa
Assistant Professor

braces are called member of the class. And note that all the program activities are
defined inside the class.
 For all class names the first letter should be in Upper Case. If several words
are used to form a name of the class, each inner word's first letter should be in
Upper Case.

 Comment Lines:

/* This is my first java program.


* This will print 'Hello World' as the output
*/
This is called comment. This is for us to enter the comments about the program for
our own convenience. The contents of a comment will be ignored by the compiler.
Actually java supports three styles of comments. The above one is called multi-line
comment which may contain several lines. This type of comment must begin with
/* and end with */
 Main method :

public static void main(String []args) {

 This line begins with main method as like functions or subroutines in other
languages. The program will start execute by calling this main method. Let us
see briefly about the other attributes declared in main method.
 The keyword public is an access specifier.
 The keyword static is a kind of modifier.
 The keyword void means that the method main() does not return any value.
 All the java program will start execute by calling the main method. In a
main() method there is only one parameter ,String args[] . args[] is a name of
the parameter that is an array of the objects of data type String. String
store sequences of characters and args will receive the command line
arguments.
 All the method in java must be start with opening curly brace ({) and ends with
closing curly brace (}).
 Body of the method:

System.out.println("Hello World"); // prints Hello World

Advanced Computer Programming UNIT 3 Page 8


Vidya Murugendrappa
Assistant Professor

It contains statements related to processing of the java program. All statements


in the body of method should end with semicolon.
In this program it contains System.out.println which helps to display the output in the
command line.
 User defined methods:
All method names should start with a Lower Case letter. If several words are used
to form the name of the method, then each inner word's first letter should be in Upper
Case.
Example: public void myMethodName()

D. List out and explain any two primitive data-types of java.

a. Java defines eight primitive types of data: byte, short, int, long, char, float, double, and
boolean. The primitive types are also commonly referred to as simple types. These can be
put in four groups:
 Integers: This group includes byte, short, int, and long, which are for whole-valued
signed numbers.
 Floating-point numbers: This group includes float and double, which represent
numbers with fractional precision.
 Characters: This group includes char, which represents symbols in a character set, like
letters and numbers.
 Boolean: This group includes boolean, which is a special type for representing true/false
values.
b. INTEGERS: Java defines four integer types: byte, short, int, and long. All of these are
signed, positive and negative values. Java does not support unsigned, positive-only
integers.
 Byte:
 Byte data type is an 8-bit signed two's complement integer.
 Minimum value is -128 (-2^7)
 Maximum value is 127 (inclusive)(2^7 -1)
 Default value is 0
 Byte data type is used to save space in large arrays, mainly in place of integers,
since a byte is four times smaller than an int.
 Example: byte a = 100 , byte b = -50
 short:
 Short data type is a 16-bit signed two's complement integer.
 Minimum value is -32,768 (-2^15)

Advanced Computer Programming UNIT 3 Page 9


Vidya Murugendrappa
Assistant Professor

 Maximum value is 32,767 (inclusive) (2^15 -1)


 Short data type can also be used to save memory as byte data type. A short is
2 times smaller than an int
 Default value is 0.
 Example: short s = 10000, short r = -20000

 int:
 Int data type is a 32-bit signed two's complement integer.
 Minimum value is - 2,147,483,648.(-2^31)
 Maximum value is 2,147,483,647(inclusive).(2^31 -1)
 Int is generally used as the default data type for integral values unless there is
a concern about memory.
 The default value is 0.
 Example: int a = 100000, int b = -200000

 long:
 Long data type is a 64-bit signed two's complement integer.
 Minimum value is -9,223,372,036,854,775,808.(-2^63)
 Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
 This type is used when a wider range than int is needed.
 Default value is 0L.
 Example: long a = 100000L, long b = -200000L

Example:

// Compute distance light travels using long variables.


class Light {
public static void main(String args[]) {
int lightspeed;
long days;
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}

Advanced Computer Programming UNIT 3 Page 10


Vidya Murugendrappa
Assistant Professor

}
This program generates the following output:
In 1000 days light will travel about 16070400000000 miles.
c. Floating point numbers:
 Floating-point numbers, also known as real numbers, are used when evaluating expressions that
require fractional precision.
 For example, calculations such as square root, or transcendentals such as sine and cosine, result in a
value whose precision requires a floating-point type.
 Java implements the standard (IEEE–754) set of floating-point types and operators.
 There are two kinds of floating-point types, float and double, which represent single- and double-
precision numbers, respectively. Their width and ranges are shown here:

 Float:
 Float data type is a single-precision 32-bit IEEE 754 floating point.
 Float is mainly used to save memory in large arrays of floating point numbers.
 Default value is 0.0f.
 Float data type is never used for precise values such as currency.
 Example: float f1 = 234.5f

 double:
 Double data type is a double-precision 64-bit IEEE 754 floating point.
 This data type is generally used as the default data type for decimal values,
generally the default choice.
 Double data type should never be used for precise values such as currency.
 Default value is 0.0d.
 Example: double d1 = 123.4

C.2. CHARACTER
 Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative
chars. The standard set of characters known as ASCII still ranges from 0 to 127 as
always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255.
 Char data type is a single 16-bit Unicode character.
 Minimum value is '\u0000' (or 0).

Advanced Computer Programming UNIT 3 Page 11


Vidya Murugendrappa
Assistant Professor

 Maximum value is '\uffff' (or 65,535 inclusive).


 Char data type is used to store any character.
 Example: char letterA ='A'
Example program:
// Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
This program displays the following output:
ch1 and ch2: X Y

C.3. BOOLEAN
 Boolean data type represents one bit of information.
 There are only two possible values: true and false.
 This data type is used for simple flags that track true/false conditions.
 Default value is false.

 Example: boolean one = true

 Java has a primitive type, called boolean, for logical values. It can have only one of
two possible values, true or false. This is the type returned by all relational operators,
as in the case of a < b. boolean is also the type required by the conditional
expressions that govern the control statements such as if and for. Here is a program
that demonstrates the boolean type:

// Demonstrate boolean values.


class BoolTest {
public static void main(String args[]) {
boolean b;
b = false;
System.out.println("b is " + b);
b = true;

Advanced Computer Programming UNIT 3 Page 12


Vidya Murugendrappa
Assistant Professor

System.out.println("b is " + b);


// a boolean value can control the if statement
if(b) System.out.println("This is executed.");
b = false;
if(b) System.out.println("This is not executed.");
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
The output generated by this program is shown here:
b is false
b is true
This is executed.
10 9 is true
E. Explain general form for declaring variable in java with example.
Definition: A variable is a container that holds values that are used in a Java
program.
In Java, all variables must be declared before they can be used. The basic form of a
variable declaration is shown here:
type identifier [ = value][, identifier [= value] ...] ;
The type is one of Java’s atomic types, or the name of a class or interface. The identifier
is the name of the variable. You can initialize the variable by specifying an equal sign and
a value.
Example:

int a, b, c; // declares three ints, a, b, and c.


int d = 3, e, f = 5; // declares three more ints, initializing d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x'; // the variable x has the value 'x'.
F. Discuss lexical issues of java.

Java programs are a collection of whitespace, identifiers, literals, comments, operators,


separators, and keywords.

 Whitespace : Java is a free-form language. This means that you do not need to
follow any special indentation rules.
 Identifiers: Identifiers are used for class names, method names, and variable names.
An identifier may be any descriptive sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters. They must not begin with a
number, lest they be confused with a numeric literal. Again, Java is case-sensitive, so
VALUE is a different identifier than Value.

Advanced Computer Programming UNIT 3 Page 13


Vidya Murugendrappa
Assistant Professor

Some examples of valid identifiers are:

Invalid identifier names include these:

 Literals: A constant value in Java is created by using a literal representation of it. For
example, here are some literals:

Left to right, the first literal specifies an integer, the next is a floating-point value, the
third is a character constant, and the last is a string. Aliteral can be used anywhere a
value of its type is allowed.

 Comments:
There are three types of comments defined by Java. You have already seen two:
single-line and multiline. The third type is called a documentation comment. This type
of comment is used to produce an HTMLfile that documents your program. The
documentation comment begins with a /** and ends with a */.
 Separators :

In Java, there are a few characters that are used as separators. The most commonly
used separator in Java is the semicolon. As you have seen, it is used to terminate
statements. The separators are shown in the following table:

 Java Keywords:
There are 50 keywords currently defined in the Java language.
These keywords, combined with the syntax of the operators and separators, form the
foundation of the Java language. These keywords cannot be used as names for a
variable, class, or method.

Advanced Computer Programming UNIT 3 Page 14


Vidya Murugendrappa
Assistant Professor

G. Summarize general form of declaring one-dimensional and multi-dimensional array


with example.

A one-dimensional array is, essentially, a list of like-typed variables.

General form declaration one-dimensional array:

To create an array, you first must create an array variable of the desired type. The general
form of a one-dimensional array declaration is

type var-name[ ];
Here, type declares the base type of the array. The base type determines the data
type
of each element that comprises the array. Thus, the base type for the array
determines what type of data the array will hold.
Example:
The following declares an array named month_days with the type “array of int”:
int month_days[];
Multi-dimensional arrays:
 In Java, multidimensional arrays are actually arrays of arrays.
 Declaration and instantiation of multidimensional arrays:
To declare a multidimensional array variable, specify each additional index using
another set of square brackets.
New is the special operator used to allocate memory for multidimensional arrays.
Example:
The following declares a two dimensional array variable called twoD.
int twoD[][] = new int[4][5];

Advanced Computer Programming UNIT 3 Page 15


Vidya Murugendrappa
Assistant Professor

This allocates a 4 by 5 array and assigns it to twoD. Internally this matrix is


implemented as an array of arrays of int. Conceptually , this array will look like the
one shown in Figure 4.

Figure 4: A conceptual view of a 4 by 5, two-dimensional array


The following program numbers each element in the array from left to right, top to
bottom, and then displays these values:

// Demonstrate a two-dimensional array.


class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}

Advanced Computer Programming UNIT 3 Page 16


Vidya Murugendrappa
Assistant Professor

H. Design a java program to perform basic arithmetic operations.

public class BasicArithmeticDemo


{

public static void main(String[] args)


{
int number1 = 10;
int number2 = 5;

//calculating number1 + number2;


int sum = number1 + number2;

//calculating number1 - number2;


int difference = number1 - number2;

//calculating number1 * number2;


int product = number1 * number2;

//calculating number1 / number2;


int quot = number1 / number2;

//calculating number1 % number2;


int rem = number1 % number2;

//Displaying the values


System.out.println("number1 : "+number1);
System.out.println("number2 : "+number2);
System.out.println("sum : "+sum);
System.out.println("difference : "+difference);
System.out.println("product : "+product);
System.out.println("quot : "+quot);
System.out.println("rem : "+rem);
}
}

I. Classify and explain the following:


i. Arithmetic operators in java.
ii. Relational operators in java.
iii. Boolean operators in java.

Advanced Computer Programming UNIT 3 Page 17


Vidya Murugendrappa
Assistant Professor

Advanced Computer Programming UNIT 3 Page 18

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