Assignment -||
21459-Prashant Vilasrao Shitole
1. Write a Java program to get a number from the user and print
whether it is positive or negative.
import java.util.Scanner;
class posnegnumber
public static void main(String args[])
Scanner p =new Scanner(System.in);
System.out.println("Enter Any Number");
int NUMBER =p.nextInt();
if(NUMBER>0)
System.out.println(NUMBER+"\n"+"given Number is
an Positive Number");
else if(NUMBER<0)
{
System.out.println(NUMBER+"\n"+"given
Number is an Negative Number");
else
System.out.println(NUMBER+"\n"+"Is Not AN
Positive or Negative ");
Output :-
2.Write a Java program to find the number of days in a
month.
import java.util.Scanner;
class daysinmonths
public static void main(String args[])
Scanner n=new Scanner(System.in);
System.out.println("Enter the Month Number You want
to See the days");
int month=n.nextInt();
if(month == 1 || month==3 || month==5 ||
month==7 || month==8 || month==10 || month==12 )
System.out.println("This Month has 31 days ");
else if(month==4 ||month==6 ||month==9 ||
month==11)
{
System.out.println("This Month has 30 days ");
else if(month==2)
System.out.println("This Month Has 28 or 29
days ");
Output :-
3. Write a program in Java to display n terms of natural numbers
and their sum.
import java util.Scanner;
class natural_numbers
public static void main(String args[])
int sum , n;
System.out.println("Enter Any Number");
Scanner b =new Scanner();
n=b.nextInt();
for(int i=0;i<=n;i++)
sum=sum+i;
System.out.println(" Sumetion of Natural Numbers is "+
sum );
}
Output :-
4. Write a program for object and class in java.
class demo
protected int a,b,c;
demo()
System.out.println("object is created");
void sum()
a=150;
b=150;
c=a+b;
System.out.println("Summetion is "+c);
public class democlassobject
public static void main(String args[])
demo n=new demo();
n.sum();
Output :-
5. Write a program to explain the concept of this keyword in java.
class Student
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee)
this.rollno=rollno;
this.name=name;
this.fee=fee;
void display()
System.out.println(rollno+" "+name+" "+fee);
class Testthis
public static void main(String args[])
Student s1=new Student(111,"Prashant",5000f);
Student s2=new Student(112,"Sumit",6000f);
s1.display();
s2.display();
Output:-
6. Write a program to explain the concept of super keyword in java.
class Animal
String color="white";
class Dog extends Animal
String color="black";
void printColor()
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal
class
class TestSuper1
public static void main(String args[])
Dog d=new Dog();
d.printColor();
Output :-
7. Write a program for overloading in java.
class overload2
void show()
System.out.println("This method shows No prameter");
void show(int i)
System.out.println("This method shows Single
prameter");
void show(int i,int j)
System.out.println("This method shows Double
prameter");
class overloading
public static void main(String args[])
{
overload2 n=new overload2();
n.show();
n.show(5,8);
n.show(8);
Output :-
8. Write an abstract class program in java.
abstract class Base
Base()
System.out.println("Base Constructor Called");
abstract void fun();
class Derived extends Base
Derived()
System.out.println("Derived Constructor Called");
void fun()
System.out.println("Derived fun() called");
}
class abstractdemo
public static void main(String args[])
Derived d = new Derived();
d.fun();
Output :-
9. Write an Interface program in java.
interface printable
{
void print();
}
class demointerface implements printable
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
demointerface obj = new
demointerface();
obj.print();
}
}
Output:-
10. Write a Java program to compare a given string to the specified
character sequence
public class Stringdemo
{
public static void main(String[] args)
{
String str1 = "Prashant Shitole", str2 = "
prashant shitole";
CharSequence cs = "Prashant Shitole";
CharSequence cs1 =" prashant shitole";
System.out.println("Comparing "+str1+"
and "+cs+": " + str1.contentEquals(cs));
System.out.println("Comparing "+str2+"
and "+cs1+": " + str2.contentEquals(cs1));
}
}
Output :-