0% found this document useful (0 votes)
12 views5 pages

Main Pgms

Uploaded by

Naveen Sattur
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)
12 views5 pages

Main Pgms

Uploaded by

Naveen Sattur
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/ 5

PRIMECIRCLE

import java.util.Scanner;

public class CirclePrime {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("program executed by SHREYA JATTI");
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();

double area = Math.PI * radius * radius;


double circumference = 2 * Math.PI * radius;

System.out.println("Area of the circle: " + area);


System.out.println("Circumference of the circle: " + circumference);

System.out.print("Enter a number: ");


int number = scanner.nextInt();

boolean isPrime = true;

if (number <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}

if (isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}

scanner.close();
}
}

METHOD OVERLOADING
class MyClasss {
public void printNumber() {
System.out.println("No parameter method");
}

public void printNumber(int a) {


System.out.println(" one parameter: " +a);
}

public void printNumber(int a, int b) {


System.out.println(" two parameters: " + a + ", " +b);
}

public void printNumber(double a, double b) {


System.out.println("two double parameters: " + a + ", " + b);
}

public static void main(String[] args) {


System.out.println("program executed by SHREYA JATTI");
MyClasss obj = new MyClasss();

obj.printNumber(); // Calling method with no parameters


obj.printNumber(10); // Calling method with one integer parameter
obj.printNumber(20, 30); // Calling method with two integer parameters
obj.printNumber(3.14, 2.71); // Calling method with two double parameters
}
}

CONSTRUCTOR OVERLOADING
class MyClass
{
int x;
MyClass()
{
System.out.println("program Executed by Harish H Kulkarni");
System.out.println("no parameter");
x=0;
}
MyClass(int i)
{
System.out.println("one parameter");
x=i;
}
MyClass(double d)
{
System.out.println("one double parameter");
x=(int)d;
}
MyClass(int i,int j)
{
System.out.println("two parameter");
x=i*j;
}
}
class ConstructorOverload
{
public static void main(String args[])
{
MyClass t1=new MyClass(2,4);
MyClass t2=new MyClass();
MyClass t3=new MyClass(17.23);
MyClass t4=new MyClass(88);
System.out.println(t1.x);
System.out.println(t2.x);
System.out.println(t3.x);
System.out.println(t4.x);
}
}

MULTIPLE INHERITANCE
interface Shape
{
double getArea();
}
class Triangle implements Shape
{
private double base;
private double height;

public Triangle(double base, double height)


{
this.base = base;
this.height = height;
}
public double getArea()
{
return 0.5 * base * height;
}
}
class Rect implements Shape
{
private double length;
private double width;

public Rect(double length, double width)


{
this.length = length;
this.width = width;
}
public double getArea()
{
return length * width;
}
}

public class J7_multipleinheritance


{
public static void main(String[] args)
{
System.out.print("Program excueted by Sudhakar\n ");
Triangle triangle = new Triangle(2.0, 4.0);
double triangleArea = triangle.getArea();
System.out.println("Area of the Triangle: " + triangleArea);
Rect rectangle = new Rect(5.0, 3.0);
double rectangleArea = rectangle.getArea();
System.out.println("Area of the Rectangle: " + rectangleArea);
}
}

INNER CLASS
class Outer {
int[] nums;

Outer(int[] n) {
nums = n;
}

void analyze() {
Inner inob = new Inner();
System.out.println("min = " + inob.min());
System.out.println("max = " + inob.max());
System.out.println("avg = " + inob.avg());
}

class Inner {
int min() {
int m = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] < m) {
m = nums[i];
}
}
return m;
}

int max() {
int m = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] > m) {
m = nums[i];
}
}
return m;
}

int avg() {
int a = 0;
for (int i = 0; i < nums.length; i++) {
a += nums[i];
}
return a / nums.length;
}
}
}

public class J5_NestedDemo {


public static void main(String[] args) {
System.out.println("Program executed by Sudhakar");
int[] x = {3, 2, 1, 5, 6, 7, 8};
Outer ob = new Outer(x);
ob.analyze();
}
}

You might also like