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
Why should computers have a file structure?
UkoKoshka [18]
I am guessing "A" because you can store code anywhere and file structure does not prevent viruses.

hope this helped
 <span />
4 0
2 years ago
Read 2 more answers
If you Buy my group clothing in R.o.b.l.o.x for a donation i will make you brainliest
erastova [34]

Answer:kk ima do it

Explanation:

7 0
3 years ago
Read 2 more answers
Name three situations when a copy constructor executes.
Vesna [10]

Answer:

1. When an object of the class is passed (to a function) by value as an argument.

2.  When an object is constructed based on another object of the same class.

3. When compiler generates a temporary object.

Explanation:

7 0
2 years ago
What is a good coding site
Helga [31]

Sratch.com

-Agarvated

6 0
2 years ago
If a system's instruction set consists of an 8-bit opcode, what is the maximum number of output signal lines required for the co
otez555 [7]

Answer:

D. 256

Explanation:

Given

Instructions = 8\ bit

Required

Determine the maximum number of output

To get the required value, we make use of the following:

Maximum = 2^n

Where n is the bits of the opcode.

i.e.

n = 8

Substitute 8 for n in Maximum = 2^n

Maximum = 2^8

Maximum = 256

<em>Hence, option D answers the question</em>

6 0
2 years ago
Other questions:
  • Which is a function of network media?
    14·2 answers
  • 1. To type a capital "C", you would use the ring finger of your left hand.
    15·2 answers
  • https://brainly.com/app/ask?entry=top&amp;q=What+did+you+learn+during+this+course+that+reinforces+your+belief+in+your+technology
    5·1 answer
  • Your boss is very skeptical about the idea of storing his files up in the cloud rather
    8·1 answer
  • [1] Please find all the candidate keys and the primary key (or composite primary key) Candidate Key: _______________________ Pri
    6·1 answer
  • Which of these is an example of input?
    6·1 answer
  • Need the answer ASAP!!!!!!!!!!!!! I’ll mark brainliest if correct
    8·1 answer
  • Uhaul general mechanical questions 2021
    9·1 answer
  • Briefly describe the working of computer processing system?
    11·1 answer
  • Assume the name of your data frame is flavors_df. What code chunk lets you review the structure of the data frame?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!