0% found this document useful (0 votes)
28 views11 pages

Arraylist Exercise 508

This document provides details about a data structure course for the semester of October 2023, including the course name and lecturer, and lists 5 students who are enrolled in the course and their student IDs and names.

Uploaded by

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

Arraylist Exercise 508

This document provides details about a data structure course for the semester of October 2023, including the course name and lecturer, and lists 5 students who are enrolled in the course and their student IDs and names.

Uploaded by

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

CSC508 - DATA STRUCTURE

SEMESTER OCTOBER 2023

EXERCISE ARRAYLIST

SEMESTER : OCTOBER 2022 – FEBRUARY 2023


COURSE NAME : Data Structure (508)
LECTURER : Dr Anis Amilah Bt Shari
PROGRAM : BACHELOR OF COMPUTER SCIENCE MULTIMEDIA
NAME COMPUTING
(CS253)

STUDENT STUDENT NAME GROUP


ID
2022864564 NURSYAFIQAH SYAHIRAH BT KHAIRULANUAR M3CS2534C

2022457742 SHAKIRA AFIQAH OOI BT ARIFF SHAH M3CS2534C

2022876724 MUHAMMAD AQIL HAKIMI BIN MUHAMMAD M3CS2534C


BAKRI

2022800194 NUR ADRINA MAISARA BINTI RUSLAN M3CS2534C

2022844894 NUR KHAIRENA NAJWA BINTI ZULKEPLEE M3CS2534C


Question 1:

Given the following MyString and MyArrayList ADTs, and the main method of the
application class:

class MyString {

private String str;

/**** Definition of the other methods including constructor, mutator,


accessor and printer
***/
}

class MyArrayList {

public MyArrayList() {…}


public MyString get(int index){…}
public void add(MyString e){…}
public void add(int index, MyString e) {…}
public void set(int index, MyString e) {…}
public MyString remove(int index) {…}
public int size() {…}
public String toString() {…}

/*** Definition of the other methods ***/


}

public static void main(String[] args) {

MyArrayList strArr = new MyArrayList ();

strArr.add(new MyString("honest"));
strArr.add(new MyString("humble"));
strArr.add(new MyString("kind"));
strArr.add(3, new MyString("humble"));
strArr.add(new MyString("cheerful"));
strArr.add(new MyString("polite"));

System.out.println("Value of output1: " + strArr.get(4));


System.out.println("Value in the strArr now: " + strArr.toString());

MyString strTemp = strArr.get(0);


strArr.set(0, strArr.get(1));
strArr.set(1, strTemp);
strTemp = strArr.get(0);
strArr.set(0, strArr.get(strArr.size()-1));
strArr.set(strArr.size()-1, strTemp);

System.out.println(“Value in the strArr now: ” + strArr.toString());

String key = strArr.remove(3).getStr();


boolean found = false;
int low = 0, high = strArr.size()-1;

while(high >= low) {


int mid = (low + high) / 2;
String str = strArr.get(mid).getStr();

if(key.compareTo(str)<0)
high = mid – 1;
else if(key.equalsIgnoreCase(str)) {
found = true;
break;
}
else
low = mid + 1;
}

if(found == true)
System.out.println(key + “ is found”);
else
System.out.println(key + “ is not found”);

System.out.println(“Value in the strArr now: ” + strArr);

} // end of main()

Trace and write the output of the program.


(10 marks)

ANSWER Q1

(CODING)

Class MyString

public class MyString {


private String str;

public MyString(String str) {


super();
this.str = str;
}

public String getStr() {


return str;
}

public void setStr(String str) {


this.str = str;
}

public void print() {


System.out.println(str);
}

public String toString() {


return str;
}
}

Class MyArrayList

public class MyArrayList {

private static final int DEFAULT_ARRAY_SIZE = 100;


private MyString[] myArray;
private int numElements = 0;

public MyArrayList() {
myArray = new MyString[DEFAULT_ARRAY_SIZE];
}

public MyArrayList(int size) {


if (size > 0) {
myArray = new MyString[size];
} else {
myArray = new MyString[DEFAULT_ARRAY_SIZE];
}
}

public void add(MyString element) {


this.myArray[this.numElements++] = element;
}

public void add(MyString element, int index) {


if (index >= 0) {
if (index == this.numElements) {
this.myArray[index] = element;
}else if (index < this.numElements) {

for (int i = numElements; i > index; i--) {


this.myArray[i] = this.myArray[i - 1];
}
this.myArray[index] = element;
this.numElements++;
}
}
}

public MyString get(int index) {


if ((index >= 0) && (index < this.numElements)) {
return this.myArray[index];
}
return null;
}

public void set(int index, MyString element) {


if (index >= 0 && index < numElements) {
myArray[index] = element;
}
}

public MyString remove(int index) {


MyString temp = null;
if ((index >= 0) && (this.numElements > 0) && (index <
this.numElements)) {
temp=myArray[index];
if (index != this.numElements - 1) {
for (int i = index; i < this.numElements - 1; i++)
{
this.myArray[i] = this.myArray[i + 1];
}
}
this.numElements--;
}
return temp;
}

public int size() {


return this.numElements;
}

public String toString() {


String output = "[";
for (int i = 0; i < this.numElements; i++) {
output = output + this.myArray[i];
if (i != this.numElements - 1) {
output = output + ", ";
}
}
output = output + "]";
return output;
}

public static void main(String[] args) {


MyArrayList strArr = new MyArrayList();
strArr.add(new MyString("honest"));
strArr.add(new MyString("humble"));
strArr.add(new MyString("kind"));
strArr.add(new MyString("humble"));
strArr.add(new MyString("cheerful"));
strArr.add(new MyString("polite"));

System.out.println("Value of output1: " + strArr.get(4));


System.out.println("Value in the strArr now: " +
strArr.toString());

MyString strTemp = strArr.get(0);


strArr.set(0, strArr.get(1));
strArr.set(1, strTemp);
strTemp = strArr.get(0);
strArr.set(0, strArr.get(strArr.size() - 1));
strArr.set(strArr.size() - 1, strTemp);

System.out.println("Value in the strArr now: "+strArr.toString());

String key = strArr.remove(3).getStr();


boolean found = false;

int low=0, high=strArr.size()-1;

while(high>=low){
int mid = (low+high)/2;
String str = strArr.get(mid).getStr();
if(key.compareTo(str)<0)
high=mid-1;
else if(key.equalsIgnoreCase(str)){
found=true;
break;
}else{
low = mid+1;
}
}

if(found==true){
System.out.println(key+" is found");
}else{
System.out.println(key+" is not found");
System.out.println("value in the strArr now: "+strArr);
}
}
}
(OUTPUT)

Question 2:

Given the following Smartphone and ArrayList ADTs:

class Smartphone {

private String brand; // e.g Apple, Samsung etc


private String sNumber; // serial number, e.g. 11111
private int mYear; // year of manufacture
private double price;

// normal constructors
// mutators and accessors
// toString() printer
}

class ArrayList {

public ArrayList() {…}


public void add(Smartphone e){…}
public Smartphone get(int index){…}
public void set(int index, Smartphone e) {…}

/*** Definition of the other methods ***/


}

In statistics studies, mean is the average of a data set. Therefore, if �1 , �2 … �� is a collection of


the Smartphone price and � represents the number of elements in the smartphone list, then:

mean = �1 , �2 … ��

Write a Java application program segment to do the following tasks:

a. Declare and create an object of ArrayList named phoneList. Then write statements
to prompt a user to insert 50 Smartphone objects into phoneList. Assume Scanner
object has been declared and created.
(4 marks)
b. Write a class method definition named calculateAverage(ArrayList) to calculate
and return the average price of all smartphones. This class method received phoneList
through its parameter.

static double calculateAverage(ArrayList phoneList) {…}


(3 marks)

c. Display the information of all smartphones which prices are higher than the average price.
The average price is returned by the method in (b) above.

ANSWER Q2

(OUTPUT)

Class SmartPhone

private String sNumber;


private int mYear;
private double price;
@Override
public String toString() {
return "SmartPhone{" +
"brand='" + brand + '\'' +
", sNumber='" + sNumber + '\'' +
", mYear=" + mYear +
", price=" + price +
'}';

}
public SmartPhone(String brand, String sNumber, int mYear, double price) {
this.brand = brand;
this.sNumber = sNumber;
this.mYear = mYear;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getsNumber() {
return sNumber;
}
public void setsNumber(String sNumber) {
this.sNumber = sNumber;
}
public int getmYear() {
return mYear;
}
public void setmYear(int mYear) {
this.mYear = mYear;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

Class Arraylist

import java.util.List;
public class ArrayList {
List<SmartPhone> smartPhoneList;
public ArrayList() {
this.smartPhoneList = new java.util.ArrayList<>();
}
public void add(SmartPhone e) {
this.smartPhoneList.add(e);
}
public SmartPhone get(int index) {
return this.smartPhoneList.get(index);
}
public void set(int index, SmartPhone e) {
smartPhoneList.set(index,e);
}
public int size() {
return this.smartPhoneList.size();
}
}

Class Test

import java.util.Scanner;
public class Test {
public static double calculateAverage(ArrayList phoneList) {
double totalPrice = 0;
for(int i=0;i<phoneList.size();++i) {
SmartPhone sp = phoneList.get(i);
totalPrice += sp.getPrice();
}
double average = totalPrice / phoneList.size();
return average;
}
public static void displatSmartPhonePriceGreaterThanAvg(ArrayList phoneList) {
double average = calculateAverage(phoneList);
for(int i=0;i<phoneList.size();i++) {
SmartPhone sp = phoneList.get(i);
if(sp.getPrice()>average) {
System.out.println(sp.toString());
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList phoneList = new ArrayList();
for(int i=0;i<50;i++) {
System.out.println("Enter phone brand name:");
String phoneBrand = in.next();
System.out.println("Enter serial number:");
String sNumber = in.next();
System.out.println("Enter year of manufacture:");
int mYear = in.nextInt();
System.out.println("Enter price:");
double price = in.nextDouble();
SmartPhone sp = new SmartPhone(phoneBrand,sNumber,mYear,price);
phoneList.add(sp);
}
System.out.println("Average :: "+calculateAverage(phoneList));
displatSmartPhonePriceGreaterThanAvg(phoneList);
}
}
(OUTPUT)

You might also like