Question: May you fix it? Also, may you make sure it compiles correctly too. In this Programming Activity, you will write a short program using some

May you fix it? Also, may you make sure it compiles correctly too.

In this Programming Activity, you will write a short program using some of the classes and methods discussed in this chapter. Your program will perform the following operations:

    1. Prompt the user for his or her first name
    2. Print a message saying hello to the user
    3. Tell the user how many characters are in his or her name
    1. Ask the user for the year of his or her birth
    2. Calculate and print the age the user will be this year
    3. Declare a constant for average life expectancy; set its value to 78.94
    4. Print a message that tells the user the percentage of his or her expected life lived so far formatted to one decimal place
    1. Generate a random number between 1 and 20
    2. Output a message telling the user that the program is thinking of a number between 1 and 20 and ask for a guess
    3. Output a message telling the user the number and how far away from the number the users guess was

To complete this Programming Activity, copy the contents of this chapters Programming Activity 2 folder in the supplied code accompanying this text. Open the PracticeMethods.java file and look for four sets of five asterisks (*****), where you will find instructions to write import statements and items 1, 2, and 3 for completing the Programming Activity.

Example 3.21 shows the PracticeMethods.java file, and Figure 3.28shows the output from a sample run after you have completed the Programming Activity. Your output might vary from that shown because we use 2020 as the current year for calculating age and because item 3 generates a random number.

EXAMPLE 3.21 PracticeMethods.java

1 /* Chapter 3 Programming Activity 2

2 Calling class methods

3 Anderson, Franceschi

4 */

5

6 // ***** 1. add your import statements here

7

8 public class PracticeMethods

9 {

10 public static void main( String [ ] args )

11 {

12 //*****

13 // 2. a. Create a Scanner object to read from the keyboard.

14 // b. Prompt the user for their first name.

15 // c. Print a message that says hello to the user.

16 // d. Print a message that says how many letters

17 // are in the user's name.

18 // Your code goes here

19

20

21 //*****

22 // 3. a. Skip a line, then prompt the user for the year

23 // they were born.

24 // b. Declare a constant for the current year.

25 // c. Calculate and print the age the user will be this year.

26 // d. Declare a constant for average life expectancy,

27 // set its value to 78.94.

28 // e. Calculate and print the percentage

29 // of the user's expected life they've lived.

30 // Use the DecimalFormat class to format the percentage

31 // to one decimal place.

32 // Your code goes here

33

34

35 //*****

36 // 4. a. Generate a secret random integer between 1 and 20

37 // b. Skip a line, then ask the user for a guess.

38 // c. Print a message telling the user the secret number

39 // and how far from the number the user's guess was

40 // (hint: use Math.abs).

41 // Your code goes here

42

43 }

44 }

My wrong code:

import java.util.Scanner;

public class DomainNameTest {

// drive code

public static void main (String args[])

{

Scanner sc = new Scanner (System.in);

System.out.print ("Enter the domain name : ");

String domainName = sc.next (); // user input

// check domain name is valid or invalid

if (isValidDomainName (domainName))

{

domainName = "www." + domainName + ".com";

System.out.println ("It is valid Domain Name: " + domainName);

}

else

{

System.out.println ("It is invalid Domain Name.");

}

}

// isValidDomainName() is Function to validate domain name.

// International domain names maximum charcater b 63 characters and minimum b 3 characters

//Only alphanumeric characters and hyphens are allowed. The name cannot start or end in a hyphen.*/

public static boolean isValidDomainName (String domainName)

{

// check condition for International domain names maximum charcater 63 characters and minimum 3 characters

if (domainName.length () < 3 || domainName.length () > 63)

return false;

// check condition for "domain name cannot start or end in a hyphen".

if (domainName.charAt (0) == '-'

|| domainName.charAt (domainName.length () - 1) == '-')

return false;

// check the condition for domain name "Only alphanumeric characters and hyphens are allowed"

for (int i = 0; i < domainName.length (); i++)

{

if ((domainName.charAt (i) >= 'a' && domainName.charAt (i) <= 'z')

|| (domainName.charAt (i) >= 'A' && domainName.charAt (i) <= 'Z')

|| (domainName.charAt (i) >= '0' && domainName.charAt (i) <= '9')

|| (domainName.charAt (i) == '-'))

continue;

else

return false;

}

// if all condition are ture then return true .

return true;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!