[Go to site: main page, start]

0% found this document useful (0 votes)
3 views3 pages

Java Program - Part2

The document contains six Java programs demonstrating various programming concepts. These include polymorphism for printing different lines, string manipulation for converting cases and finding lengths, constructor overloading for calculating rectangle areas, summing array elements, using conditional operators to find maximum values, and checking if a number is prime. Each program is accompanied by its expected output.

Uploaded by

jagatiyahirva123
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)
3 views3 pages

Java Program - Part2

The document contains six Java programs demonstrating various programming concepts. These include polymorphism for printing different lines, string manipulation for converting cases and finding lengths, constructor overloading for calculating rectangle areas, summing array elements, using conditional operators to find maximum values, and checking if a number is prime. Each program is accompanied by its expected output.

Uploaded by

jagatiyahirva123
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

// Program 1 : To print different line, using Polymorphism

class PrintLine
{ Output 1:
static void Print()
++++++++++
{
for(int i=1;i<=10;i++) ##########
[Link]("+"); $$$$$$$$$$
[Link]();
}
static void Print(int n)
{
for(int i=1;i<=n;i++)
[Link]("#");
[Link]();
}
static void Print(char ch,int n)
{
for(int i=1;i<=n;i++)
[Link](ch);
[Link]();
}
}
public class polyDemo
{
public static void main(String args[])
{
[Link]();
[Link](10);
[Link]('$',10);
}
}

// Program 2: To Convert given String in Uppercase, Lowercase and find its Length
public class UpperLower
Output 2. :
{
public static void main(String[] input) Given String in Upper case : VANITA VISHRAM
{ Given String in Lower case : vanita vishram
String str;
Length of Given String : Vanita Vishram is : 14
str="Vanita Vishram";
[Link]("Given String in Upper case : "
+ [Link]());
[Link]("Given String in Lower case : " + [Link]());
[Link]("Length of Given String : " + str+ " is : " + [Link]());
}
}
//Program 3. To Calculate the area of Rectangle using constructor overloading
class Rectangle
{
int length,breath; Output 3:
Rectangle(int x,int y)
Area of First Rectangle is : 341.0
{
length=x; breath=y; Area of Second Rectangle is : 2000.0
} Area of Third Rectangle is : 140000.0
Rectangle(int x)
{
length=x; breath=10;
}
Rectangle()
{
length=31; breath=11;
}
float GetData()
{
return(length*breath);
}
}
class ConstructorOverloading
{
public static void main(String args[])
{
Rectangle Rect = new Rectangle();
Rectangle Rect1 = new Rectangle(200);
Rectangle Rect2 = new Rectangle(200,700);
[Link]("Area of First Rectangle is : "+[Link]());
[Link]("Area of Secons Rectangle is : "+[Link]());
[Link]("Area of Third Rectangle is : "+[Link]());
}
}
// Program 4. To sum the elements of an array
class SumOfArray
{ Output 4:
public static void main(String args[])
Sum of array elements is:160
{
int[] array = {10, 20, 30, 40, 50, 10};
int sum = 0;
for( int num =0;num<=5;num++)
{
sum = sum+array[num];
}
[Link]("Sum of array elements is:"+sum);
}
}
// Program 5: EXAMPLE OF CONDITIONAL OPERATOR
Output 5:
public class ConiditionaOperator{ First Number is : 10
public static void main(String args[])
Second Number is : 20
{
int x=10; Maximum Number is 20
int y=20;
int max;
max=(x>y)?x:y;
[Link]("First Number is : "+x);
[Link]("Second Number is : "+y);
[Link]("Maximum Number is "+max);
}
}

// Program 6: To find the given number is prime or not.


class isPrime
{ Output 6:
public static void main(String args[])
Number 7 is prime number
{
int n, flag=0, i;
n=7;
for(i=2;i<n;i++)
{
if(n%i==0)
flag++;
}

if(flag==0)
[Link]("Number "+n+" is prime number");
else
[Link]("Number "+n+" is not a prime number");

[Link]("End of Program.");
}
}

You might also like