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

Can anyone help me with this? In need of help as soon as possible Thanks Part 1

ID: 3763807 • Letter: C

Question

Can anyone help me with this? In need of help as soon as possible Thanks

Part 1 - Variables
Example 1: LocalVars.java
/**
This program demonstrates that two methods may have
local variables with the same name.
*/
public class LocalVars
{
public static void main(String[] args)
{
texas();
california();
}
/**
The texas method has a local variable named birds.
*/
public static void texas()
{
int birds = 5000;
System.out.println("In texas there are " +
birds + " birds.");
}
/**
The california method also has a local variable named birds.
*/
public static void california()
{
int birds = 3500;
System.out.println("In california there are " +
birds + " birds.");
1}
}
1. Please refer to Example 1: LocalVars.java
a. What is california()?
b. What is the value of birds in texas() ?
c. What is the value of birds if you call california() before texas() ?
d. Is there a difference between the two values of birds?
e. After exiting texas(), what is the value of birds within the main()
method?
Example 2: Die.java
import java.util.Random;
/**
The Die class simulates a six-sided die.
*/
public class Die
{
private int sides; // Number of sides
private int value; // The die's value
/**
The constructor performs an initial
roll of the die.
@param numSides The number of sides for this die.
*/
public Die(int numSides)
{
sides = numSides;
roll();
}
/**
The roll method simlates the rolling of
the die.
*/
public void roll()
{
2// Create a Random object.
Random rand = new Random();
// Get a random value for the die.
value = rand.nextInt(sides) + 1;
}
/**
getSides method
@return The number of sides for this die.
*/
public int getSides()
{
return sides;
}
/**
getValue method
@return The value of the die.
*/
public int getValue()
{
return value;
}
}
2. Please refer to Example 2: Die.java
a. What is the value of numSides
b. Where is the variable numSides visible within the application?
c. What is the value of sides?
d. What is the difference between numSides and sides?
3. Please refer to Example 2: Die.java (same as in question 2 above)
a. What does the line public class Die represent?
b. What does the line public Die(int numSides) represent?
c. What happens within public void roll() ?
d. What does public int getSides() accomplish?
e. What is the difference between getSides() and private int sides
3Example 3: DiceDemo.java
/**
This program simulates the rolling of dice.
*/
public class DiceDemo
{
public static void main(String[] args)
{
final int DIE1_SIDES = 6; // Number of sides for die #1
final int DIE2_SIDES = 12; // Number of sides for die #2
final int MAX_ROLLS = 5; // Number of times to roll
// Create two instances of the Die class.
Die die1 = new Die(DIE1_SIDES);
Die die2 = new Die(DIE2_SIDES);
// Display the initial state of the dice.
System.out.println("This simulates the rolling of a " +
DIE1_SIDES + " sided die and a " +
DIE2_SIDES + " sided die.");
System.out.println("Initial value of the dice:");
System.out.println(die1.getValue() + " " + die2.getValue());
// Roll the dice five times.
System.out.println("Rolling the dice " + MAX_ROLLS + " times.");
for (int i = 0; i < MAX_ROLLS; i++)
{
// Roll the dice.
die1.roll();
die2.roll();
// Display the values of the dice.
System.out.println(die1.getValue() + " " + die2.getValue());
}
}
}
4. Please refer to Example 3: DiceDemo.java that refers to Example2 Die.java
a. What does ** final int DIE1_SIDES = 6;** mean?
b. What is Die mean in the line
4Die die1 = new Die(DIE1_SIDES);Die die2 = new Die(DIE2_SIDES);
c. In the line, what do die1, die2 below represent?
System.out.println(die1.getValue() + " " + die2.getValue());
d. How is die1.getValue() different from die2.getValue()
e. How does changes in value from die1 relate to changes in die2?
f. Which is an Accessor method?
g. Which is a Mutator method?

Explanation / Answer

========================================================
Part 1 - Variables
Example 1: LocalVars.java

/**
This program demonstrates that two methods may have
local variables with the same name.
*/
public class LocalVars
{
   public static void main(String[] args)
   {      
       texas();
       california();
   }
   /**
   The texas method has a local variable named birds.
   */
   public static void texas()
   {
       int birds = 5000;
       System.out.println("In texas there are " +birds + " birds.");
   }
   /**
   The california method also has a local variable named birds.
   */
   public static void california()
   {
       int birds = 3500;
       System.out.println("In california there are " +   birds + " birds.");
   }
}

1. Please refer to Example 1: LocalVars.java
  
  
   a. What is california()?
  
   -----------
   Answer :
   -----------

       california() is static function for the class LocalVars.java  
  
   b. What is the value of birds in texas() ?
      
   -----------
   Answer :
   -----------

       the value of birds in texas is 5000  
  
   c. What is the value of birds if you call california() before texas() ?

   -----------
   Answer :
   -----------

       If you are calling california() before texas()
       When california() is called, the value of birds in texas is 5000  
       When texas() is called, the value of birds in texas 3500      
      
   d. Is there a difference between the two values of birds?

   -----------
   Answer :
   -----------

       Both are independent variables , where each of the varible scope ends in the function.
  
  
   e. After exiting texas(), what is the value of birds within the main()
   method?

   -----------
   Answer :
   -----------

       You can not access the birds value in main, Since the scope of birds is in the function only.

========================================================
Example 2: Die.java

import java.util.Random;
/**
The Die class simulates a six-sided die.
*/
public class Die
{
   private int sides; // Number of sides
   private int value; // The die's value
   /**
   The constructor performs an initial
   roll of the die.
   @param numSides The number of sides for this die.
   */
   public Die(int numSides)
   {
       sides = numSides;
       roll();
   }
   /**
   The roll method simlates the rolling of
   the die.
   */
   public void roll()
   {
       // Create a Random object.
       Random rand = new Random();
       // Get a random value for the die.
       value = rand.nextInt(sides) + 1;
   }
   /**
   getSides method
   @return The number of sides for this die.
   */
   public int getSides()
   {
       return sides;
   }
   /**
   getValue method
   @return The value of the die.
   */
   public int getValue()
   {
       return value;
   }
}

   2. Please refer to Example 2: Die.java
  
   a. What is the value of numSides
  
   -----------
   Answer :
   -----------

       numSides value is depends on the costructor call from where the object is initiated.
       Here in this code the value is default value i.e numSides=0

   b. Where is the variable numSides visible within the application?

   -----------
   Answer :
   -----------

       the variable numSides visible only within the constructor public Die(int numSides)

   c. What is the value of sides?

   -----------
   Answer :
   -----------

       sides value is depends on the costructor call from where the object is initiated.
       Here in this code the value is default value i.e sides=0

   d. What is the difference between numSides and sides?

   -----------
   Answer :
   -----------

       sides is a class variable
       numSides is local variable where it scope ends as soon as the constructor code is ends.

========================================================
3. Please refer to Example 2: Die.java (same as in question 2 above)

   a. What does the line public class Die represent?

   -----------
   Answer :
   -----------

       public class Die is the declaration of class with score.
       public mean accessible from all the classes.

   b. What does the line public Die(int numSides) represent?

   -----------
   Answer :
   -----------

       public Die(int numSides) is constructor of the class Die.

   c. What happens within public void roll() ?

   -----------
   Answer :
   -----------

       public void roll() assigns a random value within the limit of sides.
       If sides = 6, roll() method return a value within 1 to 6


   d. What does public int getSides() accomplish?

   -----------
   Answer :
   -----------

       public int getSides() retunr the value of the sides.

   e. What is the difference between getSides() and private int sides

   -----------
   Answer :
   -----------

       public int getSides() is a method return the value of the side.
       private int sides is declaration of variable

========================================================
      
3Example 3: DiceDemo.java

/**
This program simulates the rolling of dice.
*/
public class DiceDemo
{
   public static void main(String[] args)
   {
       final int DIE1_SIDES = 6; // Number of sides for die #1
       final int DIE2_SIDES = 12; // Number of sides for die #2
       final int MAX_ROLLS = 5; // Number of times to roll
       // Create two instances of the Die class.
       Die die1 = new Die(DIE1_SIDES);
       Die die2 = new Die(DIE2_SIDES);
       // Display the initial state of the dice.
       System.out.println("This simulates the rolling of a " +
       DIE1_SIDES + " sided die and a " +
       DIE2_SIDES + " sided die.");
       System.out.println("Initial value of the dice:");
       System.out.println(die1.getValue() + " " + die2.getValue());
       // Roll the dice five times.
       System.out.println("Rolling the dice " + MAX_ROLLS + " times.");
       for (int i = 0; i < MAX_ROLLS; i++)
       {
           // Roll the dice.
           die1.roll();
           die2.roll();
           // Display the values of the dice.
           System.out.println(die1.getValue() + " " + die2.getValue());
       }
   }
}
4. Please refer to Example 3: DiceDemo.java that refers to Example2 Die.java
  
   a. What does ** final int DIE1_SIDES = 6;** mean?
   -----------
   Answer :
   -----------

       final int DIE1_SIDES = 6 means creating DIE1_SIDES variable with value 6 and no modification further
   since it is delcared as final.
  
   b. What is Die mean in the line
   -----------
   Answer :
   -----------

       Die refers to class.
  
   4   Die die1 = new Die(DIE1_SIDES);
       Die die2 = new Die(DIE2_SIDES);
   c. In the line, what do die1, die2 below represent?
   -----------
   Answer :
   -----------

       die1, die2 are the objects of the class Die

   System.out.println(die1.getValue() + " " + die2.getValue());

   d. How is die1.getValue() different from die2.getValue()
   -----------
   Answer :
   -----------

       die1 object return value between 1 to 6 since it is 6 sided die
       die2 object return value between 1 to 12 since it is 12 sided die


   e. How does changes in value from die1 relate to changes in die2?
   -----------
   Answer :
   -----------

       die1 object return value between 1 to 6 since it is 6 sided die
       die2 object return value between 1 to 12 since it is 12 sided die

   f. Which is an Accessor method?
   -----------
   Answer :
   -----------

       An accessor method is used to return the value of a private field of class.      
  
   g. Which is a Mutator method?
   -----------
   Answer :
   -----------

       An mutator method is used to set the value of a private field of class.      

========================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote