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

OOPJ Lab Week-3 Programs

OOPJ Lab week-3 programs

Uploaded by

Mohd Jahangir
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)
21 views

OOPJ Lab Week-3 Programs

OOPJ Lab week-3 programs

Uploaded by

Mohd Jahangir
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/ 4

OBJECT ORIENTED PROGRAMMING Through JAVA Lab

WEEK-3 Programs

1.Example program using Switch statement

class SwitchExample
{
public static void main(String args[])
{
int month = 4;
String season;
switch (month)
{
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autumn";
break;
default:
season = "Bogus Month";
}
System.out.println("April is in the " + season + ".");
}
}
2.Write Java program to find average of an array of values.

class Average
{
public static void main(String args[])
{
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double sum = 0,avg;
int i;
for(i=0; i<5; i++)
sum = sum + nums[i];
avg=sum/5;
System.out.println("Average is " + avg);
}
}

3.Write Java program to demonstrate type conversion.

class Conversion
{
public static void main(String args[])
{
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
4.Write Java program to print the following pattern.
..........
.........
........
.......
......
.....
....
...
..
.
class Nested
{
public static void main(String args[])
{
int i, j;
for(i=0; i<10; i++)
{
for(j=i; j<10; j++)
System.out.print(".");
System.out.println();
}
}
}

5. Write Java program to find volume of box.

class Box
{
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo
{
public static void main(String args[])
{
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
6.Write Java program to check whether a given number is palindrome or not.

class PalindromeNumber
{
public static void main(String args[])
{
int r,sum=0,temp;
int n=343;//It is the number variable to be checked for palindrome

temp=n;
while(n>0)
{
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}

You might also like