Created
December 22, 2015 16:08
Solution to Daily Programmer challenge #246
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package challenge246; | |
import java.math.BigDecimal; | |
import java.math.RoundingMode; | |
import java.util.Scanner; | |
public class XMassLights { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Numbers in brackets are examples"); | |
// LED Values | |
System.out.println("Enter LED voltage: (1.7)"); | |
double ledVoltage = sc.nextDouble(); | |
System.out.println("Enter LED current in mA: (20)"); | |
int ledCurrent_mA = sc.nextInt(); | |
int maxLEDs; | |
// Battery values. | |
System.out.println("Enter battery voltage: (9)"); | |
int batteryVoltage = sc.nextInt(); | |
System.out.println("Enter battery capacity in mAh: (1200)"); | |
int batteryCapacity_mAh = sc.nextInt(); | |
// Time | |
System.out.println("Enter duration in hours: (20)"); | |
int time = sc.nextInt(); | |
// Calculate amount of LEDs in series. | |
double tmp = batteryVoltage / ledVoltage; | |
int ledsInSeries = (int) tmp; | |
// Current | |
int current_mA; | |
double current_A; | |
// Regarding resistance | |
double resistance; | |
double totalLEDVoltage = ledVoltage * ledsInSeries; | |
double resistorVolatage = batteryVoltage - totalLEDVoltage; | |
/* Part 1: Max amount of lights */ | |
System.out.println("\r\nPart 1: Max amount of lights."); | |
System.out.println("Enter duration in hours: (1)"); | |
current_mA = batteryCapacity_mAh / time; | |
maxLEDs = (current_mA / ledCurrent_mA) * ledsInSeries; | |
System.out.println("Max number of LEDs: " + maxLEDs + "\r\n"); | |
/* Part 2: Drawing circuit */ | |
System.out.println("Part 2: Drawing circuit."); | |
int numLEDs = maxLEDs; | |
int numLEDsLeft = numLEDs; | |
int t; | |
System.out.print("*"); | |
while (numLEDsLeft > 1) { | |
if (numLEDsLeft >= ledsInSeries) | |
t = ledsInSeries; | |
else | |
t = numLEDsLeft; | |
// Printing LEDs. | |
for (int i = 0; i < t; i++) { | |
System.out.print("-" + "-|>|-" + "-"); | |
numLEDsLeft--; | |
} | |
// Printing end asterix | |
if (numLEDsLeft == numLEDs - ledsInSeries) | |
System.out.print("*"); | |
// Printing first LED joiner | |
if (numLEDsLeft > 0) | |
System.out.print("\n |"); | |
// Printing spaces | |
for (int i = 0; i < ledsInSeries; i++) { | |
System.out.print(" "); | |
} | |
// Printing end LED joiner | |
if (numLEDsLeft > 0) | |
System.out.print(" |\n"); | |
} | |
/* Part 3: Resistors */ | |
System.out.println("\r\n"); | |
System.out.println("Part 3: Resistors"); | |
current_mA = batteryCapacity_mAh / time; | |
current_A = (double) current_mA / 1000; | |
resistance = resistorVolatage / current_A; | |
resistance = XMassLights.round(resistance, 3); | |
System.out.println("Resistance: " + resistance + " Ohms"); | |
} | |
public static double round(double value, int places) { | |
BigDecimal bd = new BigDecimal(value); | |
bd = bd.setScale(places, RoundingMode.HALF_UP); | |
return bd.doubleValue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment