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

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