DN07
This commit is contained in:
parent
6983d96cf8
commit
8f40ed195b
59
src/DN07.java
Normal file
59
src/DN07.java
Normal file
|
@ -0,0 +1,59 @@
|
|||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class DN07 {
|
||||
public static void main(String[] args) {
|
||||
Planet[] planeti;
|
||||
try {
|
||||
planeti = preberiPlanete(args[0]);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Datoteka ne obstaja");
|
||||
return;
|
||||
}
|
||||
|
||||
String[] imena = args[1].toLowerCase().split("\\+");
|
||||
|
||||
double sum = 0;
|
||||
for (String ime : imena) {
|
||||
for (Planet planet :planeti) {
|
||||
if (planet.ime.equals(ime)) {
|
||||
sum += planet.povrsina();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
System.out.printf("Povrsina planetov \"%s\" je %d milijonov km2\n", args[1], Math.round(sum / 1000000));
|
||||
}
|
||||
|
||||
static Planet[] preberiPlanete(String datoteka) throws FileNotFoundException {
|
||||
File file = new File(datoteka);
|
||||
Scanner scanner = new Scanner(file);
|
||||
|
||||
Planet[] planeti = new Planet[8];
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (!scanner.hasNextLine()) break;
|
||||
|
||||
String line = scanner.nextLine();
|
||||
String[] parts = line.split(":");
|
||||
|
||||
planeti[i] = new Planet(parts[0].toLowerCase(), Integer.parseInt(parts[1]));
|
||||
}
|
||||
|
||||
return planeti;
|
||||
}
|
||||
}
|
||||
|
||||
class Planet {
|
||||
String ime;
|
||||
int radij;
|
||||
|
||||
public Planet(String ime, int radij) {
|
||||
this.ime = ime;
|
||||
this.radij = radij;
|
||||
}
|
||||
|
||||
public double povrsina() {
|
||||
return 4*Math.PI*radij*radij;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user