Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Java* How do I consolidate the if statements for changeDial. These three if statements are repeated 5 times, can I create a method to consolidate

*Java*

How do I consolidate the if statements for changeDial. These three if statements are repeated 5 times, can I create a method to consolidate this?

import java.util.Scanner;

public class Project_5 {

public static void main(String[] args) {

// Declaration

Scanner input = new Scanner(System.in);

final int SENTINEL = -1;

int currentDial = 0, previousDial = 3, changeDial = 0;

int value1 = 0, value2 = 0, value3 = 0, value4 = 0, value5 = 0;

int negativechangeDial = 0, positivechangeDial = 0, nochangeDial = 0;

// Greeting

System.out.println(

"Response Dial Simulator " + "----------------------- " + "Initial setting: " + previousDial);

// Execution Loop

while (currentDial != SENTINEL) {

System.out.println("Enter the next setting (1-5) or -1 to stop.");

currentDial = input.nextInt();

changeDial = currentDial - previousDial;

// 1 Response Entered

if (currentDial == 1) {

value1++;

if (changeDial < 0) {

System.out.println("Negative change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

negativechangeDial++;

previousDial = currentDial;

}

if (changeDial == 0) {

System.out.println("No change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

positivechangeDial++;

previousDial = currentDial;

}

if (changeDial > 0) {

System.out.println("Positive change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

nochangeDial++;

previousDial = currentDial;

}

}

// 2 Response Entered

if (currentDial == 2) {

value2++;

if (changeDial < 0) {

System.out.println("Negative change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

negativechangeDial++;

previousDial = currentDial;

}

if (changeDial == 0) {

System.out.println("No change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

positivechangeDial++;

previousDial = currentDial;

}

if (changeDial > 0) {

System.out.println("Positive change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

nochangeDial++;

previousDial = currentDial;

}

}

// 3 Response Entered

if (currentDial == 3) {

value3++;

if (changeDial < 0) {

System.out.println("Negative change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

negativechangeDial++;

previousDial = currentDial;

}

if (changeDial == 0) {

System.out.println("No change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

nochangeDial++;

previousDial = currentDial;

}

if (changeDial > 0) {

System.out.println("Positive change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

positivechangeDial++;

previousDial = currentDial;

}

}

// 4 Response Entered

if (currentDial == 4) {

value4++;

if (changeDial < 0) {

System.out.println("Negative change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

negativechangeDial++;

previousDial = currentDial;

}

if (changeDial == 0) {

System.out.println("No change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

nochangeDial++;

previousDial = currentDial;

}

if (changeDial > 0) {

System.out.println("Positive change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

positivechangeDial++;

previousDial = currentDial;

}

}

// 5 Response Entered

if (currentDial == 5) {

value5++;

if (changeDial < 0) {

System.out.println("Negative change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

negativechangeDial++;

previousDial = currentDial;

}

if (changeDial == 0) {

System.out.println("No change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

nochangeDial++;

previousDial = currentDial;

}

if (changeDial > 0) {

System.out.println("Positive change: " + previousDial + " to " + currentDial);

System.out.println("Current setting: " + currentDial);

positivechangeDial++;

previousDial = currentDial;

}

}

// -1 Response Entered Quit

if (currentDial == SENTINEL) {

// Response Greeting

System.out.print(" Response Summary " + "---------------- ");

// Count Break Down

System.out.println("1 was chosen " + value1 + " time(s). " + "2 was chosen " + value2 + " time(s). "

+ "3 was chosen " + value3 + " time(s). " + "4 was chosen " + value4 + " time(s). "

+ "5 was chosen " + value5 + " time(s). ");

// +/- Breakdown

System.out.println("Positive change: " + positivechangeDial + " " + "Negative change: "

+ negativechangeDial + " " + "No change: " + nochangeDial);

}

}

input.close();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

They provide a neat frame around the type.

Answered: 1 week ago

Question

1. In what ways has flexible working revolutionised employment?

Answered: 1 week ago