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
xenn [34]
3 years ago
6

Fix the code in Oops2.java so that it is better structured and avoids redundancy. There are some lines of code that are repeated

. Think about where you can move the repeated lines in the code so that they only appear once. You do not need to add any error checking on the user input, assume that the user will always enter 1 or 2. Here is the code:import java.util.Scanner;public class Oops2 {public static void main(String[] args) { int sum = 0; int count = 0; int total = 0; Scanner console = new Scanner(System.in); System.out.print("Is your money multiplied 1 or 2 times?"); int times = console.nextInt(); if (times == 1){ System.out.print("And how much are you contributing? "); int donation = console.nextInt(); sum = sum + donation; count++; total = total + sum; } if (times == 2){ System.out.println("And how much are you contributing? "); int donation = console.nextInt(); sum = sum + 2 * donation; count++; total = total + sum; } System.out.println("The total is " + total);}}
Computers and Technology
1 answer:
ICE Princess25 [194]3 years ago
7 0

Answer:

Fixed code is as follows:

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {

       int sum = 0; int count = 0;

       int total = 0;

       Scanner console = new Scanner(System.in);

       System.out.print("Is your money multiplied 1 or 2 times?");

       int times = console.nextInt();

       System.out.print("And how much are you contributing? ");

       int donation = console.nextInt();

       if (times == 1){

           sum = sum + donation;

       

       }

       if (times == 2){

           sum = sum + 2 * donation;

           

       }

       count++;

       total = total + sum;

       System.out.println("The total is " + total);

       

   }

   

}

sample output:

Is your money multiplied 1 or 2 times?2

And how much are you contributing? 1

The total is 2

Explanation:

In the above code following changes are performed:

1. The statement

       System.out.print("And how much are you contributing? ");

       int donation = console.nextInt();

was repeating in both if blocks. So better to provide them before the if statement since these are only used to read the value of donation.

2. The statement

       count++;

       total = total + sum;

was also repeating in both if blocks. So better to provide them after the if block since both of these are trying to increment two independent variable count and total.

The code is better structured using indentation rules.

You might be interested in
Explain the steps users can take to maintain an operating system
Mars2501 [29]

First is it to always clean out all the junk that is left behind by the operating system and the browsers. You can easily do this using the Disk Cleanup utility included on the windows systems. You can clean up the registry. However, you should be very careful with the registry. Any wrong move might mess up everything


Search and destroy viruses and malware on your PC by installing an anti-virus security program and setting up an automated maintenance schedule.


Defragment main and partitioned drives to keep your computer running smoothly.


Uninstall software programs and personal files like pictures, movies and music that are no longer in use to free up more space. The more the space, the faster the PC will go. You can also run the msconfig command in the RUN command prompt to uncheck start up programs that you do not use

4 0
4 years ago
What does it mean to have liability for a company?
astraxan [27]

you can get tax identification number, open a bank account and do business all under its own name


3 0
3 years ago
Use the drop-down menus to complete the statements about using column breaks in word 2016
Naily [24]

Answer: Breaks in the word 2016 what do you mean

Explanation:

7 0
3 years ago
Read 2 more answers
The capacity of a communication channel is measured in _______.
PSYCHO15rus [73]
<span>The capacity of a communication channel is measured in gb and gps.</span>
4 0
3 years ago
Read 2 more answers
Sam plans to use this image in artwork for a brochure about airplanes. Which principles of page layout is Sam planning to use in
Fittoniya [83]

Answer: Alignment

Explanation:

The principle of repitition indicates that some aspect of a design are repeated. This can be in form of bullet list, lines, color etc.

Balance has to do with how the weight is distributed.

The Principle of Alignment means that the pictures on a page should be connected visually to another thing.

Principle of Proximity simoly means that the items that are related on a page should be close to each other.

Therefore, the principles of page layout that Sam is planning to use in this artwork is alignment.

6 0
3 years ago
Other questions:
  • Two electronics technicians are looking at the testing instrument in the figure above. Technician A says that this instrument is
    7·1 answer
  • Business Risks are ​
    15·1 answer
  • Why isn't my rank move from ambitious to virsto i met all the requirements
    8·2 answers
  • You are manually configuring a tcp/ip host. another administrator gives you the router's ip address. what is the tcp/ip term for
    6·2 answers
  • Plis is a unit test !!!!!!! HelP ASAP only 30 mins left, will mark as brainliest
    8·1 answer
  • Which term refers to the blank areas surrounding a document page? *
    15·1 answer
  • I was killed <br><br> යඞ <br><br> what do i do
    10·1 answer
  • I love my baby boy and can someone help me with this
    6·1 answer
  • One of your suppliers has recently been in the news. Workers complain of long hours, hot and stuffy workrooms, poor lighting, an
    15·1 answer
  • The first person to say something in here will get brainliested.
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!