37. Develop program that has the following methods: ODD(), EVEN(), NEGATIVE(), POSITIVE(). Each method will be visited in order
of sequence as above stated. Every visit will print the string or word the same name of the class if the number entered is TRUE.
Otherwise, no value is returned or NULL. Then, call each method until it reaches the method name POSTIVE() will then stop the
whole process and display the final output.
Example#1:
Enter a number: 3
3 is ODD POSITIVE
Example#2:
Enter a number: -4
-4 IS EVEN NEGATIVE
import javax.swing.JOptionPane;
public class Number {
private static String str="";
public static void main(String[] args) {
int num=Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
String display=ODDEVEN(num);
JOptionPane.showMessageDialog(null, num+" is "+display);
}
public static String ODDEVEN(int num){
if(num==0){
str="";
}
else if(num%2==0){
str="EVEN";
}
else {
str="ODD";
}
NEGAPOS(num);
return str;
}
public static String NEGAPOS(int num){
if(num<0){
str=str+"-NEGATIVE";
}
else if(num>0){
str=str+"-POSITIVE";
}
else{
str=str+"ZERO";
}
return str;
}
public static boolean isEVEN(int num){
if(num%2==0){
return true;
}
else{
return false;
}
}
public static boolean isPOSITIVE(int num){
if(num>0){
return true;
}
else{
return false;
}
}
}
Walang komento:
Mag-post ng isang Komento