Introduction to Program Coding – Java and BlueJ

A Fill in the blanks.

  1. A text editor is used to write the programs.
  2. Object is a real-world entity.
  3. A class is a blueprint of an object.
  4. A token is the smallest element of a program.
  5. A variable is used to save data temporarily for later use in the program.

B State whether the given statements are true or false.

  1. Logical operators are used to find whether one or both the conditions are true or false. True
  2. In Java programming, you use the variable even if it is not declared. False
  3. Keywords are the words that have already been defined by a Java compiler. True
  4. A comment is ignored by a computer. True
  5. A class is a collection of different objects. False

C Choose the correct answer.

  1. What does OOP mean?
    a. Object-Oriented Programming
    b. Object-Oriented Procedure
    c. Object-Origin Programming
    d. Object-Origin Procedure
  2. Class whose properties are inherited is known as ________ class.
    a. parent
    b. father
    c. child
    d. mother
  3. ________ keyword does not return any value.
    a. void
    b. String
    b. main
    d. int
  4. char is a ________.
    a. data type
    b. bit
    c. word
    d. unit
  5. ________ are the values that do not change during the execution of a program.
    a. Constants
    b. Instances
    c. Keywords
    d. Characters

D Name the following.

  1. A method that can be accessed by anyone. public
  2. A place where program’s execution begins. main() method
  3. The smallest element of a program that is meaningful to the compiler. Tokens
  4. A collection of similar objects. class
  5. Java development environment. BlueJ

E Answer the following questions.

  1. What is a class?
    A class is a collection of similar objects.
  2. What are operators? Explain relational operators.
    Operators are the symbols to perform calculations. Relational operators are used to compare data. Following are the various types of relational operators:
    > means greater than
    < means less than
    >= means greater than or equal to
    <= means less than or equal to
    == means equal to
    != means not equal to
  3. What is a variable? How is variable different from constant?
    Variables are identifiers of memory locations, used to save data temporarily for later use in the program. Constants are fixed values, whereas variables are not fixed values.
  4. Explain different types of constants.
    Integer constants represent integers. Example: 12
    Real constants represent real numbers. Example: 2.75
    Character constants represent character value. Example: ‘A’
    String constant represents String value. Example: “Hello”
    Boolean constant represents Boolean value. Example: true/false
  5. Explain class declaration with an example.
class Student{
    String name;
    int roll;
    public void assign(){
        name = "Shalini";
        roll = 45;
    }
    public void display(){
        System.out.println("Name: " + name);
        System.out.println("Roll: " + roll);
    }
}

In the above code fragment, we have defined a class ‘Student’ with data members ‘name’ and ‘roll’. We also have defined two methods named ‘assign’ and ‘display’. The ‘assign’ method stores values in the data members, whereas the ‘display’ method displays those values with proper message.

Quick Check

Fill in the blanks.
1. A programming language has its own rules known as syntax.
2. Object-Oriented Programming (OOP) attempts to eliminate most of the drawbacks of Procedure-Oriented Programming.
3. Abstraction means to ignore unnecessary details of an object and access all the essential details.
4. Encapsulation prevents data from unauthorized access and from getting misused.
5. Polymorphism is derived from Latin words ‘poly’ meaning many and ‘morph’ meaning forms.

Choose True or False for the following statements.
1. To start BlueJ, you should click on the Compile button present on top of the Ribbon. False
2. Identifiers in Java can start with a digit. False
3. Constants in Java are variables whose values can be changed during program execution. False
4. Variable initialization is the process of assigning a value to a variable. True

Match the columns.

  1. && → Checks if both conditions are true.
  2. + → Used to add two numbers.
  3. = → Assignment operator that assigns a value to a variable.
  4. <= → Checks if the first number is less than or equal to the second.
  5. ++ → Increment operator that adds 1 to a variable.

Chapter Notes

A programming language has its own rules that are called syntax. Syntax is known as the grammar of a programming language. A program is a set of instructions. The program written in human-readable form is known as the source code. The object code is the program translated into a form that a computer understands. The language used to write computer programs is known as programming language. The person who writes computer programs is known as a programmer.

The Java Programming Language is a ‘Write Once Run Anywhere’ language. Java was developed in January 1996 by a team of Sun Microsystems Inc. headed by James Gosling. Java is an object-oriented programming language.

BlueJ is an IDE (Integrated Development Environment) for the beginners to write, edit and execute Java programs. It includes:
a) A viewer: to view parts of a program.
b) An editor: to write and modify programs.
c) A debugger: to find errors in the program.
BlueJ is a Java development environment.

An object is a real-world entity. They have two characteristics: state and behavior. State determines the properties of an object, whereas behavior determines what that object does. State is represented through variables and behavior is represented through methods.

A class is a collection of similar objects. A class is a blueprint of an object. Multiple objects can be created using one class. All objects belonging to a particular class are called instances of that class.

OOP (Object-Oriented Programming) is the latest approach in programming. It eliminates the drawbacks of POP (Procedure-Oriented Programming). Following are the four principles of OOP:
1) Abstraction: It is the process of hiding unnecessary details and showing only necessary information. It helps in reducing code complexity.
2) Encapsulation: It is the process of wrapping of data and methods in a single unit. It prevents data from unauthorized access.
3) Inheritance: It is the process in which the properties of one class are inherited by another class. The class that inherits is known as the child class, and the class that gets inherited is known as the parent class. Inheritance helps in code reuse and establish relationships between different classes.
4) Polymorphism: It is an ability of an object to take many forms. It allows you to define one interface and have multiple implementations.

Working in BlueJ
Download and install BlueJ from: bluej.org website.
Start button→BlueJ option
Project menu→New Project option→Enter project name→Create button
New Class option→Enter class name→OK button
Double-click the class icon→Press Ctrl + A and delete existing code→type your program.
Compile button (Rectify errors if any)→Close button
Right-click class icon→Select void main() option→OK button

Steps to install BlueJ on Windows.

A comment is ignored by the compiler. It is used to add notes in your program. Single line comments begin with // and end as soon as the Enter key is pressed. Multiline comments begin with /* and end with */.

The class keyword allows us to define a new class. The main() method is the point from where the program begins its execution. If main() terminates, the entire program terminates. The public keyword indicates that main() can be accessed from anywhere. The void keyword indicates that main() doesn’t return any value. String args[] is used to store all the command line arguments for the main() method. System.out.println() method is used to display a message on the console.

class MyApp{
    public static void main(String args[]){
        System.out.println("Hello World!");
    }
}

A token is the smallest element of a program that is meaningful to the compiler. Following are the types of tokens:
1) Identifiers: These are the names given to classes, methods and variables.
2) Keywords: These are the words that are reserved for the Java compiler.
3) Constants: These are the values that do not change during program execution. They are also known as literals.

Variable Declaration: int marks;
Variable Initialization: marks = 45;
Rules for naming variables/identifiers:
1. Must begin with a letter, or an underscore, or a $ sign.
2. May include digits.
3. Must not be a keyword.
4. They are case-sensitive.

Data Type defines the type of data that a variable can hold. There are 8 primitive data types in Java.

Data TypeSize in Bytes
byte1 byte
short2 bytes
int4 bytes
long8 bytes
float4 bytes
double8 bytes
char2 bytes
boolean1 byte (uses 1 bit)

Operators are the symbols used to perform calculations. Following are the various types of operators:
1) Arithmetic Operators:
+ for addition
– for subtraction
* for multiplication
/ for division
% for modulo division (remainder)
2) Relational Operators:
> means greater than
< means less than
>= means greater than or equal to
<= means less than or equal to
== means equal to
!= means not equal to
3) Increment and Decrement Operators:
++ means increment (increases the value of a variable by 1)
— means decrement (decreases the value of a variable by 1)
4) Logical Operators:
&& means Logical AND (evaluates to true when both the operands are true, otherwise evaluates to false)
|| means Logical OR (evaluates to true when at least one of the operands is true, otherwise evaluates to false)
! means Logical NOT (converts true to false and vice-versa)

Assignment Operator: The = sign is the assignment operator. It assigns values to variables.