import java.util.Scanner;
public class Yarn {
public static void main(String[] args) {
// Scanner class object to take
user input
Scanner scan = new
Scanner(System.in);
// Input yarn name
System.out.println("Enter the name
of the first type of yarn");
String yarn1 = scan.nextLine();
// Input yards per ball
// Validate : If < 0, ask user
to enter again
System.out.println("Enter the
number of yards per ball of yarn");
int yards1 = scan.nextInt();
while (yards1 < 0) {
System.out.println("The number of yards per ball must be positive,
please re-enter");
yards1 =
scan.nextInt();
}
scan.nextLine();
// Input number of balls
// Validate: If < 0, ask user to
enter again
System.out.println("Enter the
number of balls needed");
int balls1 = scan.nextInt();
while (balls1 < 0) {
System.out.println("The number of balls must be positive, please
re-enter");
balls1 =
scan.nextInt();
}
scan.nextLine();
// Input another yarn
System.out.println("Enter the name
of substituted yarn");
String yarn2 = scan.nextLine();
// Input yards per ball
// Validate : If < 0, ask user
to enter again
System.out.println("Enter the
number of yards per ball");
int yards2 = scan.nextInt();
while (yards2 < 0) {
System.out.println("The number of yards per ball must be positive,
please re-enter");
yards2 =
scan.nextInt();
}
scan.nextLine();
// Calculate yards needed by
first yarn
int yardsneeded = yards1 *
balls1;
// Calculate balls needed by
second yarn
// Take the ceil value of result
means 3.76 will be converted to 4
int balls2 = (int)
Math.ceil((double) yardsneeded / yards2);
// Print result
System.out.println("You should
purchase " + balls2 + " balls of " + yarn2 + " instead of " +
balls1
+ " balls of " + yarn1);
}
}
OUTPUT-
Description When you want to knit an object by hand you purchase a pattern and yan....