Java Notes - Term 2 2022
Java Notes - Term 2 2022
It is used for:
Let’s create a basic Java program. Through this program, we will be able to
understand the difference in the syntax between C++ and Java.
class MyProgram
{
void main()
{
System.out.println("Hello World");
}
}
The above program explained:
Every line of the program in Java must be inside a class. In our example, we named the
class as MyProgram.
The main() method is required and you will see it in every Java program.
Inside the main() method, we can use the System.out.println() to print a line of text to
the output screen.
Most of the concepts of variables, data types and operators remain same in Java.
So, in this chapter, we will convert all our programs done in C++ to Java.
We will use BlueJ, which is an integrated development environment (IDE) for
the Java programming language, developed mainly for educational purposes, but also
suitable for small-scale software development. We will write our Java programs in BlueJ.
The following image shows a BlueJ window with the class MyProgram.
1
***********
2
CHAPTER 2: Data types used in Java
In Java Programming we have to deal with the various types of data types, hence
becomes necessary for the programmer to select appropriate data type according to the
data taken in the program.
Data types are divided into two categories
• Primitive Data type (Basic data/inbuilt data type)
• Non-Primitive Data type (Composite/ Derived Data type)
Different primitive and non-primitive data type data types used in Java are
Name Data type Size Default value
Boolean boolean 1 bit false
Byte byte 1 byte 0
Character char 2 bytes ‘\u0000’
Short short 2 bytes 0
Integer int 4 bytes 0
Long long 8 bytes 0L
Float float 4 bytes 0.0f/0.0F
Double double 8 bytes 0.0d/0.0D
String String No of characters null/ “ ”
* 2 bytes
int b=10;
System.out.println(a+b);
Output : 15;
Mixed expression : Expression with more than one data type is called as mixed expression
int a=5;
float b=10.0f;
System.out.println(a+b);
Output : 15.0;
3
Comment Lines: They are known to be Non executable statements in Java used to put
comments in your coded program for programmer reference. They are of 3 types
• // Single line Statement: The single-line comment is used to comment only one line of
the code. Any text in front of // is not executed by Java.
Example
It makes easy to maintain the code and to find the errors easily.
The comments can be used to provide information or explanation about the variable,
method, class, or any statement.
It can also be used to prevent the execution of program code while testing the alternative code.
4
CHAPTER 3:
Inputs in Java
This chapter we will learn various was to input data values in the program.
They are of three types.
1. By using Assignment statements.
2. By using Function Arguments.
3. By using Scanner class (will study in std 9)
2. Function Arguments: This is one of the methods to accept the value from the user at
the time of execution of the program. The values are to be input in method call window
must be provided as arguments in the main( ) function.
Example
Write a program to find the area of rectangle whose length is 3.5 cm and breadth is 5 cm.
public class area
{
static void main(float l, int b) //Input length & breadth of rectangle through arguments
{
float a;
a=l*b; // to calculate area of rectangle
System.out.println("Area of rectangle is = " + a);
}
}
Exercise
Convert c++ programs to Java programs
5
CHAPTER 4 : IF – ELSE - IF STATEMENT WITH LOGICAL
OPERATORS IN JAVA
Example 1
“If you have money and if your mother gives you permission then you can go and have
an ice cream”.
In the above statement, we understand that unless both the conditions are satisfied,
you cannot eat ice cream.
Example 2
If x is greater than y and x is greater than z, then x is the largest number amongst the
three numbers x, y and z.
In the above statement, we compare x with the other two numbers y and z and then
arrive at a conclusion that x is the largest.
In order to solve the above program, we require Logical operators, which help us to
combine two or more conditions together.
or || a= =b || b= =c
Program
public class hotel
{
static void main(int age,int money)
{
if(age>=15 && money>=300)
{
System.out.println("You an eat outside");
}
else
{
System.out.println("You cannot eat outside");
6
}
}
In the above program, only one condition is satisfied or true. Hence, the above output.
Exercise:
1.Write a program to enter a character and check if it is a vowel or a consonant.
2.Write a program to input a number and find out if it is single digit or a double
digit number. Display the output with proper messages.
3. Write a program to enter a character and check whether it is a capital letter or a small
letter.
IF -ELSE-IF STATEMENT
When you have to check for multiple conditions, then we use the if else-if statement in
C++.
Syntax
if (condition 1)
{
action to be taken if condition 1 is true;
}
else if (condition 2)
{
action to be taken if condition 2 is true;
}
else
{
action to be taken if both conditions 1 and 2 are false;
}
Multiple else –if statements is also called as IF -ELSE-IF LADDER.
Program To find the greatest among 3 numbers.
public class greatest
{
static void main(int a,int b,in c)
{
Exercise:
1. Write a program to accept three numbers and find the largest among them.
2. Write a program to accept three sides of a triangle and check if the triangle is
equilateral, isosceles or scalene.
3. Write a program to input a number and find out if it is single digit, double digit,
7
three digit or more than three digits. Display the output with proper messages.
4. Write a program in C++ to accept a number and check :
(a) whether it is divisible by 3 and 7.
(b) whether it is divisible by 3 but not by 7.
(c) whether it is divisible by 7 but not by 3.