0% found this document useful (0 votes)
23 views13 pages

Pratical OOPs Part 1

Uploaded by

linuxk859
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)
23 views13 pages

Pratical OOPs Part 1

Uploaded by

linuxk859
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
You are on page 1/ 13

ExperimentNo–1:WriteAProgramtodemonstrateallthe

data types with example.


publicclassDataTypesDemo{
publicstaticvoidmain(String[]args){ byte
byteValue = 127;
System.out.println("ByteValue:"+byteValue);

short shortValue = 32767;


System.out.println("ShortValue:"+shortValue);

int intValue = 2147483647;


System.out.println("IntValue:"+intValue);

long longValue = 9223372036854775807L;


System.out.println("LongValue:"+longValue);

float floatValue = 3.14f;


System.out.println("FloatValue:"+floatValue);

double doubleValue = 3.141592653589793;


System.out.println("DoubleValue:"+doubleValue);

boolean booleanValue = true;


System.out.println("BooleanValue:"+booleanValue);

charcharValue = 'A';
System.out.println("CharValue:"+charValue);
}
}
Output:
ByteValue:127
ShortValue:32767
IntValue:2147483647
LongValue:9223372036854775807
FloatValue:3.14
DoubleValue:3.141592653589793
Boolean Value: true
CharValue:A

ExperimentNo–2:Writeaprogramtodemonstratecontrol
flow statement. (if or if/else statement)
import java.util.Scanner;
publicclassLeapYearCheck{
publicstaticvoid main(String[]args){

Scannerscanner=newScanner(System.in);
System.out.print("Enter a year: ");
intyear=scanner.nextInt();
scanner.close();
if((year% 4 == 0&& year%100 != 0) ||(year % 400 ==0))
{
System.out.println(year+ "isaleap year.");
}
else
{
System.out.println(year+ "isnota leap year.");
}
}
}
Output:
Enter a year: 2016
2016isaleapyear.

Enter a year: 2005


2005isnotaleapyear.

ExperimentNo–
3:WriteaprogramtodemonstratetheScan
ner class in Java.
import java.util.Scanner;
publicclassScannerDemo
{
publicstaticvoidmain(String[]args)
{
//Create a Scanner object
Scannerscanner =new Scanner(System.in);

System.out.print("Enter your name: ");


String name = scanner.nextLine();
System.out.println("Hello,"+name+"!");

System.out.print("Enteryourage:"); int
age = scanner.nextInt();
System.out.println("Youare "+ age+" years old.");
System.out.print("Enter your height (in meters): ");
double height = scanner.nextDouble();
System.out.println("Youare"+height+"meterstall.");

System.out.println("Areyoumarried?(true/false):");
boolean married = scanner.nextBoolean();
System.out.println("Married: " + married);

scanner.close();
}
}

Output:
Enteryourname:John
Hello, John!
Enter your age: 30
Youare30yearsold.
Enteryourheight(inmeters):1.75 You
are 1.75 meters tall.
Areyoumarried?(true/false):true
Married: true

ExperimentNo–4:Writeaprogramtodemonstrate“Nested
If” conditional statement.
importjava.util.Scanner;
publicclassGreatestAmongThree{
public static void main(String[] args) {
Scannerscanner=newScanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Entersecondnumber:");
int num2 = scanner.nextInt();
System.out.print("Enter third number: ");
int num3 = scanner.nextInt();
if(num1 >= num2) {
if(num1 >= num3) {
System.out.println(num1+"isthe greatest.");
}
else{
System.out.println(num3+"isthe greatest.");
}
}
else{
if(num2 >= num3) {
System.out.println(num2+"isthe greatest.");
}
else{
System.out.println(num3+"is the greatest.");
}
}
scanner.close();
}
}

Output:
Enter first number: 10
Entersecondnumber:50
Enter third number: 20
50 is the greatest.
ExperimentNo–5:WriteaprogramtodemonstrateForloop
control statement.
publicclassForLoop {
publicstaticvoidmain(String[]args){
System.out.println("For Loop:");for
(int i = 1; i <= 5; i++) {
System.out.println(i); }
}
}

Output:
For Loop:
1
2
3
4
5

ExperimentNo–6:WriteaprogramtodemonstrateWhile
loop control statement.
publicclassWhileLoop {
public static void main(String[] args) {
System.out.println("\nWhileLoop:");
int j = 1;
while (j <= 5) {
System.out.println(j);
j++; }
}
}
Output:
While Loop:
1
2
3
4
5

ExperimentNo–
7:WriteaprogramtodemonstratedoWhile loop
control statement.
publicclassDoWhileLoop {
public static void main(String[] args) {
System.out.println("\nDo-WhileLoop:");
int k = 1;
do {
System.out.println(k);
k++;
} while (k <= 5);
}
}

Output:
Do-WhileLoop:
1
2
3
4
5
ExperimentNo–8:WriteaJavaprogramonAbstractClass.
abstractclassShape{
publicabstractvoid draw();
}
classCircleextendsShape{
@Override
public void draw() {
System.out.println("Drawingacircle.");
}
}
classRectangleextendsShape{
@Override
public void draw() {
System.out.println("Drawingarectangle.");
}
}
publicclassMain {
publicstaticvoidmain(String[]args){ Shape
circle = new Circle();
Shape rectangle = newRectangle();

circle.draw();
rectangle.draw();
}
}

Output:
Drawing a circle.
Drawing a rectangle.
ExperimentNo– 9:Write a programon condition( Even
- Odd ) Class.

importjava.util.Scanner;
publicclassOddEvenChecker{
public static void main(String[] args) {
Scannerscanner=newScanner(System.in);
System.out.print("Enter a number: ");
int num=scanner.nextInt();

if (num % 2 == 0) {
System.out.println(num+"iseven.");
}else{
System.out.println(num+ "isodd.");
}
scanner.close();
}
}

Output:
Enteranumber:10 10
is even.

Enteranumber:23 23
is odd.
ExperimentNo–
10:WriteaprogramusingCheckException/Ex
ceptionHandling.

importjava.util.Scanner;
importjava.util.InputMismatchException;

publicclassMain {
public static void main(String[] args) {
Scannerscanner=newScanner(System.in);

try{
System.out.print("Enter numerator: ");
int num = scanner.nextInt();
System.out.print("Enterdenominator:");
int den = scanner.nextInt();

int result = divide(num, den);


System.out.println("Result:"+result);
} catch (ArithmeticException e) {
System.out.println("Error:Cannotdividebyzero.");
}catch (InputMismatchExceptione) {
System.out.println("Error:Invalidinput.Pleaseenterintegers.");
}
scanner.close();
}
publicstaticintdivide(intnum,intden){ return
num / den;
}
}
Output:
Enternumerator:10
Enterdenominator:0
ERROR!
Error:Cannotdividebyzero.

Enter numerator: 50
Enterdenominator:abc
ERROR!
Error:Invalidinput.Pleaseenter integers.

ExperimentNo–11:Write aprogramusingInterfall.
interfaceAnimal{ void
sound();
}
classDogimplementsAnimal{ public
void sound() {
System.out.println("Dogbarks.");
}
}
classCatimplementsAnimal{ public
void sound() {
System.out.println("Catmeows.");
}
}
publicclassInterfaceExample{
publicstaticvoidmain(String[]args){ Animal
dog = new Dog();
Animalcat= new Cat();
dog.sound();
cat.sound();
}
}

Output:

Dog barks.
Catmeows.

You might also like