CS110 Java Programming Assignment 4

Instructor:Trish Cornez

PROGRAM 1:
Write a program that asks the user to input three int values, a, b, and c.
The goal of this program is to output the sum of the input values. However, there is one rule: Execution 1:
    Input integer 1: 2
    Input integer 2: 3
    Input integer 3: 1
    Result: 6
Execution 2:
    Input integer 1: 5
    Input integer 2: 13
    Input integer 3: 2
    Result: 5
Execution 3:
    Input integer 1: 13
    Input integer 2: 7
    Input integer 3: 2
    Result: 0
Execution 4:
    Input integer 1: 7
    Input integer 2: 5
    Input integer 3: 13
    Result: 12




PROGRAM 2:
Assume the following rules:
  1. Two integers are "close" to each other if their values differ from each other by at most one.
  2. Two integers are "far" to each other if their values differ from each other by two or more.

Write a program that asks the user to input three int values, a, b, and c.
Display true if one of b or c is "close" to a and the other is "far" from the other two.
Otherwise, display false.
TIP: Math.abs() computes the absolute value of a number.

Execution 1:
    Input integer 1: 1
    Input integer 2: 2
    Input integer 3: 10
    Result: true
Execution 2:
    Input integer 1: 1
    Input integer 2: 2
    Input integer 3: 3
    Result: false
Execution 3:
    Input integer 1: 10
    Input integer 2: 8
    Input integer 3: 9
    Result: false
Execution 4:
    Input integer 1: -1
    Input integer 2: 10
    Input integer 3: 0
    Result: true




PROGRAM 3:
Write a program that will prompt the user for a date and determine its validity. If the date is invalid, an error message should be displayed explaining precisly where the error is. If the date is valid, the program should compute the day of the year.

    Computing the number of days in February
    During leap years, there are 29 days in February. During non-leap years, there are exactly 28 days. To be a leap year, the year must be evenly divisible by 4. However, not all years evenly divisible by 4 are leap years. Years whose last two digits are zero are century years; for examble, 1800, 1900, and 2000 are century years. Century years are leap years only if they are evenly divisible by 400. The years 1600 and 2000 are leap years. 1700, 1800, and 1900 are not leap years.



Sample run number 1:

Please enter a date (mm dd yyyy): 13 1 2001

This date has an error! 13 is an invalid month.



Example run number 2:

Please enter a date (mm dd yyyy): 3 12 3012

This is 3-12-3012 and is day number 72 of the leap year 3012.



Example run number 3:

Please enter a date (mm dd yyyy): 2 29 2002

The day 2-29-2002 is invalid. There are only 28 days in February during non-leap years.






PROGRAM 4:
Write a program that validates a right triangle. Your program should prompt and read three edges representing a right triangle and determine whether the input is valid. The input is valid only they adhere to the pythagorean theorem. Don't forget to validate each edge.

Use the following input for your program. Sample run number 1:

Please enter the three edges of a triangle: 3 5 4

This is a right triangle.

Sample run number 2:

Please enter the three edges of a triangle: 4 5 2

This is NOT a right triangle.

Sample run number 3:

Please enter the three edges of a triangle: 1 - 2 1

The second edge is invalid.