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 2 Practice Programs
1. Create a program to find the factors of a number taken as user input, store the factors in an
array, and display the factors. Also find the sum, sum of square of factors and product of the
factors and display the results
Hint =>
a. Take the input for a number
b. Write a static Method to find the factors of the number and save them in an array and
return the array.
c. To find factors and save to array will have two loops. The first loop to find the count and
initialize the array with the count. And the second loop save the factors into the array
d. Write a method to find the sum of the factors using factors array
e. Write a method to find the product of the factors using factors array
f. Write a method to find the sum of square of the factors using [Link]() method
2. Write a program to find the sum of n natural numbers using recursive method and compare
the result with the formulae n*(n+1)/2 and show the result from both computations is correct.
Hint =>
a. Take the user input number and check whether it's a Natural number
b. Write a Method to find the sum of n natural numbers using recursion
c. Write a Method to find the sum of n natural numbers using the formulae n*(n+1)/2
d. Compare the two results and print the result
3. Write a program that takes a year as input and outputs the Year is a Leap Year or not
Hint =>
a. The LeapYear program only works for year >= 1582, corresponding to a year in the
Gregorian calendar.
b. Also Leap year is divisible by 4 and not divisible by 100 or divisible by 400
c. Write a method to check for Leap Year using the conditions a and b
4. Extend or Create a UnitConvertor utility class similar to the one shown in the notes to do
the following. Please define static methods for all the UnitConvertor class methods. E.g.
public static double convertKmToMiles(double km) =>
a. Method To convert kilometers to miles and return the value. Use the following code
double km2miles = 0.621371;
b. Method to convert miles to kilometers and return the value. Use the following code
double miles2km = 1.60934;
c. Method to convert meters to feet and return the value. Use the following code to convert
double meters2feet = 3.28084;
d. Method to convert feet to meters and return the value. Use the following code to convert
double feet2meters = 0.3048;
3
5. Extend or Create a UnitConvertor utility class similar to the one shown in the notes to do
the following. Please define static methods for all the UnitConvertor class methods. E.g.
public static double convertYardsToFeet(double yards) =>
a. Method to convert yards to feet and return the value. Use following code to convert
double yards2feet = 3;
b. Method to convert feet to yards and return the value. Use following code to convert
double feet2yards = 0.333333;
c. Method to convert meters to inches and return the value. Use following code to convert
double meters2inches = 39.3701;
d. Method to convert inches to meters and return the value. Use following code to convert
double inches2meters = 0.0254;
e. Method to convert inches to centimeters and return the value. Use the following code
double inches2cm = 2.54;
6. Extend or Create a UnitConvertor utility class similar to the one shown in the notes to do
the following. Please define static methods for all the UnitConvertor class methods. E.g.
public static double convertFarhenheitToCelsius(double farhenheit) =>
a. Method to convert Fahrenheit to Celsius and return the value. Use the following code
double farhenheit2celsius = (farhenheit - 32) * 5 / 9;
b. Method to convert Celsius to Fahrenheit and return the value. Use the following code
double celsius2farhenheit = (celsius * 9 / 5) + 32;
c. Method to convert pounds to kilograms and return the value. Use the following code
double pounds2kilograms = 0.453592;
d. Method to convert kilograms to pounds and return the value. Use the following code
double kilograms2pounds = 2.20462;
e. Method to convert gallons to liters and return the value. Use following code to convert
double gallons2liters = 3.78541;
f. Method to convert liters to gallons and return the value. Use following code to convert
double liters2gallons = 0.264172;
7. Write a program to take user input for the age of all 10 students in a class and check
whether the student can vote depending on his/her age is greater or equal to 18.
Hint =>
a. Create a class public class StudentVoteChecker and define a method public
boolean canStudentVote(int age) which takes in age as a parameter and returns
true or false
b. Inside the method firstly validate the age for a negative number, if a negative return is
false cannot vote. For valid age check for age is 18 or above return true; else return
false;
c. In the main function define an array of 10 integer elements, loop through the array by
take user input for the student's age, call canStudentVote() and display the result
4
8. Create a program to find the youngest friends among 3 Amar, Akbar and Anthony based on
their ages and tallest among the friends based on their heights and display it
Hint =>
a. Take user input for age and height for the 3 friends and store it in two arrays each to
store the values for age and height of the 3 friends
b. Write a Method to find the youngest of the 3 friends
c. Write a Method to find the tallest of the 3 friends
9. Write a program to take user input for 5 numbers and check whether a number is positive or
negative. Further for positive numbers check if the number is even or odd. Finally compare
the first and last elements of the array and display if they are equal, greater, or less
Hint =>
a. Write a Method to Check whether the number is positive or negative
b. Write a Method to check whether the number is even or odd
c. Write a Method to compare two numbers and return 1 if number1 > number2 or 0 if both
are equal or -1 if number1 < number2
d. In the main program, Loop through the array using the length call the method
isPositive() and if positive call method isEven() and print accordingly
e. If the number is negative, print negative.
f. Finally compare the first and last element of the array by calling the method compare()
and display if they are equal, greater, or less
10.An organization took up the exercise to find the Body Mass Index (BMI) of all the persons in
the team of 10 members. For this create a program to find the BMI and display the height,
weight, BMI and status of each individual
Hint =>
a. Take user input in double for the weight (in kg) of the person and height (in cm) for the
person and and store it in the corresponding 2D array of 10 rows and 3 columns. The
First Column storing the weight, the second column storing the height in cm and the third
column is the BMI
b. Create a Method to find the BMI of every person and populate the array. Use the formula
BMI = weight / (height * height). Note unit is kg/m^2. For this convert cm to meter
c. Create a Method to determine the BMI status using the logic shown in the figure below.
and return the array of all the persons BMI Status.
5
2
11.Write a program Quadratic to find the roots of the equation 𝑎𝑥 + 𝑏𝑥 + 𝑐. Use Math
functions [Link]() and [Link]()
Hint =>
a. Take a, b, and c as input values to find the roots of x.
b. The roots are computed using the following formulae
2
𝑑𝑒𝑙𝑡𝑎 = 𝑏 + 4 * 𝑎 * 𝑐
If delta is positive the find the two roots using formulae
𝑟𝑜𝑜𝑡1 𝑜𝑓 𝑥 = (− 𝑏 + 𝑑𝑒𝑙𝑡𝑎)/(2 * 𝑎)
𝑟𝑜𝑜𝑡1 𝑜𝑓 𝑥 = (− 𝑏 − 𝑑𝑒𝑙𝑡𝑎)/(2 * 𝑎)
If delta is zero then there is only one root of x
𝑟𝑜𝑜𝑡 𝑜𝑓 𝑥 = − 𝑏/(2 * 𝑎)
If delta is negative return empty array or nothing
c. Write a Method to find find the roots of a quadratic equation and return the roots
12.Write a program that generates five 4 digit random values and then finds their average
value, and their minimum and maximum value. Use [Link](), [Link](), and
[Link]().
Hint =>
a. Write a method that generates array of 4 digit random numbers given the size as a
parameter as shown in the method signature
public int[] generate4DigitRandomArray(int size)
b. Write a method to find average, min and max value of an array
public double[] findAverageMinMax(int[] numbers)