Java All : Expressions Statements and Formula Based Calculations
Concept Notes and Resources

Expressions, Statements, and Blocks #3752

Concept Learning Code Sheets

Volume of Cuboid #559

This is a java program in which we will find out the volume of a cuboid whose dimensions are given.

class VolumeCuboid
{
public static void main(String[] args)
{
int l=10;
int b=20;
int h=9;
long v=l*b*h;
System.out.println("The volume of the cuboid is "+v);
}
}

Triangle Area #567

This is a program to learn the use of compatability of data types with certain different type of values.

class TriangleArea
{
public static void main(String[] args)
{
int b=19;
int h=3;
double a=(b*h)/2;
System.out.println("The area of the triangle is :"+a+" unit sq");
}
}

Circle Area #3718

Finding the circle area with PI initialised to an unchangeable value.

class CircleArea
{
public static void main(String[] args)
{
final double PI=3.14;
int r=5;
double area=PI*r*r;
System.out.println("Area of circle with radius "+r+" = "+area);
}
}

Expression building for Unit conversion #3773

Building of expressions can be done for different units using different divisors.

class ExpUnitConversion
{
public static void main(String[] args)
{
double d,t,s;
d=1200;t=400;
s=d/t;
System.out.println("Speed (in m/s) = "+s);
s=(d/1000)/(t/3600);
System.out.println("Speed (in km/hr) = "+s);
}
}

Swapping two variables using temporary third variable. #3777

Swapping of value between two integer variables using the intermediate third variable.

class SwapTemp
{
public static void main(String[] args)
{
int a=10,b=20,temp;
System.out.println("Before swapping: a = "+a+" b = "+ b);
temp=a;
a=b;
b=temp;
System.out.println("After swapping : a = "+a+" b = "+b);
}
}

Swapping two variables without using third variable. #3780

Swapping of value between two integer variables without using the third variable.

class SwapWoTemp
{
public static void main(String[] args)
{
int a=10,b=20;
System.out.println("Before swapping: a = "+a+" b = "+ b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After swapping : a = "+a+" b = "+b);
}
}

Hypotenuse calculation using formulas with power and square root #3784

Creating formulas that contain powers and square root.

class PowRoot
{
public static void main(String[] args)
{
double b,h,hy ;
b=4.2;
h=3.3;
hy=Math.sqrt(Math.pow(b,2)+Math.pow(h,2));
hy=(int)(hy*100)/100.0;
System.out.println("RA triangle given base = "+b+" height = "+h);
System.out.println("Computed hypotenuse = "+hy);
}
}

Simple expression forming #3744

Forming expressions with simple variables.

class SimpleExp
{
public static void main(String[] args)
{
float a=1.3f,b=2.5f,c;
c=a+(a+b)/a*b;
System.out.println("Output c = "+c);
}
}

Pure and mixed expressions #3746

Forming expressions with operands of same data type and mixed data types.

class PureMixedExp
{
public static void main(String[] args)
{
int a=5,b=6;
int c=a+b-b%a;
double x=3.5,y=4.5;
double z=x+y*y;
double m=c+z/a;
System.out.println("Output from pure integer arithmetic expression = "+c);
System.out.println("Output from pure real arithmetic expression = "+z);
System.out.println("Output from mixed arithmetic expression = "+m);
}
}

Java Statement Types #3755

Demonstration of different types of java statements.

class AllStatements
{
public static void main(String[] args)
{
int x=1;
System.out.println("Output after declaration statement = "+x);
x=13;
System.out.println("Output after assignment statement = "+x);
x++;
System.out.println("Output after increment statement = "+x);
Character c=new Character('A');
System.out.println("Output after object creation statement = "+c);
boolean mi=Character.isDigit(c);
System.out.println("Output after method invocation statement = "+mi);
for(int i=0;i<2;i++) x++;
System.out.println("Output after flow control statement = "+x);
}
}

Blocks in Java #3770

Demonstration of use of blocks in java.

class Blocks
{
public static void main(String[] args)
{
boolean x=true;
//int a=0,b=0;
if(x)
{
int a=1;
int b=2;
System.out.println("a+b = "+(a+b));
}
//System.out.println("a+b = "+(a+b));
}
}
Solved Problems

Expression equivalent to math formula #3713

Write the java syntax for the following formula
x= 0.01 – ab^3 /

Average of three integers to float output #3716

Write a java program to calculate the average of three fixed integers 10,15 a

Diameter of a cylindrical pencil #3720

A thread of length 25 centimeters was wound around a cylindrical pencil. It w

Simple Interest #3723

Write a java program to calculate the amount to be received on a principal a

Time and Distance – Train catching problem #3726

A man walks at an average speed of 5 Km/Hour. The train station is at 6.5 Km

Expression evaluation problem #3728

Evaluate the following expression using the correct precedence and  other e

Statement Identification #3730

Each of the following lines are a different type of statement in Java. Identi

Exam Questions

Quizzes

Java Blocks Quiz #3786

Concept Notes:1  Code Sheets:11  Solved Problems:7  Exam Questons:7  Quizzes:3
×