Hello there, please help me solve this. I would greatly
appreciate it.
Exercise 1
- Implement the following method that returns the maximum
element in an array:
public static > E max( E[ ] list )
-Write a generic method that returns the maximum element in a
two-dimensional array.
public static > E max( E[ ] [ ] list )
- Implement the following method using binary search:
public static > int binarySearch( E[ ] list, E key )
A binary search requires that the elements of the array be in
alphabetical order. Verify how to order the list based on the
compareTo method implementation of the elements in the list.
- Write the following method that sorts an
ArrayList:
> public static > void sort( ArrayList list )
- Use the Person and Car class below to test the methods you've
defined. For each, make an array (or arrayList) of Person objects
and Car objects. Ensure that the list you use for the binarySearch
method is in ascending order (alphabetically/numerically). Test
each of the methods.
Person.java
public class Person implements Comparable{
private String name;
private int age;
public Person( String name, int age ){
this.name = name;
this.age = age;
}
public String getName(){ return this.name;
}
public int getAge(){ return this.age; }
public void setName(){
this.name = name;
}
public void setAge(){
this.age = age;
}
@Override
public int compareTo( Person p ){
return this.age -
p.getAge();
}
@Override
public String toString(){
return "Name: " + this.name
+ "\n" + "Age: " + this.age;
}
}
Car.java
public class Person implements Comparable{
private String name;
private int age;
public Person( String name, int age ){
this.name = name;
this.age = age;
}
public String getName(){ return this.name;
}
public int getAge(){ return this.age; }
public void setName(){
this.name = name;
}
public void setAge(){
this.age = age;
}
@Override
public int compareTo( Person p ){
return this.age -
p.getAge();
}
@Override
public String toString(){
return "Name: " + this.name
+ "\n" + "Age: " + this.age;
}
}
Hello there, please help me solve this. I would greatly appreciate it. Exercise 1 - Implement the following method that
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am