1.You should define a class named IntegerArraySorting_YourLastName that maintains an integer array and a sorting method
The constructor should accept the list of numbers entered from keyboard as a String and the sorting method. Based on the sorting method chosen from users, a method of sorting algorithm will be called, and the result will be displayed.
The sort methods could insertionSort, bubbleSort, selectionSort and their algorithms are provided below
The following are some sorting algorithm provide for your reference. You should not just copy these algorithms to your class; learn the logic and find the way to place them to your class.
public void insertionSort( )
{
for (int i = 1; i < n; i++)
{
int j = i;
int B = array[i];
while ((j > 0) && (array[j-1] > B))
{
array[j] = array[j-1];
j--;
}
array[j] = B;
}
} //where array is an array with size = n
public void bubbleSort( ) {
int n = x.length;
for (int pass=1; pass < n; pass++) { // count how many times
// This next loop becomes shorter and shorter
for (int i=0; i < n-pass; i++) {
if (x[i] > x[i+1]) {
// exchange elements
int temp = x[i];
x[i] = x[i+1];
x[i+1] = temp;
}
}
}
} //where x is an array with size = n
public void selectionSort( ) {
for (int i=0; ifor (int j=i+1; j if (x[i] > x[j]) {
//... Exchange elements
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
}
} //where x is an array with size = n
2.Based on the above psuedo-code, write the code of the application named SP15LAB2_LastName_SortingApplication.java This class contains main() that provide the menu, read input, create the object and display the result. Ensure it is compiled successfully and do all the requirement asks for.
Apply Inheritance and Polymorphism to provide the UML of the following shapes (7 UML) and pseudo-code of the application (main) that allows users to calculate the perimeter, the area and the volume then display the result in the following format Example for the square and the cylinder where side, radius and height are entered from the keyboard
Shape: SQUARE
Side: 4.5
Perimeter: 18.0
Area: 20.25
Shape: CYLINDER
Radius: 2.1
Height: 3.5
Area: 60.0
Volume: 48.47
The program is only terminiate when users select exit. The formulas to calculate the perimeter, area and volume are provided in the following table:
SHAPE NAME PERIMETER AREA VOLUME
Square 4 x Side Side2
Triangle Side1 + Side2 + Side3 Base x height / 2
circle 2 x pi x radius Pi x radius2
Trapezoid Height x (base1 + base2) /2 Height x (base1 + base2)/2
Cube 6 x side2 Side3
cylinder (2 x pi x radius x height) + 2 x pi x radius2 Pi x radius2 x height
HOW TO DO THE LAB
Create the project named SP15LAB2_LastName_PROJECT2
Add the following classes into the project:
You should define 8 classes:
Class Shape: has a side(double), name(String), some methods to calculate the perimeter, area and volume but do nothing and method toString to display the information
All the other data type classes inherit from class Shapes that define more sides as needed and provide the body of methods to calculate perimeter, area, volume and method toString
controlling class SP15LAB2_LastName_Geometry that stores main()
main(): includes the menu
SP15LAB2_LastName_Geometry
1. Square
2. Triangle
3. Circle
4. Trapezoid
5. Cube
6. Cylinder
7. exit
You have to apply polymorphism in this lab.
To do that you should declare an object of class Shape (for example: Shape shapeObject;) then after selecting a shape, you should use this shapeObject to point to the new selected shape object
For example:
Shape shapeObject;
shapeObject = new Square(side);
After creating selected object as above, then you can use shapeObject to call any methods of class Square
Remember to use the method toString to display the result and the output should be as above