JAVA
1. Working:
Coding -> java compiler ->bytecode > jvm ->
Machine readable -> execute
2. Simple example:
Class hello {
Public static void main(String args[])
{
[Link](“hello world”);
}
}
3. Variables and Datatype
Variables are container that helps you to store
data
Example
class hello{
public static void main(String args[])
{
int container = 67;
String container2 = "j";
[Link](container);
[Link](container2);
}
JAVA
}
Data type – The specification of the container
For example 67 is an integer so it is declared as int
And j is alphabet so it is declared as string and enclosed
with double quotes
Example 2:
class hello{
public static void main(String args[])
{
int a = 67;
int b = 55;
int c = 80;
int d = a+b+c;
[Link](d);
}
}
User input
1. Java program uses the “[Link]” package implicitly
2. The system class within “[Link]” provides access to
system resource
3. “[Link]” is a part of the “system” class and
represents the standard output stream, usually
connected to the terminal
JAVA
4. By using ‘[Link]()’, you print text to the
terminal.
5. Package- import [Link]
How to get user input
1. Sending data from code to terminal – system
2. Sending data from terminal to code - Scanner
Scanner package
1. Package- import [Link]
Example
import [Link];
class hello{
public static void main(String args[])
{
Scanner jemi = new Scanner([Link]);
int a= [Link]();
int b= [Link]();
[Link](a+b);
}
}
String
import [Link];
class hello{
public static void main(String args[])
{
Scanner jemi = new Scanner([Link]);
String a= [Link]();
JAVA
[Link](a);
}
}
Practice Session question
1. Get input for variable : Name and Age.(print it)
2. Get input for variable : Name ,Age and Address.(print
it)
3. Get input for 3 integer variables: a, b and c. Now
multiply all variables and store in in d. Now add all
variable and store it in e. Now divide d by e
4. Get input for 3 variables: Name, score, department .Let
the user enter the score for 100 marks, you convert it
for 10 and print it.
Answer
1.
import [Link];
public class practise {
public static void main(String args[])
{
Scanner jemi =new Scanner([Link]);
String name =[Link]();
int age = [Link]();
[Link]("name:"+ name);
[Link]("age:" +age);
}
JAVA
}
2.
import [Link];
public class practise {
public static void main(String args[])
{
Scanner jemi =new Scanner([Link]);
String name =[Link]();
int age = [Link]();
[Link]();
String address = [Link]();
[Link]("name:"+ name);
[Link]("age:" +age);
[Link]("address:"+ address);
3.
import [Link];
public class practise {
public static void main(String args[])
{
Scanner scan =new Scanner([Link]);
int a = [Link]();
int b = [Link]();
int c = [Link]();
int d =a*b*c;
int e =a+b+c+d;
int f =d/e;
[Link](d);
[Link](e);
[Link](f);
JAVA
}
4.
import [Link];
public class practise {
public static void main(String args[])
{
Scanner scan =new Scanner([Link]);
String name = [Link]();
int score = [Link]();
[Link]();
String department = [Link]();
[Link]("name:"+name);
[Link]("score:"+ score/10 +"/10");
[Link]("department:"+department);
Datatypes
1. datatypes are classified into two types they are
primitive data type
reference/data object type
Primitive data type
byte
short
JAVA
int
long
float
double
Boolean
Char
Bytes-It can store number from -128 to 127
Float-it is stored as float a=5.23f
Char-it takes only one value and it should be enclosed in
single quotes.
public class practise {
public static void main(String args[])
{
String name = "jemi";
[Link]([Link](0));
charAt is used to print the output of the string from the
given index
Boolean-true, false
IF-ELSE
public class practise {
public static void main(String args[])
{
boolean rain=true;
if(rain){
JAVA
[Link]("take an umberella");
}
else{
[Link]("enjoy the sunlight");
}
The Boolean value is true it print the statement inside if
condition
The Boolean value is false it print the statement inside
else condition
COMPARISION OPERATOR
Example1:
To compare whether the number is greater than or not
public class practise {
public static void main(String args[])
{
int num1=30;
int num2=70;
if(num1>num2){
[Link]("number1 is greater");
}
else{
[Link]("number2 is greater");
}
}
JAVA
Example2:
To compare whether the two numbers are equal or not
public class practise {
public static void main(String args[])
{
int num1=20;
int num2=20;
if(num1==num2){
[Link]("The numbers are equal");
}
else{
[Link]("The numbers are not equal");
}
Example 3:
To get the number from the user and print whether the
two number is equal or not.
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int num1=[Link]();
int num2=[Link]();
if(num1==num2){
[Link]("The numbers are equal");
}
else{
[Link]("The numbers are not equal");
}
}
JAVA
}
STRING COMPARISION
Stack -> integer
Heap -> object
String pool ->string
(It indicates the reference of the string if two string is
compared not the value of the string)
To compare the value of two strings we can use the
following syntax
([Link](b))
Example1:
public class practise {
public static void main(String args[])
{
String a="one";
String b="one";
String c=b;
[Link](a==c);
Example2:
public class practise {
public static void main(String args[])
{
String a="one";
String b="one";
JAVA
String c=b;
[Link](a==c);
Exercise
1. Get an input from user, for the variable called RCB.
If RCB == Win print(“ee sala cup namdhey”)
If RCB == loose print(“cup illa”)
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
String Rcb=[Link]();
if([Link]("win")){
[Link]("Ee Sala Cup Namde");
}
else{
[Link]("cup illa");
}
2. Get an input from user, for the variable called
meghna.
If meghna is death print(“Surya meets ramya”)
JAVA
else
print(“Surya weds meghna”)
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
String meghna=[Link]();
if([Link]("dead")){
[Link]("Surya meets ramya");
}
else{
[Link]("Surya weds meghna");
}
3. Get an input from user, for the variable called
mark.
If mark>35 print(“pass”)
Else
Print(“fail”)
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int mark =[Link]();
if(mark>=35){
[Link]("pass");
}
JAVA
else{
[Link]("fail");
}
4. Get an input from user, for the variable called
scholarship.
If scholarship>7000 print(“scholarship available”)
Else
Print(“scholarship not available”)
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int income =[Link]();
if(income>7000){
[Link]("scholarship is available");
}
else{
[Link]("scholarship not available");
}
}
JAVA
LOGICAL OPERATORS
Logical operators are symbol that
connect two or more expression of Boolean type
(i.e.,true or false) and return a Boolean value as a
result
And-> && (two condition must be true)
OR->|| (one condition true then the statement is true)
Example1:
public class practise {
public static void main(String args[])
{
// cricket or football =play
// true or true = true(play)
// true or false= true(play)
// false or true =true(play)
// false or false =false(don't play)
boolean cricket=false;
boolean football=false;
if(cricket||football){
[Link]("play");
}
else{
[Link]("don't play");
}
}
JAVA
Exercise 1
Get input for a number and check whether it is divisible
by both 3 and 5 or not. If yes then print, the number is
divisible by 3 and 5 print the number is not divisible by 3
and 5.
How to find whether a number is divisible by 3 or not?
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int num=[Link]();
if(num%3==0 && num%5==0){
[Link]("the number is divisible by 3 and 5");
}
else{
[Link]("the number not divisible by 3 and 5");
}
}
[Link] input for a number and find it is even or odd
number
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int num=[Link]();
if(num%2==0 ){
[Link]("The number is even");
}
JAVA
else{
[Link]("The number is odd");
}
}
ELSE IF AND NESTED IF
ELSE IF
public class practise {
public static void main(String args[])
{
int mark=80;
if(mark>35 && mark<60){
[Link]("videogame");
}
else if(mark>60 && mark<90){
[Link]("iphone");
}
else if(mark>90){
[Link]("macbook pro");
}
}
NESTED IF
public class practise {
public static void main(String args[])
{
boolean kfc=true;
boolean chicken=true;
boolean pepsi=true;
if(kfc){
[Link]("enter into kfc");
JAVA
if(chicken){
[Link]("eat chicken");
if(pepsi){
[Link]("drink pepsi");
}
}
}
Exercise1:
1. What is the score in the game?
-if the score is less than 50, print ” you need to
improve.”
-if the score is between 50 and
70(inclusive),print”good job!”
-if the score is greater than 70, print “excellent
performance!”
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
int score=[Link]();
if(score<50){
[Link]("you need to improve");
}
else if(score>=50 && score<=70){
[Link]("good job!");
}
else if(score>70){
[Link]("Excellent performance!");
}
JAVA
2. get input for five subjects marks. Add all of it, and
find average. if average mark is less than 35. Print”
additional class is required” else print ”you are good
to go
3. import [Link];
4. public class practise {
5. public static void main(String args[])
6. {
7. Scanner sc=new Scanner([Link]);
8. int s1=[Link]();
9. int s2=[Link]();
10. int s3=[Link]();
11. int s4=[Link]();
12. int s5=[Link]();
13. int mark=s1+s2+s3+s4+s5;
14. int average=mark/5;
15. if(average<35){
16. [Link]("additional class is required");
17. }
18. else{
19. [Link]("you are good to go");
20. }
21. }
22.
23.
24.
25. }
26.
3. What is the color of the traffic light?
JAVA
-if the answer is “red”, print “stop”
-if the answer is “yellow”, print “get ready”
-if the answer is “green”, print “go”
import [Link];
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
String color=[Link]();
if([Link]("red")){
[Link]("stop");
}
else if([Link]("yellow")){
[Link]("get ready");
}
else if([Link]("green")){
[Link]("go");
}
}
4. Get input for salary and age
If salary greater than or equal to 20000 or age less
than or equal to 25,
Get input for required loan amount. If not print you
are not eligible for loan,
If required loan amount is less than or equal to
50,000 print you are eligible for loan, if it is greater
than 50,000 print maximum loan amount is 50000
import [Link];
JAVA
public class practise {
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("enter your salary:");
int salary=[Link]();
[Link]("enter your age:");
int age=[Link]();
if(salary>=20000 || age<=25){
[Link]("eligible");
[Link]("enter your required loan amount");
int loan=[Link]();
if(loan<=50000){
[Link]("eligible for loan");
}
else{
[Link]("maximum loan amount is 50000");
}
}
else{
[Link]("you are not eligible");
}
}
TERNARY OPERATOR
The ternary operator in java is the short hand for if else
statement and it is used to assign a value to a variable
based on the condition. It is called ternary because it
involves three parts
1. Condition: An expression that evaluates to true or
false.
JAVA
2. First value(if true): The value is assigned if the
condition is true
3. Second value(if false): The value is assigned if the
condition is false
import [Link];
public class practise {
public static void main(String args[])
{
String result=5>2?"yes":"no";
[Link](result);
4. Syntax:
Variable= (condition)? Value if True: Value if False;
import [Link];
public class practise {
public static void main(String args[])
{
boolean rain=false;
String result=rain?"bring umberella":"enjoy the sunlight";
[Link](result);
Example 1:
JAVA
1. Get input for two integer and find which number is
greater using ternary operator.
2. import [Link];
3. public class practise {
4. public static void main(String args[])
5. {
6. Scanner sc=new Scanner([Link]);
7. int n1=[Link]();
8. int n2=[Link]();
9. String result=n1>n2?"n1 is greater":"n2 is greater";
10. [Link](result);
11.
12.
13. }
14.
15.
16.
17. }
18.
FOR LOOP
A for loop is used to repeatedly execute a block of
statements for a specific number of times
JAVA