Programming Exercises: (From the Textbook) - 13.2 (Shuffle ArrayList) Write the
ID: 3854441 • Letter: P
Question
Programming Exercises: (From the Textbook) - 13.2 (Shuffle ArrayList) Write the following method that sorts an ArraysList of numbers.
public static void shuffle(ArrayList<Number> list)
this is the problem out of the book but i think they're non related. If you can do it without this part then do that. I'm a bit confused myself
13.2 The getArea() and getPerimeter() methods may be removed from the GeometricObject class. What are the benefits of defining getArea() and getPerimeter() as abstract methods in the GeometricObject class?
Explanation / Answer
Please find below the solution i have also explained the second question also.
//Shuffle ArrayList
//SortList.java
import java.util.ArrayList;
import java.util.Arrays;
public class SortList {
public static void shuffle(ArrayList<Number> list)
{
//declared an temp array of size of arraylist
int temp[] = new int[list.size()];
int i=0;
//adding numbers to temp array
for(Number n : list )
{
temp[i]=n.intValue();
i++;
}
//sorting the temp array
Arrays.sort(temp);
//removed the elements from arraylist
list.removeAll(list);
//adding the sorted value to arraylist
for(int j=0;j<temp.length;j++)
{
list.add(temp[j]);
}
}
public static void main(String[] args) {
// created a arraylist list
ArrayList<Number> list = new ArrayList<>();
//added some values to list
list.add(56);
list.add(23);
list.add(67);
list.add(40);
list.add(90);
list.add(34);
//printing the list before sorting
System.out.println("list before sorted ");
for(Number n : list)
System.out.println(n);
//sorting the list by calling suffle
shuffle(list);
//printing the sorted list
System.out.println(" List after sorted : ");
for(Number n : list)
System.out.println(n);
}
}
OUTPUT
list before sorted
56
23
67
40
90
34
List after sorted :
23
34
40
56
67
90
//2 nd part
if u make getArea() and getperimeter() methods as abstract in class GeometricObject
then the another class(not abstract) which will extends GeometricObject have to overide those methods
and have to provide its implementation of it.
it may helps to achieve polumorphism , one of the most valuable piller of OOPS.
There may different type of geometric object like square,rectangle,triangle,circle and much more.
every object have its own definition of getArea() and getperimeter().
Exapmle:
abstract class GeometricObject
{
abstract public float getArea();
abstract public float getperimeter();
}
//class rectangle have its own implementation of getArea and getperimeter
class Rectangle extends GeometricObject
{
private float length=10;
private float width=30;
@overriden
public float getArea()
{
return length*width;
}
@overriden
public float getperimeter()
{
return 2*(length+width);
}
}
//class circle have its own type of implementation of getArea and getperimeter
class Circle extends GeometricObject
{
private float pi = 3.14;
private float radius = 5.20;
@overriden
public float getArea()
{
return pi*r*r;
}
@overriden
public float getperimeter()
{
return 2*pi*radius;
}
}
//please do let me know if u have any doubts..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.