WAP to calculate the factorial of a number by using Scanner class.
import java.util.Scanner; //Importing Scanner Class
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int number = sc.nextInt();
int fact = 1;
for(int i = 2; i <= number; i++){
fact = fact*i;
}
System.out.println(number+"! = "+fact);
}
}
Enter a number : 7
7! = 5040
Enter a number : 8
8! = 40320
WAP to show the concept of class and object in java.
import java.util.Scanner;
public class Students {
int rollno;
String name;
public void learn(){
System.out.println("The student " + name + " whose
roll no. is " + rollno + " is learning Java.");
}
public static void main(String[] args) {
Students s = new Students();
Scanner sc = new Scanner(System.in);
System.out.print("Enter your roll no. : ");
s.rollno = sc.nextInt();
sc.nextLine(); //consuming the leftover new line i.e.
'\n'
System.out.print("Enter your name : ");
s.name = sc.nextLine();
s.learn();
}
}
Enter your roll no. : 20
Enter your name : Mufaddal Saifuddin
The student Mufaddal Saifuddin whose roll no. is 20 is
learning Java.
WAP to show the concept of multiple classes in java.
class Continents{
int total_continents = 7;
public void my_continent(){
System.out.println("There are " + total_continents + "
continents in the world");
System.out.println("India is part of the continent of
Asia");
}
}
class States{
int total_states = 28;
public void my_state(){
System.out.println("There are " + total_states + "
states in India");
System.out.println("I live in Madhyapradesh");
}
}
public class Multiple_Classes {
public static void main(String[] args) {
Continents c = new Continents();
States s = new States();
c.my_continent();
s.my_state();
}
}
There are 7 continents in the world
India is part of the continent of Asia
There are 28 states in India
I live in Madhyapradesh
WAP to show the concept of single inheritance in java.
class Animal{
void eats(){
System.out.println("Animal is eating");
}
void runs(){
System.out.println("Animal is running");
}
}
public class Dog extends Animal{
void barks(){
System.out.println("Dog is barking");
}
public static void main(String[] args) {
Dog d = new Dog();
d.runs();
d.barks();
d.eats();
}
}
Animal is running
Dog is barking
Animal is eating
WAP to show the concept of multilevel inheritance in java.
class Grandparents{
void grandfather(){
System.out.println("I am your Grand father");
}
void grandmother(){
System.out.println("I am your Grand mother");
}
}
class Parents extends Grandparents{
void parents() {
System.out.println("My parents have inherited
properties from my Grand parents");
}
}
public class Child extends Parents {
void I(){
System.out.println("I have inherited properties from
my Parents");
}
public static void main(String[] args) {
Child c = new Child();
c.grandfather();
c.grandmother();
c.parents();
c.I();
}
}
I am your Grand father
I am your Grand mother
My parents have inherited properties from my Grand
parents
I have inherited properties from my Parents
WAP to show the concept of hierarchical inheritance in java.
class Animals{
void types(){
System.out.println("Animals are of 2 types : Domestic
and Wild");
}
}
class Domestic extends Animals{
void examples(){
System.out.println("Cow, Dog, Goat, Horse, Sheep,
etc.");
}
}
public class Wild extends Animals{
void examples(){
System.out.println("Lion, Tiger, Bear, Deer, Fox,
etc.");
}
public static void main(String[] args) {
Domestic d = new Domestic();
d.types();
d.examples();
Wild w = new Wild();
w.types();
w.examples();
}
}
Animals are of 2 types : Domestic and Wild
Cow, Dog, Goat, Horse, Sheep, etc.
Animals are of 2 types : Domestic and Wild
Lion, Tiger, Bear, Deer, Fox, etc.
WAP to show the concept of abstract keyword in java.
abstract class A{
abstract void method1();
abstract void method2();
}
abstract class B extends A{
void method1() {
System.out.println("Method1 of class A is implemented
in class B");
}
abstract void method3();
}
class C extends B{
void method2(){
System.out.println("Method2 of class A is implemented
in class C");
}
void method3(){
System.out.println("Method3 of class B is implemented
in class C");
}
}
public class Abstraction {
public static void main(String[] args) {
B obj = new C();
obj.method1();
obj.method2();
obj.method3();
}
}
Method1 of class A is implemented in class B
Method2 of class A is implemented in class C
Method3 of class B is implemented in class C
WAP to show the concept of interface in java.
interface Base1{
void B1M1();
void B1M2();
}
interface Base2{
void B2M1();
void B2M2();
}
public class Derived implements Base1, Base2 {
public void B1M1(){
System.out.println("Method1 of Base Class 1");
}
public void B1M2(){
System.out.println("Method2 of Base Class 1");
}
public void B2M1(){
System.out.println("Method1 of Base Class 2");
}
public void B2M2(){
System.out.println("Method2 of Base Class 2");
}
public static void main(String[] args) {
Derived obj = new Derived();
obj.B1M1();
obj.B1M2();
obj.B2M1();
obj.B2M2();
}
}
Method1 of Base Class 1
Method2 of Base Class 1
Method1 of Base Class 2
Method2 of Base Class 2
WAP to show the concept of final keyword in java.
class FM{
final void Greet(){
System.out.println("Hello User!");
}
}
final public class FinalClass extends FM {
final int x = 100;
public static void main(String...a){
FinalClass obj = new FinalClass();
System.out.println(obj.x);
obj.Greet();
}
}
100
Hello User!