In: Computer Science
IN JAVA
Question 1
The following classes along with the driver has been created and compiled.
public class Car { public void method1() { System.out.println("I am a car object"); } } class Point { public void method2() { System.out.println("I am a Point object"); } } class Animal { public void method3() { System.out.println("I am an animal object"); } } The following driver class has been created and all the lines of code inside the for loop is not compiling. Modify the code and write it in the answer area so that it compiles and display the output. To get the full credit you cannot change anything else in the given code. you cannot change the code inside the above classes class Driver { public static void main(String[] args) { Object[] things = {new Car(), new Animal(),new Point()};//do not change this line for(int i = 0; i < things.length; i++) { things[i].method1(); //compiler error things[i].method2(); //compiler error things[i].method3(); //compiler error } } }
********************************************************************************************************************************************
Question 2
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList
Content of the ArrayList before push
[alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte]
content of the ArrayList after the push method
[alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa
khan, Jose Martinex]
import java.util.*; class Person { private String name; private String last; public Person(String name, String last) { this.name = name; this.last = last; } public String getLast() { return last; } public String getFirst() { return name; } public String toString() { return name + " " + last; } } public class ArrayList { public static void main(String[] args) { ArrayList<Person> list = new ArrayList<Person>(); list.add(new Person ("alex","Bus")); list.add(new Person("Mary", "Phillips")); list.add(new Person("Nik", "Lambard") ); list.add(new Person("Rose","Rodd")); list.add(new Person("Esa","khan")); list.add(new Person("Jose","Martinex")); list.add(new Person("Nik","Patte")); System.out.println(list); push(list); System.out.println(list); } //this method pushes all the people with the even length last name to the end of the list public static void push(ArrayList<Person> list) { } }
Question 1
Class Driver should be as follows
class Driver
{
public static void main(String[] args)
{
Object[] things = {new Car(), new Animal(),new Point()};//do not change this line
// You must typecast the object type to its corresponding class type
// You cannot use a for loop, as it may not throw a Compile time Exception
// but throws a Runtime Exception because method1() is called on the thing[0]
// and typecasted to Car class, which is not available in Animal and Point,
// So changing the Driver class as below, gives the desired output without any error.
((Car) things[0]).method1();
((Animal) things[1]).method3();
((Point) things[2]).method2();
}
}
OUTPUT
Question 2
//this method pushes all the people with the even length last name to the end of the list
public static void push(ArrayList<Person1> list) {
ArrayList<Person1> evenList = new ArrayList<Person1>();
// Filtering only list items that has even lengthed last name
evenList = (ArrayList<Person1>) list.stream().filter(action-> action.getLast().length()%2==0).collect(Collectors.toList());
list.removeAll(evenList);
list.addAll(evenList);
}
There can be many more approaches as well to deal with this program, I have made use of streaming.
OUTPUT