calling methods, parameters and arguments

evan99simmons :

This is probably a dumb question. My following code looks fine but on the output, it does not expect results from my testing scenarios. Code follows:

 import java.util.Scanner;
  public class PartyPlannerLab {
                      public static Scanner input = new Scanner(System.in);

    public static int getGuestCount(int guests) {
        while(true) {
            System.out.print("Enter number of guests: ");
            guests = input.nextInt();
            if (guests >= 1 && guests <= 100)
                break;
            else 
                System.out.println("The guest count must be at least 1, but does not exceed 100. Please enter again.");
        }
        return guests;
    }
    public static int getSlicesPerPerson(int slicesPerPerson) {
        while(true) {
        System.out.print("Enter number of slices per person: ");
        slicesPerPerson = input.nextInt();
        if (slicesPerPerson >= 1 && slicesPerPerson <= 8)
            break;
        else
            System.out.println("The pizza slice count must be at least 1, but does not exceed 8. Please try again.");
        }
        return slicesPerPerson;
    }
    public static double computeRoomCost(int guests, double roomCost) {
        if (guests <= 30)
            roomCost = 100.00;
        else
            roomCost = 200.00;
        return roomCost;
    }
    public static double computeSodaCost(double sodaCost, int guests) {
        sodaCost = guests * 1.50;
        return sodaCost;

    }
    public static void printSummary(int guests, double roomCost, double sodaCost, double pizzaCost) {
        System.out.println("Total Guests: " + guests);
        System.out.println("RoomCost: $" + roomCost);
        System.out.println("SodaCost: $" + sodaCost);
        System.out.println("PizzaCost: $" + pizzaCost);
        System.out.println("Total Cost: $" +(roomCost + sodaCost + pizzaCost));
    }
    public static void main(String[] args) {
        int guests = 0;
        int slicesPerPerson = 0;
        double roomCost = 0.0;
        double sodaCost = 0.0;
        double pizzaCost = 0.0;
        getGuestCount(guests);
        getSlicesPerPerson(slicesPerPerson);
        computeRoomCost(guests, roomCost);
        computeSodaCost(sodaCost, guests);
        printSummary(guests, roomCost, sodaCost, pizzaCost);

        input.close();
    }


    }

One output is as follows:

Enter number of guests: 10
Enter number of slices per person: 2
Total Guests: 0
RoomCost: $0.0
SodaCost: $0.0
PizzaCost: $0.0
Total Cost: $0.0
Sweeper :

You are not making use of the return values of getGuestCount, getSlicesPerPerson etc.

Those methods return a value, which basically means that you can use them as if they are a value. input.nextInt returns a value too, which is why you can put it on the right of =.

Inside the method, getGuestCount seems to change the value of guests passed in, but this change won't actually reflect on the caller's side, because Java is pass-by-value. You are kind of throwing away the value that was passed in.

In fact, your methods will only work as they are if the arguments are passed by reference, so that the methods can modify the variables passed in. But this is not possible in Java. See this post for the difference between pass-by-value and pass-by-reference.

The right way to rewrite your methods in Java is to return the value (which they are already doing, but you are not making use of the return value), and remove the extraneous parameter.

public static int getGuestCount() {
    int guests;
    while(true) {
        System.out.print("Enter number of guests: ");
        guests = input.nextInt();
        if (guests >= 1 && guests <= 100)
            break;
        else 
            System.out.println("The guest count must be at least 1, but does not exceed 100. Please enter again.");
    }
    return guests;
}
public static int getSlicesPerPerson() {
    int slicesPerPerson;
    while(true) {
    System.out.print("Enter number of slices per person: ");
    slicesPerPerson = input.nextInt();
    if (slicesPerPerson >= 1 && slicesPerPerson <= 8)
        break;
    else
        System.out.println("The pizza slice count must be at least 1, but does not exceed 8. Please try again.");
    }
    return slicesPerPerson;
}
public static double computeRoomCost(int guests) {
    double roomCost;
    if (guests <= 30)
        roomCost = 100.00;
    else
        roomCost = 200.00;
    return roomCost;
}
public static double computeSodaCost(int guests) {
    double sodaCost = guests * 1.50;
    return sodaCost;
}

This is how you "make use of the return values": instead of passing in the variable you want the method to modify, put it on the left hand side of = in an assignment statement:

    guests = getGuestCount();
    slicesPerPerson = getSlicesPerPerson();
    roomCost = computeRoomCost(guests);
    sodaCost = computeSodaCost(guests);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=3705&siteId=1