Assg 1 Java
Assg 1 Java
Assg 1 Java
class Mobile
{
String mob;
Mobile()
{
mob = "Iphone";
}
public String getProperties()
{
return mob;
}
public static void main(String[] args)
{
Mobile m = new Mobile();
System.out.println("Mobile: " + m.getProperties());
}
}
Overloading :
It is a Compile-time polymorphism. Once using the tactic
overloading, you'll notice quite one methodology with different
signatures sharing a similar name in any given class. The return
type might or might not be the same in a method overloading.
However, a user must modify or change their parameters. It's as
a result of one can’t reach method overloading in Java by
creating changes solely within the method’s return type.
Overriding :
It is a Run time polymorphism. Once using the method
overriding, the derived class comes up with the precise
implementation of any method. The parent class or the base
class already provides it. Within the case of method overriding,
the return type is also co-variant or similar. It implies that the
return type might have variations within the same direction as
that of the derived class.
import java.util.*;
public class fact
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number: ");
int num=sc.nextInt();
int i=1,fact=1;
while(i<=num)
{
fact=fact*i;
i++;
}
System.out.println("Factorial of the number: "+fact);
sc.close();
}
import java.util.*;
public class primefinder {
public static int isprime(int n){
int c=0;
for(int i=2 ; i<n ; i++){
if(n%i==0){
c++;
break;
}
}
if(c==0){
return 0;
}
else{
return 1;
}
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array : ");
n=sc.nextInt();
int[] arr = new int[n];
for(int i=0 ; i<n ; i++){
arr[i] = sc.nextInt();
}
for(int i=0 ; i<n ; i++){
if(isprime(arr[i])==0){
System.out.print(arr[i]+" ");
}
}
sc.close();
}
}
class Maximum
{
public static void main(String[] args) {
d1.makeSound();
d1.eat();
}
}
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}
}
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");}
}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}
}
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
}
}
2 2
3 3 3
class pattern
{
void triangle(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" "+i);
}
System.out.println();
System.out.println();
}
}
}
public class Main
{
public static void main(String[] args)
{
int n=3;
pattern p = new pattern();
p.triangle(n);
}
}
8. Write a program to print the sum and average of the even
and odd numbers separately
given on command line argument.
public class Ques1{
public static void main(String[] args)
{
int n, sumE = 0, sumO = 0,x=0,y=0,avgO,avgE;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number of elements in array:");
n = s.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements of the array:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for(int i = 0; i < n; i++)
{
if(a[i] % 2 == 0)
{
x++;
sumE = sumE + a[i];
}
else
{
y++;
sumO = sumO + a[i];
}
}
avgO= sumO/y;
avgE = sumE/x;
System.out.println("Sum of Even Numbers:"+sumE);
System.out.println("Sum of Odd Numbers:"+sumO);
System.out.println("Average of Even Numbers: "+avgE);
System.out.println("Average of Odd Numbers: "+avgO);
import java.util.Scanner;
int n;
n = sc.nextInt();
array[i] = sc.nextInt();
int max=0;
for(int i=0;i<n;i++){
if(array[i]>max){
max=array[i];
System.out.println(max);
sc.close();
}
11. Write a program to implement encapsulation.
class EncapsulationDemo{
private int ssn;
private String empName;
private int empAge;
import java.util.Scanner;
int salary;
float tax=0;
if(salary<=50000)
tax=0;
else if(salary<=60000)
tax=(salary - 50000)/10;
else if(salary<=150000)
else
}
}
import java.util.Scanner;
public class Area
{
public static void main(String[] args)
{
int r;
double pi = 3.14, area;
Scanner s = new Scanner(System.in);
System.out.print("Enter radius of circle:");
r = s.nextInt();
area = pi * r * r;
System.out.println("Area of circle:"+area);
}
}