Skip to content

Instantly share code, notes, and snippets.

@ad-m
Created September 10, 2015 23:29
Show Gist options
  • Select an option

  • Save ad-m/0ef46b7a4ba974dc18d4 to your computer and use it in GitHub Desktop.

Select an option

Save ad-m/0ef46b7a4ba974dc18d4 to your computer and use it in GitHub Desktop.
public class Circle extends Figure {
private double radius;
public Circle(double radius) {
super();
this.radius = radius;
}
double area() {
return 3.14*radius*radius;
}
@Override
public String toString() {
return "Circle [radius=" + radius + "]";
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class CLI {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int choose;
do{
System.out.print("Select option:");
choose = sc.nextInt();
Circle[] tab1 = new Circle[0];
Rectangle[] tab2 = new Rectangle[0];
Integer[] tab3;
if(choose == 1){
Circle c = new Circle(sc.nextDouble());
System.out.print("Area: "+c.area());
}else if(choose == 2){
Rectangle r = new Rectangle(sc.nextInt(), sc.nextInt());
System.out.print("Area: "+r.area());
}else if(choose == 3){
System.out.print("Circle count:");
int c = sc.nextInt();
tab1 = new Circle[c];
for(int i=0; i<c; i++){
tab1[i] = new Circle(sc.nextDouble());
}
System.out.print("Rectangle count:");
c = sc.nextInt();
tab2 = new Rectangle[c];
for(int i=0; i<c; i++){
tab2[i] = new Rectangle(sc.nextInt(), sc.nextInt());
}
}else if(choose == 4){
double epsilon = sc.nextDouble();
int smaller;
if(tab1.length > tab2.length){
smaller = tab1.length;
}else{
smaller = tab2.length;
}
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0; i<smaller; i++){
if(Math.sqrt(Math.pow(tab1[i].area()-tab2[i].area(), 2)) < epsilon){
list.add(i);
}
}
tab3 = list.toArray(new Integer[list.size()]);
}else{
System.out.print("Unknown option!");
}
}while(true);
}
}
abstract class Figure {
abstract double area();
}
public class Rectangle extends Figure {
private int width;
private int height;
public Rectangle(int width, int height) {
super();
this.width = width;
this.height = height;
}
double area() {
return this.width*this.height;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment