OOPS Lab-6 [ INTERFACES]

Post date: Oct 31, 2017 5:27:23 AM

I.Develop a JAVA program to simulate the movement of chess pieces on a chess board, using interfaces. Define an interface movable with a method move(String newPos). Further define an abstract class Piece, and classes for each type of chess piece (eg King, Queen etc) and override the method move(String newPos). (Pl refer to the sample implementation shown below)

interface Movable() { void move(String newPos); }

abstract class ChessPiece implements movable {

String name; // eg “King”, “Queen” etc.

String color; // “White”, “Black”

String curPos; // “a1”,“c5” std chess board naming convention

public ChessPiece (/*appropriate parameters */ ) { }

}

class King extends ChessPiece {

/* appropriate implementation details */

}

In the main program create an ArrayList of Pieces, (any 3 of your choice) and simulate the movement on these pieces on the chess board. You need not check if the move is a legal move, just print the message <Color> <PieceName> moving from <curPos> to <newPos> as shown below.

Eg: (Comp) : Created 3 chess Pieces (eg. King(White):1, Queen(Black):2 , Pawn(White):3).

(Comp) : Enter which piece to be moved (0 to Quit) & new Position

(User) : 1 a6

(Comp) : White King moving from a7 to a6

(Comp) : Enter which piece to be moved (0 to Quit) & new Position

(User): 2 c5

(Comp) : Black Queen moving from c2 to c5

(Comp) : Enter which piece to be moved (0 to Quit) & new Position

(User) : 0 0

(Comp) : Bye, Thank you !!

II Define a class Book which contains private members name (type String), cost (type double). This class implements the comparable interface as shown below.

class Book implements Comparable<Book> {

private String name,

private double cost;

// fill in the constructor.

// fill in the implementation to compare by name

// Any other methods required..

} // of class Book

Define another class GeneralizedSearch, having a method search(), as shown below.

class GeneralizedSearch {

public static boolean search (Object[] arr, Object item) {

// provide the implementation to return true or false,

// depending on whether the item is present in arr or not.

}

}

Define a class containing the main method, which creates an array of objects of class Book and uses the class GeneralizedSearch to serach of a specific book (by name). Finally display the book details if present.

Also implement the Comparator interface and sort the books on descending order of price and display.