182196-1739335271-Grade VIII - Study Material - Java 2
182196-1739335271-Grade VIII - Study Material - Java 2
Study Material
1. An act of using essential features and hiding background details is called Data
Abstraction.
2. In an Object-Oriented Programming, the stress is given on data.
3. Wrapping of data and function together as a single unit is called Encapsulation.
4. The process by which a class acquires the property of another class is known as
inheritance.
5. In Object Oriented Programming, using a function for many purposes is termed as
polymorphism.
What is Java?
Java is a programming language and a platform. Java is a high level, robust, object-oriented
and secure programming language.
Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year
1995. James Gosling is known as the father of Java. Before Java, its name was Oak. Since
Oak was already a registered company, so James Gosling and his team changed the name
from Oak to Java.
Four features of Java are:
• As java uses compiler and interpreter both , first Source code will be converted into
byte code by Compiler and then byte code will be converted into machine code also
known as binary code by Interpreter (JVM- Java Virtual Machine)
Ch2 – Class & Object
Class- A class in Java is a set of objects which shares common characteristics/ behaviour and
common properties/ attributes. It is a user-defined blueprint or prototype from which objects
are created. For example, Student is a class while a particular student named Ravi is an
object.
Object- Objects are the instances of a class that are created to use the attributes and methods
of a class.
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
char 2 bytes
• 1byte =8 bits
Ch4- Operators in Java
What are the Java Operators?
Operators in Java are the symbols used for performing specific operations in Java. Operators
make tasks like addition, multiplication, etc which look easy although the implementation of
these tasks is quite complex.
Arithmetic Operators:
These operators involve the mathematical operators that can be used to perform various
simple or advanced arithmetic operations on the primitive data types referred to as the
operands. These operators consist of various unary and binary operators that can be applied
on a single or two operands. Let’s look at the various operators that Java has to provide under
the arithmetic operators.
Example:
22/10=2 (Integer Division)
22.0/10=2.2(Normal Division)
22%4= 2(Remainder)
Precedence of the operator:
Level1: *, /, % (Associativity- L->R)
Level1: +, - (Associativity- L->R)
Example:
6+3*5/2%2
6+15/2%2
6+7%2(Integer Division)
6+1
7
Relational Operators:
Operator 1: ‘Equal to’ operator (==)
Operator 2: ‘Not equal to’ Operator(!=)
Operator 3: ‘Greater than’ operator(>)
Operator 4: ‘Less than’ Operator(<)
Operator 5: Greater than or equal to (>=)
Operator 6: Less than or equal to (<=)
Logical Operators:
AND Operator ( && ) – if( a && b ) [if true execute else don’t]
OR Operator ( || ) – if( a || b) [if one of them is true to execute else don’t]
NOT Operator ( ! ) – !(a<b) [returns false if a is smaller than b]
Ch5- Input in Java
In Java, Scanner is a class in java.util package used for obtaining the input of the primitive
types like int, double, etc. and strings.
Using the Scanner class in Java is the easiest way to read input in a Java program, though not
very efficient if you want an input method for scenarios where time is a constraint like in
competitive programming.
Method Description
The java.lang.Math class contains methods for performing basic numeric operations such as the
elementary exponential, logarithm, square root, and trigonometric functions. This class provides
mathematical functions in Java.
Math.max()
The Math.max() method takes two parameters that can be of int, long, float, and double data types
and gives the largest of two.
Math.min()
Math.min() is an inbuilt method in Java which is used to return Minimum or Lowest value from
the given two arguments.
Math.sqrt()
In Java Math.sqrt() returns the square root of a value of passed to it as argument. It returns a double
value.
If the argument is NaN or negative, then the result is NaN.
Math.cbrt()
In Java Math.cbrt() returns the cube root of a value of type double passed to it as argument.
Math.abs()
Return the absolute (positive) value of a given number.
Math.pow()
The power function in Java is Math.pow(). It is used to get the power of the first argument to the
second argument. It takes two arguments and returns the value of the first argument raised to the
second argument. It returns a double type value.
The pow() function takes place in java.lang.Math.pow () library.
Program illustrating all Math functions:
import java.util.Scanner;
public class arithmetic
{
public static void main()
{
Scanner ob=new Scanner(System.in);
int a,b,c;
System.out.println("Enter two numbers");
a=ob.nextInt();
b=ob.nextInt();
c=a+b;
System.out.println("Addition="+c);
c=a-b;
System.out.println("Difference="+c);
c=a*b;
System.out.println("Product="+c);
c=a/b;
System.out.println("Division="+c);
}
}
2. Write a java program to display square and cube of a number accepted from
user.
import java.util.Scanner;
public class Sqrandcube
{
public static void main()
{ Scanner ob=new Scanner(System.in);
int n,s,c;
System.out.println("Enter a number");
n=ob.nextInt();
s=n*n;
c=n*n*n;
System.out.println("Square ="+s);
System.out.println("Cube ="+c);
}
}
3. Write a java program to accept radius from the user and display area and
circumference of the circle.
import java.util.Scanner;
public class circle
{
public static void main()
{
Scanner ob=new Scanner(System.in);
System.out.println("Enter radius of the circle");
int r;
double a,c;
r=ob.nextInt();
a= 3.14*r*r;
c=2*3.14*r;
System.out.println("Area= "+a+"\n Circumference="+c);
}
}
4. Write a java program to accept length and breadth from the user and display
area and perimeter of the rectangle.
import java.util.Scanner;
public class discount
{
public static void main()
{
double p,a,f;
Scanner ob= new Scanner(System.in);
System.out.println("Enter the price");
p=ob.nextDouble();
a= p-10.0/100*p;
f=a+6.0/100*a;
System.out.println("Final Amount= "+f);
}
}
6. Write a java program to accept the basic salary from the user. Calculate and
display net salary.
Hra =25% of Basic
Da= 20% of Basic
Ta =18% of basic
Net_salary=basic+hra+da+ta
import java.util.Scanner;
public class salary
{
public static void main()
{
double basic;
Scanner ob=new Scanner (System.in);
System.out.println("Enter Basic salary");
basic=ob.nextDouble();
double hra=25.0/100*basic;
double da=20.0/100*basic;
double ta=18.0/100*basic;
double net=basic+hra+da+ta;
System.out.println("Your Final Salary=" + net);
}
}
7. Write a java program to accept the side and radius the user. Calculate and
display the volume of the Cube and Sphere.
import java.util.Scanner;
public class volume
{
public static void main ()
{
double s, r, vc,vs;
Scanner ob = new Scanner(System.in);
System.out.println("Enter the side and radius");
s=ob.nextDouble();
r=ob.nextDouble();
vc=s*s*s;
vs=4.0/3*3.14*r*r*r;
System.out.println("Volume of Cube="+vc);
System.out.println("Volume of Sphere="+vs);
}
}
8. Write a java program to accept the salary from the user. Calculate and display
the savings.
Spending on food =1/2 of Salary
Spending on fuel =1/10 of Salary
Spending on other =1/15 of Salary
import java.util.Scanner;
public class savings
{
public static void main ()
{
double salary ;
Scanner ob = new Scanner(System.in);
System.out.println("Enter Your Salary");
salary=ob.nextDouble();
double food = 1.0/2*salary;
double rent=1.0/10* salary;
double other=1.0/15*salary;
double spending=food+rent+other;
double savings =salary-spending;
System.out.println("savings="+savings);
}
}
9. Write a java program to accept number of votes from the user. Calculate and
display votes received by Candidate X and Candidate Y.
80% of votes are registered, out of which 60% votes are received by candidate X.
import java.util.Scanner;
public class election
{
public static void main()
{
double totalvotes,x,y,regvotes;
Scanner ob =new Scanner(System.in);
System.out.println("Enter Total votes");
totalvotes=ob.nextDouble();
regvotes=80.0/100*totalvotes;
x=60.0/100*regvotes;
y=regvotes-x;
System.out.println("Number of votes received by Candidate X="+x);
System.out.println("Number of votes received by Candidate Y="+y);
}
}
10. Write a java program to accept number of seconds from the user. Calculate and
display it in the format- Hours Minutes and Seconds.
import java.util.Scanner;
public class seconds
{
public static void main()
{
int totsec,remsec,hr,min,sec;
Scanner ob =new Scanner(System.in);
System.out.println("Enter Total seconds");
totsec=ob.nextInt();
hr=totsec/3600;
remsec=totsec%3600;
min=remsec/60;
sec=remsec%60;
System.out.println("Final Time:"+hr+"hr"+min+"min"+sec+"sec");
}
}
• Programs on conditional Statements-
11. Write a program to accept an age and display user is eligible to vote
or not
import java.util.Scanner;
public class voter
{
public static void main()
{
int age;
Scanner ob = new Scanner(System.in);
System.out.println("Enter Your age: ");
age=ob.nextInt();
if(age>=18)
{
System.out.println("You are ELigible to vote");
}
else
{
System.out.println("You are not ELigible to vote");
}
}
}
import java.util.Scanner;
public class evenodd
{
public static void main()
{
int a;
Scanner ob = new Scanner(System.in);
System.out.println("Enter a number: ");
a=ob.nextInt();
if(a%2==0)
System.out.println("Number is Even");
else
System.out.println("Number is Odd");
}
}
13.Write a program to accept a number and display it is positive or
negative
import java.util.Scanner;
public class posneg
{
public static void main()
{
int a;
Scanner ob = new Scanner(System.in);
System.out.println("Enter a number: ");
a=ob.nextInt();
if(a>0)
System.out.println("Number is Positive");
else
System.out.println("Number is Negative");
}
}
Sample Questions
i. s = ut + (1/2)at2
s = u * t + (1.0 / 2) * a * t * t
ii. f = uv / (u + v)
f = u * v / (u + v)
iii. (a + b)2 + b
Math.pow(a+b,2) + b
if( condition)
-------(if block)
else
-------(else block – only one else block is allowed )
Logical Errors occur due to our mistakes in programming logic. It will not show
any error.