Lab4 Oop

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Department of Computer Science

Faculty of Computing

LAB 4
(Object Oriented Programming)

Programme : Bachelor of Computer Science


(Computer Network and Security)

Subject : SECR1033
Code
Subject : Computer Organization & Architecture
Name
Session-Sem : 2022/2023-2

Prepared by : 1) Mohamad Luthfi Bin Shohime


(SX222134ECJHF03)
Section : 01

Lecturer : Dr. Zalmiyah Binti Zakaria

Video Link : (if any) http://utm.webex.com/meet/muhalim


Date : 12 Jun 2023
Complete the program below using ArrayList class by writing at the empty
underlined space.

import java.util.ArrayList;

public class Cuba {


public static void main(String[] args) {
// declare ArrayList object named student
ArrayList<String> student = new ArrayList<>();

// add two elements "Siti Rahimah" and "Robert Lau" to student


ArrayList
student.add("Siti Rahimah");
student.add("Robert Lau");

// display all the content of student ArrayList using a loop


statement
System.out.println("Original elements in the student ArrayList:");
for (String name : student) {
System.out.println(name);
}

// replace the content of the first element to "Muhammad"


if (!student.isEmpty()) {
student.set(0, "Muhammad");
}

// display the updated content of student ArrayList


System.out.println("\nUpdated elements in the student ArrayList:");
for (String name : student) {
System.out.println(name);
}
}
}
EXERCISE 3: PROBLEM SOLVING

1. Employee class has the following attributes and methods


I. name: A string that holds the employee's name.

II. idNumber: An integer variable that holds the employee's ID number


III. department: a string that holds the name of the department where the

employee works.
IV. position: a string that holes the employee's job title.

V. A default constructor that sets the null value to member variables:


employee's name, department and position, and 0 to employee's ID

number.
VI. A constructor that accepts the following values as arguments and

assigns them to the appropriate member variables: employee's name,


employee's ID number, department and position.

VII. appropriate mutator method that store values in these member


variables

VIII. appropriate accesor method that return the value in these member
variables
public class Employee {

private String name;


private int idNumber;
private String department;
private String position;

public Employee() {
this.name = null;
this.idNumber = 0;
this.department = null;
this.position = null;
}

public Employee(String name, int idNumber, String department, String position)


{
this.name = name;
this.idNumber = idNumber;
this.department = department;
this.position = position;
}

// Mutator Methods
public void setName(String name) {
this.name = name;
}

public void setIdNumber(int idNumber) {


this.idNumber = idNumber;
}

public void setDepartment(String department) {


this.department = department;
}

public void setPosition(String position) {


this.position = position;
}

// Accessor Methods
public String getName() {
return name;
}

public int getIdNumber() {


return idNumber;
}

public String getDepartment() {


return department;
}

public String getPosition() {


return position;
}

public static void main(String[] args) {

Employee employee1 = new Employee();

employee1.setName("John Doe");
employee1.setIdNumber(12345);
employee1.setDepartment("IT");
employee1.setPosition("Software Engineer");

System.out.println("Name: " + employee1.getName());


System.out.println("ID Number: " + employee1.getIdNumber());
System.out.println("Department: " + employee1.getDepartment());
System.out.println("Position: " + employee1.getPosition());
}
}
2. Write a main (1 method in the EmpLoyeetest class that does the follwinge

I. Create objects that have the information in Table 5.1


import java.util.ArrayList;

public class EmployeeTest {

public static void main(String[] args) {


// i. Create objects with the information in Table 5.1.
Employee emp1 = new Employee("Dr. Nor Sabrina", 47899,
"Accounting", "Vice President");
Employee emp2 = new Employee("Dr. En Ahmad", 39119, "IT",
"Programmer");
Employee emp3 = new Employee("Prof M Izzudin", 66666,
"Consultation", "Manager");
Employee emp4 = new Employee("Dr N Izzati", 34521, "Elect
& Electronic", "CEO");
Employee emp5 = new Employee("Dr A Rahman", 12321, "PR",
"Manager");

II. Create an object from class Arraylist and populate it with the Employee
objects using method add () .
// ii. Create an object from class ArrayList and populate it with
Employee objects using method add().
ArrayList<Employee> employeeList = new ArrayList<>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);

III. Print all elements of the list.


// iii. Print all elements of the list.
System.out.println("Employee List:");
for (Employee employee : employeeList) {
System.out.println("Name: " + employee.getName() + ",
Position: " + employee.getPosition());
}

IV. Invoke other available methods from class ArrayList such as: contains (),

isEmpty (), set (), size (), remove ()


// iv. Invoke other available methods from class ArrayList
System.out.println("\nAdditional ArrayList Methods:");
System.out.println("Contains emp3: " +
employeeList.contains(emp3));
System.out.println("Is the list empty? " +
employeeList.isEmpty());
a. Modify your EmployeeTest class to display name and position for each

employee from the list.


// Modify the position of emp2
emp2.setPosition("Senior Programmer");
System.out.println("Updated Position of emp2: " +
emp2.getPosition());

System.out.println("Size of the list: " + employeeList.size());

b. Modify the declaration of the ArrayList object in the EmployeeTest to a

type-safe ArrayList object for Employees by using generics.


// Remove emp4 from the list
employeeList.remove(emp4);
System.out.println("Employee List after removing emp4:");
for (Employee employee : employeeList) {
System.out.println("Name: " + employee.getName() + ",
Position: " + employee.getPosition());
ALL COMAND:
import java.util.ArrayList;

public class EmployeeTest {

public static void main(String[] args) {


// i. Create objects with the information in Table 5.1.
Employee emp1 = new Employee("Dr. Nor Sabrina", 47899,
"Accounting", "Vice President");
Employee emp2 = new Employee("Dr. En Ahmad", 39119, "IT",
"Programmer");
Employee emp3 = new Employee("Prof M Izzudin", 66666,
"Consultation", "Manager");
Employee emp4 = new Employee("Dr N Izzati", 34521, "Elect &
Electronic", "CEO");
Employee emp5 = new Employee("Dr A Rahman", 12321, "PR",
"Manager");

// ii. Create an object from class ArrayList and populate it with


Employee objects using method add().
ArrayList<Employee> employeeList = new ArrayList<>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);

// iii. Print all elements of the list.


System.out.println("Employee List:");
for (Employee employee : employeeList) {
System.out.println("Name: " + employee.getName() + ", Position:
" + employee.getPosition());
}

// iv. Invoke other available methods from class ArrayList


System.out.println("\nAdditional ArrayList Methods:");
System.out.println("Contains emp3: " +
employeeList.contains(emp3));
System.out.println("Is the list empty? " + employeeList.isEmpty());

// Modify the position of emp2


emp2.setPosition("Senior Programmer");
System.out.println("Updated Position of emp2: " +
emp2.getPosition());

System.out.println("Size of the list: " + employeeList.size());

// Remove emp4 from the list


employeeList.remove(emp4);
System.out.println("Employee List after removing emp4:");
for (Employee employee : employeeList) {
System.out.println("Name: " + employee.getName() + ", Position:
" + employee.getPosition());
}
}
}
3. Student class has the following attributes and methods:

I. Name: oftype String


II. Age :of type integer

III. Majoring: of type String


IV. Country: of type String

V. Two constructors, with no-argument and three (3) arguments.


VI. Appropriate mutator functions that store values in the attributes.

VII. Appropriate accessor functions that return the values in the attributes

public class Student {


// Attributes
private String name;
private int age;
private String majoring;
private String country;

// Constructors
// No-argument constructor
public Student() {
// Default values
this.name = "";
this.age = 0;
this.majoring = "";
this.country = "";
}

// Constructor with three arguments


public Student(String name, int age, String majoring, String country) {
this.name = name;
this.age = age;
this.majoring = majoring;
this.country = country;
}

// Mutator methods
public void setName(String name) {
this.name = name;
}

public void setAge(int age) {


this.age = age;
}

public void setMajoring(String majoring) {


this.majoring = majoring;
}

public void setCountry(String country) {


this.country = country;
}

// Accessor methods
public String getName() {
return name;
}

public int getAge() {


return age;
}

public String getMajoring() {


return majoring;
}

public String getCountry() {


return country;
}

// Example Usage
public static void main(String[] args) {
// Creating an instance of the Student class using no-argument
constructor
Student student1 = new Student();

// Setting values using mutator methods


student1.setName("John Doe");
student1.setAge(20);
student1.setMajoring("Computer Science");
student1.setCountry("United States");

// Retrieving values using accessor methods


System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());
System.out.println("Majoring: " + student1.getMajoring());
System.out.println("Country: " + student1.getCountry());
}
}
4. Write a main () method in the studentest class that does the following:

I. Create objects that have the information in Table 5.2


II. Create an object from class Vector and populate it with the Student

objects using method addElement ()


III. .Add or insert country of origin of the student.

IV. Print the second element of the list.


V. Invoke other available methods from class vector such as: contains (),

insertElement (x), isEmpty (), set (), capacity (), remove ()


VI. public class Student {
// Attributes
private String name;
private int age;
private String majoring;
private String country;

// Constructors
// No-argument constructor
public Student() {
// Default values
this.name = "";
this.age = 0;
this.majoring = "";
this.country = "";
}

// Constructor with three arguments


public Student(String name, int age, String majoring,
String country) {
this.name = name;
this.age = age;
this.majoring = majoring;
this.country = country;
}

// Mutator methods
public void setName(String name) {
this.name = name;
}

public void setAge(int age) {


this.age = age;
}

public void setMajoring(String majoring) {


this.majoring = majoring;
}

public void setCountry(String country) {


this.country = country;
}

// Accessor methods
public String getName() {
return name;
}

public int getAge() {


return age;
}

public String getMajoring() {


return majoring;
}

public String getCountry() {


return country;
}

// Example Usage
public static void main(String[] args) {
// Creating an instance of the Student class using no-
argument constructor
Student student1 = new Student();

// Setting values using mutator methods


student1.setName("John Doe");
student1.setAge(20);
student1.setMajoring("Computer Science");
student1.setCountry("United States");

// Retrieving values using accessor methods


System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());
System.out.println("Majoring: " +
student1.getMajoring());
System.out.println("Country: " +
student1.getCountry());
}
}

You might also like