Credits

Saturday, July 3, 2010

Another Java Constructor with Static Methods

It is possible that when we define methods to be static, we can access them  using classname.methodname() format.

The program below is the other version of this  program.
[+/-] show/hide


import java.io.*;
public class Householdv2 {
static int numOccupants;
static double annualIncome;
Householdv2()
{ numOccupants=1;
annualIncome=0;}
public static void main(String args[]) throws IOException{

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//Household x=new Household();
String str;
int numOcc;
double xincome=0.0;

System.out.println("use of Constructors:");
System.out.println("Number of Occupants "+Householdv2.getOccupants());
System.out.println("Annual Income"+Householdv2.getIncome());

System.out.println("After Use:");
System.out.println("Input number of occupants :");
numOcc=Integer.parseInt(br.readLine());

System.out.println("Input Annual Income: ");
str=br.readLine();
xincome=Double.parseDouble(str);

Householdv2.setOccupants(numOcc);
Householdv2.setIncome(xincome);

if (numOcc<=0) Householdv2.numOccupants=1; if (xincome<0.0) Householdv2.annualIncome=0.0; System.out.println("Number of Occupants "+Householdv2.getOccupants()); System.out.println("Annual Income"+Householdv2.getIncome()); } static double getIncome() { return annualIncome;} static void setOccupants(int x) { numOccupants=x;} static double getOccupants() { return numOccupants;} static void setIncome(double x) { annualIncome=x;} }

AddThis