Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - Software reuse through polymorphism

craigster
craigster 


One benefit of polymorphism is making code reusable. One piece of algorithm can be used to serve different data types. The file Sorting.java contains the Sorting class. This file can be found below. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable (interface) objects in ascending order. You will use the Sorting class to sort two different types of objects: Circle and Book. 1. Define a new class named Circle. Each circle object should have a radius variable. Declare any method you think usful for this class. Also make sure your Circle class is a compitable type to use the sorting methods. 2. Define another class named Book. A book has two variables: a title and the number of pages. Make your Book class is a compitable type to use the sorting methods too. 3. Create a tester class. In the main method, declare two arrays, with size 5 for both. One array is Circle type and saves 5 Circle objects. The other array is Book type and saves 5 Book objects. Instantiate the arrays with your own data. 4. In the tester class, call the sorting method (either is ok) in the provided Sorting class to sort both array. Print out the sorted result. You need to call the sorting method twice, one for each array. You do not need to modify anything in the Sorting class, but again, you must do something in the Book and Circle classes to make sure they are compatable types to use the sorting methods. Instructions and the required java file are all in the attached microsoft word document. I need this answered by 10:30 pm today, eastern time. That is why I am listing it at a high price. Thank you ! Attachments Tags Clarifications

Answers

Java - Software reuse through polymorphism - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - Polymorphism assignment including source and comments Attachments
Sorting.zip (1 K) Download
Preview
= temp; } } //----------------------------------------------------------------- // Sorts the specified array of objects using the insertion // sort algorithm. //----------------------------------------------------------------- public static void insertionSort (Comparable[] list) { for (int index = 1; index < list.length; index++) { Comparable key = list[index]; int position = index; //

Preview
Circle(5); circle[4] = new Circle(8); //sort books Sorting.selectionSort(book); //print books System.out.println("------------Books sorted------------"); for(Book newbook : book) { System.out.println(newbook); } //sort circles Sorting.selectionSort(circle); //print circles System.out.println(); System.out.println("------------Circles sorted------------"); for(Circle newcircle : circle)

Preview
name name of book * @param numberPages number of pages */ public Book(String name, int numberPages) { this.name = name; this.numberPages = numberPages; } /** * Get book name * @return the name */ public String getName() { return name; } /** * Set book name * @param name

Preview
Get radius * @return radius */ public int getRadius() { return radius; } /** * Set radius * @param radius radius */ public void setRadius(int radius) {

Screenshots
Purchase Answer

Assigment

mariobeka
mariobeka 



In the zip file you find implemented classes that were asked in the attached word document. Attachments
Screen Shot 2014-03-13 at 10.13.59.png (17 K) Download Preview

AssigmentSorting.zip (6 K) Download







Preview
} /** * @param numberPages * the numberPages to set */ public void setNumberPages(int numberPages) { this.numberPages = numberPages; } public boolean equals(Object other) { return (name.equals(((Book) other).getName())); } @Override public int compareTo(Book o) { if (!getName().equals(o.getName())) { return this.name.compareTo(o.name); }

Preview
int getRadius() { return radius; } /** * @param radius the radius to set */ public void setRadius(int radius) { this.radius =

Preview
{ int min; Comparable temp; for (int index = 0; index < list.length-1; index++) { min = index; for (int scan = index+1; scan < list.length; scan++) if (list[scan].compareTo(list[min]) < 0) min = scan; // Swap the values temp = list[min]; list[min] = list[index]; list[index]

Preview
variables Book[] book = new Book[5]; Circle[] circle = new Circle[5]; //initialize book variable book[0] = new Book("Code Complete", 250); book[1] = new Book("The Art of Computer Programming", 450); book[2] = new Book("The Art of

Screenshots
Screenshot
Purchase Answer