0% found this document useful (0 votes)
13 views7 pages

Java Operator and Class Examples

The document contains multiple Java programs demonstrating the use of operators, including arithmetic, logical, and bitwise operations, as well as a program for solving quadratic equations and defining constructors. Each program is accompanied by its output, illustrating the results of the operations performed. Additionally, there is a program that defines a Circle class to calculate its circumference and area.

Uploaded by

cse4671
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)
13 views7 pages

Java Operator and Class Examples

The document contains multiple Java programs demonstrating the use of operators, including arithmetic, logical, and bitwise operations, as well as a program for solving quadratic equations and defining constructors. Each program is accompanied by its output, illustrating the results of the operations performed. Additionally, there is a program that defines a Circle class to calculate its circumference and area.

Uploaded by

cse4671
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

Operator.

java
/* Java Program to practice Operators*/
import [Link].*;
class Operator
{
public static void main(String args[])
{
int a=10;
[Link]("a++ is"+ a++);
[Link]("++a is"+ ++a);
[Link]("a-- is"+ a--);
[Link]("--a is"+ --a);
}
}
Output:
C:\JAVA>javac [Link]
C:\JAVA>java Operator
a++ is10
++a is12
a-- is12
--a is10
[Link]
/* Java Program to practice Operators*/
import [Link].*;
class Operator
{
public static void main(String args[])
{
int a=10;
int b=5;
[Link]("a+b is"+ a+b);
[Link]("(a+b) is"+ (a+b));

1
[Link]("(a-b) is"+ (a-b));
[Link]("a*b is"+ a*b);
[Link]("a/b is"+ a/b);
}
}
Output:
C:\JAVA>javac [Link]
C:\JAVA>java Operator
a+b is105
(a+b) is15
(a-b) is5
a*b is50
a/b is2
[Link]
/* Java Program to practice Operators*/
import [Link].*;
class Operator
{
public static void main(String args[])
{
boolean a=true;
boolean b=false;
[Link]("a is"+a);
[Link]("b is"+b);
[Link]("!a is"+!a);
[Link]("!b is"+!b);
}
}
Output:
C:\JAVA>javac [Link]
C:\JAVA>java Operator
a istrue

2
b isfalse
!a isfalse
!b istrue
[Link]
/* Java Program to practice bit shift Operators*/
import [Link].*;
class Operator
{
public static void main(String args[])
{
[Link](10<<2);
[Link](-10<<2);
[Link](10>>2);
[Link](-10>>2);
[Link](10>>>2);
[Link](-10>>>2);
[Link](20<<3);
[Link](-20<<3);
[Link](24>>3);
[Link](-32>>1);
[Link](10>>>1);
[Link](-10>>>24);
}
}
Output:
C:\JAVA>javac [Link]
C:\JAVA>java Operator
40
-40
2
-3
2

3
1073741821
160
-160
3
-16
5
255
[Link]
/* Java Program to find the roots of a Quadratic Equation. Calculate discriminate d
and based on the value of d, describe the nature of roots */
import [Link].*;
import [Link];
class Quadratic
{
public static void main(String args[])
{
double a,b,c,d;
double r1,r2;
Scanner scan=new Scanner([Link]);
[Link]("Enter value of a:");
a=[Link]();
[Link]("Enter value of b:");
b=[Link]();
[Link]("Enter value of c:");
c=[Link]();
d=b*b-4*a*c;
[Link]("Discriminate d is: "+d);
if(d<0)
{
[Link]("No real roots");
}
else if(d==0)

4
{
[Link]("Roots are real and Equal");
r1=r2=-b/(2*a);
[Link]("Roots are :"+r1+"and"+r2);
}
else
{
[Link]("Roots are Real and distinct");
double d1=[Link](d);
r1=-b+d1/(2*a);
r2=-b-d1/(2*a);
[Link]("Roots are :"+r1+"and"+r2);
}
}
}

Output:
C:\JAVA>javac [Link]

C:\JAVA>java Quadratic
Enter value of a:
2
Enter value of b:
6
Enter value of c:
4
Discriminate d is: 4.0
Roots are Real and distinct
Roots are :-5.5and-6.5
[Link]
/* Java Program to Define a Constructor*/
import [Link].*;

5
class Cons
{
Cons()
{
[Link]("In Constructor");
}
public static void main(String args[])
{
Cons c=new Cons();
}
}
Output:
C:\JAVA>javac [Link]
C:\JAVA>java Cons
In Constructor
[Link]
/* java Program to define a class and object. Access members using object*/
import [Link].*;
class Circle
{
double x;
double y;
double r;
double circumference()
{
return 2*3.14159*r;
}
double area()
{
return 3.14159*(r*r);
}
public static void main(String args[])

6
{
Circle c=new Circle();
c.x=0.0;
c.y=0.0;
c.r=5.0;
[Link]("Circumference is :"+[Link]());
[Link]("Area is :"+[Link]());
}
}
Output:
C:\JAVA>javac [Link]
C:\JAVA>java Circle
Circumference is :31.4159
Area is :78.53975

Common questions

Powered by AI

Operator precedence can lead to unintended results, especially in print statements. Without proper use of parentheses, arithmetic operations like addition can be overridden by concatenation, resulting in '105' instead of expecting arithmetic addition. Using parentheses as in '(a+b)' ensures correct evaluation prioritizing arithmetic over string concatenation .

Unexpected results may arise when arithmetic operations are intertwined with strings due to Java's left-to-right evaluation without parentheses. For instance, 'a+b' computed within 'System.out.println' might produce '105' due to string concatenation rule precedence, instead of '15'. To correct, use parentheses like '(a+b)' to prioritize arithmetic evaluation .

Directly accessing and modifying object attributes can impact methods that use those attributes in calculations. In the Circle class, setting 'c.r=5.0' directly updates the radius, affecting the results of methods like 'circumference()' and 'area()', producing outputs of 31.4159 and 78.53975 respectively .

In Java, the right shift operator (>>) maintains the sign of the number whereas the unsigned right shift operator (>>>) does not. For a negative number like -10, -10>>2 results in -3 (preserving the sign bit), whereas -10>>>2 results in a large positive value due to zero-fill, i.e., 1073741821 .

The left shift operation (<<) multiplies a number by 2 raised to the power of the specified shift amount, maintaining the sign of the number. For example, 10<<2 results in 40 (10*2^2), while -10<<2 results in -40. The operation just shifts the bit pattern to the left for both positive and negative numbers, filling with zeros .

The discriminant (d = b^2 - 4ac) of a quadratic equation indicates the nature of the roots. If d < 0, the equation has no real roots. If d = 0, the roots are real and equal. If d > 0, the roots are real and distinct. For example, when a = 2, b = 6, c = 4, d = 4 which is > 0, indicating real and distinct roots .

A constructor in Java is a block of code similar to a method that is invoked when an object of a class is created. It is used to initialize objects. For example, in the 'Cons' class, creating a new instance by 'new Cons()' calls the constructor and prints 'In Constructor' .

Post-increment (a++) and pre-increment (++a) operations in Java affect the value of a variable differently. With a post-increment, the original value is used in the expression before the increment occurs, resulting in 'a++ is 10'. In contrast, pre-increment increments the value first and then applies it, yielding '++a is 12' when initially a = 10 .

A common mistake is forgetting to use parentheses to control operation order. For instance, 'a+b' results in '105' instead of performing arithmetic addition because concatenation occurs first. Using parentheses as in '(a+b)' ensures the arithmetic calculation happens first, resulting in '15' .

The logical NOT operator (!) inverts the boolean value of a variable. When applied to a boolean 'a' which is true, the result is false, and similarly, when applied to 'b' which is false, the result is true. This shows the inversion effect: '!a is false' and '!b is true' .

You might also like