0

I am new to Java and I am trying to understand how classes, constructors, and methods work. In order to do this, I programmed a class that calculates with two different methods the deductions from an import, which is given as imput.

import java.text.NumberFormat;
import java.util.Locale;
import java.math.*;

public class Deductions {
    public double linearValueperYears;
    public double linearUse;
    public double percentage;
    Locale locale = Locale.GERMANY; //java Util
    NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale); //java Locale
    
    Deductions(double purchaseValue, double durability){    
        linearValueperYears = purchaseValue;
        linearUse = durability;
    }
    
    public void linearDeduction(double acquisitionCost, double years) {
        this.linearValueperYears = years; 
        this.linearUse = acquisitionCost;
        double deduction = linearUse/linearValueperYears; //67.500 €
        double counter = linearUse; // 540.000 €
        System.out.println("Initial import is " + Math.round(linearUse));
        System.out.println("For " + Math.round(linearValueperYears) + " Years");
        for(int i=1; i<=linearValueperYears; i++) {
           counter -= deduction;
           System.out.println("For year " + i + " the import is " + numberFormat.format(counter));
        }   
}
    
    public void geometricDegressiv(double acquisitionCost, double years, double percentage) {
        
        this.linearValueperYears = years; // 8 years
        this.linearUse = acquisitionCost; // 540.000€
        double geomDegImport = Math.round(linearValueperYears - (100.0/percentage) + 1); // 4th year
        double counter = linearUse;
        double percentageDeduction = percentage/100.0; // 0.20
        double deduction= 0;
        for(int i=1; i<=geomDegImport; i++) {
            counter -= counter * percentageDeduction;
            System.out.println("Year nr. " + Math.round(i) + "  " + numberFormat.format(counter));
        }   
        deduction = counter;
        double restYear= linearValueperYears - (linearValueperYears - geomDegImport); // 8-4 = 4 
        System.out.println("The import at the end of the year nr: " + Math.round((restYear)) + " is: " + numberFormat.format(deduction));
        
        //System.out.println("RestYear is: " + restYear);
        
        double linearDeduction= deduction / (restYear); // import at the end of year 3 with linear method (276. 480 / 5) = 55.296 €
        for(double j= restYear+1; j<=linearValueperYears; j++) {
            deduction-=linearDeduction;
            System.out.println("For year " + Math.round(j) + " the import is " + numberFormat.format(deduction));
        }
        System.out.println("The import at the end of the year nr: " + Math.round(linearValueperYears) + " is: " + numberFormat.format(deduction));
    }
    
}
import java.util.Scanner;

public class Main {
    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner imputScanner = new Scanner(System.in);
        System.out.println("Imput an import!");
        int imputAcquisition = imputScanner.nextInt();
        System.out.println("Imput a number of years");
        int imputYear = imputScanner.nextInt();
        
        Deductions deduction1 = new Deductions(imputAcquisition, imputYear);
        
        System.out.println("Deduction of " + imputAcquisition + " after linear-method is: ");
        deduction1.linearDeduction(imputAcquisition, imputYear);
        
        System.out.println("To know the import after geometric/degressiv-method imput a percentage");
        int imputPercentage = imputScanner.nextInt();
        System.out.printf("You chose the percent: %d%%", imputPercentage); // printf (format) --> %
        System.out.println();
        System.out.println("Deduction of " + imputAcquisition + " after geometric/degressiv-method is: ");
        deduction1.geometricDegressiv(imputAcquisition, imputYear, imputPercentage);
    }
}

I must admit that I used "this" a bit after intuition and I don't understand 100% why it is working without being used before in the constructor. What happens exactly if I use it inside a void method? Is it considered "good coding" or is it something to avoid? I searched long in the web and in the documentation but I am still very confused about the different utilization. Thank you very much in advance.

Franz Biberkopf
  • 191
  • 1
  • 11
  • The return type of a method is orthogonal to `this`. `this` refers to the current instance. It's redundant if not needed to disambiguate between parameters/locals; whether or not it "should" be used is opinion-based. – Dave Newton Jun 30 '21 at 17:04
  • @DaveNewton: sorry, in which sense "orthogonal"? – Franz Biberkopf Jun 30 '21 at 17:05
  • 1
    "Unrelated." Sorry--I over-use "orthogonal." I mean that `this` is `this` regardless of the method return type; the time you *can't* use it is in static (class) methods since there's no instance to refer to. – Dave Newton Jun 30 '21 at 17:09

0 Answers0