Assg 1 Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Some codes are not compiled so kindly check it while doing

1. Create a Mobile class with properties, which can be set


once while creating object using constructor arguments.
Create getProperties() methods which are having public
access modifiers.

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());
}
}

2. What is the difference between overloading and


overriding?

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.

3. Write a Java program to find the factorial of a number.

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();
}

4. WAP to print all the prime numbers in an array of n


elements by taking command line arguments.

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();
}
}

5. Write a program to create an abstract class and abstract


method and implements all the abstract method.

abstract class puppy


{
abstract void makeSound();

public void eat() {


System.out.println("I can eat.");
}
}

class Cow extends puppy {


// provide implementation of abstract method
public void makeSound() {
System.out.println("Bark bark");
}
}

class Maximum
{
public static void main(String[] args) {

// create an object of Dog class


Cow d1 = new Cow();

d1.makeSound();
d1.eat();
}
}

6. Implement the concept of inheritance with example.


Three types of inheritance in java are : single, multilevel
and hierarchical.

Single Inheritance Example:

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();
}
}

Multilevel Inheritance Example:

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();
}
}

Hierarchical Inheritance Example:

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();
}
}

7. Write a program to print the following pattern given n as


argument:- [for input 3]
1

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);

9. Create a class Box that uses a parameterized method to


initialize the dimensions of a box.(dimensions are width,
height, depth of double type). The class should have a
method that can return volume. Obtain an object and print
the corresponding volume in main() function.

public class Box


{
double h,w,d;

Box(double width,double height,double depth)


{
h=height;
w=width;
d=depth;
}
double volume()
{
double v;
v=h*w*d;
return v;
}
public static void main(String[] args)
{
Box bc = new Box(8.5,80.3,9.6);
System.out.println(bc.volume());

10. Write a program to input n number of elements on


command line argument and calculate the maximum.

import java.util.Scanner;

public class omprakash {

public static void main(String[] args) {

int n;

Scanner sc = new Scanner(System.in);

System.out.println("enter the size of the array :");

n = sc.nextInt();

int[] array = new int[10];


System.out.println("Enter the elements of the array: ");

for (int i = 0; i < n; i++) {

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;

//Getter and Setter methods


public int getEmpSSN(){
return ssn;
}

public String getEmpName(){


return empName;
}

public int getEmpAge(){


return empAge;
}

public void setEmpAge(int newValue){


empAge = newValue;
}

public void setEmpName(String newValue){


empName = newValue;
}

public void setEmpSSN(int newValue){


ssn = newValue;
}
}
public class EncapsTest{
public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSSN(112233);
System.out.println("Employee Name: " +
obj.getEmpName());
System.out.println("Employee SSN: " +
obj.getEmpSSN());
System.out.println("Employee Age: " +
obj.getEmpAge());
}
}

12. Write a java program with the initialization earning of


an employee. The program should calculate the income tax
to be paid by the employee as per the criteriagiven below:
Slab rate IT rate

Upto Rs. 50,000 Nil

Upto Rs. 60,000 10% on additional amount

Upto Rs. 1,50,000 20% on additional amount


Above Rs. 1,50,000 30% on the additional amount

Hint: - Run: - java calculates 1,25,000

import java.util.Scanner;

public class Main

public static void main(String[] args)

int salary;

float tax=0;

Scanner s = new Scanner(System.in);

System.out.print("Enter the salary:");


salary = s.nextInt();

int[] a = new int[salary];

if(salary<=50000)

tax=0;

else if(salary<=60000)

tax=(salary - 50000)/10;

else if(salary<=150000)

tax=1000 + (salary - 60000)/5;

else

tax=1000 + 18000 + 3*(salary-150000)/10;


System.out.println("Total tax to be paid is:"+tax);

}
}

13. Write a program to find the area of a circle

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);
}
}

You might also like