Monthly Asset Depreciation - java

I am writing a desktop application which will be used to calculate the monthly depreciation and Accumulation of an asset.
The accumulation is based on the number of months the user specifies.
therefore if the user specified 12 months, it should accumulate over those months.
Also the user should be able to view the accumulation value for for every month so far it is in the specified number of months. e.g if i select January it should give me the accumulated value for January and so on......
CHALLENGES : first challenge is getting the accumulation values for all the user specified months 2nd one is how to link this concept with my j form and my database since the codes i have works but how to fuse it into my program is a problem.
Depreciation Method: Straight line
below is a sample code
private void getAccumulation() {
try {
for (int row = 0; row <= 5; row++) {
double Cost_Of_Acquisition = 2000;
double Estimated_Residual_Value = 250;
int Estimated_Useful_Life = 5;
int depreciationMethod = 1;
System.out.println(" acquisition cost of asset = " + (int) Cost_Of_Acquisition);
System.out.println(" salvage value = " + (int) Estimated_Residual_Value);
System.out.println(" number of months = " + Estimated_Useful_Life);
System.out.println(" depreciation method = " + depreciationMethod);
for (int y = 1; y <= 5; y++) {
switch (depreciationMethod) {
case 1:
System.out.println(" straight line depreciation ");
double StraightLineDepreciation = ((Cost_Of_Acquisition - Estimated_Residual_Value) / Estimated_Useful_Life);
double AccumulatedDeprecaitionSL = (StraightLineDepreciation * y);
System.out.println("Asset number " + "1" + " uses straight line depreciation");
System.out.println(" depreciation charge for asset number " + "1" + " in month " + y + "=" + StraightLineDepreciation);
System.out.println("accumulated depreciation is " + AccumulatedDeprecaitionSL);
break;
case 2:
System.out.println(" sum of months digits depreciation ");
int SumofMonths = (0);
break;
}
}
}
}
}

Related

Rolling dice project: code gives out of bounds exception below 7 and fails to save the first "Die Number"

I understand what out of bounds exception means, and how it happens, but I can't find out why it's happening in my code. Also, the output "Count for side 1" always states 0. This is my first post, but I think I am posting this right.
This is where I think the problem is.
System.out.println("Now rolling " + chosenRollNumber + " times. ");
int[] count = new int[chosenRollNumber];
for (x = 0; x < chosenRollNumber; x++) {
dieNumber = RNG(randomNum, min, max);
System.out.println("dieNumber " + dieNumber);
count[dieNumber]++;
}
System.out.println("Done rolling all dice");
for(x = 0; x < numberOfSides; x++) {
System.out.println("Count for side " + (x + 1) + " is " + count[x]); }
while(true) {
Method RNG(randomNum, min, max) is expected to return values in the range [min...max] (inclusive), while dieNumber as the index in count array needs to be in the range [0; numberOfSides), and the following relation exists numberOfSides == max - min + 1.
So, a correction is needed to transform dieNumber into a valid index:
System.out.println("Now rolling " + chosenRollNumber + " times. ");
int[] count = new int[numberOfSides];
for (x = 0; x < chosenRollNumber; x++) {
dieNumber = RNG(randomNum, min, max);
System.out.println("dieNumber " + dieNumber);
int dieIndex = (dieNumber - min) % numberOfSides;
count[dieIndex]++;
}

Trying to make a while loop work properly

I'm making a program that calculates the population for a year given the start year (2011) and increases the population by 1.2% every year. The population for 2011 is 7.000 (I'm using decimals, instead of billions). Here is the working part of my code
package src;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Demographics {
public static void main(String[] args) {
Scanner user_input= new Scanner(System.in);
//Part 1
System.out.println("===--- Part 1 ---===");
System.out.println("Population in 2011: 7.000");
System.out.print("What is the desired year? ( > 2011) ");
int startYear = 2011;
int endYear = user_input.nextInt();
while (endYear <= startYear){
System.out.println("Invalid end year.");
System.out.print("What is the desired year? ( > 2011) ");
endYear = user_input.nextInt();
break;
}
double t = 0.012;
double nbr = (endYear - startYear);
double pStart = 7.000;
double pEnd = pStart * Math.exp(nbr * t);
DecimalFormat nf = new DecimalFormat("#.000");
System.out.println("Population in " + endYear + ":(nf.format(pEnd)));
//Part 2
System.out.println("===--- Part 2 ---===");
System.out.print("What is the target population? ( > 7.000) ");
double pTarget = user_input.nextDouble();
while (pTarget <= pStart){
System.out.println("Invalid target population.");
System.out.print("What is the target population? ( > 7.000) ");
pTarget = user_input.nextDouble();
break;
}
while (pStart < pTarget){
startYear++;
pStart = pStart + (pStart * 0.012);
System.out.println("Population in " + startYear + ": " + nf.format((pStart)));
}
}
}
Part 1 of my code calculates the population of a year when the user enters it, then part 2 shows the calculations of how many years it will take when a user enters a population to get to that point.
Here is the code that doesn't work
//Part 3
System.out.println("===--- Part 3 ---===");
t = 1.2;
pStart = 7.000;
pEnd = pStart * Math.exp(nbr * t);
while (pStart < pTarget){
startYear++;
pEnd = pStart + (pStart * 0.012);
if (pEnd >= pStart * 2 ){
System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t / 2));
}else{
System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t);
}
}
Currently when i have part 3 in my code it does an infinite loop without multiplying the population. What I'm trying to do in part 3 is pretty much the same thing in part 2, but in part 3 it will display the population growth rate (t) and divide it by 2 every time the population doubles. For example:
Population in 2019 : 7.705 ; population growth rate : 1.2%
Population in 2020 : 7.798 ; population growth rate : 1.2%
Population in 2021 : 7.892 ; population growth rate : 1.2%
...
Population in 2068 : 13.873 ; population growth rate : 1.2%
Population in 2069 : 14.040 ; population growth rate : 0.6%
Anyone have any ideas on how to achieve this?
if you have problems in the loop the reason is in the condition!
while (pStart < pTarget){
so to end this code, inside the loop one of this situation has to happen:(according with your condition)
A)pStart should increase in value
B)pTarget should decrement in value
C)a "break;" have to occur
in your code you increase: startYear and pEnd, but this are not the condition to close the loop according with your condition. (i write it before:A,B,C)
1) also startYear are not re initialize it before the loop, and start already at a high value. you have to add bedore the loop:
startYear = 2011;
2) you should as far as possible to create new variables for the segment 3, is not to have problems like the one just described, is to be clear about what you are doing.
my advice for part three is this:
(Considering that I have no clear what I wanted to do reading your code, you have to cange it and make it good for you)
System.out.println("===--- Part 3 ---===");
t = 1.2;
startYear = 2011; // I add it
double pEveryYear = 7000;
while (pEveryYear < pTarget){
startYear++;
pEveryYear = pEveryYear + (pEveryYear * 0.012);
if (pEveryYear >= pTarget ){ // this condition cange only the print in the console
System.out.println("Population in " + startYear + ": " + nf.format((pEveryYear)) + " Population growth rate " + ": " + (t / 2));
break; // if you write it before the system.out you can't read it in the console.
}else{
System.out.println("Population in " + startYear + ": " + nf.format((pEveryYear)) + " Population growth rate " + ": " + t);
}
}
}
this the console output for input like "8000":
===--- Part 3 ---===
Population in 2012: 7084.000 Population growth rate : 1.2
Population in 2013: 7169.008 Population growth rate : 1.2
Population in 2014: 7255.036 Population growth rate : 1.2
Population in 2015: 7342.097 Population growth rate : 1.2
Population in 2016: 7430.202 Population growth rate : 1.2
Population in 2017: 7519.364 Population growth rate : 1.2
Population in 2018: 7609.596 Population growth rate : 1.2
Population in 2019: 7700.912 Population growth rate : 1.2
Population in 2020: 7793.323 Population growth rate : 1.2
Population in 2021: 7886.842 Population growth rate : 1.2
Population in 2022: 7981.485 Population growth rate : 1.2
Population in 2023: 8077.262 Population growth rate : 0.6
It seems you have two variables in the while loop that you try to compare however these do not actually get updated while iterating in the while loop. So after every iteration within the loop, the while condition just stays true.
See the last few lines below for a minor change to your part 3 code :
System.out.println("===--- Part 3 ---===");
t = 1.2;
pStart = 7.000;
pEnd = pStart * Math.exp(nbr * t);
while (pStart < pTarget){
startYear++;
pEnd = pStart + (pStart * 0.012);
if (pEnd >= pStart * 2 ){
System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t / 2));
} else {
System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t);
}
pStart = startYear; // <-- NEW, you can probably use pStart instead of startYear in this code
}
The only missing thing I see in your code is that you are not updating pStart every time you go through the loop. This will not only make your loop infinite but it's also doing a wrong calculation every time except the first iteration. I added only one line from your code:
System.out.println("===--- Part 3 ---===");
t = 1.2;
pStart = 7.000;
pEnd = pStart * Math.exp(nbr * t);
while (pStart < pTarget){
startYear++;
pEnd = pStart + (pStart * 0.012);
if (pEnd >= pStart * 2 ){
System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + (t / 2));
}else{
System.out.println("Population in " + startYear + ": " + nf.format((pEnd)) + " Population growth rate " + ": " + t);
}
pStart = pEnd;
}

Outputting int but carrying a decimal

So I have a piece of code where I want to output the seconds as an integer, but I don't want to lose the decimal value as I need to use it as part of the count in a for loop to code the program correctly, here is the code (for loop is unfinished obviously as there is no count)
System.out.println("Recommended Splits for Marathon for time of "
+ hours + ":" + minutes + ":" + seconds);
hoursInSeconds = hours * 3600;
minutesInSeconds = minutes * 60;
totalTime = hoursInSeconds + minutesInSeconds + seconds;
double totalSecondsPerKM = totalTime/42;
int hoursPerKM = (int) (totalSecondsPerKM/3600);
double remainderHours = totalSecondsPerKM%3600;
int minutesPerKM = (int) (totalSecondsPerKM/60);
double secondsPerKM = (totalSecondsPerKM%60);
for (int index = 1; index <=42; index++){
System.out.print(index + " ");
System.out.print("" +hoursPerKM);
System.out.print("");
System.out.print(":"+minutesPerKM);
System.out.print("");
System.out.print(":"+secondsPerKM);
System.out.println("");
You could use
System.out.println(String.format("%.0f", 1.6));
or
System.out.println((int)1.6);
The first will round the value, output 2, the second will truncate it, outputting 1

Java - asking questions x times depending on the users answer

I want to ask three questions x times depending on the answer and then I want to calculate the total cost of depending on the answers given. I'm wondering if this way works and if so how do I calculate the total cost?
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("x ingredients?");
int amount = sc.nextInt();
for(int i = 1 ; i <= amount; i++) {
System.out.print("Nr " + i + ": How much do you have?\n");
int have = sc.nextInt();
System.out.print("Nr " + i + ": How much do you need?\n");
int need = sc.nextInt();
System.out.print("Nr " + i + ": How much does it cost?");
int cost = sc.nextInt();
if(i == amount) {
// calculate total cost
}
}
}
You need to keep track of the total cost outside of the loop, otherwise it will go out of scope. For example, before the loop, initialize the total cost:
int totalCost = 0; //you used sc.nextInt() so I assume no decimals
Then, in the loop, just get the amount to buy and multiply by the cost.
int toBuy = need - have;
//you do NOT need if (i == amount) because you will add to the cost whenever it's necessary, not just at the end of the loop
if (toBuy > 0){ //don't buy things you don't need
totalCost += toBuy * cost;
}
Then, outside the loop, print the total cost:
for (int i = 1; i <= amount; i++){
//...
}
System.out.println("Total cost of ingredients: " + totalCost);

Implementing unit conversions [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am working on a function where I am calculating meter units to amount. Rules for meter calcualtions are :
Minimum fare on 1.6km amt - 12/-
Subsequent 1 km amt - 7/-
25% additional fare for journey for midnight
The function I have written is :
public static void CalculateAutoFare(int kmIndex) {
double fare = 12.00;
double night = 15.00;
double subseIncre = 1.50;
double nightIncre = 0.25;
int j=0;
for (int i=1; i <= kmIndex; i++) {
if (i == 3 || j == 4) {
fare += 1.00f;
j =0;
}
else {
fare += subseIncre;
j++;
}
fare = Math.round(fare*2f)/2f;
double extra = ((double) fare * nightIncre);
night = fare + extra;
night = Math.round(night*2f)/2f;
System.out.println("I = " + i + " Fare = " + fare + " Night = " + night + " 25%Extra = " + extra);
}
System.out.println("Day = " + fare + " Night = " + night);
}
kmIndex is the index of km. Meter readings are as 1.00, 1.10, 1.20, 1.30...1.90, 2.00.... Thus kmIndex for 1.00 = 0, 1.10 = 1, 1.20 - 3 and so on.
Results that I get as per code and should be is mentioned below :
I have worked till 4.00 and where the results are not right are declared undet Should Be of relevant to Day or Night.
I worked a lot on this but couldn't get results as expected. If I try to correct 1 thing then other gets wrong or doesn't give expected results. Can anyone help me out with this. Have spent almost whole day trying to solve this.
I've analyzed the worksheet and I haven't found a formula that gives the results shown there. It seems that some values have some rounding differences. I've realized that only the values with 25 or 75 cents were different. For example, you calculated 18,5 and it should be 19 (the result was 18,75 and you rounded down to 18,5, but it should be rounded up to 19).
So, if you don't have the original formula that was used to create the table, I think the only way to be sure that the results will match the worksheet is to hardcode it. It's not an elegant solution, but it guarantees the correct result.
Small tweak in the code did the trick. You wanted .75 round as .5 so instead of multiply by 2 multiply by it's immediate smaller float.
{
double fare = 12.00;
double night = 15.00;
double subseIncre = 1.50;
double nightIncre = 0.25;
int j=0;
for (int i=1; i <= 22; i++) {
if (i == 3 || j == 4) {
fare += 1.00f;
j =0;
}
else {
fare += subseIncre;
j++;
}
double extra = ((double) fare * nightIncre);
night = fare + extra;
;
System.out.println("I = " + i + " Fare = " + fare + " Night = " + night + " 25%Extra = " + extra);
}
System.out.println("actual Day = " + fare + " Night = " + night);
fare = Math.round(fare*1.99f)/2f;
night = Math.round(night*1.99f)/2f;
System.out.println("rounded Day = " + fare + " Night = " + night);
}

Categories

Resources