P1. Write a program to perform all the arithmetic operations (i.e.
, Sum, Difference, Product, Quotient, Modulo) by
taking inputs from command line arguments.
import [Link];
public class P1 {
public static void main(String[] args) {
Scanner in = new
Scanner([Link]); int num1 =
[Link]();
int num2 = [Link]();
[Link](num1);
[Link](num2);
int sum = num1 + num2;
int difference = num1 -
num2; int product = num1 * num2;
int quotient = num1 / num2;
int modulo = num1 %
num2;
[Link]("Sum : " + sum);
[Link]("Difference : " +
difference); [Link]("Product : "
+ product);
[Link]("Quotient : " + quotient);
[Link]("Modulo : " + modulo);
}
}
P2. Write a program to show use of Switch Statement and Continue Statement.
import [Link];
public class P2 {
public static void main(String[] args) {
String Day;
Scanner in = new
Scanner([Link]); int value =
[Link]();
int id =
[Link](); switch
(value) {
case 1:
Day =
"Monday";
break;
case 2:
Day = "Tuesday";
break;
case 3:
Day = "Wednesday";
break;
case 4:
Day = "Thursday";
break;
case 5:
Day =
"Friday";
break;
case 6:
Day = "Saturday";
break;
case 7:
Day = "Sunday";
break;
default:
Day = "Invalid Input";
}
[Link](Day);
for(int i=1;i<=5;i+
+){ if(i==2)
{
continue;
}
[Link](i);
}
}
}
P3. Write a program to calculate the area of two different rectangles using class and objects. All basic initialization
and operations will be performed by creating a constructor and methods respectively.
import [Link];
class P3
{
int area,length,breadth;
P3()
{
length=10;
breadth=20;
}
P3(int l, int b)
{
length=l;
breadth=b;
}
void area()
{
area = length*breadth;
[Link](area);
}
public static void main(String args[])
{
P3 r1=new P3();
P3 r2=new P3(15,15);
[Link]("Area of rectangle with default constructor is:");
[Link]();
[Link]("Area of rectangle with parameterized constructor is:");
[Link]();
}
}
P4. Write a program to show how methods can return object and how methods can pass objects as argument using
class and object concept.
import [Link];
class ObjectPass
{ int a, b;
ObjectPass(int i, int j)
{
a=
i; b
= j;
}
boolean equalTo(ObjectPass op)
{
return (op.a == a && op.b == b);
}
}
public class P4 {
public static void main(String[] args) {
ObjectPass obj1 = new ObjectPass(100,
22); ObjectPass obj2 = new ObjectPass(100, 22);
ObjectPass obj3 = new ObjectPass(-1, -
1); ObjectPass obj4 = new ObjectPass(-
1, -1);
[Link]("obj1 == obj2 " +
[Link](obj2)); [Link]("obj1 == obj3 "
+ [Link](obj3)); [Link]("obj2 == obj4
}
}
" + [Link](obj4));
}
}
P5. Write a program to show code reusability by using Inheritance.
import [Link];
class
Car {
float Price=400000;
}
public class P5 extends Car{
float Insurance=80000;
public static void main(String args[])
{
P5 p=new P5();
[Link]("Cost of car is:"+[Link]); [Link]("\
nInsurance cost of car is:"+[Link]);
}
}
P6. Write a program to show string handling (Character Extraction and String
Comparison). import [Link];
public class P6 {
public static void main(String[] args) {
String str="Manipal
University"; char[] ch = new
char[10];
try{
[Link](1,5,ch,0);
[Link](ch);
}
catch(Exception ex)
{
[Link](ex);
}
String string1 = new
String("Manipal"); String string2 = new
String("University"); String string3 = new
String("Jaipur");
String string4 = new
String("Jaipur"); String string5 =
new String("jaipur");
[Link]("Compare " + string1 + " and " + string2 + ":" + [Link](string2));
[Link]("Compare " + string3 + " and " + string4 + ":" + [Link](string4));
[Link]("Compare " + string4 + " and " + string5 + ":" + [Link](string5));
[Link]("Compare " + string1 + " and " + string3 + ":" + [Link](string3));
}
}
[Link]("Compare " + string4 + " and " + string5 + ":" + [Link](string5));
}
}
P7. Write a program to show Streams basics in
java. import [Link].*;
import [Link].*;
public class P7 {
public static void main(String args[])
{
//list of integers Creation
List<Integer> number = [Link](10,23,11,8,6);
//list of String creation
List<String> names = [Link]("Manipal","University","Jaipur");
// list of integers creation
List<Integer> numbers = [Link](10,5,1,4,12);
// Print square values of number variable
List<Integer> squareSet = [Link]().map(x->x*x).collect([Link]());
[Link](squareSet);
// Print name that start with 'U' characters
List<String> result = [Link]().filter(s-
>[Link]("U")).collect([Link]()); [Link](result);
// Print list in sorted order
List<String> show = [Link]().sorted().collect([Link]());
[Link](show);
// Print square values of number list using forEach method
[Link]().map(x->x*x).forEach(y-
>[Link](y));
// Print total of odd values from number list
int odd = [Link]().filter(x->x%2!=0).reduce(0,(ans,i)-> ans+i);
[Link]("Total of odd values is:"+odd);
}
}
Protect pdf from copying with [Link]