Chapter 7 -Java Basics Practical Manjoosha P
Write the programs in practical NB . Write the output after each pgm.
//Write a java pgm to print a msg( print & println diff )
class mus1
{
public static void main(String args[])
{
[Link]("Mus Eng Med School");
[Link]("Mus Eng Med School");
}
}
Output
// WJP to print your details using escape characters
class Name2
{
public static void main(String args[])
{
[Link]("mus eng med school\n class \t div");
[Link]("\"Welcome\"");
[Link]("This java is a \u2122");
}
}
//WJP to explain the arithmetic operations( add, sub , mul, Div(Quotient & remainder)
class ao3
{
public static void main(String args[])
{
int a=25,b=3;
[Link]("Sum is"+(a+b)); //S.o.p("Sum is"+a+b); output is 253
[Link]("Difference is"+(a-b));
[Link]("Product is"+(a*b));
[Link]("Quotient is"+(a/b));
[Link]("Remainder is"+(a%b));
}
}
class Sum_Avg4
{
public static void main(String arg[])
{
int a=5,b=2,c=10;
int sum=a+b+c;
double avg=sum/3;
[Link]("Sum is "+sum);
[Link]("Average is "+avg);
}
}
//WJP to print area of a rectangle
class area5
{
public static void main(String arg[])
{
int l=6,b=7,area;
area=l*b;
[Link]("area is"+area);
}
// wjp to find circumference of circle .(c=2*PI*r)
class circle6
{
public static void main (String args [])
{
double c,Pi=3.14 r=2f;
c = 2*Pi*r;
[Link] ("The circumference of circle is "+c);
}
}
//WJP TO FINDOUT THE COST OF PHONECALL AND THE BALANCE AMOUNT
class CallCost7
{
public static void main(String arg[])
{
double balance=100, rate=1.92,duration=37,cost;
cost=duration*rate;
balance=balance-cost;
[Link]("Call duration is "+duration+" seconds");
[Link]("Call Cost is "+ cost+" rupees");
[Link]("Balance is "+balance+" rupees");
}
}
//WJP to understand the Integer data types
//byte, short ,int,long
class Integer8
{
public static void main(String args[])
{
byte a=127; //1 byte(8 bits) of memory needed
short b=555; //2 byte(16 bits) of memory needed
int c=55555; //4 byte(32 bits) of memory needed
long d=555555555L; //8 byte(64 bits) of memory needed
[Link]("The value of a "+a);
[Link]("The value of b"+b);
[Link]("The value of c "+c);
[Link]("The value of d "+d);
}
}
// wjp to explain real value data type
class real9
{
public static void main(String args [])
{
double Pi=3.14;//8 bytes
float s=7.5f; // 4 bytes
[Link] ("The value of Pi is "+Pi);
[Link] ("The value of S is "+s);
}
}
//WJP for the demo of boolean data type
class booleandemo10
{
public static void main(String args[])
{
// booleandata types
boolean isPass=true;
if(isPass)
[Link]("Passed the Exam");
else
[Link]("Failed ");
}
}
//WJP to understand the character data type
class P11_Character
{
public static void main(String args[])
{
char grade='A'; //Within single quotes,2 bytes(16 bits) of memory needed
to store one character)
[Link](" Your grade is " +grade);
}
}
//WJP to understand the String data type(not a Primitive data type)
//String is the combination of characters
class P12_String
{
public static void main(String args[])
{
String name= "MUS ENGLISH MED SCHOOL"; //Within double quotes
[Link](" OUR SCHOOL is " +name);
[Link](" \"HEARTY CONGRATULATIONS\""); //Double Quote Character
[Link](" This String brought you by Java \u2122");
}
}
//WJP TO explain comments in [Link] ignores the comments in java
class P13_Comments
{
public static void main(String arg[])
{
[Link]("There are 3 types of comments in java");
[Link]("//Single line comment "); //Single line
comment
[Link](" /*Multy line comment*/");
/* more than
one line
comments */
[Link](" /** Documentation comment*/");
}
}
//WJP to explain the Incriment and Decrement operators(++ , --)
class indeop14
{
public static void main(String args[])
{
int a=25;
[Link]("The value of a++ is"+(++a));
int x=3;
int y=4 + x++;
[Link]("The value of y is"+y);
[Link]("The value of x is"+x);
}
//WJP to explain the Comparison operators(<, >, <= ,>=,==,!=) 6 comparison or relational operators
are there
class comp_op15
{
public static void main(String args[])
{
int a=25,b=15;
if(a<=b)
[Link]("a less than or equal to b");
if(a>=b)
[Link]("a greater than or equal to b");
if(a==b)
[Link]("a is equal to b");
if(a!=b)
[Link]("a not equal to b");
}
//WJP to explain the Logical operators(&&,||, !,^)
class logical_op16
{
public static void main(String args[])
{
int A=5,B=0;
if(A==0 && B==0)
[Link]("A and B are zero");
if(A==0 || B==0)
[Link]("A or B is zero");
if(A==0 ^ B==0)
[Link]("both are different");
boolean pass=true;
if(!pass)
[Link]("Failed");
}
}
// wjp to explain conditional operators(ternary operator)
class conditional17
{
public static void main(String args [])
{
int marks=95;
String x= (marks>90)?("A1"):("A2");
[Link](x) ;
int N=8;
int next=(N%2==0)?(N/2):(3*N+1);
[Link](next);
}
}
//WJP to explain the shorthand Assignment operators(=,+=,-=,/=,%=...)
class Assign_op18
{
public static void main(String args[])
{
int A=5;
A+=5; //A=A+5
[Link](A);
}
}
// Demonstrate block scope.
class P19_scope
{
public static void main(String args[])
{
int n1=10; // Visible in main (global variable)
block1: // start of block1
{
[Link](" Inside the block1");
int n2 = 20; // visible only to this block
[Link]("n1 and n2 : "+ n1 +""+ n2);
} // end of block1
// n1 is still visible here but n2 is not visible
[Link](" Outside the block1");
[Link]("n1 is " + n1);
//[Link]("n2 is " + n2); //ERROR
}
}
//WJP to check whether a person's age is eligible for voting or not using IF....ELSE
class p20_age
{
public static void main(String arg[])
{
int age=5;
if(age>=18)
[Link]("you r eligible for voting");
else
[Link]("you r not eligible for voting");
}
}
//Wjp to explain if else ladder
class P21_ifElseLadder {
public static void main(String[] args)
{
int marks = 76;
char grade;
if (marks >= 90) { grade = 'A'; }
else if (marks >= 80) { grade = 'B'; }
else if (marks >= 70) { grade = 'C'; }
else if (marks >= 60) { grade = 'D'; }
else { grade = 'F'; }
[Link]("Grade = " + grade);
}
}
//wjp FOR SWITCH
class P22_switchDemo
{
public static void main(String[] args)
{
int day=10;
switch (day)
{
case 1 :
[Link]("Sunday");
break;
case 2 :
[Link]("Monday");
break;
case 3 :
[Link]("Tuesday");
break;
case 4 :
[Link]("Wednesday");
break;
case 5 :
[Link]("Thursday");
break;
case 6 :
[Link]("Friday");
break;
case 7 :
[Link]("Saturday");
break;
default:
[Link]("Invalid Entry");
}
}
}
//wjp for printing the range of marks if grade is given as the input
class P23_switchchar
{
public static void main(String[] args)
{
char grade='B';
switch (grade)
{
case 'A' :
[Link]("Marks between 90 and 100");
break;
case 'B' :
[Link]("Marks between 80 and 90");
break;
case 'C' :
[Link]("Marks between 70 and 60");
break;
case 'D' :
[Link]("Marks between 60 and 50");
break;
default:
[Link]("Study Well");
}
}
//WJP to print 1 to 100 using for loop
class P24_forloop
{
public static void main(String[] args)
{
for(int i=1;i<=100;i++)
{
[Link](i);
}
}
}
//wjp to write the numbers from 501 to 600 using WHILE LOOP
class P25_whileLoop
{
public static void main(String[] args)
{ int i=501;
while(i<=600)//entry controlled loop
{
[Link](i);
i++;
}
}
}
//WJP to print numbers from 1 to 100 using do ...while loop
class P26_dowhileloop
{
public static void main(String arg[])
{
int i=1;
do
{
[Link](i++);
} while(i<=100); //exit controlled loop
}
}
//WJP to print even numbers from 1 to 100
class P27_evenNo
{
public static void main(String[] args)
{
for(int i=1;i<=100;i++)
{
if(i%2==0)
[Link](i);
}
}
}
//WJP to swap the numbers
class P28_swap
{
public static void main(String[] args)
{
int x=100,y=25;
int temp;//temporary var
temp=x;
x=y;
y=temp;
[Link]("the value of x is "+x);
[Link]("the value of y is "+y);
}
}
/*
WJP To check whether a number is a Prime or not
*/
class P29_prime
{
public static void main(String args[])
{
int i=2,n=7,c=0;
while(i<n)
{
if(n%i==0)
c++;
i++;
}
if(c==0)
[Link]("the no is prime");
else
[Link]("the no is not prime");
}
}