简体   繁体   中英

Multiplying user input variables together in another class and then naming that result as a variable

Sorry if this is a bit vague. I am new to learning Java.

In my program I have two classes and one of the classes is for user input. The other class calculates that user input and then returns the calculations to the other class. In my calculations class I'm pretty sure I'm making myself work harder and than I should be. I want to have the result of my user input multiplied together but doing that in the calculations class.

Here is my Calculations class.

class Calculations{
    double length, width ;

    public double floorArea (double length, double width){
        return length * width ;
    }

    public double floorAreaCost (double length, double width) {
        return length * width * 6.50 ;
    }

    public double serviceCharge (double length, double width){
        return length * width / 10 + 12.50 ;
    }
}

What I want to be able to do is have return length * width = area. Then use that area variable for future reference in the floorAreaCost method and the service charge method. So instead of return length * width * 6.50 I would have area * 6.50

Here's my user input class as well.

import java.util.Scanner;

public class ApartmentUser{
    static Scanner input = new Scanner(System.in);

    public static void main (String args[]){
        int length, width;
        System.out.println("Enter the length of the apartment floor: " );
        length = input.nextInt();
        System.out.println("Enter the width of the apartment floor: " );
        width = input.nextInt();
        Calculations area = new Calculations();
        System.out.println("The area of the apartment floor is: " + area.floorArea(length, width));
        Calculations cost = new Calculations();
        System.out.println("The cost of the apartment is: " + cost.floorAreaCost(length, width));
        Calculations charge = new Calculations();
        System.out.println("The service charge cost is: " + charge.serviceCharge (length, width));
    }
}

Your methods should call the floorArea method, so for example method shown below

public double floorAreaCost (double length, double width) {
return length * width * 6.50 ;
}

would become

public double floorAreaCost (double length, double width) {
return this.floorArea(length, width) * 6.50 ;
}

That way, the floor area calculation is encapsulated inside one method only and can easily change in one step

First off all when you define fields in your class, it's common practice to define the scope of the variable. So it would look something like this. Which only makes the variable accessible within the class, if you would access it from the main method, you should declare em public. But add your area as a variable.

private double area ;

You need to store your calculated Area on the object, use the keyword this for accessing that variable. When operations on the same object is done, it can be fetched in a similar fashion.

Update your code to this:

public double floorArea (double length, double width){

    this.area = length * width;

    return this.area;
}

public double serviceCharge (){

    return this.area / 10 + 12.50 ;

}

First of all you shouldn't make so many Calculations objects, one is enough. So what you should do is give the Calculations class a constructor like this.

class Calculations{

     public double length, width, area;

     public Calculations (int length, int width) {
         this.length = length;
         this.width = width;
         area = width * length;
     }

Now when you create youre Calculations object:

Calculations floor = new Calculations(int length, int width);

You directly have the area calculated and you can call the methods without having to input the parameters, because they're already saved in the Calculations class. You can also work with multiple "rooms", because the informations are saved in the Calculations class. Hope i could help you.

As written, your Calculations class defines a "stateless" object. Within each function, the function parameters length and width hide the member variables length and width , so that the member variables are never use at all. You should be able to delete the declaration of those member variables without noticing any change in the behavior of your program.

This is not necessarily a bad thing. Stateless classes can be very useful. For example, because Calculations is stateless, you do not need to allocate three different instances to perform your three different functions. You can call all the functions on the same instance, because none of the functions can affect the "state" of the object and therefore cannot have any hidden "side effects" on the results of functions called later. The return from each function is determined just by the values you pass to its two parameters.

The program does end up multiplying the same length and width together three times when once would have been enough. You will hardly notice the extra computing time in this example (it is vastly overshadowed by everything else going on here), but if you had to do millions of these calculations for one user input you might then notice a difference. One way to avoid the redundant multiplications is to return area from the floorArea function, but pass area (not length and width) as a single parameter to each of the other functions.


You might also consider creating member variables of Calculations to store the numbers 6.5, 10, and 12.5 that you use in some of your functions. That would allow you to give those numbers meaningful, descriptive names. It would also permit a more sophisticated version of the program to accept new values of those constants to use in a Calculations object, allowing the store to change its pricing without rewriting its software. If you set those values during the construction of a Calculations object and do not change them in any of the other functions, the object is still stateless.


Or you could decide to change the class some other way. I see at least three other answers already, each of which proposes a legitimate design of a Calculations class, no two of those designs the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM