Answer to Question #276099 in Java | JSP | JSF for Candy

Question #276099

Try to refactor below codes for computing score (underlined part) with extract methods

public class Customer

{

void foo()

{

int a, b, c, xfactor;

int score; Scanner myInput = new Scanner(System.in);

a = myInput.nextInt();

b = myInput.nextInt();

c = myInput.nextInt();

xfactor = myInput.nextInt();

System.out.println(“Computer score:”);

// Compute score score = a*b+c;

score *= xfactor;

}

}


1
Expert's answer
2021-12-07T09:21:37-0500
import java.util.Scanner;


class Customer {
	private int a, b, c, xfactor;


	public void inputData() {
		Scanner myInput = new Scanner(System.in);
		System.out.print("Enter a: ");
		a = myInput.nextInt();
		System.out.print("Enter b: ");
		b = myInput.nextInt();
		System.out.print("Enter c: ");
		c = myInput.nextInt();
		System.out.print("Enter xfactor: ");
		xfactor = myInput.nextInt();
	}


	public void displayScore() {
		System.out.println("Computer score: " + computeScore());
	}


	private int computeScore() {
		// Compute score = a*b+c;
		int score = a * b + c;
		score *= xfactor;
		return score;
	}
}


public class App {


	/**
	 * The start point of the program
	 * 
	 * @param args
	 * 
	 */
	public static void main(String[] args) {
		Customer customer = new Customer();
		customer.inputData();
		customer.displayScore();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS