Define a class MyNumber having one private int data member. Write a default constructor to initialize it to 0 and another constructor to initialize it to a value (Use this). Write methods isNegative, isPositive, isZero, isOdd, isEven. Create an object in main. Use command line arguments to pass a value to the object (Hint : convert string argument to integer) and perform the above tests. Provide javadoc comments for all constructors and methods and generate the html help file.
Mynumber.java
import java.io.*;
class Mynumber
{
int a;
Mynumber()
{
a=0;
}
Mynumber(int a)
{
this.a=a;
}
boolean ispositive(int a)
{
if(a>0)
return true;
else
return false;
}
boolean isnegative(int a)
{
if(a<0)
return true;
else
return false;
}
boolean iseven(int a)
{
if(a%2==0)
return true;
else
return false;
}
boolean isodd(int a)
{
if(a%2!=0)
return true;
else
return false;
}
boolean iszero(int a)
{
if(a==0)
return true;
else
return false;
}
}
class Mynumber1
{
public static void main(String args[]) throws IOException
{
int a=Integer.parseInt(args[0]);
Mynumber m=new Mynumber();
if(m.isodd(a))
System.out.println("Odd");
if(m.iseven(a))
System.out.println("Even");
if(m.ispositive(a))
System.out.println("Positive");
if(m.isnegative(a))
System.out.println("Negative");
if(m.iszero(a))
System.out.println("Zero");
}
How to compile and run above program in windows
Step 3:
In this step, we will compile the program. For this, open command prompt (cmd) on Windows .
To compile the program, type the following command and hit enter.
javac Mynumber.java
You may get this error when you try to compile the program: “javac’ is not recognized as an internal or external command, operable program or batch file“. This error occurs when the java path is not set in your system
If you get this error then you first need to set the path before compilation.
Set Path in Windows:
Open command prompt (cmd), go to the place where you have installed java on your system and locate the bin directory, copy the complete path and write it in the command like this.
set path=C:\Program Files\Java\jdk1.8.0_121\bin
Step 4:
After compilation the .java file gets translated into the .class file(byte code). Now we can run the program. To run the program, type the following command and hit enter:
java Mynumber 67 (any number for parsing )
For Download Notepad++ - Click Here
For Download Java SE Development Kit 8 - Click Here
0 Comments
If anyone has Doubts or suggestions please let me know