Tuesday, June 9, 2015

Project 7 / FindAreaAndPerimeter using INTERFACE


package areaAndPerimeter;

public class InheritanceWithPolymorphismDemo4
{
public static void main(String[] args)
{
Measurable4 measurable = new Rectangle4(10, 10);
measurable.getArea();
measurable.getPerimeter();
System.out.println("Using measurable interface reference the area and parimeter is:\n "
+measurable.getArea()+" and "+ measurable.getPerimeter()+" \n ");

Measurable4 measurable3 = new Circle4(10);
measurable3.getArea();
measurable3.getPerimeter();
System.out.println("Using measurable interface reference the area and parimeter is:\n "
+measurable3.getArea()+" and "+ measurable3.getPerimeter()+"\n");

getTotalArea(measurable, measurable3);
}

public static void getTotalArea(Measurable4 m1 , Measurable4 m2)
{
System.out.println("The total area of the two object is: " + (m1.getArea()+m2.getArea()));
}
}

-------------------------------------------------

interface Measurable4
{
double PI = 3.1428;
public double getArea();
public double getPerimeter();
}

----------------------------------------------------

class Rectangle4 implements Measurable4
{
double length , breadth ;

public Rectangle4(double length , double breadth)
{
this.length = length;
this.breadth = breadth;
}

@Override
public double getArea()
{
return length * breadth;
}

@Override
public double getPerimeter()
{
return 2 * length * breadth ;
}
}

----------------------------------------------------------

class Circle4 implements Measurable4
{
double radius;

public Circle4(double radius)
{
this.radius = radius;
}

@Override
public double getArea()
{
return PI * radius * radius;
}

@Override
public double getPerimeter()
{
return 2 * PI * radius ;
}
}

--------------------------------------------------------------


Project 6 / Move (x,y) coordinate HANGUP


/*
Our application involves many objects that can move. You can define an interface called movable,
containing the signatures of the various movement methods. So define the following abstract methods
to be implemented by the subclasses
                     moveUp(), moveDown(),  moveLeft(),  moveRight() &  moveUp()
Derive a subclass called MovablePointand provide implementation to all the abstract methods declared in the interface.
*/


public class MianActivity {

public static void main(String[] args) {

MovablePoint m = new MovablePoint(10, 10);
m.moveUp();
m.moveDown();
m.moveLeft();
m.moveRight();
}
}

---------------------------------------------------------------------------


public interface Movable 
{
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
}

---------------------------------------------------------------------------


public class MovablePoint implements Movable 
{
private int x , y ;
public MovablePoint(int x , int y) {
this.x = x;
this.y = y;
}
@Override
public void moveUp() {
y++;
System.out.println("Point moveUp to point : "+y );
}

@Override
public void moveDown() {
y--;
System.out.println("Point moveDown to point : "+y );
}

@Override
public void moveLeft() {
x--;
System.out.println("Point moveLeft to point : "+x );
}

@Override
public void moveRight() {
x++;
System.out.println("Point moveRight to point : "+x );
}

}

-------------------------------------------------------------------------------


Project 5 / Area and Color




/*
Our program uses many kinds of shapes, such as triangle, rectangle and so on. You should design a
super class called Shape, which defines the public interface/class (or behaviours) of all the shapes.
 And, we would like all the shapes to have a method called getArea(), which returns the area of that
  particular shape. Define a Shape class as mentioned in the above class diagram.
*/

public class MainActivity
{
public static void main(String[] args)
{
Ractangle r = new Ractangle(666, 10, 10);
r.toString();
r.getArea();

System.out.println();

Traingle t = new Traingle(999, 10, 10);
t.toString();
t.getArea();
}
}

------------------------------------------------------------------


public class Shape
{
int color ; 
public Shape(int color) 
{
this.color = color;
}
@Override
public String toString() 
{
System.out.println("The color of the shape is: "+color);
return super.toString();
}
public void getArea() 
{

}
}

--------------------------------------------------------------------------


public class Ractangle extends Shape{

double length, breadth;

public Ractangle(int color, double length , double breadth)
{
super(color);
this.length = length;
this.breadth = breadth;
}
@Override
public void getArea() 
{
super.getArea();
double result = length * breadth;
System.out.println("The area of Rectangle is: "+result);
}
}

--------------------------------------------------------------------------------


public class Traingle extends Shape{

double base, height;
public Traingle(int color, double base, double height)
{
super(color);
this.base = base;
this.height = height;
}

@Override
public void getArea() 
{
double result = 0.5 * base*height;
System.out.println("The area is: "+ result);
super.getArea();
}
}

---------------------------------------------------------------------------------------



Project 4 / getterSettersWithConstructors

package gettersSettersWithConstructors;

/*
3.  Write a Teacher class that extends the parent class Person.
a.  Add instance variables to the class for subject (e.g. “Computer Science”, “Chemistry”,, “English”, “Other”) and salary (the teachers annual salary). Subject should be of type String and salary of type double. Choose appropriate names for the instance variables.
b.  Write a constructor for the Teacher class. The constructor will use five parameters to initialize myName, myAge, myGender, subject, and salary.  Use the super reference to use the constructor in the Person superclass to initialize the inherited values.
c.  Write “setter” and “getter” methods for all of the class variables. For the Teacher class they would be: getSubject, getSalary, setSubject, and setSalary.
d.  Write the toString() method for the Teacher class. Use a super reference to do the things already done by the superclass.
*/


public class MianActivity
{
public static void main(String[] args)
{
Teacher teacher = new Teacher("Bpn", 22, "Male", "JAVA", 2000);
teacher.getName();
System.out.println(teacher.getName()+" "+teacher.getAge()+" "+teacher.getGender()+" "+teacher.getSubject()+" "+teacher.getSalary());
}
}

-----------------------------------------------------------------

package gettersSettersWithConstructors;

public class Person 
{
private String name;
private int age;
private String gender;
public Person(String name, int age, String gender) 
{
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName()
{
return name;
}
public void setName(String name) 
{
this.name = name;
}
public int getAge() 
{
return age;
}
public void setAge(int age) 
{
this.age = age;
}
public String getGender() 
{
return gender;
}
public void setGender(String gender) 
{
this.gender = gender;
}
}

--------------------------------------------------------------------------

package gettersSettersWithConstructors;

public class Teacher extends Person
{
private double salary;
private String subject;
public Teacher(String name, int age, String gender,String subject, double salary) 
{
super(name, age, gender);
this.subject = subject;
this.salary = salary;
}

public double getSalary() 
{
return salary;
}
public String getSubject() 
{
return subject;
}

public void setSalary(double salary) 
{
this.salary = salary;
}
public void setSubject(String subject) 
{
this.subject = subject;
}
}

-------------------------------------------------------------------------


Project 3 / getSetInstanceVariables


/*
1.         Add methods to “set” and “get” the instance variables in the Person class. These would consist of: getName, getAge, getGender, setName,  setAge, and setGender.
2.         Add methods to “set” and “get” the instance variables in the Student class. These would consist of: getIdNum, getGPA, setIdNum, and setGPA.
*/


package gettersAndSettersForInstanceVariables;

public class MainActivity {

public static void main(String[] args)
{
Person p = new Person();
p.setName("Bpn");
p.setAge(22);
p.setGender("Male");

System.out.println("Name is: "+p.getName());
System.out.println("Age is: "+p.getAge());
System.out.println("Gender is: "+p.getGender());
}
}

----------------------------------------------------------

package gettersAndSettersForInstanceVariables;

public class Person
{
private String name;
private int age;
private String gender;

public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}


public void setAge(int age)
{
this.age = age;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
}

--------------------------------------------------------------------------


Project 2 / College Info


public class CollegeInfo {

public static void main(String[] args) {

College collegeSky = new College("Sky college", "Butwal", 300, 10, 20);
collegeSky.getInfo();
collegeSky.getStudent();
collegeSky.getTeachers();
collegeSky.getExtraStaffs();

System.out.println("\n");

College collegeKshitiz = new College("Kshitiz", "Kalika Nagar", 3000, 100, 80);
collegeKshitiz.getInfo();
collegeKshitiz.getStudent();
collegeKshitiz.getTeachers();
collegeKshitiz.getExtraStaffs();
}
}

-----------------------------------------------------------

public interface CollegeInterface {

void getInfo();
void getStudent();
void getTeachers();
void getExtraStaffs();
}

---------------------------------------------------------------

public class College implements CollegeInterface{

String clzName;
String city;
int studNo;
int staffNo;
int teachersNo;

public College(String clzName, String city, int studNo, int staffNo , int teachersNo) {
this.clzName = clzName;
this.city = city;
this.studNo = studNo;
this.staffNo = staffNo;
this.teachersNo = teachersNo;
}
@Override
public void getInfo() {
System.out.println("This "+ clzName + " college is situated in "+city+" city");
}

@Override
public void getStudent() {
System.out.println("This "+ clzName+ " college have minimum "+ studNo + " students in the college");
}

@Override
public void getTeachers() {
System.out.println("The "+ clzName+" college have "+teachersNo+" teachers including secondary and higher secondary");
}

@Override
public void getExtraStaffs() {
System.out.println("This "+ clzName+ " college have minimum "+ teachersNo + " teachers in the college");
}
}

----------------------------------------------------------------------------------


Thursday, June 4, 2015

Project 1 / PersonDetials


// DESCRIPTION:
// Variable initialization and create display() on the super class
// Create subClass which extends superClass Person
// Assign value with the help of constructors
// Display the assigning values with the display() method as an output

// LEARN:
// Calling supper class constructor and its own constructor too
// Overriding Superclass Method and its own method too
// in sub class

public class MainActivity
{
public static void main(String[] args)
{
Dad d = new Dad("OM", 47, "male", "Pokhara", 50000, "Marketin");
d.display();

Mum m = new Mum("Uma", 44, "Female", "Butwal", 2000, "Main", "Sorry no desc");
m.display();
}
}

-------------------------------------------------------------

public class Person
{
String name;
int age;
String sex;
String city;
public void display() 
{
System.out.println
(
name+"\n"+
age+"\n"+
sex+"\n"+
city
);
}
}

------------------------------------------------------------------------

// Overriding Superclass Method and its own method too

public class Dad extends Person
{
double salary ; 
String job;
Dad(String name, int age , String sex, String city, double salary, String job)
{
this.name = name;
this.age = age;
this.sex = sex;
this.city = city;
this.salary = salary;
this.job = job;
}
@Override
public void display() 
{
super.display();
System.out.println(salary);
System.out.println(job);
}
}


--------------------------------------------------------------------------

// Calling supper class constructor and its own constructor too
// Overriding Superclass Method and its own method too


public class Mum extends Dad
{
String desc;
public Mum(String name, int age , String sex, String city, double salary, String job,String desc) 
{
super(name, age, sex, city, salary, job);  // assigning super constructor
this.desc = desc;
}
@Override
public void display() 
System.out.println();
super.display();   
System.out.println(desc);
}
}

------------------------------------------------------------------------------------


Packages












Interface with Polymorphism 4


package areaAndPerimeter;

public class InheritanceWithPolymorphismDemo4
{
public static void main(String[] args)
{
Measurable4 measurable = new Rectangle4(10, 10);
measurable.getArea();
measurable.getPerimeter();
System.out.println("Using measurable interface reference the area and parimeter is:\n "
+measurable.getArea()+" and "+ measurable.getPerimeter()+" \n ");

Measurable4 measurable3 = new Circle4(10);
measurable3.getArea();
measurable3.getPerimeter();
System.out.println("Using measurable interface reference the area and parimeter is:\n "
+measurable3.getArea()+" and "+ measurable3.getPerimeter()+"\n");

getTotalArea(measurable, measurable3);
}

public static void getTotalArea(Measurable4 m1 , Measurable4 m2)
{
System.out.println("The total area of the two object is: " + (m1.getArea()+m2.getArea()));
}
}


interface Measurable4
{
double PI = 3.1428;
public double getArea();
public double getPerimeter();
}


class Rectangle4 implements Measurable4
{
double length , breadth ;

public Rectangle4(double length , double breadth)
{
this.length = length;
this.breadth = breadth;
}

@Override
public double getArea()
{
return length * breadth;
}

@Override
public double getPerimeter()
{
return 2 * length * breadth ;
}
}


class Circle4 implements Measurable4
{
double radius;

public Circle4(double radius)
{
this.radius = radius;
}

@Override
public double getArea()
{
return PI * radius * radius;
}

@Override
public double getPerimeter()
{
return 2 * PI * radius ;
}
}


Interface with Polymorphism 3


package areaAndPerimeter;

public class InheritanceWithPolymorphismDemo3
{
public static void main(String[] args)
{
Measurable3 measurable = new Rectangle3(10, 10);
measurable.getArea();
measurable.getPerimeter();
System.out.println("Using measurable interface reference the area and parimeter is:\n "
+measurable.getArea()+" and "+ measurable.getPerimeter()+" \n ");

Measurable3 measurable3 = new Circle3(10);
measurable3.getArea();
measurable3.getPerimeter();
System.out.println("Using measurable interface reference the area and parimeter is:\n "
+measurable3.getArea()+" and "+ measurable3.getPerimeter());
}
}


interface Measurable3
{
double PI = 3.1428;
public double getArea();
public double getPerimeter();
}


class Rectangle3 implements Measurable3
{
double length , breadth ;

public Rectangle3(double length , double breadth)
{
this.length = length;
this.breadth = breadth;
}

@Override
public double getArea()
{
return length * breadth;
}

@Override
public double getPerimeter()
{
return 2 * length * breadth ;
}
}


class Circle3 implements Measurable3
{
double radius;

public Circle3(double radius)
{
this.radius = radius;
}

@Override
public double getArea() {
return PI * radius * radius;
}

@Override
public double getPerimeter() {
return 2 * PI * radius ;
}
}



Interface with Polymorphism 2

Click to watch


package areaAndPerimeter;

public class InheritanceWithPolymorphismDemo2
{
public static void main(String[] args)
{
Rectangle2 rect = new Rectangle2(10 , 10);
rect.getArea();
rect.getPerimeter();
System.out.println("The area and perimeter of rectangle is "+ rect.getArea()+" and "+ rect.getPerimeter()+"\n");

Circle2 circ = new Circle2(10);
circ.getArea();
circ.getPerimeter();
System.out.println("The area and perimeter of circle is "+ circ.getArea()+" and "+ circ.getPerimeter());
}
}


interface Measurable2
{
double PI = 3.1428;
public double getArea();
public double getPerimeter();
}


class Rectangle2 implements Measurable2
{
double length , breadth ;

public Rectangle2(double length , double breadth)
{
this.length = length;
this.breadth = breadth;
}

@Override
public double getArea()
{
return length * breadth;
}

@Override
public double getPerimeter()
{
return 2 * length * breadth ;
}
}


class Circle2 implements Measurable2
{
double radius;

public Circle2(double radius)
{
this.radius = radius;
}

@Override
public double getArea() {
return PI * radius * radius;
}

@Override
public double getPerimeter() {
return 2 * PI * radius ;
}
}



Interface With Polymorphism









package areaAndPerimeter;

public class InheritanceWithPolymorphismDemo
{
public static void main(String[] args)
{
Rectangle rect = new Rectangle();
rect.breadth = 10;
rect.length = 10;
rect.getArea();
rect.getPerimeter();
System.out.println("The area and perimeter of rectangle is "+ rect.getArea()+" and "+ rect.getPerimeter()+"\n");

Circle circ = new Circle();
circ.radius = 10;
circ.getArea();
circ.getPerimeter();
System.out.println("The area and perimeter of circle is "+ circ.getArea()+" and "+ circ.getPerimeter());
}
}


interface Measurable
{
double PI = 3.1428;
public double getArea();
public double getPerimeter();
}


class Rectangle implements Measurable
{
double length , breadth ;

@Override
public double getArea()
{
return length * breadth;
}

@Override
public double getPerimeter()
{
return 2 * length * breadth ;
}
}


class Circle implements Measurable
{
double radius;

@Override
public double getArea() {
return PI * radius * radius;
}

@Override
public double getPerimeter() {
return 2 * PI * radius ;
}
}



Interface









final Keyword







public class FinalClassDemo
{
public static void main(String[] args)
{
Demo d = new Demo();
System.out.println(d.SymbolNo);
d.method1();
}
}

final class Demo
{
final int SymbolNo = 130;
public final void method1()
{
System.out.println(SymbolNo);
}
}

/*
class Demo1 extends Demo{  // we cannot extends final class

}
*/

Abstract Keyword








Polymorphism Dynamic Method Dispatch 2





import java.io.BufferedReader;
import java.io.InputStreamReader;

public class DynamicMethodDispatchCOOL
{
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the blog from where you want to read: \n"+" 1: Site1 \n 2: Site2 \n 3: Site3 \n");
int no = Integer.parseInt(reader.readLine());

Reader blogReader = null;

switch (no) {
case 1:
blogReader = new Site1();
break;

case 2:
blogReader = new Site2();
break;

case 3:
blogReader = new Site3();
break;

default:
System.out.println("Please select valid site no...");
break;
}

if(blogReader != null)
{
blogReader.readFromBlog();
}
}

}


class Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader....");
}
}


class Site1 extends Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader from site1....");
}
}


class Site2 extends Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader from site2....");
}
}


class Site3 extends Reader
{
public void readFromBlog()
{
System.out.println("Blog Reader from site3....");
}
}





Polymorphism Dynamic Method Dispatch

// Polymorphism is the ability to treat object with differences in behavior in a uniform way
// Polumorphism enables us to write programs that process objects that share
// the same superclass(directly or indirectly) as if they're all object of the superclass


public class PolymorphismDynamicMethodDispatch
{
public static void main(String[] args)
{
Animal a =new Human();
a.move();
}
}


class Animal
{
public void move()
{
System.out.println("Animal can Move. ");
}
}

class Human extends Animal
{
public void move()
{
System.out.println("Human can Move. ");
}
}


-----------------------------------------------------------------------------------------------------------------------



public class PolynomialDynamicMethodDispatch2
{
public static void main(String[] args)
{
Animal1 a =new Human1();
a.move();
}
}


class Animal1
{
public void move()
{
System.out.println("Animal can Move. ");
}
}

class Human1 extends Animal1
{

}



instanceof




// instanceof simply means objectof
// referenceVariable instanceOf className/interfaceName
// Test whether the reference variable on the left is an instance/object
// of the specified type(class or subClass or interface) on the right

// An object of subclass type is also a type of parent class
// instanceOf operator with a variable that has null value returns false
// Parent class can never be converted into child but child class can be converted into the parent



public class InstanceOfDemo
{
public static void main(String[] args)
{
Parent p = new Parent();
System.out.println(p instanceof Parent);
System.out.println(p instanceof Child);   // Parent can never be the instance of child
System.out.println();

Child cc = new Child();
System.out.println(cc instanceof Child);
System.out.println(cc instanceof Parent);
System.out.println();

Parent p1 = new Child();
System.out.println(p1 instanceof Child);
System.out.println(p1 instanceof Parent);
System.out.println();

Child c = (Child) p1;
System.out.println(c instanceof Child);
System.out.println(c instanceof Parent);
System.out.println();

Parent pp = p1;
System.out.println(pp instanceof Parent);
System.out.println(pp instanceof Child);
System.out.println();

Child c1 = (Child) p; // class cast exception
System.out.println(c1 instanceof Child);
System.out.println(c1 instanceof Parent);
}
}


class Parent
{

}


class Child extends Parent
{

}