Pleased I knocked it out pretty quickly, possibly used too many variables but reads OK to me. Side note talking at work about hardest programming languages, I had brainfuck down as hard but Malbolge is another level of toughness.
package com.company;
import java.text.DecimalFormat;
public class XmasLights {
private final int hoursLightRequired;
private final int batteryCapacity = 1200;
private final int ledmA = 20;
private final double ledVoltage = 1.7;
private final int ledsPerCiruit = 5;
private final int batteryVoltage = 9;
public XmasLights(int hoursLightRequired) {
this.hoursLightRequired = hoursLightRequired;
}
public void go() {
int leds = amountOfLedsForTime();
resistanceNeeded();
drawCircuit(leds);
}
//part 1, input is the max amount of leds we can have for that time
public int amountOfLedsForTime() {
int maxLeds = (batteryCapacity / hoursLightRequired / ledmA) * ledsPerCiruit;
return maxLeds;
}
public void drawCircuit(int lights) {
String serialCircuit = "--|>|---|>|---|>|---|>|---|>|--";
String joiningBars = " | |";
System.out.println("Scheme:");
int ledsToDraw = lights / ledsPerCiruit;
for (int i = 0; i < ledsToDraw; i++) {
if (i == 0) {
System.out.println("*" + serialCircuit + "*");
} else {
System.out.println(serialCircuit);
}
if (i < ledsToDraw - 1) {
System.out.println(joiningBars);
}
}
}
public double resistanceNeeded() {
double voltageOverResister = batteryVoltage - (ledsPerCiruit * ledVoltage);
double ohms = voltageOverResister / (batteryCapacity / 1000d);
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("Resistance needed: " + df.format(ohms));
return ohms;
}
}
No comments:
Post a Comment