Given an abstract Shape class. Implement 2 concrete classes named Rectangle and
ID: 3581650 • Letter: G
Question
Given an abstract Shape class. Implement 2 concrete classes named Rectangle and Circle. Create new rectangle and circle objects
and sort them into an Array List. Now filter the array list and output all the rectangles with a length greater than 5, using a for Loop
with an instaceof
public abstract class Shape { // abstract class, can't instantiate
// to implement an idNumber
private static int counter = 0;
private int idNumber;
public Shape () {
idNumber = ++counter;
}
public int getIdNumber() { return idNumber;}
public double area() { return 0.0; }
public double volume() { return 0.0; }
public abstract String getName(); // abstract, so omit body
}
Explanation / Answer
with my knowledge i write the above code in a simple mannner as follows with your given requirements.
here i used collection class and arraylist and sort method.
import java.util.*; //for using collection class sorting purpose
public abstract class shape
{
public abstract void display(); // abstract method
}
public class rectangle extends shape
{
int i;
void display()
{
lists.add(5);
lists.add(7);
lists.add(3);
lists.add(9);
lists.add(1);
if(lists.length>=5) //performing condition if the arraylist is more than 5 length or not?
{
Collections.sort(lists); //sorting operation performing
for(i=;0;i<= lists.length;i++)
{
system.out.println("the rectangle array list is"+list[i]);
}
}
}
public class circle extends shape
{
int j;
void display()
{
lists.add(5);
lists.add(7);
lists.add(3);
lists.add(9);
lists.add(1);
for(j=;0j<= lists.lenght;j++)
{
system.out.println("the circle array list is"+lists[j]);
}
}
public class Details
{
public static void main(String args[])
{
ArrayList<int> lists = new ArrayList<int>();//creating arraylist
rectangle rect=new rect();
rect.display();// calling display method in rectangle class
circle cir=new cir();
cir.display();calling display method in circle class
}
}
Thank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.