الأجوبة
Class Fan:
public class Fan {
final static int SLOW=1;
final static int MEDIUM=2;
final static int FAST=3;
private int speed=SLOW;
private boolean on=false;
private double radius=5;
private String color="green";
public Fan()
{
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int speed)
{
this.speed=speed;
}
public boolean getOn()
{
return on;
}
public void setOn(boolean on)
{
this.on=on;
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius=radius;
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color=color;
}
public String toString()
{
/*if(on==true)
return "fan is on , speed= "+speed+", color= "+color+", radius = "+radius;
else
return "fan is off , speed= "+speed+", color= "+color+", radius = "+radius;
*/
//using ternary operator
String s = (on==true) ? "fan is on" : "fan is off";
s=s+", speed= "+speed+", color= "+color+", radius = "+radius;
return s;
}
}
Class OOP_assignment2:
public class OOP_assignment2 {
public static void main(String[] args) {
Fan f1=new Fan();
Fan f2=new Fan();
f1.setSpeed(Fan.FAST);
f1.setRadius(10);
f1.setColor("yellow");
f1.setOn(true);
f2.setSpeed(Fan.MEDIUM);
f2.setRadius(5);
f2.setColor("blue");
f2.setOn(false);
String s=f1.toString();
System.out.println(s);
System.out.println(f2.toString());
}
}
UML Class diagram:
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال
معلومات ذات صلة
- جامعة اليمامة
- programming 2 object oriented programming using java
- برمجة جافا | Java programming
- ابحث عن مسائل برمجة جافا | Java programming بالانجليزي