Practice problem Requirements: 1.Write a Java program that plays a word game wit
ID: 3664885 • Letter: P
Question
Practice problem Requirements:
1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers.
2.The program must perform the following:
a.Uses a Scanner object to ask the user: (The program asks for no other information)
i.Full Name (First and Last name only) - stores this Full Name in one String object variable.
ii.Age – must be read in as an int.
iii.Profession or expected profession
iv.Name of favorite pet
v.Cost of their first vehicle – must be read in as a double.
b.All input data must be stored in their own variable prior to printing out.
c.Display a paragraph of the programmer’s choosing that contains the following
i.The user’s full name, first name, and last name in separate places in the paragraph, such as James Gosling founded Sun Microsystems. James was a child prodigy. The Gosling family is very proud of James.
ii.The user’s age.
iii.The user’s profession in all uppercase regardless of the case the profession was entered.
iv.The user’s favorite pet’s name in all lower case regardless of the case the name was entered.
v.The total cost of the user’s first vehicle in a rounded two decimal places including the dollar signs. The total cost must include the inputted cost plus a 7.5% sales tax. The sales tax rate is stored as a constant in the program.
vi.A statement in parenthesis stating the total number of characters in the paragraph such as (The above story contained 345 characters)
d.Include javadoc class comment following Project Comment Template on the content page.
Explanation / Answer
/**The java program that prompts user to enter
* firstname, lastname, age ,profession ,petname
* and cost of vehicle and creates an paragraphs
* as mentioned in the question and prints the
* paragrapha and number of characters in the
* paragraph.*/
//Paragraph.java
import java.util.Scanner;
public class Paragraph
{
public static void main(String[] args)
{
//a.Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//i-v)Declare variables to read input
//from user
String firstName;
String lastName;
String fullName;
int age;
String profession;
String petname;
double costOfVehicle;
//sales tax 7.5 %
final double SALES_TAX=0.075;
//b. Read variables from user
System.out.println("Enter first name :");
firstName=scanner.nextLine();
System.out.println("Enter last name :");
lastName=scanner.nextLine();
System.out.println("Enter age :");
age=Integer.parseInt(scanner.nextLine());
System.out.println("Enter profession name :");
profession=scanner.nextLine();
System.out.println("Enter favorite pet name :");
petname=scanner.nextLine();
System.out.println("Enter cost of first vehicle :");
costOfVehicle=Double.parseDouble(scanner.nextLine());
System.out.println("Paragraph");
//Create the paragraph with required specifications
String paragraph=firstName+lastName+" founded Sun Microsystems."+
firstName+" was a child prodigy. The"+ lastName+
" family is very proud of "+firstName+" and "+" his age is "+age+"."+
"His profession was "+profession.toUpperCase()+" "+
" and his favorite pet's name "+petname+"."+
" The cost of first vehicle was $"+String.format("%.2f", costOfVehicle)+
" Total cost with sales tax is $"+(costOfVehicle+costOfVehicle*SALES_TAX)+".";
System.out.println(paragraph);
//get the number of characters
int totalCharacters=paragraph.length();
//print number of characters
System.out.println("(The above story contained "+totalCharacters+" characters)");
}
}
------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Enter first name :
James
Enter last name :
Goslings
Enter age :
55
Enter profession name :
computer engineering
Enter favorite pet name :
dolmission
Enter cost of first vehicle :
100
Paragraph
JamesGoslings founded Sun Microsystems.James was a child prodigy. TheGoslings family is very proud of James and his age is 55.His profession was COMPUTER ENGINEERING and his favorite pet's name dolmission. The cost of first vehicle was $100.00 Total cost with sales tax is $107.5.
(The above story contained 283 characters)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.