Java Program to check weather number is even, odd, positive, negative - IProgramX

Q.  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. 


Program:

public class MyNumber {
private int x;
public MyNumber(){
x=0;
}
public MyNumber(int x){
this.x=x;
}
public boolean isNegative(){
if(x<0)
return true;
else return false;
}
public boolean isPositive(){
if(x>0)
return true;
else return false;
}
public boolean isZero(){
if(x==0)
return true;
else return false;
}
public boolean isOdd(){
if(x%2!=0)
return true;
else return false;
}
public boolean isEven(){
if(x%2==0)
return true;
else return false;
}

public static void main(String [] args) throws ArrayIndexOutOfBoundsException
{
int x=Integer.parseInt(args[0]);
MyNumber m=new MyNumber(x);
if(m.isNegative())
System.out.println("Number is Negative");
if(m.isPositive())
System.out.println("Number is Positive");
if(m.isEven())
System.out.println("Number is Even");
if(m.isOdd())
System.out.println("Number is Odd");
if(m.isZero())
System.out.println("Number is Zero");
}

}


Command: 

                    javac MyNumber.java
                    java MyNumber -9

Output:

Number is Negative
Number is Odd

Post a Comment

2 Comments

  1. Slip6_2: Create an abstract class “order” having members id, description. Create two
    subclasses “PurchaseOrder” and “Sales Order” having members customer name and
    Vendor name respectively. Definemethods accept and display in all cases. Create 3
    objects each of Purchase Order and Sales Order and accept and display details.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    abstract class Order{
    String id,des;
    }
    class Porder extends Order{
    String cnm, vnm;
    public void accept()throws IOException{
    System.out.println("enter id, description,names of customers and vendors");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    id = br.readLine();
    des= br.readLine();
    cnm = br.readLine();
    vnm = br.readLine();
    }
    public void display(){
    System.out.println("id"+id);
    System.out.println("Description"+des);
    System.out.println("Customer Name"+cnm);
    System.out.println("Vendor Name"+vnm);
    System.out.println("-------------------");
    }
    }
    class Sorder extends Order
    {
    String cnm, vnm;
    public void accept()throws IOException{
    System.out.println("enter id, description,names of customers and vendors");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    id = br.readLine();
    des= br.readLine();
    cnm = br.readLine();
    vnm = br.readLine();
    }
    public void display(){
    System.out.println("id:"+id);
    System.out.println("Description:"+des);
    System.out.println("Customer Name:"+cnm);
    System.out.println("Vendor Name:"+vnm);

    System.out.println("-------------------");
    }
    }
    class Main{
    public static void main(String args[])throws IOException{
    int i;
    System.out.println("Select any one:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("1.purchase order:");
    System.out.println("2.Sales order:");
    System.out.println("3.Exit:");
    int ch = Integer.parseInt(br.readLine());
    switch(ch){
    case 1:
    System.out.println("enter the no of purchas order:");
    int n = Integer.parseInt(br.readLine());
    Porder[] l = new Porder[n];
    for(i=0;i<n;i++)
    {
    l[i] = new Porder();
    l[i].accept();
    }
    for(i=0;i<n;i++)
    {
    l[i].display();
    System.out.println("Object is created:");
    }
    case 2:
    System.out.println("enter the no of sales order:");
    int m = Integer.parseInt(br.readLine());
    Porder[] h = new Porder[m];
    for(i=0;i<m;i++)
    {
    h[i] = new Porder();
    h[i].accept();
    }
    for(i=0;i<m;i++)
    {
    h[i].display();
    System.out.println("Object is created:");
    }
    case 3:
    System.out.println("exit:");
    System.exit(0);
    }
    }
    }

    ReplyDelete