Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assignment 1

Please consider the following points before starting the assignment:


• This assignment must be done individually.
• If you have any questions, please first discuss it with the TA (sandy.nguyen2@mcgill.ca).
• Make sure that all classes and method names are spelled and capitalized exactly as described in the
document.
• Make sure that your code compiles if it doesn’t the code will get 0 points!
• Write your name and student ID as a comment at the top of all .java files.
• Use proper indentation for your code.

• Choose a meaningful name for your variables.


• Comment your work sufficiently. You don’t need to comment every line of your code, but there should be
enough comments to fully understand your program.
Question 1 (3 points):
Write a program (DivisibilityCheck.java) that takes two integers as input and checks if the first number is
divisible by the second number. If it is divisible, display "Divisible", otherwise display "Not divisible".
First try to write the program without any hints, if you are stuck in any part of the code use the following
hints to figure out what might be a potential solution for that. (YOU DON’T HAVE TO FOLLOW THIS
INSTRUCTION!)
1. Understand the problem: Read and understand the problem statement carefully. Make sure you
have a clear understanding of what needs to be done.
2. Prompt the user for input: Use the Scanner class to prompt the user to enter the first and second
numbers.
3. Read and store the input: Use appropriate variables to store the values entered by the user for the
first and second numbers.
4. Check for divisibility: Use an if statement to check if the second number is not zero and if the
first number is divisible by the second number. You can use the modulus operator (%) to check if
the remainder is zero.
5. Display the result: Within the if-else statement, print "Divisible" if the condition is true, and "Not
divisible" if the condition is false.
6. Test with different inputs: Verify that your program works correctly by testing it with different
input values. Test cases should include both numbers that are divisible and numbers that are not
divisible.
7. Handle divide-by-zero scenario: Ensure that you handle the case where the second number is zero
separately to avoid a potential divide-by-zero error. You can check if the second number is zero
before checking for divisibility.
8. Use meaningful variable names: Choose meaningful variable names for the first and second
numbers to make your code more readable and easier to understand.

Question 2 (2 points):
Write a program (LargestNumber.java) that takes three integers as input and determines the largest among
them. Display the largest number. First your program must prompt the user to enter three integers.
Note: It is a good practice to close the Scanner to free up system resources.
Question 3 (5 point):
Write a program (CharacterTypeCheck.java) that takes a character as input and checks if it is a lowercase,
uppercase or number letter. If the character is a lowercase letter such as ‘a’ it must display “Lowercase
letter” message to the user. If the character is an uppercase letter such as ‘A’ it must display “Uppercase
letter” message to the user. If the character is a number such as ‘1’ it must display “Number” message to
the user. In any other cases such as ‘(’ 1’ it must display “Number” message to the user “Other character”
to the user.

Note#1: Your program must prompt the user to enter a character. Also, it must convert the character to its
corresponding ASCII value and identify lowercase, uppercase, and number based on their ASCII values.
Note#2: To write this code you will need to use the information in the ASCII Conversion Chart at
https://web.alfredstate.edu/faculty/weimandn/miscellaneous/ascii/ascii_index.html
First try to write the program without any hints, if you are stuck in any part of the code use the following
hints to figure out what might be a potential solution for that. (YOU DON’T HAVE TO FOLLOW THIS
INSTRUCTION!)
1. Understand ASCII values: Familiarize yourself with the ASCII table, which assigns numerical
values to characters. Lowercase letters have ASCII values ranging from 97 to 122, uppercase
letters from 65 to 90, and numbers from 48 to 57.
2. Prompt for user input: Use the Scanner class to prompt the user to enter a character.
3. Read and store the input: Use the charAt(0) method to read the first character entered by the user
and store it in a char variable.
4. Convert to ASCII value: Use type casting (int) to convert the character to its corresponding ASCII
value and store it in an int variable.
5. Check lowercase condition: Use an if statement to check if the ASCII value falls within the range
of lowercase letters (97 to 122). If true, print "Lowercase letter".
6. Check uppercase condition: Use an if statement to check if the ASCII value falls within the range
of uppercase letters (65 to 90). If true, print "Uppercase letter".
7. Check number condition: Use an if statement to check if the ASCII value falls within the range of
numbers (48 to 57). If true, print "Number".
8. Handle other characters: If none of the conditions are met, assume the character is something
other than a lowercase letter, uppercase letter, or number, and print "Other character".
9. Test with different characters: Verify that your program works correctly by testing it with various
input characters. Test cases should include lowercase letters, uppercase letters, numbers, and
other characters.
10. Use meaningful variable names: Choose meaningful variable names, such
as character and asciiValue, to make your code more readable and easier to understand.
11. Use comments: Add comments to your code to explain what each section or line of code does.
This will make your code more readable and easier to understand.

You might also like