Ex No 1 Programming in Java Environment
Date
Aim:
To implement basic java programs to process data types, operators and expression
evaluation.
Program:
1. Consider Length and breadth of a rectangle are 12 and 7 respectively. Write a program
to calculate the area and perimeter of the rectangle.
Algorithm:
Code:
import [Link];
class Rectangle{
public static void main(String args[]){
int len,bre,rec;
[Link]("Enter length and Breadth :");
Scanner sc=new Scanner ([Link]);
len=[Link]();
bre=[Link]();
int area= len*bre;
int perimeter=2*(len+bre);
[Link]("The area is :"+area);
[Link]("The perimeter is :"+perimeter);
}
1
Output:
2. Write a program to calculate the perimeter of a triangle having sides of length 2, 4 and
5 units.
Algorithm:
Code:
import [Link];
class Rectangle
{
public static void main(String args[])
2
{
int len,bre,rec;
[Link]("Enter length and Breadth :");
Scanner sc=new Scanner ([Link]);
len=[Link]();
bre=[Link]();
int area= len*bre;
int perimeter=2*(len+bre);
[Link]("The area is :"+area);
[Link]("The perimeter is :"+perimeter);
}
}
Output:
3. Write a Java program to find simple Interest and Compound interest
Formula:
Simple Interest = (P × N × R)/100
Compound Interest=P(1+(R/100))T
(For example [Link](x, y) returns xy
Algorithm:
3
Code:
import [Link];
class SimpleCompound
{
public static void main(String args[])
{
int p,n,r;
[Link]("Enter P ,N and R value:");
Scanner sc=new Scanner([Link]);
p=[Link]();
n=[Link]();
r=[Link]();
int SI=(p*n*r)/100;
double CI=p*[Link](1+(r/100.0),n);
[Link]("The Simple Interest is :"+SI);
[Link]("The Compound Interest is :"+CI);
}
}
Output:
4. Write a program to add 8 to the number 2345 and then divide it by 3. Now, the modulus
of the quotient is taken with 5 and then multiply the resultant value by 5. Display the
final result.
Algorithm:
4
Code:
class Result
{
public static void main(String args[])
{
int temp=2345;
temp=temp+8;
[Link]("TEMP+2345:"+temp);
temp=temp/3;
[Link]("TEMP/3:"+temp);
temp=temp%5;
[Link]("TEMP%5:"+temp);
temp=temp*5;
[Link]("TEMP*5:"+temp);
}
}
Output:
5. If the marks of Robert in three subjects are 78,45 and 62 respectively (each out of 100
), write a program to calculate his total marks and percentage marks.
Algorithm:
5
Code:
public class Main {
public static void main(String[] args) {
int sub1 = 78;
int sub2 = 45;
int sub3 = 62;
int total = sub1 + sub2 + sub3;
double percentage = (total / 300.0) * 100;
[Link]("Total Marks = " + total);
[Link]("Percentage = " + percentage + "%");
}
}
Output:
6. Suppose the values of variables 'a' and 'b' are 6 and 8 respectively, write two programs
to swap the values of the two variables.
1 - first program by using a third variable
2 - second program without using any third variable
(Swapping means interchanging the values of the two variables E.g.- If entered value
of x is 5 and y is 10 then after swapping the value of x and y should become 10 and 5
respectively.)
6
Algorithm:
Code:
public class Main {
public static void main(String[] args) {
int a = 6;
int b = 8;
int temp;
temp = a;
a = b;
b = temp;
[Link]("After Swapping:");
[Link]("a = " + a);
[Link]("b = " + b);
}
}
Output:
7
PRACTICE PROBLEMS
Relevant to Data Types, Operators and Expression Evaluation
A. LeetCode Problems
1. Divide Two Integers [Topic: Operators - Arithmetic]
Description: Practice arithmetic operators and integer division.
Link: [Link]
Code:
class Solution {
public int divide(int dividend, int divisor) {
if (dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
long a = [Link]((long) dividend);
long b = [Link]((long) divisor);
int ans = 0;
while (a >= b) {
long t = b, m = 1;
while ((t << 1) <= a) {
t <<= 1;
m <<= 1;
}
a -= t;
ans += m;
}
return ((dividend < 0) ^ (divisor < 0)) ? -ans : ans;
}
}
8
Output:
2. Final Value of Variable After Performing Operations [Topic: Type Casting]
Description: Practice operator-based expression evaluation and type casting.
Link: [Link]
9
Code:
class Solution {
public int finalValueAfterOperations(String[] operations) {
int x = 0;
for (String op : operations) {
if ([Link](1) == '+') {
x++;
} else {
x--;
}
}
return x;
}
}
Output:
10
11
B. HackerRank Problems
1. Java Datatypes [Topic: Data Types - Primitive and Wrapper]
Description: Practice identifying and using primitive and wrapper data types.
Link: [Link]
Code:
import [Link].*;
class Solution{
public static void main(String []args){
Scanner sc = new Scanner ([Link]);
int t = [Link]();
for(int i = 0; i < t; i++){
try{
long x = [Link](); [Link](x + " can
be fitted in:");
if(x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE)
[Link]("* byte");
if(x >= Short.MIN_VALUE && x <= Short.MAX_VALUE)
[Link]("* short");
if(x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE)
[Link]("* int");
[Link]("* long");
}
catch(Exception e){
[Link]([Link]() + " can't be fitted anywhere.");
}
}
[Link]();
}
}
12
Output:
13
2. Java Output Formatting [Topic: Variables and Operators]
Description: Practice formatted output with variables and expression results.
Link: [Link]
Code:
import [Link].*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("================================");
for (int i = 0; i < 3; i++) {
String s = [Link]();
int x = [Link]();
[Link]("%-15s%03d%n", s, x);
}
[Link]("================================");
[Link]();
}
}
Output:
14
Result:
Thus, the implementation of basic java programs to process data types, operators and
expression evaluation.
Criteria Marks
Algorithm Preparation /20
Program Execution /25
Viva /10
Output & Result /20
Total /75
15