Java

OOP GUIDE / WORK

 

DUDE CLASS

 

Topics

  • Analyzing code
  • Creating objects

 

 

TASK – PART 1 – SETUP

 

Copy the following Dude class into your IDE.

 

public class Dude

{

public String dudeName;

public int coolness;

  

public Dude(String dname)

{

   dudeName = dname;

   coolness = 0;

}

  

public void addToCoolness(int amount)

{

   System.out.println(dudeName + " just got cooler!");

   coolness = coolness + amount;

}

  

public void removeFromCoolness(int amount)

{

   System.out.println(dudeName + " just got less cool!");

   coolness = coolness - amount;

}

  

public String toString()

{

   if (coolness < 0)

        return dudeName + " isn't that cool.";

   else if (coolness == 0)

        return dudeName + " is alright.";

   else

        return dudeName + " is very cool.";

}

}

 

 

 

TASK – PART 2 – ANALYZING DUDE

 

a)    Inside a class called DudeTester, create a Dude object.

b)    Analyze the Dude class and predict what the value of the instance variables will be immediately after your object is created.  Then, output the value of each instance variable to see if you were correct.

c)     Call the three instance methods to see how they work.  Analyze the code.  Explain how the coolness instance variable works.