import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Shape {
private final String type;
private final int x;
private final int y;
private final int size;
private final int[] color;
private final double area;
public Shape(String type, int x, int y, int size) {
this.type = type;
this.x = x;
this.y = y;
this.size = size;
this.color = new int[]{0, 0, 0};
if (type.equals("Circle")) {
area = Math.PI * Math.pow(size, 2);
} else {
area = Math.pow(size, 2);
}
}
public Shape(String type, int x, int y, int size, int r, int g, int b) {
this.type = type;
this.x = x;
this.y = y;
this.size = size;
this.color = new int[]{r, g, b};
if (type.equals("Circle")) {
area = Math.PI * Math.pow(size, 2);
} else {
area = Math.pow(size, 2);
}
}
public String getType() {
return type;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSize() {
return size;
}
public int[] getColor() {
return color;
}
public double getArea() {
return area;
}
public static void main(String[] args) {
int circleCount = 0;
int squareCount = 0;
double totalArea = 0;
try {
File file = new File("practical8.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
String[] tokens = line.split("\\s+");
if (tokens.length < 4) {
continue;
}
String type = tokens[0];
int x = Integer.parseInt(tokens[1]);
int y = Integer.parseInt(tokens[2]);
intsize = integer.parseint(tokens[3]);
shape shape;
if (type.equals("circle")) {
circlecount++;
if (tokens.length == 7) {
int r = integer.parseint(tokens[4]);
int g = integer.parseint(tokens[5]);
int b = integer.parseint(tokens[6]);
shape = new shape(type, x, y, size, r, g, b);
} else {
shape = new shape(type, x, y, size);
}
} else {
squarecount++;
if (tokens.length == 7) {
int r = integer.parseint(tokens[4]);
int g = integer.parseint(tokens[5]);
int b = integer.parseint(tokens[6]);
shape = new shape(type, x, y, size, r, g, b);
} else {
shape = new shape(type, x, y, size);
}
}
system.out.println("shape type: " + shape.gettype());
system.out.println("position: (" + shape.getx() + ", " + shape.gety() + ")");
system.out.println("size: " + shape.getsize());
system.out.println("color: [" + shape.getcolor()[0] + ", " + shape.getcolor()[1] + ", " + shape.getcolor()[2] + "]");
system.out.println("area: " + shape.getarea());
totalarea += shape.getarea();
}
scanner.close();
} catch (filenotfoundexception e) {
system.out.println("file not found!");
}
system.out.println("number of circles: " + circlecount);
system.out.println("number of squares: " + squarecount);
system.out.println("total area of all shapes: " + totalarea);
}
}
|