I recently worked on a program that asked the user for his age in: years, months, and days.
After receiving that input, it had to calculate and print
a) Age in Seconds (totalAgeInSecs) and
b) the Amount of Seconds left to live. b is going to be based on the average lifespan in seconds (avgLifeSpan = 250000000000l. So secondsLeft = avgLifeSpan - totalAgeInSecs)
.
Anyway,I was able to get the program to work utilizing (switch) statements for simplicity purposes and not having to write a bunch of if/else statements, but I feel that in doing that, I ended up writing repetitive lines, and I'd like to be able to not have to repeat the calculation or the print statements.
I know there are classes and arrays I can combine with loops, but for the sake of simplicity and logic understanding I didn't use them to understand the bareback bones and logic of this project in "English." haha.
Anyway, check the code out below and let me know your thoughts on how to simplify the repetitive lines or better ways of approaching this. Thanks.
import java.util.*;
public class AgeInSeconds {
static Scanner kbd = new Scanner(System.in);
public static void main(String[] args) {
int totalNumDays, daysInMonth, daysToHours;
int yrsToDays,minsInHr, secsInMin;
long timeRemaining, avgLifeSecs;
System.out.println("Enter your age in years months and days: ");
System.out.print("Years: ");
int years = kbd.nextInt();
System.out.print("Months: ");
int months = kbd.nextInt();
System.out.print("Days: ");
int days = kbd.nextInt();
yrsToDays = years * 365;
avgLifeSecs = 2500000000l;
switch (months){
case 1:
daysInMonth = 31;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 2:
daysInMonth = 59;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 3:
daysInMonth = 90;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 4:
daysInMonth = 120;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 5:
daysInMonth = 151;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 6:
daysInMonth = 181;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 7:
daysInMonth = 212;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 8:
daysInMonth = 243;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 9:
daysInMonth = 273;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 10:
daysInMonth = 304;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 11:
daysInMonth = 334;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
break;
case 12:
daysInMonth = 365;
totalNumDays = yrsToDays + daysInMonth + days;
daysToHours = totalNumDays * 24;
minsInHr = daysToHours * 60;
secsInMin = minsInHr * 60;
timeRemaining = avgLifeSecs - secsInMin;
System.out.printf("You have been alive for %,d seconds.\n", secsInMin);
System.out.printf("The average human life is %,d seconds.\n", avgLifeSecs);
System.out.printf("You have %,d seconds.\n", timeRemaining);
default:
}
kbd.close();
}
}
This is the output when: years = 24, months = 5, days = 8.
Enter your age in years months and days:
Years: 24
Months: 5
Days: 8
You have been alive for 770,601,600 seconds.
The average human life is 2,500,000,000 seconds.
You have 1,729,398,400 seconds.