Best Programming Practice
1. All values as variables including Fixed, User Inputs, and Results
2. Proper naming conventions for all variables
3. Proper Program Name and Class Name
4. Proper Method Name which indicates action taking inputs and providing result
Sample Program 1: Create a program to find the sum of all the digits of a number given by a
user using an array and display the sum.
a. Use [Link]() and get a 4-digit random integer number
b. Write a method to count digits in the number
c. Write a method to return an array of digits from a given number.
d. Write a method to Find the sum of the digits of the number in the array
e. Finally, display the sum of the digits of the number
Java
// Create SumOfDigit Class to compute the sum of 4 digits random number
class SumOfDigits {
// Get a 4 digit random number
public int get4DigitRandomNumber() {
return (int) ([Link]() * 9000) + 1000;
}
// Find the count of digits in the number
public int countDigits(int number) {
int count = 0, temp = number;
while (temp > 0) {
count++;
temp /= 10;
}
return count;
}
// Store the digits of the number in an array
public int[] getDigits(int number, int count) {
int[] digits = new int[count];
int temp = number;
for (int i = count - 1; i >= 0; i--) {
digits[i] = temp % 10;
temp /= 10;
}
return digits;
}
1
// Find the sum of the elements in an array
public int sumArray(int[] array) {
int sum = 0;
for (int i = 0; i < [Link]; i++) {
sum += array[i];
}
return sum;
}
public static void main(String[] args) {
// Get 4 digit random integer number
SumOfDigits sumOfDigits = new SumOfDigits();
int number = sumOfDigits.get4DigitRandomNumber();
[Link]("The Random Mumber is: " + number);
// Get the count of digits
int count = [Link](number);
[Link]("The count of digit is: " + count);
// Get the array of digits from the number
int[] digits = [Link](number, count);
// Find the sum of the digits of the number
int sum = [Link](digits);
// Display the sum of the digits of the number
[Link]("\nSum of Digits: " + sum);
}
}
2
Level 1 Practice Programs
1. Write a program to input the Principal, Rate, and Time values and calculate Simple Interest.
Hint =>
a. Simple Interest = Principal * Rate * Time / 100
b. Take user input for principal, rate, time
c. Write a method to calculate the simple interest given principle, rate and time as
parameters
d. Output “The Simple Interest is ___ for Principal ___, Rate of Interest ___ and Time ___”
2. Create a program to find the maximum number of handshakes among students.
Hint =>
a. Get integer input for the numberOfStudents variable.
b. Use the combination = (n * (n - 1)) / 2 formula to calculate the maximum number of
possible handshakes.
c. Write a method to use the combination formulae to calculate the number of handshakes
d. Display the number of possible handshakes.
3. An athlete runs in a triangular park with sides provided as input by the user in meters. If the
athlete wants to complete a 5 km run, then how many rounds must the athlete complete
Hint =>
a. Take user input for 3 sides of a triangle
b. The perimeter of a triangle is the addition of all sides and rounds is distance/perimeter
c. Write a Method to compute the number of rounds user needs to do to complete 5km run
4. Write a program to check whether a number is positive, negative, or zero.
Hint => Get integer input from the user. Write a Method to return -1 for negative number, 1
for positive number and 0 if number is zero
5. Write a program SpringSeason that takes two int values month and day from the command
line and prints “Its a Spring Season” otherwise prints “Not a Spring Season”.
Hint => Spring Season is from March 20 to June 20. Write a Method to check for Spring
season and return a boolean true or false
6. Write a program to find the sum of n natural numbers using loop
Hint => Get integer input from the user. Write a Method to find the sum of n natural numbers
using loop
7. Write a program to find the smallest and the largest of the 3 numbers.
Hint =>
a. Take user input for 3 numbers
b. Write a single method to find the smallest and largest of the three numbers
3
public static int[] findSmallestAndLargest(int number1, int number2, int number3)
8. Write a program to take 2 numbers and print their quotient and reminder
Hint =>
a. Take user input as integer
b. Use division operator (/) for quotient and moduli operator (%) for reminder
c. Write Method to find the reminder and the quotient of a number
public static int[] findRemainderAndQuotient(int number, int divisor)
9. Create a program to divide N number of chocolates among M children. Print the number of
chocolates each child will get and also the remaining chocolates
Hint =>
a. Get an integer value from user for the numberOfchocolates and numberOfChildren.
b. Write the method to find the number of chocolates each child gets and number of
remaining chocolates
public static int[] findRemainderAndQuotient(int number, int divisor)
10.Write a program calculate the wind chill temperature given the temperature and wind speed
Hint =>
a. Write a method to calculate the wind chill temperature using the formula
0.16
𝑤𝑖𝑛𝑑𝐶ℎ𝑖𝑙𝑙 = 35. 74 + 0. 6215 * 𝑡𝑒𝑚𝑝 + (0. 4275 * 𝑡𝑒𝑚𝑝 − 35. 75) * 𝑤𝑖𝑛𝑑𝑆𝑝𝑒𝑒𝑑
public double calculateWindChill(double temperature, double windSpeed)
11.Write a program to calculate various trigonometric functions using Math class given an angle
in degrees
Hint =>
a. Method to calculate various trigonometric functions, Firstly convert to radians and then
use Math function to find sine, cosine and tangent.
public double[] calculateTrigonometricFunctions(double angle)