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
Write code that prints: Ready! firstNumber ... 2 1 Run!
motikmotik

Answer:

The program to this question can be defined as follows:

Program:

num=int(input("Enter any number: ")) #defining variable num, that take value from user

val=0; #defining variable val

for i in range(num): #defining loop to calculate value in decreasing order

   val=num-i; #holding value in variable

   print(val) #print value

Output:

Enter any number: 3

3

2

1

Explanation:

In the above-given code, a variable "num" is declared, that uses input function to take value from the user end, in the next line a "val" variable is defined, that calculates values and print it in decreasing order.

  • In the next step, a for loop is declared, inside the loop a "val" variable uses num and loop variable "i" to calculate the value in decreasing order.
  • In the last print method is used that prints "val" variable value.
3 0
3 years ago
A picture icon that is a direct link to a file program or folder
Tems11 [23]
It's a folder button or desktop button mostly I recomend the folder button

4 0
3 years ago
Sensitive security information can be shared with whom
lukranit [14]
Details? Referring to what? Using what?


8 0
3 years ago
Read 2 more answers
How are BGP neighbor relationships formed
slamgirl [31]

Answer:

They are set up manually

Explanation:

BGP neighbor relationships formed "They are set up manually."

This is explained between when the BGP developed a close to a neighbor with other BGP routers, the BGP neighbor is then fully made manually with the help of TCP port 179 to connect and form the relationship between the BGP neighbor, this is then followed up through the interaction of any routing data between them.

For BGP neighbors relationship to become established it succeeds through various phases, which are:

1. Idle

2. Connect

3. Active

4. OpenSent

5. OpenConfirm

6. Established

6 0
3 years ago
Create a page using PHP for a business website that will ask the user to enter his or her name into text boxes and will display
Jet001 [13]

Answer:

1         <?php

2            if (isset($_GET["submit"])) {

3                 echo "Welcome " . $_GET["name"];

4                }  

5         ?>

6

7

8         <form method="get">  

9            <input name="name" type="text" placeholder="Enter your name"/>

10           <button name="submit" type="submit">Submit</button>

11          </form>  

12

13         <?php

14          ?>

<h2>Explanation:</h2>

Lines 1 - 5    check if the user has clicked on the submit button.

                    If the button has been clicked the a welcome message is    

                    shown.

Lines 8 - 11   create a form to hold the text box and the submit button

                    Give the form a <em>method</em> attribute of value <em>get        [Line 8]</em>

<em>                     </em>Give the input field a <em>name</em> attribute of value <em>name </em>and a

                    placeholder attribute of value <em>Enter your name      [Line 9]</em>

<em>                     </em>Give the button a <em>name </em>attribute of value <em>submit</em> and a <em>type</em>

                    attribute of value <em>submit</em>                                            <em>[Line 10]</em>

<em />

<em />

<em />

<em />

PS: Save the file as a .php file and run it on your server. Be sure to remove the line numbers before saving. A sample web page is attached to this response.

4 0
2 years ago
Other questions:
  • A portable electronic device that can be used in an emergency to stop someone’s heart from going out of rhythm is called a/an
    6·2 answers
  • Which kind of device is hardware capable of transferring items from computers and devices to transmission media and vice versa?
    10·1 answer
  • Which of these is not one of the main parts of an email
    9·1 answer
  • What method is used to manage contention-based access on a wireless network?
    7·1 answer
  • A network that has locations in different countries is considered a WAN.<br> a. Trueb. False
    12·1 answer
  • Why is it important to study in a quiet well lit area
    5·2 answers
  • Point out the correct statement.
    11·1 answer
  • WILL MARK BRIAN PLZ HELP PLZ I BEG YOU While levels often change in order to give a game some more variability, they might also
    11·1 answer
  • Consider the following class interfaces:
    8·1 answer
  • PLZ ANSWER ASAP WILL GIVE BRAINLIEST
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!