Skip to content

Instantly share code, notes, and snippets.

@umuturan
Last active June 23, 2016 14:38
Show Gist options
  • Save umuturan/bc105626c8e34ab5b5f1 to your computer and use it in GitHub Desktop.
Save umuturan/bc105626c8e34ab5b5f1 to your computer and use it in GitHub Desktop.
Lab-03A
/*
*LAB03
*Circle CLASS
* Extends Shape implements Selectable
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public class Circle extends Shape implements Selectable {
// properties
int radius;
boolean selected;
// constructor
public Circle(int radius) {
super();
this.radius = radius;
selected = false;
x=0;
y=0;
}
// methods
@Override
public double getArea() {
return Math.PI * Math.pow(radius, 2);
}
public String toString() {
String select;
if (selected) {
select = "selected";
} else
select = "unselected";
return "Circle with radius :" + radius + " (x,y = " + x + "," + y + ") " + select + '\n';
}
@Override
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
public boolean getSelected() {
return selected;
}
public void setSelected(boolean set) {
selected = set;
}
public Shape contains(int x, int y) {
Circle cir;
boolean checkX, checkY;
cir = new Circle(radius);
if (Math.abs(this.x - x) <= radius)
checkX = true;
else
checkX = false;
if (Math.abs(this.y - y) <= radius)
checkY = true;
else
checkY = false;
if (checkX && checkY)
return cir;
else
return null;
}
}
public Shape contains2(int x, int y){
// properties
Rectangle rec;
boolean checkX, checkY;
// initialise
rec = new Rectangle(height, width);
// algorithm
if (this.x + width > x )
checkX = true;
else
checkX = false;
if (this.y + height > y)
checkY = true;
else
checkY = false;
if (checkX && checkY)
return rec;
else
return null;
}
/*
*LAB03
*Locatable Interface
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public interface Locatable {
public int getX();
public int getY();
public void setLocation(int x,int y);
}
/*
*LAB03
*Rectangle CLASS
* Extends Shape implements Selectable
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public class Rectangle extends Shape implements Selectable {
// properties
int width;
int height;
boolean selected;
// constructor
public Rectangle(int width, int height) {
super();
this.width = width;
this.height = height;
selected = false;
}
// methods
@Override
public double getArea() {
return width * height;
}
public String toString() {
String select;
if (selected) {
select = "selected";
} else
select = "unselected";
return "Rectangle with heigth :" + height + " and width :" + width + " (x,y = " + x + "," + y + ") " + select
+ '\n';
}
@Override
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
public boolean getSelected() {
return selected;
}
public void setSelected(boolean set) {
selected = set;
}
public Shape contains(int x, int y) {
// properties
Rectangle rec;
double wlen;
double hlen;
boolean checkX, checkY;
// initialise
rec = new Rectangle(height, width);
wlen = width / 2;
hlen = height / 2;
// algorithm
if (Math.abs(this.x - x) <= wlen)
checkX = true;
else
checkX = false;
if (Math.abs(this.y - y) <= hlen)
checkY = true;
else
checkY = false;
if (checkX && checkY)
return rec;
else
return null;
}
}
/*
*LAB03
*Locatable Interface
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public interface Locatable {
public int getX();
public int getY();
public void setLocation(int x,int y);
}
/*
*LAB03
*Shape CLASS
* Abstract class implements Locatable
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public abstract class Shape implements Locatable {
//constructor
public Shape(){
x=0;
y=0;
}
public abstract double getArea();
// ---------implementation of Locatable interface---------------
int x, y; // x and y locations of a shape
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
}
import java.util.ArrayList;
/*
*LAB03
*ShapeContainer CLASS
* This class is to hold a set of shapes.
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public class ShapeContainer {
// properties
ArrayList<Selectable> sContainer;
// constructor
public ShapeContainer() {
sContainer = new ArrayList<Selectable>();
}
// methods
public void add(Selectable s) {
sContainer.add(s);
}
public double getArea() {
int sum;
sum=0;
for (int i = 0; i < sContainer.size(); i++) {
sum += ((Shape)sContainer.get(i)).getArea();
}
return sum;
}
public void removeAllSelected(){
boolean selectedCheck; // checks if no selected shape
selectedCheck =false;
for (int i = 0; i < sContainer.size(); i++) {
if ((sContainer.get(i)).getSelected()) {
System.out.println("Removed shape :" + sContainer.get(i));
sContainer.remove(i);
selectedCheck = true;
// boolean turns true if any selected shape exists
}else if (!selectedCheck) {
System.out.println("No such shape found!"+'\n');
}
}
}
public void findFirstContains(int x,int y){
boolean containCheck; // checks the first shape to contain given x,y
containCheck = true;
for (int i = 0; i < sContainer.size() && containCheck; i++) {
if (sContainer.get(i).contains(x, y) != null) {
sContainer.get(i).setSelected(true);
System.out.println("Shape is found :" + sContainer.get(i));
containCheck = false;
// boolean turns false when first shape is found
} else if (i == sContainer.size() - 1
&& sContainer.get(i).contains(x, y) == null) {
System.out.println("No such shape found!"+'\n');
}
}
}
public String toString(){
String result;
result="Shape container includes :"+'\n';
for(int i=0; i<sContainer.size();i++){
result+= sContainer.get(i).toString();
}
return result;
}
}
import java.util.ArrayList;
import java.util.Scanner;
/*
*LAB03
*ShapeTester CLASS
* Class with a menu that allows the user to
* choose an selection from options listed
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public class ShapeTester {
public static void main(String[] args) {
String input, shapeSelect;
Scanner scan;
ShapeContainer sContain;
int height, width;
int side;
int radius;
int x, y;
boolean check;
scan = new Scanner(System.in);
sContain = null;
check = false;
do {
System.out.println("---Welcome to Shape Tester!---");
System.out.println("1-Create an empty set of shapes.");
System.out.println("2-Add a shape");
System.out.println("3-Compute and print out the total surface area");
System.out.println("4-Print out info about all of the shapes in container");
System.out.println("5-Find the first Shape that contains a given x, y");
System.out.println("6-Removes all selected shapes from the set of shapes");
System.out.println("7-Quit");
// ***********************************************************************************
System.out.println("------------------------------------------------------------");
System.out.print("please choose one option :");
input = scan.next();
System.out.println();
if (!input.equals("1") && !check) {
System.out.println("You have to create an empty container first!" + '\n');
} else {
if (input.equals("1")) {
sContain = new ShapeContainer();
check = true;
System.out.println("New shape container have been created!" + '\n');
} else if (input.equals("2")) {
System.out.println("Enter the coordinates of center of the shape");
System.out.println("Please enter x :");
x = scan.nextInt();
System.out.println("Please enter y :");
y = scan.nextInt();
System.out.println("A-Rectangle" + '\n' + "B-Circle" + '\n' + "C-Square");
System.out.println("Please choose one of the shapes above:");
shapeSelect = scan.next();
if (shapeSelect.toLowerCase().equals("a")) {
System.out.println("Please enter the height :");
height = scan.nextInt();
System.out.println("Please enter the width :");
width = scan.nextInt();
Rectangle rec = new Rectangle(width, height);
rec.setLocation(x, y);
sContain.add(rec);
} else if (shapeSelect.toLowerCase().equals("b")) {
System.out.println("Please enter the radius :");
radius = scan.nextInt();
Circle circ = new Circle(radius);
circ.setLocation(x, y);
sContain.add(circ);
} else if (shapeSelect.toLowerCase().equals("c")) {
System.out.println("Please enter the side length:");
side = scan.nextInt();
Square sq = new Square(side);
sq.setLocation(x, y);
sContain.add(sq);
} else {
System.out.println("invalid input!");
}
} else if (input.equals("3")) {
System.out.println("The total area is:" + sContain.getArea() + '\n');
} else if (input.equals("4")) {
System.out.println(sContain);
} else if (input.equals("5")) {
System.out.println("Please enter x :");
x = scan.nextInt();
System.out.println("Please enter y :");
y = scan.nextInt();
sContain.findFirstContains(x, y);
// find the first Shape that contains a given x, y point and
// toggle its selected state.
} else if (input.equals("6")) {
sContain.removeAllSelected();
}
}
} while (!input.equals("7"));
System.out.println("Thanks for using ShapeTester!");
}
// End of the ShapeTester class
}
/*
*LAB03
*Square CLASS
* Extends Shape implements Selectable
*Author:@Umut Can Turan
*21401929
*Section 06
*/
public class Square extends Rectangle implements Selectable {
// properties
int side;
boolean selected;
// constuctor
public Square(int side) {
super(side, side);
this.side = side;
selected = false;
x=0;
y=0;
}
// methods
public String toString() {
String select;
if (selected) {
select = "selected";
} else
select = "unselected";
return "Square with a side :" + side + " (x,y = " + x + "," + y + ") " + select + '\n';
}
@Override
/*
* public void setLocation(int x, int y) { // TODO Auto-generated method
* stub this.x = x; this.y = y;
*
* }
*/
/*
* public boolean getSelected() { return selected; }
*
* public void setSelected(boolean set) { selected = set;
*
* }
*/
public Shape contains(int x, int y) {
Square sq;
boolean checkX, checkY;
double sLen;
sq = new Square(side);
sLen = side / 2;
if (Math.abs(this.x - x) <= sLen)
checkX = true;
else
checkX = false;
if (Math.abs(this.y - y) <= sLen)
checkY = true;
else
checkY = false;
if (checkX && checkY)
return sq;
else
return null;
}
}
@b-basaran
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment