OddAndEven object coderinme

OddAndEven object coderinme

Write Java program involving two classes: OddAndEven and TestOddAndEven. OddAndEven and countOfEven both of type int parameter and increment countOfOdd, if the number is odd, else increment x, Number of Even : y”, where x and y are the values of the instance variables. The TestOddAndEven class first creates OddAndEven object, then in a loop, read a number and use it to call the addNumber method until the user enters Q. Finally, it prints the count of odd and even numbers entered.
Program:

import java.io.*;
class OddAndEven 
{
private int countOfOdd;
private int countOfEven;
public OddAndEven() 
{
countOfOdd=0;
countOfEven=0;
}
public void addNumber(int n) 
{
if(n%2==0) countOfEven++;
else countOfOdd++;
}
public String toString() 
{
return "\nNumber of Odd:"+countOfOdd+" , Number of Even:"+countOfEven+"";
}
}
public class Q19 {
public static void main(String[] args) throws IOException 
{
OddAndEven OandE=new OddAndEven();
System.out.print("Enter next number or Q to stop : ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s=in.readLine();
while(!(s.equals("q"))) {
int num=Integer.parseInt(s);
OandE.addNumber(num);
System.out.print("Enter next number or Q to stop : ");
s=in.readLine();
}
System.out.println(OandE);
}
}

For more programs on Java, visit our Java Archives

odd even

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.