object oriented assignment using java

جامعة اليمامة programming 2 - object oriented programming using java

برمجة جافا | Java programming

Assignment 2 - Object Oriented Programming - Draw UML + Design the Class

A. Create a class named Fan to represent a fan. This class contains; 

* Three constants named SLOWMEDIUM, and FAST with the values 12, and to denote the fan speed. 

* A private int data field named speed that specifies the speed of the fan (the default is SLOW).

* A private boolean data field named on that specifies whether the fan is on (the default is false). 

* A private double data field named radius that specifies the radius of the fan (the default is 5). 

* A private String data field named color that specifies the color of the fan (the default is green). 

* A no-arg constructor that creates a default fan. 

* The accessor and mutator methods for all four data fields. (for Windows click alt + insert) 

* A method named toString() that returns the following string description for the fan; If the fan is on, the method returns the fan speed, color, and radius along with the string “fan is on” in one combined string. If the fan is not on, the method returns the fan speed, color and  radius along with the string “fan is off" in one combined string. 

Try to use one line If Statement "(condition) ? TrueExpression : FalseExpression" ;)  

B. Write a test program that creates two Fan objects. 

1. First object: assign maximum speed, radius 10, color yellow, and turn it on. 

2. Second object: assign medium speed, radius 5, color blue, and turn it off

* Display these objects by invoking their toString method *

C. Draw the UML diagram for Fan class. (Using LucidChart)

الأجوبة

ابحث عن مسائل برمجة جافا | Java programming بالانجليزي

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:

أسئلة مشابهة

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!