Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please explain how to code this project as I am having difficulty with this concept. Thanks // /** * @(#)DemoPrintf.java * * * PROGRAM PURPOSE:

image text in transcribed

Please explain how to code this project as I am having difficulty with this concept.

Thanks

//

/** * @(#)DemoPrintf.java * * * PROGRAM PURPOSE: TO DEMONSTRATE THE USE OF THE printf METHOD IN GENERATING OUTPUT. * */ public class DemoPrintf {//BEGIN DemoPrintf /** * TWO LOCAL VARIABLES ARE DECLARED TO DEMONSTRATE A printf STATEMENT. * - THE printf STATEMENT IS CODED WITH FORMAT SPECIFIERS AND WITHOUT * FORMAT SPECIFIERS. * - THE printf STATEMENT IS CODED WITH A MESSAGE THAT GENERATES MULTIPLE * OUTPUT LINES. */ public static void main(String[] args) {//BEGIN main() int age = 20; //VARIABLE THAT STORES SOMEONE'S AGE AS AN INTEGER. String name = "Hector"; //VARIABLE THAT STORES SOMEONE'S NAME AS TEXT ALSO KNOWN AS String IN JAVA. /************************************************************** * BEST APPROACH TO CODING printf IS TO USE FORMAT SPECIFIERS. ************************************************************** * %s IS A FORMAT SPECIFIER FOR A String. * %d IS A FORMAT SPECIFIER FOR AN int. * %n IS A FORMAT SPECIFIFER FOR A line advance THAT IS MORE UNIVERSALLY ACCEPTED. * GO TO APPENDIX I OF YOUR TEXTBOOK FOR MORE ON VARIOUS FORMAT SPECIFIERS. */ System.out.printf("%nMESSAGE USING FORMAT SPECIFIERS: %n%s is %d years old.", name, age); /************************************************************** * BEST APPROACH TO CODING printf IS TO USE FORMAT SPECIFIERS. ************************************************************** * USE WHEN YOU NEED A LINE ADVANCE INSIDE A STRING LITERAL THAT * IS USED AS AN ARGUMENT FOR A %s. */ System.out.printf("%n%s: %n%s is %d years old.", " MESSAGE USING \"\ \" TO LINE ADVANCE IN STRING LITERAL", name, age); /********************************************************************* * TEDIOUS APPROACH IS TO NOT USE FORMAT SPECIFIERS. ********************************************************************* * ANOTHER WAY TO CODE THE OUTPUT MESSAGE IS TO BREAK THE MESSAGE * INTO PIECES. HOW MANY PIECES YOU HAVE IS DETERMINED BY HOW MANY * VARIABLES AND WHERE THEY NEED TO BE INSERTED IN THE MESSAGE. THE * PIECES ARE A COMBINATION OF STRING LITERALS AND VARIABLES JOINED * USING THE + SIGN (CONCATENATION SYMBOL). IN THE printf BELOW THERE * ARE 6 OPERANDS: * * 1. FORMAT STRING: "%n%nMESSAGE USING \"%%n\" AND CONCATENATION: " * 2. FORMAT STRING: "%n" * 3. VARIABLE: name * 4. STRING LITERAL: " is " * 5. VARIABLE: age * 6. STRING LITERAL: " years old." * * OPERAND #6 IS ADDED TO 5 WHICH GIVES YOU A NEW STRING OBJECT WITH 5 * AND 6. THAT NEW STRING OBJECT IS ADDED TO 4 WHICH GIVES YOU ANOTHER * NEW STRING OBJECT WHICH IS ADDED TO 3, AND SO FORTH UNTIL YOU WIND * UP WITH A NEW STRING OBJECT THAT HAS EVERYTHING AND IT IS THAT STRING * OBJECT THAT GETS PRINTED. THE VARIABLE OPERANDS ARE CHANGED INTO STRINGS, * SO THEY CAN BE ADDED. * * STRINGS ARE "IMMUTABLE/UNCHANGEABLE," SO YOU ARE ESSENTIALLY * COMBINING 6 SEPARATE STRING OBJECTS INTO A BRAND NEW ONE THAT * IS THEN PRINTED. TO PROPERLY SEPARATE THE WORDS IN A MESSAGE, SPACES * ALSO HAVE TO BE INSERTED IN THE NECESSARY PLACES. NOTICE HOW THERE ARE * SPACES BEFORE AND AFTER " is " AND BEFORE " years old.". A PERIOD IN A * MESSAGE WILL ONLY PRINT IF IT IS INSERTED. NOTICE THAT THE PERIOD IS PART * OF THE STRING LITERAL " years old." BECAUSE IT IS WITHIN THE DOUBLE QUOTES. */ System.out.printf("%n%nMESSAGE USING \"%%n\" AND CONCATENATION: " + "%n" + name + " is " + age + " years old."); /******************************************************************************* * BEST WAY TO CODE AN OUTPUT MESSAGE WITH MULTIPLE LINES AND FORMAT SPECIFIERS ******************************************************************************* * - NOTE THAT THE VARIABLES ASSOCIATED WITH THE FORMAT SPECIFIERS ARE * LISTED LAST. * - VARIABLES ASSOCIATED WITH FORMAT SPECIFIERS ARE REFERRED TO AS * "ARGUMENTS." * - THE 2ND LINE OF THE MESSAGE IS LONG, SO IT IS DIVIDED INTO TWO * STRING LITERALS JOINED BY CONCATENATION (USING THE + SIGN). * - A LONG MESSAGE WHEN CODED FROM LEFT TO RIGHT IS HARD TO READ * UNLESS IT'S BROKEN INTO PIECES. * - CODE SHOULD BE WITHIN THE "LINE OF SIGHT" WHERE THE PROGRAMMER * DOESN'T HAVE TO **SCROLL** FROM LEFT TO RIGHT TO READ IT. */ System.out.printf("%n%n%nMESSAGE WITH MULTIPLE LINES OF OUTPUT: " + "%n%n%s is %d years old." + "%nAnd his family is planning a surprise " + "birthday party for him!", name, age); System.exit(0); //TO SMOOTHLY EXIT PROGRAM }//END main() }//END APPLICATION CLASS DemoPrintf

image text in transcribed

Instructions: Name your program YourLastNameFirstInitialLE32. Code a program that 1. asks for the number of traffic violations, 2. then asks for the number of months within which these violations were incurred. 3. If the number of violations is greater than 3 and the number of months is less than or equal to 12, or the number of violations is greater than 6 and the number of months is less than 25, a. set a boolean variable called suspendLicense to true. b. If the license is suspended print the suspension message; otherwise, print the non- suspension message. c. The messages can be found in the attached sample output. d. Code 2 single-selection IFs that nest the test for the month to determine whether the license is suspended. e. Code a double-selection IF (if-else) to determine which message to print based on the truth value of suspendLicense. 4. Use a sentinel-controlled while loop. The while loop contains all the prompts (except for the priming-read prompt), ifs, nested ifs, if-else, and messages along with the re- prompt for the sentinel variable. a. You'll have to reset suspendLicense to false within the while loop. b. Don't forget an empty call to nextLine(). c. Refer to the sample output for the sentinel variable prompt. 5. Use printf(): refer to Demo Printf.java. 6. Exit the main(). 7. Refer to sample output for additional output details. Submission Requirements: Zip ALL your files (.java, java-, .class) in a folder with the same name as your program. Upload the zip file to Blackboard. Capturing Character Input.pdf Demo Printf.java ***SAMPLE OUTPUT *** + NOT PART OF OUTPUT Do you want to see if your license is suspended? Enter 'Y' or 'N': y How many traffic violations have you had? 4 within a period of how many months were these violations committed? 6 Your license is suspended! Do you want to see if your license is suspended? Enter 'Y' or 'N': y How many traffic violations have you had? 7 within a period of how many months were these violations committed? 12 Your license is suspended! Do you want to see if your license is suspended? Enter 'Y' or 'N': y How many traffic violations have you had? 2 within a period of how many months were these violations committed? 18 Lucky day! Your license is NOT suspended. Do you want to see if your license is suspended? Enter 'Y' or 'N': n

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

Recommended Textbook for

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions

Question

What is one of the skills required for independent learning?Explain

Answered: 1 week ago