What is an integer overflow error what happens when an


Questions:

Submit your answers to these questions as a text file named "Questions.txt". You should not need to write more than a few sentences for each answer.

  1. Many sports and games have constants embedded in their rules. For example, baseball has 9 innings, 3 outs per inning, 3 strikes in an out, and 4 balls per walk. We can program Java named constants for a program involving baseball as follows:
      public static final int INNINGS = 9;
      public static final int OUTS_PER_INNINGS = 3;
      public static final int STRIKES_PER_OUT = 3;
      public static final int BALLS_PER_WALK = 4;
    Select a game or sport that you are familiar with, and give at least three lines of Java code initializing the constants for a program involving that game or sport. You should use conventional Java naming conventions for constants. Also include as a comment, an explanation of the constants you have chosen (e.g., "baseball has 9 innings, 3 outs per inning...").
  2. Consider the following two boolean expressions, and explain why they each are probably not what the programmer intended.
      a. ((x > 10)|| (x < 100))

      b. ((2 < 5) && (x < 100))
  3. Complete a program memory trace of your solution to Lab 3 (Conversation.java). You should also turn in your code for Lab 3 again (to be compared to your trace). For your trace, assume the user enters the sentence: "When it rains, I hate getting wet."
  4. Now consider your solution to Lab 4 (Approximation.java). Think about the six boolean operators. What can you conclude about using floating point numbers in boolean expressions, based on what you saw when you ran the program?
  5. Review your answer to the Type Casting Activity (especially question 5 in Model 1). Explain in your own words:
      a. What is an integer overflow error?

      b. What happens when an integer overflow error occurs? Describe the behavior of the program.

      c. Why is multiplication particularly risky for integer overflow errors?

Program 1: Write a program (TypeCast.java) that demonstrates type casting of double values by performing the following tasks:

Use Scanner to read a floating-point value into a variable called number.
Type cast number to an int value and store the result in a variable called integer.
Display number and integer, clearly labeled.
Typecast number to a byte value and store the result into the result other.
Display number and other clearly labeled.

Try your program on positive and negative values of number that range from 2e-11 to 2e11. What can you conclude?

Program 2:

Write a program (DateCheck.java) that reads a String from the keyboard using the nextLine method, and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid.

The input date will have the format mm/dd/yyyy (m/d/yyyy is also valid, as are m/dd/yyyy and mm/d/yyyyy). A valid year value must be four digits. A valid month value mm must be from 1 to 12 (January is 1). The day value dd must be from 1 to a value that is appropriate for the given month. April, June, September and November each have 30 days. February has 28 days except for leap years when it has 29. A leap year is any year that is divisible by 4, but not divisible by 100 unless it is already divisible by 400. All remaining months have 31 days each.

Java hints:

You cannot use the nextInt method to read in the String.
You will need two versions of each date field (month, day, and year): one String variable and one int variable.
You will need to isolate the mm, dd, and yy as Strings first.
To convert Strings to ints, use the Integer.parseInt method, which takes a String as an argument and returns the corresponding int.
You should use many if-statements and many boolean expressions to check if the date is valid.
For the homework assignment, you only need to catch errors where the numeric values of mm, dd, or yyyy are invalid. All other errors would be caught as extra credit.

Don't forget to write this program in steps, and compile and test after each step. You may want to write down your steps before beginning.

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: What is an integer overflow error what happens when an
Reference No:- TGS01082608

Expected delivery within 24 Hours