So my for loop causes a crash and I know exactly why, but I'm unsure of a way to rewrite this to get it to do what I'm trying to accomplish.
I'm trying to get it to take out money from a compounding interest each day and total it up for the month (which is set for ever 30 days). The user inputs the number of days they are trying to calculate for. So what is causing the crash is if they put in anything other than something divisible by 30.
I'm having difficulty coming up with a way to rewrite this and could use any suggestions. I would like it to do something if they put in 65 it calculates 2 months worth and then display like insuficiant days for month 3. Any help is always greatly appreciated. Thanks in advance.
for (int i = 0; i < numDays; i+=30){
double cash=0;
for (int n=1; n < 30; n++){
int currentDay;
currentDay= n+i;
cash=cash+dailyMoney[currentDay];
}
month++;
if(monthlyChecks == null)
monthlyChecks = "\nCheck on month "+month+": $"+df.format(cash)+"\n";
else
monthlyChecks = monthlyChecks+"\nCheck on month "+month+": $"+df.format(cash)+"\n";
}
numDays is the user inputted number of days....
monthlyChecks is a String....
dailyMoney[] is the array that holds the amount for each day.
Okay, sounds to me like what you are trying to do with your "insufficient days for month 3" is this: (Assuming numDays is an int)
numMonths = numDays / 30 //If numDays is an int, this will round down.
extraDays = numDays % 30
for (int i = 0; i < numMonths; i+=30){
double cash=0;
for (int n=1; n < 30; n++){
int currentDay;
currentDay= n+i;
cash=cash+dailyMoney[currentDay];
}
month++;
if(monthlyChecks == null)
monthlyChecks = "\nCheck on month "+month+": $"+df.format(cash)+"\n";
else
monthlyChecks = monthlyChecks+"\nCheck on month "+month+": $"+df.format(cash)+"\n";
}
}
//Then at some point
Systme.out.println("Insuffiecent days for " + numMonth + 1 " months. " + 30 - extraDays " days short).")
Given that user is required to enter days, maybe something like
int numMonths = numDays / 30;
int remainder = numDays % 30;
System.out.println ("Calculating for " + numMonths + " months");
if ( remainder != 0 ) {
numDays -= remainder;
System.err.println ("Insufficient days for month " + (numMonths + 1));
}
This should all be placed before the rest of your code, including (and especially) before the dailyMoney = new double[numDays] part.
Related
I am not sure whether my solution is justifiable (ans. 171) - Project Euler Q.19 since I am having a hard time getting my head around modular arithmetic and not really sure whether my approach to it was correct or not... I was having trouble on trying to get the equivalence of having 0 as a key rather than 1 to Monday for reference in a hash table. The question was;
1 Jan 1900 was a Monday.
Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, which has twenty-eight, rain
or shine. And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?
So what I did was start the sum of days at 1 (reference for days in hash table) and subtract 1 after finding the sum of Sunday's, since doing it by 0 caused problems when the total sum of days were divisible by 3 and 6 (3:Wednesday, 6:Sunday). How could I have done this by using 0 as reference for Monday?
import java.util.*;
public class p19 {
public static void main(String[] args) {
Hashtable<Integer, String> days = new Hashtable<Integer, String>();
days.put(1, "Monday");
days.put(2, "Tuesday");
days.put(3, "Wednesday");
days.put(4, "Thursday");
days.put(5, "Friday");
days.put(6, "Saturday");
days.put(7, "Sunday");
Hashtable<Integer, String> months = new Hashtable<Integer, String>();
months.put(1, "January");
months.put(2, "February");
months.put(3, "March");
months.put(4, "April");
months.put(5, "May");
months.put(6, "June");
months.put(7, "July");
months.put(8, "August");
months.put(9, "September");
months.put(10, "October");
months.put(11, "November");
months.put(12, "December");
int min, max;
min = 1900;
max = 2000;
String[][] arr = new String[12 * (max - min + 1)][];
// Total days starts at 1 to make modular arithmetic easier when accounting for days
// (i.e., 1 Monday, 2 Tuesday, etc.) and since the first day, hence, 0th day on 1 Jan 1900 is Monday.
for (int year = min, index = 0, totalDays = 1; year <= max; year++) {
for (int month = 1; month <= 12; month++, index++) {
arr[index] = new String[numberOfDays(month,year)];
int sum = 1;
System.out.println(months.get(new Integer(month)) + " " + year);
for (int day = 1; day <= numberOfDays(month, year); day++, totalDays++) {
if (totalDays % 7 == 0) {
arr[index][day - 1] = days.get(new Integer((totalDays % 7 + 7) % 365));
}
else {
arr[index][day - 1] = days.get(new Integer((totalDays % 7) % 365));
}
if (sum > 7) {
System.out.println();
sum = 1;
}
System.out.print(totalDays + ":= " + arr[index][day - 1] + ", " + day + " | ");
sum++;
}
System.out.println("\n");
}
}
int count = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i][0] == "Sunday") {
count++;
}
}
// Subtract 1 from count since the total days were overstated by 1 before inititallizing array
System.out.println("Number of Sundays that fell on the first of the month from: 1/Jan/1901 - 31/Dec/2000: " + (count - 1));
}
public static int numberOfDays (int month, int year) {
int days = 0;
switch (month) {
case 7:
case 4:
case 6:
case 11:
days = 30;
break;
case 2:
if (isLeapYear(year)) {
days = 29;
}
else {
days = 28;
}
break;
default: days = 31;
break;
}
return days;
}
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
Your daysInMonth check is incorrect - the result must have been correct by incidence:
switch (month) {
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
The rest of the program can be simplified - note that the start year has to be corrected too, dow stands for DayOfWeek:
public static void main (String[] args) {
int count = 0;
// dow = 2, since 1.1.1901 was a Thuesday (2)
for (int year = 1901, dow = 2; year <= 2000; ++year)
{
for (int month = 1; month <= 12; ++month)
{
if (dow == 0) {
// System.out.println ("Date: " + year + "-" + month);
++count;
}
dow = (dow + numberOfDays (month, year)) % 7;
}
}
System.out.println ("Number of Sundays that fell on the first of the month from: 1/Jan/1901 - 31/Dec/2000: " + count);
}
I have to get input for a certain number of days and a starting number. With that, the number of days gets split in half, in first half starting number decremented by 2. Last half incremented by 1. If days is uneven, last half has one more day. Heres what I have:
int days;
int num;
int 1half;
int 2half;
int new_num;
System.out.print("Enter number of days: ");
days = keyboard.nextInt();
System.out.print("Enter number of first day: ");
num = keyboard.nextInt();
int half = days/2;
if (days %2 == 0){
1half = (half);
2half = (half);
} else {
1half = (half);
2half = (half) + 1;
}
int first_half[] = {1,(half)};
int last_half[] = {((half)+1), days};
while (1half > 1 || 1half < half) {
new_temp = temp - 2;
System.out.println("Day \t Num");
System.out.println(new_num);
first_half--;
}
while(2half >= (half + 1) && (2half <= days)) {
new_temp = temp++;
System.out.println("Day \t Num");
System.out.println(new_num);
last_half--;
}
Im stuck now though.
If you see anything Id be happy to hear about it. I just need some help/advice. I'll clarify anything if this is hard to understand. Thank you in advance
Edit:
The final output should look something like this:
Day Num
1 -10
2 -12
3 -14
4 -16
5 -18
6 -17
7 -16
8 -15
9 -14
10 -13
11 -12
If this makes sense.
Heres my output:
Day Temperature
8
Day Temperature
8
Day Temperature
8
Day Temperature
continues forever
I don't know why do you need those arrays and other stuff, but you may have a look at my code. It produces exactly what you want.
int days = 11;
int num = -10;
int output = num + 2;
System.out.println("Day \t Num");
int half = days / 2;
for (int i = 1; i <= half; i++) {
output -= 2;
System.out.println(i + "\t" + output);
}
for (int i = half + 1; i <= days; i++) {
output++;
System.out.println(i + "\t" + output);
}
Your endless loop is caused by your condition statement of your while:
while (1half > 1 || 1half < half).
If 1half is set to be any number greater than 1 then this will loop forever. You probably want to use an && instead of an || as well. But don't see anything in the body of the loop that would affect things to make it get out of the loop.
I am trying to right a program for my introduction to java course. The user enters their birthdate in the following format(19900506), the amount of days the person is then displayed. The program uses the GregorianCalendar class to get today's date and compares the two. Leap years are taken into account. I was able to right the program, but I need to write another version that calculates the difference using my own algorithm. I have hit a wall and can't figure out how to do this. I was thinking of converting the difference between the two dates to milliseconds and then converting to days again. But there is a lot of things to be considerd, like days in months, days remaining from todays date, etc. Any help would be appreciated.
Here is my code:
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class DayssinceBirthV5 {
public static void main(String[] args) {
GregorianCalendar greg = new GregorianCalendar();
int year = greg.get(Calendar.YEAR);
int month = greg.get(Calendar.MONTH);
int day = greg.get(Calendar.DAY_OF_MONTH);
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your birthday: AAAAMMDD): ");
int birthday = keyboard.nextInt();//
int testyear = birthday / 10000;// year
int testmonth = (birthday / 100) % 100;// Month
int testday = birthday % 100;// Day
int counter = calculateLeapYears(year, testyear);
GregorianCalendar userInputBd = new GregorianCalendar(testyear, testmonth - 1, testday);// Input
long diffSec = (greg.getTimeInMillis() - userInputBd.getTimeInMillis());// Räkna ut diff
// long diffSec = greg.get(Calendar.YEAR)-birthday;//calc Diff
long total = diffSec / 1000 / 60 / 60 / 24;// calc dif in sec. Sec/min/hours/days
total += counter;
System.out.println("Today you are : " + total + " days old");
}
private static int calculateLeapYears(int year, int testyear) {
int counter = 0;
for (int i = testyear; i < year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
counter++;
System.out.println("Amount of leap years: " + counter);
}
}
return counter;
}
}
You can calculate the number of days like this -
Write a method that finds the number of days in a year: Leap years have 366 days, non-leap years have 365.
Write another method that gets a date and finds the day of year - January 1st is day 1, January 2nd is day 2 and so on. You'll have to use the function from 1.
Calculate the following:
Number of days until year's end from date of birth.
Number of days from year's begining until current date.
Numer of days of all years between.
Sum up all of the above.
def daysBetweenDates(self, date1: str, date2: str) -> int:
y1, m1, d1 = map(int, date1.split('-'))
y2, m2, d2 = map(int, date2.split('-'))
m1 = (m1 + 9) % 12
y1 = y1 - m1 // 10
x1= 365*y1 + y1//4 - y1//100 + y1//400 + (m1*306 + 5)//10 + ( d1 - 1 )
m2 = (m2 + 9) % 12
y2 = y2 - m2 // 10
x2= 365*y2 + y2//4 - y2//100 + y2//400 + (m2*306 + 5)//10 + ( d2 - 1 )
return abs(x2 - x1)
I've been trying to convert a float number to years, months, weeks, days, hours, minutes and seconds but I'm not getting it.
For example, if the user enters 768.96 the total would be 2 years, 1 month, 1 week, 1 day, 23 hours, 0 minutes and 2 seconds.
This is what I have.
import javax.swing.JOptionPane;
public class timePartition {
public static void main(String[] args) {
float totalTime;
float userInput;
int years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0;
do{
userInput = Float.parseFloat(JOptionPane.showInputDialog("Enter a positive number to decompose"));
totalTime = userInput;
years = (int) userInput / 365;
userInput = userInput % 10;
months = (int) userInput / 12;
userInput = userInput % 10;
weeks = (int) userInput / 4;
userInput = userInput % 10;
days = (int) userInput / 30;
userInput = userInput % 10;
hours = (int) userInput / 24;
userInput = userInput % 10;
minutes = (int) userInput / 60;
userInput = userInput % 10;
seconds = (int) userInput / 60;
userInput = userInput % 10;
}while (userInput >=1);
System.out.print("The number " +totalTime+ " is " +years+ " years, " +months+ " months, " +weeks+ " weeks, " +days+ " days, " +hours+ " hours, " +minutes+ " minutes, " +seconds+ " seconds.");
}
I don't think you can use modulo 10 to reduce the input after you pull out each of the denominations. Also, you don't need a while loop at all for this.
You have to do something like
years = (int) (userInput / 365);
userInput = userInput - years*365;
and so on. Also, since the input is in days, you have to keep thinking in days when you divide out, so dividing by 12 to get the number of months doesn't make sense. You would instead divide by 30, 31 or 28. Similarly for hours, you would have to multiply the remaining fraction of days by 24, and then take the fractional part of the hours and decompose it similarly into minutes and seconds.
Ok, several things here:
just to make sure you're aware: date/time arithmetic is not as simple as it may seem. Several fields are not uniform in duration, including years (due to leap years in some years but not others), months (28-31 days depending on the month), and even minutes (due to rare but strictly necessary leap seconds). This means that technically you can't properly decompose a total duration count (e.g. "x days") into duration fields, and vice-versa (at least not without some "anchoring" date/time point).
if you want to make incorrect assumptions like "all years have exactly 365 days" and "all months have exactly 30 days" and "all minutes have exactly 60 seconds" then this can be done.
I'm not sure whether you wanted your program to take and decompose a single value, in which case the loop is not necessary, or multiple values, in which case the final print statement should be inside the loop. I've assumed the latter.
based on your example input and the start of your decomposition code, it appears that you want the integral part of the input float value to represent a number of days, with the fractional part representing a time value as a fraction of one day. Your decomposition code is incorrect according to this interpretation; you first must separate the ipart and fpart to decompose independently, and in each step of the decomposition, you must take the remainder on the previous field duration size (e.g. 7 days for a week, 3600 seconds for an hour), rather than a fixed value of 10 (not sure where that came from...) to prepare for the next step of the decomposition. This can be done with the mod-assign operator %=.
Here's working code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TimePartition {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Enter a positive number to decompose");
String input = br.readLine();
if (input.equals("")) break;
float inputAsFloat = Float.parseFloat(input);
if (inputAsFloat == 0.0) break;
// the input is an integral day count, with a possible fractional part representing time as a fraction of one day
int totalDays = (int)inputAsFloat;
int totalSeconds = (int)((inputAsFloat-totalDays)*60.0*60.0*24.0);
// decompose totalDays into date fields
int years = 0;
int months = 0;
int weeks = 0;
int days = 0;
// ignores leap years
years = (int)totalDays/365;
totalDays %= 365;
// assumes all months have 30 days
months = (int)totalDays/30;
totalDays %= 30;
weeks = (int)totalDays/7;
totalDays %= 7;
days = (int)totalDays;
// decompose totalSeconds into time fields
int hours = 0;
int minutes = 0;
int seconds = 0;
hours = (int)totalSeconds/3600;
totalSeconds %= 3600;
// ignores leap seconds
minutes = (int)totalSeconds/60;
totalSeconds %= 60;
seconds = (int)totalSeconds;
System.out.println("The number "+inputAsFloat+" is "+years+" years, "+months+" months, "+weeks+" weeks, "+days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds.");
} // end while
} // end main()
} // end class TimePartition
Demo:
bash> ls
TimePartition.java
bash> javac TimePartition.java
bash> ls
TimePartition.class* TimePartition.java
bash> CLASSPATH=. java TimePartition
Enter a positive number to decompose
768.96
The number 768.96 is 2 years, 1 months, 1 weeks, 1 days, 23 hours, 2 minutes, 25 seconds.
I suppose the input is in day.
There are few strange thing in your code :
the while loop is not necessary
(int) userInput will cast userInput in int before the division, not really important here, but be careful ;)
userInput % 10 everywhere
divide by 12 to get the number of month, by 4 to get the number of weeks, and so on
Here is a skeleton of the solution:
float userInput /* = ... */ ;
int years = (int)(userInput/365) ;
userInput = userInput - years*365 ; // or userInput%365 ;
int month = (int)(userInput/30);
userInput = userInput - month*30 ; // or userInput%30 ;
int day = (int) userInput ;
userInput = userInput - day ;
userInput = userInput * 24 ; //transform in hours
int hours = (int)hours ;
userInput = userInput - hours ;
userInput = userInput * 60 ; // transform in minute
int minutes = (int)userInput ;
userInput = userInput - minutes ;
userInput = userInput * 60 ; // transform in second
int seconds = (int) userInput ;
Part of an assignment I have for a beginning Java class is to take a time entered in as a string and convert it (while rounding to the nearest quarter hour) to a double and store it in an array. The part I am having difficult with is what to do with the two integers I receive from the split method of the String class. How do I make the two integers into one double to use in the array? (So it would be like hours.minutes, or 5.25)
Here is a snippet of code from a program I am working on:
public static double convertClockOutTimes(String clockOut){
double convertedTimeOut = 0;
String time = clockOut;
int hours;
int minutes;
String[]splitFields;
splitFields = time.split(":");
hours = Integer.parseInt(splitFields[0]);
minutes = Integer.parseInt(splitFields[1]);
if (minutes <= 7)
{
minutes = 0;
}
else if (minutes >= 8 || minutes <= 22)
{
minutes = 15;
}
else if (minutes >= 23 || minutes <= 37)
{
minutes = 30;
}
else if (minutes >= 31 || minutes <= 53)
{
minutes = 45;
}
else
minutes = 0;
hours = hours + 1;
convertedTimeOut = //This is where I don't know what to do!!
return convertedTimeOut;
}
I think the trick is to use a bit of math here. You could always do the following:
double convertedTimeOut = Math.round(minutes / 15.0) * 0.25 + hours;
then you don't need the if-else tree to figure out the nearest quarter hour.
There are 15 minutes in a quarter hour, and a quarter hour is 0.25 hours. Using the formula above, you are dividing the minutes into how many quarters of an hour you've got (0-4), which you then multiply by how many hours are in a quarter hour. Then add that to the hours you've got.
Math.round just does the rounding for you.
I'm guessing what's giving you the most trouble is how to deal with the minutes. Try this:
double minutesAsDecimal = 0.01 * minutes;
So if the number of minutes was 24, you would end up with 0.24. I bet you'll know where to go from there.
Just as a side note, your if and else ifs are not doing what you think they are. But since it's homework.. Just take a closer look at your logic there.
Your conditions should be AND not OR:
if(minutes >= 8 && minutes <= 22)
Etc, or better yet, a single statement, simply
minutes = ((minutes + 7) % 15) * 15;
which calculates the rounding using arithmetic rather than logic.