A. M.
NAIK SCHOOL (ICSE)
POWAI MUMBAI
Grade 8
Subject - Computer Studies
Ch 4 Fundamental Concepts of Java & Ch 5 - Control Structures
EXTRA NOTES - THEORY & PROGRAMS
A program consists of the following 4 steps
D - Declaring Variables . Example - int x,y;
I - Input/assign values to the variable. Example - x=10;y=20;
P - Processing/formula. Example - s=x+y;
O - OUTPUT - Example - [Link](“The sum is”+s);
Pls note the following instructions while writing the programs in the notebook:
1. Write the question in blue ink.
2. Write the program in black ink.
3. Start every program from a new page.
4. At the end of each program (containing variables) , a variable description table should
be made.(sample VD table has been given below in the first few programs)
5. The statements that are given below with the two front slashes (For Example - //Class
begins, //declaring variables) are called as comments. Comments can be used to
explain Java code, and to make it more readable. These are to be written in blue ink.
They are ignored by the compiler and not a part of the main code.
Q1] GENERAL OUTPUT PROGRAMS
1). WAP to display the given message :
“Welcome to Java”
“May God bless You”
public class Q1_gen
{//class begins
public static void main()
{//main begins
[Link]("Welcome to Java");
[Link]("May God bless you");
}//main ends
}//class ends
2) . Write a Program to print your name , class, and division.
public class Q2
{
public static void main ()
{
[Link]("Name: RADHE GAMI");
[Link]("Class: 8");
[Link]("Division: A");
}
}
3) Write a program to display your Details following format:
Name Class Division
Radhe 8 A
public class Q3
{
public static void main()
{
[Link]("NAME CLASS DIVISION [Link]");
[Link]("RADHE 8 A 2 ");
}
}
Q2]ASSIGNING VALUES TO VARIABLES
1). WAP to assign the name - Radhe, class - 8 and division – A and total marks –
375.5 and print the same.
public class Q1_assign
{ //class begins
public static void main()
{ // main begins
//Declaring variables.
String name;
int std;
char div;
double total;
//Input or assigning value to variables
name="RADHE";
std=8;
div='A';
total=455.0;
//output on the screen
[Link]("Student name is "+name);
[Link]("Student standard"+std);
[Link]("student division"+div);
[Link]("student total marks"+total);
} // main ends
} //class ends
2) WAP to find the sum , product, quotient and remainder when a=50 and b=10.
public class Q2_assign
{
public static void main()
{
//declaring variables
int a,b,sum,pro,quot,rem;
//Input or assigning value to variables
a=50;
b=10;
//Processing or formula.
sum=a+b;
pro=a*b;
quot=a/b; //%-modulus gives the remainder
rem=a%b;
[Link]("The sum is "+sum);
[Link]("The product is "+pro);
[Link]("The quotient is "+quot);
[Link]("The remainder is "+rem);
}
}
VARIABLE DESCRIPTION
Name Datatype Use
a int stores the first no.
b int stores the second no.
sum int stores the sum
pro int stores the product
quot int stores the quotient
rem int stores the remainder
3). WAP to calculate the DA,HRA,CTA and gross salary of an employee whose basic salary is
20000.
DA = 20% of basic
HRA = 10% of basic
CTA = 12% of basic
Gross Salary = basic + CTA + HRA+DA
public class Q3
{// class begins
public static void main ()
{//main begins
double ba,da,hra,cta,gs; //DATA MEMBERS
ba=20000.0;
da=20.0/100.0*ba;
hra=10.0/100.0*ba;
cta=12.0/100.0*ba;
gs=ba+da+hra+cta;
[Link]("The daily allowance is "+da);
[Link]("The home rent allowance is "+hra);
[Link]("The communication and travel allowance is "+cta);
[Link]("The gross salary is "+gs);
}//main ends
}//class ends
4) WAP to calculate the area , perimeter of a rectangle where l=24 and b = 25. [Hint:- a = l*b ,
p = 2*(l+b)
public class Q4_assign
{//class begins
public static void main()
{//main begins
// 1- declaring variables
double l,b,a,p;
// 2- assigning values to variable
l=24.0;
b=25.0;
//3- processing/formula
a=l*b;
p=2*(l+b);
//4- output
[Link]("the area of the rectangle is "+a);
[Link]("the perimeter of the rectangle is "+p);
}// main ends
}//class ends
VARIABLE DESCRIPTION
NAME DATA TYPE USE
l double to store the length of the rectangle
b double to store the breadth of the rectangle
a double to store the area of the rectangle
p double to store the perimeter of the rectangle
Q3] BLUEJ METHOD/FUNCTION ARGUMENT METHOD OF INPUT
1) WAP to input the name, class, division and total marks and print the same
public class Q1_bj
{
public static void main(String name,int std,char div,double total)
{
[Link]("NAME STD DIVISION TOTAL MARKS");
[Link](name + " " + std + " "+div+ " "+total);
}
}
2). WAP to input two numbers a and b. Find the sum , difference, product, quotient and
remainder .
public class Q2_bj
{
public static void main(int a,int b)
{
int sum,dif,pro,quo,rem;
sum=a+b;
dif=a-b;
pro=a*b;
quo=a/b;
rem=a%b;
[Link]("The sum is "+sum);
[Link]("The difference is "+dif);
[Link]("The product is "+pro);
[Link]("The quotient is "+quo);
[Link]("The remainder is "+rem);
}
}
3) WAP to calculate the DA, HRA, CTA and gross salary of an employee. Input the basic
salary.
DA = 20% of basic,
HRA = 10% of basic,
CTA = 12% of basic,
Gross Salary = basic + CTA + HRA+DA.
public class Q3_bj
{
public static void main(double bs)
{
double da,hra,cta,gs;
da=20.0/100.0*bs;
hra=10.0/100.0*bs;
cta=12.0/100.0*bs;
gs=da+hra+cta+bs;
[Link]("The daily allowance is "+da);
[Link]("The home and rent allowance "+hra);
[Link]("The communication and travel allowance is "+cta);
[Link]("The gross salary is "+gs);
}
}
4).WAP to calculate the area , perimeter of a rectangle .Input the length and breadth.[a = l*b,p
= 2*(l+b)]
public class Q4_bj
{
public static void main(int l, int b)
{
double a,p;
a=l*b;
p=2*(l+b);
[Link]("The area of the rectangle is "+a);
[Link]("The perimeter of the rectangle is "+p);
}
}
4) IF Else Condition Programs.
1. Input a number and print whether it is positive or negative.
public class Q1_IF
{//class begins
public static void main(int a)
{//main begins
if(a>0)
{//if begins
[Link]("The number is positive");
}//if ends
else
{//else begins
[Link]("The number is negative");
}//else ends
}//main ends
}//class ends
VARIABLE DESCRIPTION
NAMEDATA TYPE USE
a int stores the number
2. Input a number and print whether it is even or odd.
public class Q2_IF
{
public static void main(int n)
{
if(n%2 == 0)
{
[Link]("The number is even");
}
else
{
[Link]("The number is odd");
}
} }
3. Input degree of angle and print acute, obtuse or right.
public class Q3_IF
{
public static void main(int d)
{
if(d<90)
{
[Link]("The angle is acute");
}
else if (d==90)
{
[Link]("It is a right angle");
}
else if (d >90)
{
[Link]("The angle is obtuse");
} } }
4. Input year and pint whether Leap year or not.
public class Q5_IF
{
public static void main(int year)
{
if(year%4==0) {
[Link]("The year is a leap year. ");
}
else
{
[Link]("The year is not a leap year.");
}
}
}
[Link] 3 sides of the triangle and print whether it is equilateral, scalene or isosceles.
public class Q6_IF
{
public static void main(int s1,int s2,int s3)
{
if((s1==s2)&&(s2==s3)&&(s3==s1))
{
[Link]("The triangle is an equilateral triangle.");
}
else if ((s1==s2)||(s2==s3)||(s3==s1))
{
[Link]("The triangle is an isosceles triangle.");
}
else
{
[Link]("the triangle is a scalene triangle.");
} }}
6. Input angles and print whether they are complementary or supplementary.
public class Q7_IF
{
public static void main(int a1,int a2)
{
if(a1+a2==90)
{
[Link]("The angles are complementary.");
}
else if(a1+a2==180)
{
[Link]("The angles are supplementary.");
}
else
{
[Link]("The angles are neither complementary nor supplementary.");
} }}
THEORY NOTES
Given below is the summary of Ch 4 and Ch 5 . Kindly ensure to read the
textbook thoroughly for detailed explanation.
● PROGRAM - Set of instructions given to the comp in order to do a task.
● SOURCE CODE - Human readable code
● OBJECT CODE- Machine code /binary code - code that computer understands
● JAVA HISTORY - James Gosling . [Link] java.
● CLASS - Blueprint or a template that has data and functions.
● OBJECT - A unique entity that contains similar data and functions
● DATA - Characteristics/state/variables/attribute/Data Member
● FUNCTION - Behaviour/ Method
● BlueJ - Integrated Development Environment.
FEATURES OF OOP
1. ENCAPSULATION- WRAPPING OF DATA AND FUNCTIONS IN A SINGLE UNIT
2. ABSTRACTION- ONLY SHOWING NECESSARY DETAILS AND HIDING THE
BACKGROUND DETAILS
3. INHERITANCE- ONE CLASS ACQUIRES PROPERTIES FROM ANOTHER CLASS
PARENT-CHILD , BASE CLASS-DERIVED CLASS
4. POLYMORPHISM ONE OBJECT HAS MANY FORMS.
FUNDAMENTALS OF JAVA
1. TOKENS – smallest meaningful element of a java program. Public, class, { , ; , 10, x , int
2. TYPES OF TOKENS – KILOPS
K - Keywords –words with special meaning .reserved by java. Cannot use keyword to name
a class, variable or an object.
Example - public,class,void, int,etc
I - Identifiers / Variables/attributes- the name given to a class, a variable or a function is
called as an identifier.
Example - public class Q1, String name, int x, main()
L – Literals/Constants- value given to a variable . name = “Anil” , std =8, div=’D’
O – Operators – symbols used for mathematical calculations.
Types of Operators:
i. Arithmetic operators + , - , *, / , % ,
ii. Relational operators - give the relationship between two [Link] true or false.
<, >, <=,>=, == ,== !=
Example 5==7 → false 5==5→true 5!=7 → true 5!=5→false 7<=4 → false 5> 7→false
iii. UNARY OP -> work on single operand.
a++, y- - ,
● int a =5;
a++; means a=a+1;
a=5+1=6
● int x=7;
x-- ; Means x=x-1; 7-1=6
int y=20;
Y++;
Means y=y+1 means 20+1=21
iv. LOGICAL OP -> &&(AND) , ||(OR) , !(NOT)
Used to join two conditions
&& → will return true only when both the conditions are true.
|| -> will return true when at least one of the conditions is true.
● (25>=17) && (4>=15)
T && F
F
● (25>=17) || (4>=15)
T || F
T
V. ASSIGNMENT OPERATOR : Assigns the value
int a=10;int b=20;
x= a+b;
Assigns the total of values stored in a and b and stores the total in the variable x;
SEPARATORS{ } ( ) [ ] and PUNCTUATORS ;.,
● DATA TYPES - Data types are defined as data storage format that a variable can store
to perform a specific task.
TYPE Data type Example
Integers int int x; x=-45;
Decimals float float x=45.89f;
double double x=-98.876;
Character char char x=’A’; A-Z,a-z,0-9,sp
Alphanumeric String String email = ”abc12@[Link]”
Boolean boolean true or false
● VARIABLE INITIALIZATION
Process of assigning value to variable
x; x=-45;
x=45.89f;
x=-98.876;
x=’A’;
● PRECEDENCE OF OPERATOR
Order in which an expression is evaluated.
B - () bracket
U- ++, – a=9; a++;
E - ^ exponent
DMM - / * % L→R means x=5*4/2; 20/2=10
AS - + - L→R means x=5-1+2; 4+2=2
******************