0% found this document useful (0 votes)
15 views

Aj0k69 1

Uploaded by

2023466072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Aj0k69 1

Uploaded by

2023466072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

AHMAD ZUNNURAIN BIN ALIAS (2023466072 )

LAB 1

INTRODUCTION TO DATA STRUCTURES

1. Implement the following ADT classes in Java:

Class Box<T> private public class Student{ private


T t; int matricno;
public void set(T t) {…} private String name;
public T get() {… } } private int mark;
public Student(String n, int
mn,int mark){...} public String getName()
{...} public int getMark(){...}
}

public class Salesman{


int s_id,age; String
name;
float basic,sales;

public Salesman(){...} public


Salesman(int id,String nm){...} public
void setSales(float s){...} public void
getSales(){...}
public boolean achieveTarget(float target){...}//if sales bigger than target return true
// else false
}

Write a main program that will do the following:

a. Create THREE Box objects named salesBox, stuBox and intBox.


b. Store ONE Salesman object into salesBox.
c. Store ONE Student object into stuBox.
d. Store an integer into intBox.
e. Display the achieved target for salesman, name for student and integer times three for
the integer.
f. Rewrite the Box Object using casting technique.
2. Now implement another ADT class named Pair. It will store two types of generic data. It will
has two get methods for both data. Use either generic class or Object.

Write a main program that will do the following:

a. Create ONE Pair object named totalPair.


b. Store TWO Salesman objects into totalPair.
c. Create ONE Pair object named markPair.
d. Store TWO Student objects into markPair.
e. Display total sales for the two salesmen and average mark for the two students.
FULLCODING

class Box<T> {
private T t;

public void set(T t) {


this.t = t; }

public T get() {
return t; }
}
public class Student {
private int matricno;
private String name;
private int mark;

public Student(String n, int mn, int mark) {


this.name = n;
this.matricno = mn;
this.mark = mark;
}

public String getName() {


return name;}

public int getMark() {


return mark;}
}

public class Salesman {


int s_id, age;
String name;
float basic, sales;

public Salesman() {}

public Salesman(int id, String nm) {


this.s_id = id;
this.name = nm;}

public void setSales(float s) {


this.sales = s; }
public float getSales() {
return sales; }

public boolean achieveTarget(float target) {


return sales > target;}
}

public class Pair<T, U> {


private T first;
private U second;

public Pair(T first, U second) {


this.first = first;
this.second = second;}

public T getFirst() {
return first;}

public U getSecond() {
return second; }
}

public class Main {


public static void main(String[] args) {
// A) Create THREE Box objects named salesBox, stuBox and intBox.
Box<Salesman> salesBox = new Box<>();
Box<Student> stuBox = new Box<>();
Box<Integer> intBox = new Box<>();

// B) Store ONE Salesman object into salesBox.


Salesman salesman = new Salesman(1, "John");
salesman.setSales(5000);
salesBox.set(salesman);

// C) Store ONE Student object into stuBox.


Student student = new Student("Alice", 123, 85);
stuBox.set(student);

// D) Store an integer into intBox.


intBox.set(10);
// E) Display the achieved target for salesman, name for student and integer times three
for the integer.
Salesman salesInBox = salesBox.get();
System.out.println("Salesman " + salesInBox.name + " achieved target: " +
salesInBox.achieveTarget(4000));

Student studentInBox = stuBox.get();


System.out.println("Student name: " + studentInBox.getName());

int intInBox = intBox.get();


System.out.println("Integer times three: " + (intInBox * 3));

// F) Rewriting Box Object using casting technique (Optional)


Box salesBoxCasted = (Box) salesBox;
Box stuBoxCasted = (Box) stuBox;
Box intBoxCasted = (Box) intBox;

// Create ONE Pair object named totalPair and store TWO Salesman objects into totalPair.
Pair<Salesman, Salesman> totalPair = new Pair<>(new Salesman(1, "John"), new
Salesman(2, "Jane"));

// Create ONE Pair object named markPair and store TWO Student objects into markPair.
Pair<Student, Student> markPair = new Pair<>(new Student("Alice", 123, 85), new
Student("Bob", 456, 90));

// Display total sales for the two salesmen and average mark for the two students.
Salesman firstSalesman = totalPair.getFirst();
Salesman secondSalesman = totalPair.getSecond();
System.out.println("Total sales for the two salesmen: " + (firstSalesman.getSales() +
secondSalesman.getSales()));

Student firstStudent = markPair.getFirst();


Student secondStudent = markPair.getSecond();
System.out.println("Average mark for the two students: " + ((firstStudent.getMark() +
secondStudent.getMark()) / 2));
}
}

You might also like