Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I RATE! IN JAVA! Objective: Write a class Lightsaber and become a Jedi Master, o

ID: 3568104 • Letter: I

Question

I RATE!

IN JAVA!

Objective:

Write a class Lightsaber and become a Jedi Master, or Sith lord.

Example Dialog:

To become a Jedi, one must build a light saber.  Let's test to see if you have constructed yours properly

Testing constructors

This saber has passed the first test!

Testing incorrect color values

There isn't a known crystal that supports that color

This has passed test two!

Testing incorrect size values

This is not a correct blade length

This has passed test three!

This is not a correct blade length

This has passed test four!

Testing to see if this can be duplicated

This passed test five!  They can be duplicated

Finally let's test out your blade

BB-ZSHOOOO

You have a working lightsaber.  Now you are a jedi

Explanation / Answer

Lightsaber.java

public class Lightsaber {
  
private String color;
private boolean isOn;
private double bladeLength;
  
public Lightsaber(String c, boolean i, double b) {

if(!(c=="green" || c == "blue" || c== "purple" || c== "red"))
{
System.out.println("There isn't a known crystal that supports that color");
return;
}

if(!(b>=130 && b<=150))
{
System.out.println("This is not a correct blade length");
return;
}
color = c;
isOn = i;
bladeLength = b;
}
  
public String getColor()
{
return color;
}
  
public boolean getIsOn()
{
return isOn;
}
  
public double getBladeLength()
{
return bladeLength;
}
  
public void setColor(String c)
{
if(!(c=="green" || c == "blue" || c== "purple" || c== "red"))
{
System.out.println("There isn't a known crystal that supports that color");
return;
}
color = c;
}
  
public void setBladeLength(double b)
{
if(!(b>=130 && b<=150))
{
System.out.println("This is not a correct blade length");
return;
}
bladeLength = b;
}
  
public void setIsOn(boolean i)
{
isOn = i;
}
  
public boolean equals(Lightsaber l)
{
if(color == l.color && isOn == l.isOn && bladeLength == l.bladeLength)
return true;
else
return false;
}
  
public String toString()
{
return "color: "+color+" , isOn: "+Boolean.toString(isOn)+" and Blade Length: "+Double.toString(bladeLength)+" ";
}
  

}

-------------------------------------------------------------

LightsaberTester.java


public class LightsaberTester {
   public static void main(String[] args)
   {
       System.out.println("To become a Jedi, one must build a lightsaber. Let's test to see if you have constructed yours properly");
      
       System.out.println("Testing constructors");
       String color = "green";
       boolean isOn = false;
       double bladeLength = 145;
       Lightsaber aSaber = new Lightsaber(color, isOn, bladeLength);
      
       if(color.equals(aSaber.getColor()) && isOn == aSaber.getIsOn() && bladeLength == aSaber.getBladeLength())
       {
           System.out.println("This saber has passed the first test!");
       }
       else
       {
           System.out.println("This saber was not constructed properly");
       }
      
       System.out.println("Testing incorrect color values");
       aSaber.setColor("brown");
       if(color.equals(aSaber.getColor()))
       {
           System.out.println("This has passed test two!");
       }
       else
       {
           System.out.println("This failed and the saber was set to a stupid color");
       }
      
       System.out.println("Testing incorrect size values");
       aSaber.setBladeLength(10);
       if(bladeLength == aSaber.getBladeLength())
       {
           System.out.println("This has passed test three!");
       }
       else
       {
           System.out.println("This failed and the saber is too short");
       }
       aSaber.setBladeLength(170.0);
       if(bladeLength == aSaber.getBladeLength())
       {
           System.out.println("This has passed test four!");
       }
       else
       {
           System.out.println("This failed and the saber is unwieldy");
       }
      
       System.out.println("Testing to see if this can be duplicated");
       Lightsaber anotherSaber = new Lightsaber(color, isOn, bladeLength);
       if(aSaber.equals(anotherSaber))
       {
           System.out.println("This passed test five! They can be duplicated");
       }
       else
       {
           System.out.println("This failed");
       }
      
       System.out.println("Finally let's test out your blade");
       aSaber.setIsOn(true);
       if(aSaber.getIsOn() == true)
       {
           System.out.println("BB-ZSHOOOO");
           System.out.println("You have a working lightsaber. Now you are a jedi");
       }
       else
       {
           System.out.println("Your light saber is broked. Go back to the moisture vaporators n00b");
       }
      
   }
}