Solving problems using a programming language is the main aim of learning programming. Let’s see an example of a problem and how to solve it using everything we learned until now (Unit 1 and 2).
To solve a problem using programming, we have to follow certain steps, usually on the same order. We first identify the classes, then design an algorithm for each operation. Only after that, we think on the specific programming language that we want to use and translate our design (class and algorithms) into the programming language.
So, let’s see what the problem is and propose a solution.
Solving problems
A customer requires a software that given a digit (0,1,…,9), the following is calculated:
- Is the number prime?
- Is the number odd or even?
- Print the letter representation of the number. i.e., if the number is 1, print on the screen One.
Now, we must apply what we learned until now to solve the previous problem.
The steps are as follow:
- Identify the classes present in the problem.
- Design the algorithms that will be used.
- Implement the classes in your favorite language (Java will be used, you can use another one if you want).
- Implement a console application that make use of the implemented classes.
From the problem we can see that we only one class. This class will represent a digit. Therefore, we will call it Digit.
Next, we identify the attributes. In this case, the class will represent a digit, so the attribute will the value of the digit (0, 1, 2, …, 9). So, we will call this attribute value.
Next, we define the constructor. Here the important part is to determine what will be the parameters. In this case, we want to pass a value as a parameter, so when we create a digit object, it is initialized on a certain value.
Then, we have the methods (operations). We will define one operation per question asked in the problem.
Find below the class definition.
Digit |
– value: int |
+ Digit(value: int) + isPrime(): boolean + isOdd(): boolean + getLetters(): String |
Now let’s design the algorithms for each method we defined. For simplicity, I’m going to use pseudo-code. I’ll encourage you to use flow diagrams to represent each one.
isPrime
Find below the pseudo-code to determine whether a number is prime or not. There is a more efficient way to do it, but for now, let’s do it in this way.
Repeat from i=2 to i<digit{
if number is divisible by i, return false
}
return true
Notice the use of a loop to iterate from 2 until number-1. If we find out that the digit is divisible by at least one of the numbers in that range (2,number-1), then we can affirm that the number (a digit in this case) is not prime.
If the execution goes out of the loop, it means that the number is prime, because the digit is no divisible by any number in the range (2,number-1.
The structure we are using here is a loop with post-condition. The pseudo-code can be easily adapted to a pre-condition loop structure. I encourage you to design the pseudo-code using a loop with pre-condition.
isOdd
The pseudo-code for this operation is straightforward and it won’t need an explanation.
if the remainder of the division of the digit by 2 equals 1, then return true (the digit is odd)
otherwise return false (the digit is not odd.)
if the remainder of the division of the digit by 2 equals 1, then return true (the digit is odd), otherwise return false (the digit is not odd.)
A shorter version will be something like this:
return digit mod 2 == 1
Why will the previous pseudo-code return the same?
Notice that after the keyword return we have a boolean expression. digit mod 2 will return a number, then that number == 1 is a boolean expression because we are using the comparison operator ==, which returns true or false.
getLetters
This method we can implemented in two ways. The general idea is to compare the digit with 1, if the condition holds, then we return “One”, and so on with the rest of the digits.
We can use the if-then-else structure.
if digit==1, return One
else if digit==2 return Two
...
But, when we have a pseudo-code like the previous one, there is a cleaner way to design our algorithm. Yes, you guess right, we can use the structure switch-case.
The pseudo-code will be as follows.
switch (digit){
case 0: result = "Zero"; break;
...
case 9: result = "Nine"; break;
default: result ="unknown digit";
}
return result;
Let’s see the Java implementation of the whole class.
Java implementation of the class Digit
public class Digit {
//attribute
private int value;
//Constructor with one parameter
public Digit(int value){
this.value = value;
}
// Methods
public boolean isOdd(){
if (value % 2 == 1)
return true;
else
return false;
}
// A simplified version of the previous method
public boolean isOddVersion1(){
return value % 2 == 1;
}
// Prime with post-condition
public boolean isPrime(){
for (int i = 2; i< value; i++){
if (value % i == 0)
return false;
}
return true;
}
// Prime with pre-condition
public boolean isPrimePreCondition(){
int n = 2;
while (n< value){
if (value % n == 0)
return false;
n++;
}
return true;
}
//Switch-case structure example
public String getLetters(){
String result;
switch (this.value){
case 0: result="Zero"; break;
case 1: result="One"; break;
case 2: result="Two"; break;
case 3: result="Three"; break;
case 4: result="Four"; break;
case 5: result="Five"; break;
case 6: result="Six"; break;
case 7: result="Seven"; break;
case 8: result="Eight"; break;
case 9: result="Nine"; break;
default: result = "Unkown digit";
}
return result;
}
}
Now, we have to use the implemented class in a way, that the user can see the answer to the questions that were asked in the problem statement.
For that, we are going to create a console application and use the class we just created.
Solving problems: Console application to use the class Digit
Find below the code for our console application.
public class Main {
public static void main(String[] args) {
// Create the object number,
// notice the lowercase n, different from the class Number
Digit number = new Digit(0);
//Check if the number is prime
if (number.isPrime()){
System.out.println("The digit is prime");
}
else
System.out.println("The digit is not prime");
//Check if the number is prime using a loop with precondition
//a.k.a. a while-loop
if (number.isPrimePreCondition()){
System.out.println("The digit is prime");
}
else
System.out.println("The digit is not prime");
//Check if the number is odd
if (number.isOdd()){
System.out.println("The digit is odd");
}
else
System.out.println("The digit is even");
//Check if the number is odd version1
if (number.isOddVersion1()){
System.out.println("The digit is odd");
}
else
System.out.println("The digit is even");
//Obtain and print the letters that represent the number
System.out.println(number.getLetters());
}
}
Notice that the first thing we have to do to use the class Digit is to create an object.
Then, we just have to use the operations (methods) we implemented as part of the class Digit and print a message so the user can see the result.
Did you execute already the code above? What are you waiting? Just do it!
If you have any problem to execute the code provided here, just leave a comment and I’ll be more than happy to reply and give you additional guidance.
H@ppy coding!