Label » Object Oriented Programming

Two Objects of the same Class are not the same

/* Two Objects of the same Class are not the same */
public class DemoOfObjects {

	private int objectID;
	private int objectValue;
	private String objectName;
	
	public void setObjectID(int id) {
		objectID = id;
	}
	
	public void setObjectValue(int val) {
		objectValue = val;
	}

	public void setObjectName(String name) {
		objectName = name;
	}
	
	public int getObjectID() {
		return objectID;
	}
	
	public int getObjectValue() {
		return objectValue;
	}
	
	public String getObjectName() {
		return objectName;
	}
	
	public static void main(String[] args) {
		DemoOfObjects obj1 = new DemoOfObjects();
		DemoOfObjects obj2 = new DemoOfObjects();
		
		obj1.setObjectID(1);
		obj1.setObjectValue(1000);
		obj1.setObjectID(2);
		
		obj2.setObjectValue(2000);
		obj2.setObjectName("Object 2");
		
		System.out.println("Object ID using obj1 is "+obj1.getObjectID());
		System.out.println("Object ID using obj2 is "+obj2.getObjectID());
		System.out.println("Object Value using obj1 is "+obj1.getObjectValue());
		System.out.println("Object Value using obj2 is "+obj2.getObjectValue());
		System.out.println("Object Name using obj1 is "+obj1.getObjectName());
		System.out.println("Object Name using obj2 is "+obj2.getObjectName());
	}	
}

Object Oriented Programming

One Class depending on another Class

/* One Class depending on another Class */
public class DemoOfObjects {

	private int objectID;
	private int objectValue;
	private String objectName;
	
	public void setObjectID(int id) {
		objectID = id;
	}
	
	public void setObjectValue(int val) {
		objectValue = val;
	}

	public void setObjectName(String name) {
		objectName = name;
	}
	
	public int getObjectID() {
		return objectID;
	}
	
	public int getObjectValue() {
		return objectValue;
	}
	
	public String getObjectName() {
		return objectName;
	}
	
	public static void main(String[] args) {
		DemoOfObjects obj1 = new DemoOfObjects();
		DemoOfObjects obj2 = new DemoOfObjects();
		
		obj1.setObjectID(1);
		obj1.setObjectValue(1000);
		obj1.setObjectID(2);
		
		obj2.setObjectValue(2000);
		obj2.setObjectName("Object 2");
		
		System.out.println("Object ID using obj1 is "+obj1.getObjectID());
		System.out.println("Object ID using obj2 is "+obj2.getObjectID());
		System.out.println("Object Value using obj1 is "+obj1.getObjectValue());
		System.out.println("Object Value using obj2 is "+obj2.getObjectValue());
		System.out.println("Object Name using obj1 is "+obj1.getObjectName());
		System.out.println("Object Name using obj2 is "+obj2.getObjectName());
	}	
}

Object Oriented Programming

Constructors with different parameters

public class DemoForConstructor {
	
	private int customerId;
	private String customerPhone;
	private String customerName;
	
	public DemoForConstructor() {
		customerId = 12345;
		customerPhone = "9876543210";
		customerName = "John Doe";
	}
	
	public DemoForConstructor(int cid) { // with one param
		customerId = cid;
		customerPhone = "9876543210";
		customerName = "John Doe";
	}

	public DemoForConstructor(int cid, String cphone) { // with two param
		customerId = cid;
		customerPhone = cphone;
		customerName = "John Doe";
	}

	public DemoForConstructor(int cid, String cphone, String cname) { // with three param
		customerId = cid;
		customerPhone = cphone;
		customerName = cname;
	}
	
	public String getCustomer() {
		return customerId+" "+customerPhone+" "+customerName;
	}

	public static void main(String[] args) {
		DemoForConstructor dfc1 = new DemoForConstructor();
		System.out.println(dfc1.getCustomer());
		
		DemoForConstructor dfc2 = new DemoForConstructor(22223);
		System.out.println(dfc2.getCustomer());
		
		DemoForConstructor dfc3 = new DemoForConstructor(33345,"9988776655");
		System.out.println(dfc3.getCustomer());

		DemoForConstructor dfc4 = new DemoForConstructor(44456,"8877665544","Adam");
		System.out.println(dfc4.getCustomer());
	}
}
Object Oriented Programming

Use of Clone while returning a mutable data field

/* Use of Clone while returning a mutable data field */
import java.util.Date;
import java.util.GregorianCalendar;

public class Employee {
	
	private int employeeId;
	private String employeeName;
	private Date hireDate;
	
	public Employee(int id, String name) {
		employeeId = id;		
		employeeName = name;
		GregorianCalendar gc = new GregorianCalendar();
		hireDate = gc.getTime();
	}
	
	public void manipulateHireDate() {
		Date d = this.hireDate;
		d.setTime(1);
	}
	
	public Date getHireDate() {
		// use clone as we are returning a copy of the mutable data field
		return (Date) hireDate.clone(); 		
	}
	
	public int getEmployeeId() {
		return employeeId;
	}
	
	public String getEmployeeName() {
		return employeeName;
	}
}

public class DemoForClone {
	
	public static void main(String[] args) {
		Employee e1 = new Employee(234,"Bob");
		System.out.println(e1.getHireDate());
		e1.manipulateHireDate();
		System.out.println(e1.getHireDate());
	}

}

Object Oriented Programming

Conceptualizing Object-oriented Programming

Last week I began Flatiron School. No, it’s not to do with architecture. Campus occupies a large loft space at the southern most tip of Broadway Ave. 969 kata lagi

IIPP P1W1: Rock-paper-scissors-lizard-Spock

Let’s get to the second week of the IIPP.

By the way, most of the numbering in this course starts from zero, as it is a convention to countĀ from zero (rather than one) in computer science. 661 kata lagi

IIPP

Static variable and Static method


/* Demo for static variable and static method */
public class Student {
	
	private int studentId;
	private String studentName;
	private String studentProgram;
	
	private static int nextId = 1;
	
	public Student(String name, String program) {
		studentId = 0;
		studentName = name;
		studentProgram = program;
	}
	
	public String getStudentName() {
		return studentName;
	}
	
	public String getStudentProgram() {
		return studentProgram;
	}
	
	public void setStudentId() {
		studentId = nextId;
		nextId++;
	}
	
	public int getStudentId() {
		return studentId;
	}
	
	public static int getNextId() {
		return nextId;
	}

}

public class DemoForStatic {

	public static void main(String[] args) {
		Student[] stud = new Student[5];
		stud[0] = new Student("Bob","Graduate");
		stud[1] = new Student("Joe","Certificate");
		stud[2] = new Student("Harry","UnderGraduate");
		stud[3] = new Student("Mary","Graduate");
		stud[4] = new Student("Tim","UnderGraduate");
		
		for (Student s:stud) {
			s.setStudentId();
			System.out.println(s.getStudentName()+" "+s.getStudentId()+" "+s.getStudentProgram());
		}
		
		System.out.println("Next available Id is "+Student.getNextId());
	}
}
Object Oriented Programming