Beyond Verses

My blog that specializes in Space Science and latest news from NASA

Monday, October 27, 2008

Assignment 3 , Prog 2 ( House )

public class House {
private String name;
private int nOfMen;
private int nOfWomen;
private String addOfHouse;
static int count = 0;

//default constructor
public House(){
name = "";
nOfMen = 0;
nOfWomen = 0;
addOfHouse = "";
count++;
}
// the parameter constructor
public House(String Name,int NOfMen,int NOfWomen,String AOfHouse){
name = Name; //the variable Name (parameter) ended when we close the braces
nOfMen = NOfMen;
nOfWomen = NOfWomen;
addOfHouse = AOfHouse;
count++;
}

// Setter
public void setName(String s){
name = s;
}
public void setNOfMen(int n){
nOfMen = n;
}
public void setNOfWomen(int n){
nOfWomen = n;
}
public void setAddOfHouse(String s){
addOfHouse = s;
}

//Getter
public String getName(){
return name;
}
public int getNOfMen(){
return nOfMen;
}
public int getNOfWomen(){
return nOfWomen;
}
public String getAddOfHouse(){
return addOfHouse;
}

//Add method
public void addMan(){
nOfMen++ ;
}
public void addWoman(){
nOfWomen++ ;
}

//The ratio of men to women living in the house
public double MFRatio()
{
if (nOfWomen > 0)
{
double x = (double)nOfMen / (double)nOfWomen ;
return x;
}
else
return -1;
}

//First function compare
public int Compare(House a)
{
if (MFRatio() > a.MFRatio()) // MFRatio() is represent the reference call the function MFRatio s1.MFRatio();
return 1; // s1.Compare(s2);
else if (MFRatio() < a.MFRatio())
return -1;
else
return 0;
}

//Second function compare
public static int secondCompare(House a,House b)
{ // static for all prog
if (a.MFRatio() > b.MFRatio()) // because it is static we call it with House.secondCompare(h1,h2);
return 1;
else if (a.MFRatio() < b.MFRatio())
return -1;
else
return 0;
}

// Display
public String tostring(){
String x;
x = "House Name : "+name+"\nMen Number : "+nOfMen+"\nWomen Number : "+nOfWomen+"\nHouse Address : "+addOfHouse;
return x;
}
}

 


// to input dialog 
import javax.swing.JOptionPane;
public class Student {
private String name;
private int id;
private String gender;
private House studentHouse;

//default constructor
public Student(){
name = "";
id = 0;
gender = "";
studentHouse = null;
}

//parameter constructor
public Student(String a,int b,String c,House d){
name = a;
id = b;
gender = c;
studentHouse = d;

if (gender == "male")
studentHouse.addMan();
else if (gender == "female")
studentHouse.addWoman();
else
System.exit(0);
}

//Setter
public void setName(String s){
name = s;
}
public void setId(int n){
id = n;
}
public void setGender(String s){
gender = s;
}
public void setHouse(House a){
studentHouse = a;
}
public void setAddress(String add){
studentHouse.setAddOfHouse(add);
}

//Getter
public String getName(){
return name;
}
public int getId(){
return id;
}
public String getGender(){
return gender;
}
public House getHouse(){
return studentHouse;
}
public String getStudentAdd(){
return studentHouse.getAddOfHouse();
}

// Display
public String tostring(){
String x;
x = "Student Name : "+name+"\nStudent ID : "+id+"\nStudent Gender : "+gender+"\nStudent House :- \n"+studentHouse.tostring();
return x;
}

public String getHouseInfo(){
return studentHouse.tostring();
}


// main function
public static void main(String[]args){

// construct 2 houses
House h1 = new House("Smart House",5,3,"Alexandria");
House h2 = new House("Classic House",3,2,"Cairo");
String x1 = h1.tostring();
String x2 = h2.tostring();
System.out.println("House Information(1) :- \n"+x1+"\nHouse Information(2) :- \n"+x2);

// construct 2 student
Student s1 = new Student("Ahmed Hamdy",2121,"male",h1);
Student s2 = new Student("Jasmen",125,"female",h2);
String y1 = s1.tostring();
String y2 = s2.tostring();
System.out.println("Student Information(1) :- \n"+y1+"\nStudent Information(2) :- \n"+y2);

// get student address
String a1 = s1.getStudentAdd();
String a2 = s2.getStudentAdd();

//get Houses Info and display them
String z1 = s1.getHouseInfo();
String z2 = s2.getHouseInfo();
System.out.println("Student house(1) :- \n"+z1+"\nStudent house(2) :- \n"+z2);

//that show the number of construct houses
int nOfHouse = House.count;
System.out.println("Num of Created Houses = "+nOfHouse);

//compare 2 house in there men to women ratio
if (h1.MFRatio()>0 && h2.MFRatio()>0)
{
if (House.secondCompare(h1,h2)==1)
System.out.println("Men to Women Ratio house1 > Men to Women Ratio house2");
else if (House.secondCompare(h1,h2)==-1)
System.out.println("Men to Women Ratio house1 < Men to Women Ratio house2");
else
System.out.println("Men to Women Ratio house1 = Men to Women Ratio house2");
}
else
System.out.println("you enter the number of women <=0");

//change the name of a house and display again a full report
h1.setName("Engineering House");
h2.setName("Java House");
String c1 = h1.tostring();
String c2 = h2.tostring();
System.out.println("Student House Info Modified(1) :- \n"+c1+"\nStudent House Info Modified(2) :- \n"+c2);

}
}

No comments:

Post a Comment