Question

Description When you want to knit an object by hand you purchase a pattern and yan. The pattern tells you how to perform the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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-

Problems JavadocDeclaration SeachConsole terminated> Yarn [Java Application] CAProgram FilesJavaljre-9.0.4binljavaw.exe (12-F

Add a comment
Know the answer?
Add Answer to:
Description When you want to knit an object by hand you purchase a pattern and yan....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT