Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6729145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:15:15+00:00 2026-05-26T10:15:15+00:00

Iam doing a school assignment in Java, and I need some help to do

  • 0

Iam doing a school assignment in Java, and I need some help to do some calculations in a method. Iam used to PHP, and I’ve appended a lot of my knowledge in the code, but this one I just cant figure out (I know how do do it without the function, but that requires much more lines of code which is stupid).

Here is some of my code:

public static void main (String[] args) {
// User inputs
calculate("Number of beers", 20, 1.50);
}

public static void calculate(String articleName, double numberOfX, double pricePerUnit) {

        double subTotal = numberOfX * pricePerUnit;
        System.out.printf("%-20s %-1s %10.2f\n", articleName, ":", subTotal);
}

This prints out a nice bill of the things I’ve bought. Furthermore I would like this method to add the totalprice to a (global?) variable which eventually shows the final price of all items. In PHP i usually wrote a variable named totalDue += subTotal;

Is there any way to do this in java? I would be a shame to write an entire new function to do the math if I just could add the total price of each item into a variable.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:15:15+00:00Added an answer on May 26, 2026 at 10:15 am

    Global variables don’t exist in Java.

    And this is not how it should be done. Rather than the method updating some variable, the method should just return the result of the computation, and the caller should be responsible of using the result as he wants to:

    double total = 0D;
    total += calculate("Number of beers", 20, 1.50);
    total += calculate("Number of pizza", 10, 8);
    // ...
    

    This way, you won’t have to change anything in the calculate method when you’ll want to compute subtotals, or averages, or anything. One method = one responsibility.

    This should be true for your PHP programs as well.

    After this is done, you should encapsulate the article name, number of items, and unit price in a class, and add methods to the class, like toString (to display the bought item), and computePrice (to compute the price of this bought item).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing an assignment for school where we are to make two php
I am doing some C programming for school and I have found myself reusing
I am doing an assignment for school and I'm not sure how to check
I am doing an assignment for school and I am using an array filled
Hello I am in the process of doing a school project, where we have
I am doing some research on Unicode for a white-paper I am writing. Does
I am doing some simple sanity validation on various types. The current test I'm
I am doing an Financial Winforms application and am having some trouble with the
I just want to simplify what I am doing before, having multiple php files
Here are some models I am trying to load data for: class School(models.Model): name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.