1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
aleksley [76]
3 years ago
14

The following code is poorly structured. Rewrite it so that it has a better structure and avoids redundancy. To help eliminate r

edundancy, convert the code into a method named spending that accepts two parameters: a Scanner for the console, and a String for a single person's name, and prints the appropriate information about that person's bills. Your method could be called twice (once for John and once for Jane) to replicate the original code's behavior.
Scanner console = new Scanner(System.in);
System.out.print("How much will John be spending? ");
double amount = console.nextDouble();
System.out.println();
int numBills1 = (int) (amount / 20.0);
if (numBills1 * 20.0 < amount) {
numBills1++;
}
System.out.print("How much will Jane be spending? ");
amount = console.nextDouble();
System.out.println();
int numBills2 = (int) (amount / 20.0);
if (numBills2 * 20.0 < amount) {
numBills2++;
}
System.out.println("John needs " + numBills1 + " bills");
System.out.println("Jane needs " + numBills2 + " bills");
Computers and Technology
1 answer:
Virty [35]3 years ago
7 0

Answer:

Explanation:

public static void main(String[] args) {

       Scanner console = new Scanner(System.in);

       spending(console,"John");  //calling spending method with "John"

       spending(console,"Jane");  //calling spending method with "Jane"

   }

   public static void spending(Scanner console, String name){

       System.out.print("How much will "+name+" be spending? ");

       double amount = console.nextDouble();

       System.out.println();

       int numBill = (int) (amount / 20.0);

       if (numBill * 20.0 < amount) {

           numBill++;

       }

       System.out.println("John needs " + numBill + " bills");

   }

Code Explanation

Method are reusable set of code which reduces redundancy. So the original code was trying to calculate the bill for John and Jane but the code to calculate was redundant which will cause more line of code and complex to manage. For example let say if we have 1000 of user to calculate there bill, will it be efficient to write bill calculation code for all 1000 users?

That's where function comes in to reduce redundancy from code and easy to manage.

Easy to Manage in a way, let say you need to change the bill calculation formula then without function, you have to change the formula for all the users but with function you only need to change the bill calculation formula in spending function.

You might be interested in
Which is an acceptable practice when a photographer shoots in windy conditions?
VARVARA [1.3K]

Answer:

d) attach a bag of weights to the tripod

Explanation:

5 0
3 years ago
_______are a set of track sectors, ranging from 2 to 32 or more, depending on the formatting scheme in use.
Nadusha1986 [10]

Answer:

pretty sure it's d. clusters

if not it's also probably a. cylinders

Explanation:

7 0
2 years ago
Zahra's softball team needs money for team T-shirts. The coach makes some fundraising suggestions, while team members brainstorm
user100 [1]

Answer:

I would think it would be D

Explanation:

that's basically what it said in the passage

5 0
3 years ago
Read 2 more answers
Identify two areas of science or technology that make your life easier, safer, or otherwise better than your grandparents lives
vladimir2022 [97]
Heres some:

Quantum computing
Machine Learning
Medicine
Aeronautical Engineering (Drones, better transportation)
8 0
3 years ago
What data type does the in operator return?
mixer [17]

\huge\red{Hi!}

The Operator data type is any expression that is parsed and returns a value, such as tod() , gui() , rtecall() , = (comparison). An operator is a special symbol or function commonly used in expressions.

7 0
1 year ago
Other questions:
  • When issued a GFE device, you are required to sign an AUP
    12·2 answers
  • It's time for you to schedule a dental checkup. The responsible thing to do is to ___
    11·1 answer
  • Which feature is an interface between the user and the file system of a computer?
    6·2 answers
  • Some classes, like the ArrayList class, use angle brackets in their documentation and their declaration. For example, the Java d
    5·1 answer
  • Give an O(log m + log n)-time algorithm that takes two sorted lists of sizes m and n, respectively, as input and returns the ith
    7·1 answer
  • If you want to insert a table which ribbon should you select
    13·2 answers
  • What are the characteristics of the sorting and grouping options in Outlook? Check all that apply. Columns can be sorted by clic
    15·2 answers
  • What is the best way of farming exotics in destiny?
    12·2 answers
  • "kannst du mir bitte helfen" in German-English from Reverso Context: Hier, kannst du mir bitte helfen?
    7·1 answer
  • Michael works for a graphic design firm. He is creating an informative poster. He needs to add a great deal of text in the poste
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!