Arraylist Exercise 508
Arraylist Exercise 508
EXERCISE ARRAYLIST
Given the following MyString and MyArrayList ADTs, and the main method of the
application class:
class MyString {
class 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"));
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”);
} // end of main()
ANSWER Q1
(CODING)
Class MyString
Class MyArrayList
public MyArrayList() {
myArray = new MyString[DEFAULT_ARRAY_SIZE];
}
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:
class Smartphone {
// normal constructors
// mutators and accessors
// toString() printer
}
class ArrayList {
mean = �1 , �2 … ��
�
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.
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
}
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)