Lab 8

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

CSIT111: Programming Fundamentals

CSIT 111 Lab 8


Objectives:
1. Functions (methods)
2. OOD
Part A
Q1. What is class?
User defined type. It is used as a blue print (prototype) to create similar objects
Examples : Student, Person, Customer, Employee Book, device, ….

Q2. What is an object


An instance of class
e.g. Student s1=new Student ( “ ….”, 2345,female);

Q3. What are the various components of a class


-name e.g. student Student
-data attributes e.g. name
Constructors (default, parameter)
-methods
Q4. Constructor is used for what?
They are used to initialize newly created objects.
They are initialized to a desired value or a default value.

Q5. Convert the following Customer class diagram into Java code and test it
private String name;
private String address;
private int cardNumber;
private String dateofBirth;
private int age;

public Customer() {
this.name=null;
this.address=null;
this.cardNumber=0;
this.dateofBirth=null;
this.age=0;
}
public Customer(String n,String add,int cn,String dob,int age) {
this.name=n;
this.address=add;
this.cardNumber=cn;
this.dateofBirth=dob;
this.age=age;
}
public void setName(String newname) {
this.name=newname;
}
public void setAddress(String newadd) {
this.address=newadd;
}
public void setcardNumber(int newcn) {
this.cardNumber=newcn;
}
public void setdob(String newdob) {
this.dateofBirth=newdob;
}
public void setage(int newage) {
this.age=newage;
}
public String get_name() {
return name;
}
public String get_add() {
return address;
}
public int get_cardNumber() {
return cardNumber;
}
public String get_dob() {
return dateofBirth;
}
public int get_age() {
return age;
}
public String toString() {
return " Name :" + name + " address : "+ address
+ " CardNUmber :" + cardNumber
+ " Date of Birth : " + dateofBirth + " Age : " + age;
}
public void Display() {
System.out.println("Name :" + name + " address : "+ address + " CardNUmber
:" + cardNumber
+ " Date of Birth : " + dateofBirth + " Age : " + age);
}
}

Q6. Write a parameter Constructor (do not use code generation. Later you can use)
public Customer(String n,String add,int cn,String dob,int age) {
this.name=n;
this.address=add;
this.cardNumber=cn;
this.dateofBirth=dob;
this.age=age;
}

Q7. Write a default constructor (do not use code generation. Later you can use)
public Customer() {
this.name=null;
this.address=null;
this.cardNumber=0;
this.dateofBirth=null;
this.age=0;
}

Q8. Create two objects (two customers)


Customer c1 = new Customer("Omar","Dubai",1234,"1/24/1999",21);
Customer c2 = new Customer("john","Abu Dhabi",3456,"10/01/2000",21);

Q9. implement the toString function and test it (do not use code generation. Later you can use)
public String toString() {
return " Name :" + name + " address : "+ address
+ " CardNUmber :" + cardNumber
+ " Date of Birth : " + dateofBirth + " Age : " + age;
}
Q10. Implement some get and set functions (do not use code generation. Later you can use)
public void setName(String newname) {
this.name=newname;
}
public void setAddress(String newadd) {
this.address=newadd;
}
public void setcardNumber(int newcn) {
this.cardNumber=newcn;
}
public void setdob(String newdob) {
this.dateofBirth=newdob;
}
public void setage(int newage) {
this.age=newage;
}
public String get_name() {
return name;
}
public String get_add() {
return address;
}
public int get_cardNumber() {
return cardNumber;
}
public String get_dob() {
return dateofBirth;
}
public int get_age() {
return age;
}

Q11. What is Meant by encapsulation?

Q12. What is meant by data hiding? Why is it needed.

Part B

• Create a list (Dynamic) of 3 customers


Customer c1 = new Customer("Omar","Dubai",1234,"1/24/1999",21);
Customer c2 = new Customer("john","Abu
Dhabi",3456,"10/01/2000",21);
Customer c3 = new
Customer("Rayan","Sharjah",5678,"25,05,2009",15);
ArrayList<Customer>CustomerList =new
ArrayList(Arrays.asList(c1,c2,c3));

• Display the list in three ways


System.out.println(CustomerList);
for(int i=0;i<CustomerList.size();i++) {
System.out.println(CustomerList.get(i));
}
for(Customer c: CustomerList) {
System.out.println(c);
}
System.out.println(c1.toString());

• Create a static array of three customers

• Display the list in three ways


• Write a function that takes as input the list of customers and customer id (assume existing id)
and return the customer with the id requested.
• Write java code that takes a dynamic list of customer and a given id it return yes if the student
exist and returns no if the student does not exist with this id.
public static Customer requests(ArrayList<Customer> arr,int num) {

for(int i=0;i<arr.size();i++) {
if(arr.get(i).getcardNumber()== num) {
return arr.get(i);
}
}
return null;

• Convert a static array to dynamic list

• Convert a dynamic list to static array


2

You might also like