0% found this document useful (0 votes)
38 views11 pages

123 Java

The document contains various Java programs that demonstrate fundamental programming concepts such as printing messages, arithmetic operations, data types, control structures, and loops. Each program includes a brief description and outputs to illustrate the results of the code. Topics covered range from basic syntax to more complex operations like conditionals and loops.

Uploaded by

nandimeshva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views11 pages

123 Java

The document contains various Java programs that demonstrate fundamental programming concepts such as printing messages, arithmetic operations, data types, control structures, and loops. Each program includes a brief description and outputs to illustrate the results of the code. Topics covered range from basic syntax to more complex operations like conditionals and loops.

Uploaded by

nandimeshva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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");

}
}

Common questions

Powered by AI

Escape characters in Java are used to provide certain text formatting capabilities, such as inserting a new line (\n), adding tabs (\t), and embedding quotes ("example") within a string. They enhance readability and control the layout of output in console applications. For example, "\n class \t div" inserts a new line followed by a tab within the output .

Ternary operators in Java simplify and compact conditional assignments of values. They follow the format (condition) ? true_value : false_value, enabling inline condition checking. As shown in int marks=95; String x= (marks>90)?("A1"):"A2";, they reduce the need for more verbose if-else structures by directly assigning values based on a condition .

In Java, pre-increment (++x) increases the variable's value before it's used in an expression, while post-increment (x++) increases the value after its current use in an expression. For instance, if int a=25; System.out.println(++a); sets 'a' to 26 before outputting, compared to int x=3; int y=4 + x++; which uses the current value of 'x' (3) before incrementing it to 4 .

Boolean data types in Java, with only two possible values, true or false, are pivotal in controlling the flow of conditional statements. For example, a boolean variable isPass can be used to decide whether a student has passed an exam, executing different outputs based on its true or false state .

Proper scope management in Java ensures variables are accessible only within intended blocks, preventing unwanted access or interference. Block-scoped variables are defined within a particular block and cease to exist outside it. For instance, in a given block1, 'int n2 = 20;' is accessible, but once block1 ends, n2 isn’t accessible anymore outside, underlining disciplined memory and error management .

The print() method in Java prints text to the console without moving to a new line, allowing subsequent outputs to follow directly. In contrast, println() adds a newline after the text, which means subsequent outputs will appear on the next line .

Logical operators in Java allow for the evaluation of complex boolean expressions, supporting sophisticated decision-making. The operators include '&&' for logical AND, '||' for logical OR, '!' for logical NOT, and '^' for logical XOR. They enable compound condition testing within if statements, such as 'if A==0 && B==0', thus facilitating multi-condition evaluations for dynamic control flow .

Java's looping constructs—'for', 'while', and 'do-while'—provide diverse control flow mechanisms to repeatedly execute a block of code. The 'for' loop is ideal for counter-driven iterations, 'while' loops are suited for entries controlled loops based on boolean conditions, and 'do-while' loops ensure at least one iteration. Each caters to specific iteration needs, optimizing repetitive task executions .

Arithmetic operations in Java include addition, subtraction, multiplication, division, and modulus, enabling basic mathematical calculations. For example, given two numbers a=25 and b=3, the Sum is 28 (a+b), the Difference is 22 (a-b), the Product is 75 (a*b), the Quotient is 8 (a/b), and the Remainder is 1 (a%b).

Java offers several integer data types each requiring different memory sizes. A byte occupies 1 byte (8 bits), a short occupies 2 bytes (16 bits), an int requires 4 bytes (32 bits), and a long necessitates 8 bytes (64 bits). For instance, assigning these types like 'byte a=127; short b=555; int c=55555; long d=555555555L;' demonstrates Java's handling of different ranges with specific memory allocations .

You might also like