0% found this document useful (0 votes)
31 views

Java Practical

The document contains 7 programming problems (P1 to P7) involving different Java concepts like arithmetic operations, switch statements, classes and objects, inheritance, string handling, and streams. P1 takes inputs and performs arithmetic operations. P2 demonstrates switch and continue statements. P3 calculates rectangle areas using classes. P4 shows object passing and returning. P5 demonstrates inheritance. P6 handles strings. P7 covers streams basics.

Uploaded by

Ranju Rajj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Java Practical

The document contains 7 programming problems (P1 to P7) involving different Java concepts like arithmetic operations, switch statements, classes and objects, inheritance, string handling, and streams. P1 takes inputs and performs arithmetic operations. P2 demonstrates switch and continue statements. P3 calculates rectangle areas using classes. P4 shows object passing and returning. P5 demonstrates inheritance. P6 handles strings. P7 covers streams basics.

Uploaded by

Ranju Rajj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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 java.util.Scanner;
public class P1 {
public static void main(String[] args) {
Scanner in = new
Scanner(System.in); int num1 =
in.nextInt();
int num2 = in.nextInt();

System.out.println(num1);
System.out.println(num2);

int sum = num1 + num2;


int difference = num1 -
num2; int product = num1 * num2;
int quotient = num1 / num2;
int modulo = num1 %
num2;

System.out.println("Sum : " + sum);


System.out.println("Difference : " +
difference); System.out.println("Product : "
+ product);
System.out.println("Quotient : " + quotient);
System.out.println("Modulo : " + modulo);
}
}

P2. Write a program to show use of Switch Statement and Continue Statement.
import java.util.Scanner;
public class P2 {
public static void main(String[] args) {
String Day;
Scanner in = new
Scanner(System.in); int value =
in.nextInt();
int id =

in.nextInt(); 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";
}

System.out.println(Day);

for(int i=1;i<=5;i+
+){ if(i==2)
{
continue;
}
System.out.println(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 java.util.Scanner;

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;
System.out.println(area);
}
public static void main(String args[])
{
P3 r1=new P3();
P3 r2=new P3(15,15);

System.out.println("Area of rectangle with default constructor is:");


r1.area();

System.out.println("Area of rectangle with parameterized constructor is:");


r2.area();
}
}

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 java.util.Scanner;

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

System.out.println("obj1 == obj2 " +


obj1.equalTo(obj2)); System.out.println("obj1 == obj3 "
+ obj1.equalTo(obj3)); System.out.println("obj2 == obj4

}
}
" + obj2.equalTo(obj4));

}
}
P5. Write a program to show code reusability by using Inheritance.
import java.util.Scanner;

class
Car {
float Price=400000;
}

public class P5 extends Car{


float Insurance=80000;
public static void main(String args[])
{
P5 p=new P5();
System.out.print("Cost of car is:"+p.Price); System.out.print("\
nInsurance cost of car is:"+p.Insurance);
}
}

P6. Write a program to show string handling (Character Extraction and String
Comparison). import java.util.Scanner;

public class P6 {

public static void main(String[] args) {


String str="Manipal
University"; char[] ch = new
char[10];
try{
str.getChars(1,5,ch,0);
System.out.println(ch);
}
catch(Exception ex)
{
System.out.println(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");

System.out.println("Compare " + string1 + " and " + string2 + ":" + string1.equalsIgnoreCase(string2));


System.out.println("Compare " + string3 + " and " + string4 + ":" + string3.equals(string4));
System.out.println("Compare " + string4 + " and " + string5 + ":" + string4.equalsIgnoreCase(string5));
System.out.println("Compare " + string1 + " and " + string3 + ":" + string1.equals(string3));
}
}
System.out.println("Compare " + string4 + " and " + string5 + ":" + string4.equals(string5));

}
}
P7. Write a program to show Streams basics in
java. import java.util.*;
import java.util.stream.*;

public class P7 {
public static void main(String args[])
{
//list of integers Creation
List<Integer> number = Arrays.asList(10,23,11,8,6);

//list of String creation


List<String> names = Arrays.asList("Manipal","University","Jaipur");

// list of integers creation


List<Integer> numbers = Arrays.asList(10,5,1,4,12);

// Print square values of number variable


List<Integer> squareSet = number.stream().map(x->x*x).collect(Collectors.toList());
System.out.println(squareSet);

// Print name that start with 'U' characters


List<String> result = names.stream().filter(s-
>s.startsWith("U")).collect(Collectors.toList()); System.out.println(result);

// Print list in sorted order


List<String> show = names.stream().sorted().collect(Collectors.toList());
System.out.println(show);

// Print square values of number list using forEach method


number.stream().map(x->x*x).forEach(y-
>System.out.println(y));

// Print total of odd values from number list


int odd = number.stream().filter(x->x%2!=0).reduce(0,(ans,i)-> ans+i);
System.out.println("Total of odd values is:"+odd);
}
}

Protect pdf from copying with Online-PDF-No-Copy.com

You might also like